5

I would like to distribute a class that uses a professional font loaded via fontspec, and falls back to one of the default fonts in TeX Live if this is not available. I know that one can detect fonts packaged with TeX Live:

\documentclass{article}
\usepackage{fontspec}
\IfFileExists{tgpagella.sty}{\setmainfont{TeX Gyre Pagella}}{}

\begin{document}

\fontname\font\ at \the\fontdimen6\font

\end{document}

This doesn't work, however, if one instead tries to detect a font file in the same directory:

\documentclass{article}
\usepackage{fontspec}
\IfFileExists{texgyrepagella-regular.otf}{\setmainfont{TeX Gyre Pagella}}{}

\begin{document}

\fontname\font\ at \the\fontdimen6\font

\end{document}

Is there a mechanism somewhere to include .otf files in what \IfFileExists is able to detect?

6
  • fontspec has a fallback mechanism these days older answer but Will added a proper interface to that if I recall correctly Jan 21, 2019 at 16:38
  • 1
    \fontspec_font_if_exist:nTF and similar commands look to be the right thing.... Jan 21, 2019 at 16:43
  • 1
    @DavidCarlisle the documentation suggests \IfFontExistsTF. Jan 21, 2019 at 16:44
  • 5
    @UlrikeFischer but it looks much more impressive with _ and : Jan 21, 2019 at 16:45
  • @DavidCarlisle Only for people not really used to _ and : ;-). Jan 21, 2019 at 16:47

1 Answer 1

10

Recent fontspec releases allow you to test for a font being loadable with \IfFontExistsTF or its internal expl3 version \fontspec_font_if_exist:nTF:

\documentclass{article}
\usepackage{fontspec}
\IfFontExistsTF{texgyrepagella-regular.otf}{\setmainfont{TeX Gyre Pagella}}{}

\begin{document}

\fontname\font\ at \the\fontdimen6\font

\end{document}

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.