Fonts – How to Specify a Different Font on Different Computers

cross platformfontsfontspec

On the Mac, the Baskerville font that comes pre-installed with OSX works wonderfully with LaTeX (as suggested here by Manuel). However, I believe that font is licensed and thus can't be freely copied to other computers. On my Ubuntu 18.04 box, I can use the Baskervaldx font, but in my opinion it does not look nearly as nice. How could I set up my .tex file so that on my Mac, it runs the commands to use Baskerville and on Ubuntu it runs the commands to use Baskervaldx?

Specifically, on the Mac, it should run these commands:

\usepackage[no-math]{fontspec}
\setmainfont[Mapping=tex-text]{Baskerville}
\usepackage[defaultmathsizes,italic]{mathastext}

and on Ubuntu it should run this command:

\usepackage{Baskervaldx}

I run latexmk -xelatex from a Makefile, so supplying a different command-line option or a different value for an environment variable is not a problem.

If there's a way to get the nice Baskerville on Ubuntu, I'd love to hear that. I'm willing to pay for the font but there are so many Baskervilles out there, I can't tell which is the right one. Or, if there's way to say, in LaTeX, "use Baskerville if you can, Baskervaldx if you must", that might actually be the best solution.

Best Answer

You can check if the font is there:

\documentclass{article}

\usepackage[no-math]{fontspec}

\IfFontExistsTF{Baskerville}
{
\setmainfont[Mapping=tex-text]{Baskerville}
\usepackage[defaultmathsizes,italic]{mathastext}
}{
\usepackage{Baskervaldx}
}

\begin{document}
Abc.
\end{document}

One slight disadvantage: if it is not there there is a slight pause while it looks and updates the font cache.

An alternative would be a quicker but less specific test, replacing \IfFontExistsTF{Baskerville} by \IfFileExists{any-file-only-on-the-mac}