12

I am setting up the packages for a shared project with someone who's not familiar with LaTeX. I want to use some of our local fonts. I used XeLaTeX and it works. However, we'll also use a notebook where these fonts might not be available.

As I intend to minimise the need of modification on the packages loaded, I would like to know whether there is a way to specify alternatives fonts to XeLaTeX in case it doesn't find the first one (as it is done in HTML/CSS for example).

Here is a mwe:

\documentclass[11 pt, a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{fontspec}
\setmainfont{Adobe Garamond Pro}
\begin{document}
This should be in \textit{Adobe Garamond Pro} if available.
\end{document}
  • 1
    Yeah, that's what I would like see as well. XeTeX is in active enough development that this type of interface might be feasible now; I don't know. – Sean Allred Sep 16 '14 at 21:50
  • 3
    @SeanAllred It would end any pretence that compiling the same document on different systems will give identical output. But I guess this is gone for XeTeX/LuaTeX, at least, anyway. – cfr Sep 16 '14 at 22:23
  • 1
    @cfr Archivability is very important, but introducing that , is the user's choice. – Sean Allred Sep 16 '14 at 22:24
  • 1
    @SeanAllred True. I'm not necessarily against the idea. It just seems to me that while it seems great to be able to use any font, on the other hand, that very advantage is also a disadvantage. (I sometimes think people are over enthusiastic about the changes. Not that there are not many benefits, of course. But there is a definite cost to those benefits. Or several, actually. This is one of them.) – cfr Sep 16 '14 at 22:30
  • 1
    Why not copy the tex files along with the font files? You can get XeTeX load fonts from the working directory, even if not installed on the system. – Ariel Jul 28 '15 at 9:08
7

This works in both xelatex and lualatex and sets the document in Pagella for me:

\documentclass[11 pt, a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{fontspec}
\ExplSyntaxOn
\NewDocumentCommand{\onlywarniffontnotfound}{}
 {
  \suppressfontnotfounderror = \c_one 
  %\font_suppress_not_found_error:
  \msg_redirect_name:nnn {fontspec}{font-not-found}{warning}
 }
\ExplSyntaxOff

\onlywarniffontnotfound
\setmainfont{Adobe Garamond Pro}
\expandafter\ifx\the\font\nullfont
  \setmainfont{TeX Gyre Pagella}
  \expandafter\ifx\the\font\nullfont
    \typeout{hmm your document might be a bit white, sorry}
  \else
    \typeout{TeX Gyre Pagella}
  \fi
\else
\typeout{you have Adobe Garamond Pro}
\fi



\begin{document}
\showoutput
This should be in \textit{Adobe Garamond Pro} if available.
\end{document}

(\onlywarniffontnotfound supplied by @egreg)

  • 1
    This doesn't compile for me in either XeLaTeX or LuaLaTeX. I don't have Adobe Garamond Pro, so I get a font-not-found fontspec error regardless of whether I use \scrollmode or \suppressfontnotfounderror=1. – Jason Zentz Jul 31 '15 at 16:51
  • 1
    @DavidCarlisle fontspec uses \suppressfontnotfounderror=1 somewhere, but then it defines some function to raise an error independently of this. – egreg Jul 31 '15 at 16:55
  • 1
    @JasonZentz you could try the modified code above, no idea what texshop will do with it but it avoids a primitive tex error. so doesn't need \scrollmode – David Carlisle Jul 31 '15 at 17:56
  • 1
    @Matthew try now, I reverted to primitive syntax for one setting – David Carlisle Oct 3 '16 at 6:52
  • 1
    change is fontspec, here github.com/wspr/fontspec/commit/… – David Carlisle Oct 3 '16 at 8:55

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.