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}
Luc M
  • 675
  • http://tex.stackexchange.com/a/53448/17423 may be a starting point, but I would hope that a nicer interface has popped up in the past two years :) – Sean Allred Sep 16 '14 at 20:49
  • @Sean Thanks, I'll try it. Sure, I was hoping for something like \setmainfont{Garamond, Times} ... – Luc M Sep 16 '14 at 21:08
  • 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 09:08
  • @cfr If only it would. – Arthur Reutenauer Aug 03 '15 at 18:04
  • @ArthurReutenauer If only what would what? – cfr Aug 03 '15 at 20:39
  • 1
    @cfr If only it would end the pretence that TeX documents are unconditionally portable (in reply to your first comment). I have never seen it to be true for users who really needed it, and for others who didn't it has made them unnecessarily picky (refusing to make a trivial change on 3 lines of a 15-year old document - I have seen it). I think we've reached a point where this claim is actually harmful. A more useful and accurate claim is that TeX documents will still be usable after decades, and under carefully controlled conditions can be made to produce the same output. – Arthur Reutenauer Aug 03 '15 at 21:30
  • @ArthurReutenauer Up to a point. But this would result in very different output on systems with identical installations of TeX, for example. But, as I say, that can happen anyway once you are using Xe/LuaTeX and system fonts. – cfr Aug 03 '15 at 21:36

1 Answers1

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)

David Carlisle
  • 757,742
  • Instead of \scrollmode you could set \suppressfontnotfounderror=1 – egreg Jul 31 '15 at 16:42
  • @egreg ooh very modern:-) – David Carlisle Jul 31 '15 at 16:43
  • @egreg I just tried it and fontspec still stops – David Carlisle Jul 31 '15 at 16:44
  • 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
  • @JasonZentz that's very odd, I don't have Garamond Pro either and it picks up pagella for me, which tex distribution do you have? It echoes the font not found error for Garamod pro to the terminal but should move on and try Pagella without stopping. – David Carlisle Jul 31 '15 at 16:52
  • 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
  • Well, I guess it does create a PDF with Pagella, but TeXStudio complains about the font-not-found error, says "exited with errors," and doesn't display the PDF automatically as it normally would. – Jason Zentz Jul 31 '15 at 16:58
  • I have TeX Live 2015, with all packages updated yesterday. – Jason Zentz Jul 31 '15 at 16:59
  • It does work for me if I use the terminal to compile, but TeXStudio (my normal editor) doesn't like it. – Jason Zentz Jul 31 '15 at 17:03
  • 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
  • Yes, the modified version works fine in TeXStudio. – Jason Zentz Jul 31 '15 at 18:00
  • Very nice ! To keep a clean preamble I could put your answer inside a command (with 3 or 4 arguments for the substitutable fonts and add a default font) without problem and that makes it exactly what I looked for, i.e. a command like \listofsubstitutablefonts{Adobe Garamond Pro}{Georgia}{Helvetica}. – Luc M Aug 03 '15 at 13:49
  • Doesn't work for me on XeLaTeX from the terminal. Undefined control sequence. [\onlywarniffontnotfound]. – Matthew Oct 03 '16 at 04:00
  • @Matthew oh something changed in expl3 or fontspec, I'll update the answer in a bit – David Carlisle Oct 03 '16 at 06:39
  • 1
    @Matthew try now, I reverted to primitive syntax for one setting – David Carlisle Oct 03 '16 at 06:52
  • 1
    change is fontspec, here https://github.com/wspr/fontspec/commit/b608b00ac57e0282da9d65b4647a42e0211ee202#diff-279d504a58ccd623471a80e1d7a3f693 – David Carlisle Oct 03 '16 at 08:55