6

Is there a way to make \setsansfont conditional on the font being available, or to skip over that line if it gives an error?

I'm using fontspec with XeLaTeX to use the TeX Gyre Heros font for a document, but I'd like to revert to the default font if that font isn't available, so that if someone else compiles the document but doesn't have that font installed, they get something that's readable, rather than just an error.

A minimal example:

\documentclass{article}

\usepackage{fontspec}
\setsansfont{TeX Gyre Heros}

\begin{document}

{\sffamily blah}

\end{document}

Update: it was reported that this is a duplicate of "Check for missing fonts/characters in XeLaTeX?", but really that question concerns whether individual characters are available in a font or not.

What I want to know is: can I test whether the whole font is available or not, and go with the default if it's not available.

  • The answer by Simurgh12 in the "duplicated" question shows a principal way how to achieve what you want. But I agree that it is not really the same question. – mafp Feb 28 '14 at 0:05
  • @mafp I'm feeling a bit dense, but I totally don't see it. – Karl Feb 28 '14 at 0:18
  • 1
    :-) The answer over there shows how to test for some character. So If you test for a common character like A, and it is not found, then you can assume the font is not there. Otherwise it is there and can be loaded. – mafp Feb 28 '14 at 0:58
3

Recent versions of fontspec have \IfFontExistsTF:

\documentclass{article}

\usepackage{fontspec}

\IfFontExistsTF{Non Existent}{%
  \setsansfont{Non Existent}%
}{}

\begin{document}

{\sffamily blah \fontname\font}

\end{document}

In the “false” branch you can add a different declaration.

enter image description here

As you see, the sans serif font is the default one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.