# Can fontspec provide a fallback font if a font doesn't exist?

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.

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.)

• 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. – Loop Space 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 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

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).

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: