10

I'm working on a document with a collaborator who is particular about their choice of font. My collaborator has provided me with the font files, but on the particular machine I'm using then I don't have administrator access and can't install the font system-wide. I can, though, simply load the font explicitly through fontspec (we're using lualatex as the engine) but this means that our documents differ slightly since my collaborator loads it from the system and I load it from a file.

Can we make it so that one preamble works for both? I imagine it being to tell fontspec to look for a particular font, and if it doesn't find it then to look for another one.

Note that this is about loading the whole font, not just for individual characters.

Some code to play with.

\documentclass{article}

\usepackage{fontspec}

% One person has this installed system-wide
\setmainfont{TeX Gyre Termes}
% Try misspelling the above and watch lualatex complain loudly about the missing font

% The other only has the file
\setmainfont{texgyretermes-regular.otf}

\begin{document}
Wouldn't it be nice if this were Termes?
\end{document}

(Note: This is a question that I know the answer to, and is actually an RTM, but I did spend a while fruitlessly searching online for an answer and didn't even get a hint that it was possible, so thought it worth posting here to improve search engine results. I'll wait before posting my answer because I'm lazy and someone else might post a better answer with nice code before I get round to it. Once an answer has been posted, I'll remove this note.)

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • there is an answer here from before Will added an interface, i'll see if I can find a link – David Carlisle Jun 29 '18 at 17:58
  • @DavidCarlisle That'd be useful to have that link, but given that Will has now added an interface, it would be good to have an answer that shows how to use it. – Andrew Stacey Jun 29 '18 at 18:03
  • not sure these are relevant now, certainly [a] isn't but: a b c (Yes I didn't mean that a current answer wouldn't be a good thing) – David Carlisle Jun 29 '18 at 18:04
  • Can't you use a .fontspec file? – Ulrike Fischer Jun 29 '18 at 18:05
  • @UlrikeFischer Never heard of one, but if you think it would provide a solution then please post an answer. I'd be happy to have answers that suggest a variety of solutions since the next person with this question might have slightly different circumstances to me. – Andrew Stacey Jun 29 '18 at 18:08
  • 1
    @UlrikeFischer not so convenient for the use case of distributing a document but you do not know which fonts are installed locally. – David Carlisle Jun 29 '18 at 18:09
  • You should be able to stick the fonts in a subdirectory of the document’s working directory and use relative paths? – Davislor Jun 30 '18 at 08:33
  • @Davislor that's what I have done, but my collaborator doesn't want to do that as they have the font already installed on their system. So we both have the font, but need to use different invocations of \setmainfont to load it. – Andrew Stacey Jun 30 '18 at 09:48
  • @LoopSpace I’m presuming there’s a reason they don’t want to use the method that works for both of you? Alternatively: even if you’re not an administrator, can you install the fonts only for yourself? On Linux, you can copy font files to ~/.fonts/. – Davislor Jun 30 '18 at 16:45
  • @Davislor From their point of view, why install a copy of something that's already on their system? Wrt installing fonts, I'm forced to use a Windows machine and I don't know if per-user fonts are possible. I don't even have access to the commandline. – Andrew Stacey Jun 30 '18 at 19:24
  • @LoopSpace So their coworkers can use it too? Possible compromise: on their machine, the font files in the local directory are symbolic links that don't make them copy any files or make a second copy. Or it's even a symlink to the system font directory. Alternative: It's so everyone is using the same version of the font? – Davislor Jun 30 '18 at 20:31
  • @DavidCarlisle I'm tempted to add a back-link to those questions as the new(ish) functionality in the package does make this easier now. – Andrew Stacey Jul 01 '18 at 13:22

2 Answers2

10

There is a command \IfFontExists(TF) in fontspec which provides the required functionality:

\documentclass{article}
%\url{https://tex.stackexchange.com/q/438577/86}
\usepackage{fontspec}

% Check to see if this font is installed system-wide
\IfFontExistsTF{TeX Gyre Termes}{%
% One person has this installed system-wide
  \setmainfont{TeX Gyre Termes}
}{
% If not, load it from the file
  \setmainfont{texgyretermes-regular.otf}
}

\begin{document}
Wouldn't it be nice if this were Termes?
\end{document}

If the TeX Gyre Termes font is installed on the system, then lualatex will find it and therefore take the true branch which loads it from the system. If it doesn't exist on the system, it will load it from the file.

This means that the person with it installed on the system doesn't have to have unnecessary files, while the person who doesn't have it installed on the system doesn't have to install a font that's only being used in a single document (or, can use a font which they can't install).

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
4

Your description doesn't sound so much as if you want to declare fallbacks but more as if you want to tell fontspec/luaotfload how to find the font.

I would put the font inside a local texmf tree in texmf/fonts/opentype (or fonts/truetype). If this out of question you could create a TeXGyreTermes.fontspec file and declare the font there. E.g. with this declaration

\defaultfontfeatures[TeX Gyre Termes]
{
 Extension=.ttf,
 UprightFont=Arial
}

your example with \setmainfont{TeX Gyre Termes} will use arial:

enter image description here

Ulrike Fischer
  • 327,261