# Select first available font XeTeX

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"?

• Why do you want to do that? Which font should the final document use? – quinmars Jan 30 '15 at 20:03
• @quinmars I am creating a document style. I wish that users of this style wouldn't have to configure anything, no matter which operating system they use. Bouth Liberation Serif and Times New Roman fonts are almost the same, so it can be any of them. – niekas Jan 30 '15 at 20:08
• I wouldn't select a font in the document class/style. Then people could also use pdflatex for example. If you insist on selecting the font in your style/class I would use something like TeX Gyre Termes, which should be available on all modern tex installations. IMHO the look of a document should not depend on the used operating system. – quinmars Jan 30 '15 at 20:18
• @quinmars Thank you very much. I used TeX Gyre Termes like this \setmainfont[Ligatures=TeX, Extension=.otf, UprightFont= *-regular, BoldFont=*-bold, ItalicFont=*-italic, BoldItalicFont=*-bolditalic]{texgyretermes}. – niekas Jan 30 '15 at 21:21
• Just to note that another issue with your original plan is that there's no guarantee that GNU/Linux users will have Liberation Serif available. I guess TNR is available by default in Windows, but there's no such assurance for GNU/Linux systems, no matter which font you choose. – cfr Jan 30 '15 at 23:36

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.

• As @cfr mentioned, there is no assurance for GNU/Linux systems that Liberation Serif font will be installed. I would like to provide a list of suitable fonts for Linux. – niekas Jan 31 '15 at 7:11
• @niekas I realize this doesn't achieve your goal as explained in the comments, but it does what you explained in the question (before the update, in which you ask a separate question). So, I thought I'd submit this answer anyway, in case it is helpful to someone searching this topic in the future. – erik Feb 1 '15 at 18:14