[Tex/LaTex] Select first available font XeTeX

fontsfontspecxetex

How to select first available font from the list of suitable fonts?

Currently I use \setmainfont{Liberation Serif} command in Ubuntu and \setmainfont{Times New Roman} in Windows environment. I would like to automate the selection of the font.
It would be perfect if \setmainfont could accept several fonts, e.g. \setmainfont{Liberation Serif, Times New Roman}, but it cannot. I am looking for any kind of workaround.

Update Is it possible to check if a font is available? Or is there a way to catch the unhandled exception fontspec error: "font-not-found"?

Best Answer

I think you're looking for the ifplatform package.

Shell escape must be enabled for this to work, but try the following:

\documentclass{article}
\usepackage{ifplatform}
\usepackage{polyglossia}
\usepackage{fontspec}   
\iflinux
\setmainfont{Liberation Serif}
\else
\ifwindows
\setmainfont{Times New Roman}
\fi
\usepackage{lipsum}
\begin{document}
\lipsum[1-3]
\end{document}

FYI, there is also ifxetex, which is nice for setting up fonts, encodings, etc. based on your chosen typesetting engine.

Related Question