4

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.

2 Answers2

7

You could use the ifplatform package, which provides \ifmacosx and \iflinux conditionals:

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

You would need to add -shell-escape to your latexmk options.

There are also other libre versions of Baskerville such as Libre Baskerville and BaskervilleF you might consider.

frabjous
  • 41,473
  • I'm accepting David Carlisle's answer because it's more general, but in my document, I'm following your suggestion here: to use Libre Baskerville. It turns out that Libre Baskerville actually looks better, especially on-screen, and also when mixed with Friz Quadrata. This led to another question. – Ben Kovitz Oct 04 '22 at 18:23
7

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}

David Carlisle
  • 757,742