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?

Andrew Dunning
  • 1,714
  • 13
  • 21

1 Answers1

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}
Andrew Dunning
  • 1,714
  • 13
  • 21
David Carlisle
  • 757,742