# Can I tell polyglossia to “try to find any appropriate font for the chosen language”?

Consider the following document:

\documentclass{minimal}
\usepackage{polyglossia}
\setdefaultlanguage{hebrew}
\setotherlanguage{english}
\newfontfamily\hebrewfont[Script=Hebrew]{David CLM}
\usepackage{bidi}
\begin{document}
קסם קסמה לי הלענה.
\end{document}


Assuming your TeX distro is configured properly, and that the culmus fonts are installed, this will compile. However, if I remove the \newfontfamily line, and try to compile this with TeXLive on a recent Linux distribution, I get:

! Package polyglossia Error: The current roman font does not contain the Hebrew
script!
(polyglossia)                Please define \hebrewfont with \newfontfamily.


Now, I think this shouldn't happen, but that's a matter for another day. My question is, suppose I don't know which Hebrew fonts are installed on the system. Maybe there's Culmus, maybe there's access to the OS' fonts, some of which support Hebrew (and those are different font families in Windows and in Linux) - whatever.

Is there a way to tell polyglossia "use whatever Hebrew font family has available fonts", as opposed to requiring a specific font family?

Bonus points for an answer which supports setting fallbacks a-la-CSS, i.e. "if you have it, then font family A; otherwise, if you have it, then font family B; etc. etc. ; and if you have none then an arbitrary Hebrew font"

This is not system independent, since I have used bash and you have to compile with -shell-escape.

But under Linux you could grab the first font that fontconfig finds supporting Hebrew if your preferred font isn't found.

Update with more elegant solution:

\documentclass{article}
\usepackage{polyglossia}
\IfFontExistsTF{David CLM}
{\newfontfamily\hebrewfont{David CLM}[Script=Hebrew]}
{\usepackage{bashful}
\splice{echo -n '\string\newfontfamily\string\hebrewfont{';
fc-list :lang=he family | head -1 | tr -d '\string\n';
echo -n '}[Script=Hebrew]'}}
\setmainlanguage{english}
\setotherlanguage{hebrew}
\begin{document}
English \texthebrew{עברית}
\end{document}


Original solution:

\documentclass{article}
\usepackage{polyglossia}
% Try to use a preferred font otherwise fall back to the one fontconfig finds.
% Make sure we are in batch mode so compilation continues when preferred font
% isn't found.
\count255=\interactionmode
\batchmode
\font\testfont="David CLM" at 10pt
\interactionmode=\count255
\ifx\testfont\nullfont
% use the bashful to run bash script
% grab the first family fontconfig finds supporting hebrew
% create hebfont.tex looking like:
% \newfontfamily\hebrewfont{Some Font}[Script=Hebrew]
% Note: Don't indent this, since bashful is picky about leading spaces
\usepackage{bashful}
\bash
echo -n '\newfontfamily\hebrewfont{' > hebfont.tex
fc-list :lang=he family | head -1 | tr -d '\n' >> hebfont.tex
echo -n '}[Script=Hebrew]' >> hebfont.tex
\END
\input{hebfont.tex}
\else
\newfontfamily\hebrewfont{David CLM}[Script=Hebrew]
\fi
\setmainlanguage{english}
\setotherlanguage{hebrew}
\begin{document}
English \texthebrew{עברית}
\end{document}

• Well, yeah, that's something, but like you said, it's pretty platform-specific. +1 though. I was disappointed to note that on my system the first font would be the horrid and inappropriate David Transparent. – einpoklum Sep 19 '17 at 12:50
• Yes, I think you'd definitely want to check for a few font options before falling back to fontconfig. – David Purton Sep 19 '17 at 12:58
• Is there some package wrapping your testfont code more elegantly? Also - shouldn't the \bash command only be executed in case "David CLM" is missing? – einpoklum Sep 19 '17 at 13:45
• Yes, you could do that. I updated the answer. bashful seems picky about leading spaces, so don't indent. I don't know of a package that tests for missing fonts, but perhaps fontspec has something buried in it. – David Purton Sep 19 '17 at 13:57
• I believe that also MiKTeX can use -shell-escape instead of -enable-write18 (the former is the name of the option for TeX Live based binaries). – egreg Sep 19 '17 at 14:56