3

I'm trying to set up a document with Polyglossia/XeLaTeX, which should be mostly in Hebrew, with some English excerpts. However, I can't get fontspec to use the correct English font for anything other than the main (roman) font - it instead uses the Hebrew-specified fonts.

Here is my minimal example file:

\documentclass{article}
\usepackage{fontspec}
\usepackage[a4paper]{geometry}

\makeatletter

\setmainfont[Language=English,Script=Latin]{Nimbus Roman No9 L}
\setsansfont[Language=English,Script=Latin]{Nimbus Sans L}
\setmonofont[Language=English,Script=Latin]{Nimbus Mono L}
\newfontfamily\hebrewfont[Language=Hebrew,Script=Hebrew,Scale=3.0]{Frank Ruehl CLM}
\newfontfamily\hebrewfontsf[Language=Hebrew,Script=Hebrew,Scale=3.0]{Nachlieli CLM}
\newfontfamily\hebrewfonttt[Language=Hebrew,Script=Hebrew,Scale=3.0]{Miriam Mono CLM}
\makeatother

\usepackage{xunicode}
\usepackage{polyglossia}
\setdefaultlanguage{hebrew}
\setotherlanguage{english}
\begin{document}
בדיקה, באיזה פונט זה יוצג \textenglish{\ttfamily{Hello, World!}}.


\begin{english}
Hello, \sffamily{Sans}, \ttfamily{Mono}
\end{english}

\end{document}

Hebrew fonts scale to better see outcome. The result looks like this:

Picture of outcome PDF

As you can see, only the default (roman) English font is used, while all other English text uses the scaled (Hebrew) fonts. I have tried with or without Language/Script specifications, but results are always the same.

Edit: Changing it so English is default and Hebrew is other seems to fix it, but this is far from ideal - the document would be 95% Hebrew at least. But maybe it hints towards the root cause?

Edit2: Changing default language to English also has the undesired outcome of making it so Hebrew Sans-Serif and Typewriter/Mono text disappears completely.

Edit3: The proposed fix in Ulrike Fischer's answer has the side effect of making it so after the first use of \textenglish, all section titles are wrongly rendered using the English fonts:

\documentclass{article}
\usepackage{fontspec}
\usepackage[a4paper]{geometry}
\usepackage{xunicode}
\usepackage{polyglossia}

\setmainfont{DejaVu Sans}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans}
\newfontfamily\englishfont{TeX Gyre Termes}
\newfontfamily\englishfontsf{TeX Gyre Heros}
\newfontfamily\englishfonttt{TeX Gyre Cursor}

\setdefaultlanguage{hebrew}
\setotherlanguage{english}

\makeatletter 
\addto\inlineextras@english{\xpg@set@normalfont{english}}
\addto\blockextras@english{\xpg@set@normalfont{english}}
\makeatother


\begin{document}
\section{חלק ראשון}
נכתוב באנגלית \textenglish{Hello, World!}
ומעכשיו כל הכותרות לא יופיעו
\section{חלק שני}
הכותרת לא מופיעה, אבל הטקסט כן.

\end{document}

Which results in:
Section titles rendered with wrong fonts

Different fonts will have different behaviors - the previously used Nimbus fonts only show spaces, while the Liberation ones show empty squares - I guess it has to do with what those fonts have in the Hebrew glyph locations.

5
  • Is this the desired result?
    – Alenanno
    Aug 21, 2016 at 11:00
  • The desired result if for the correct fonts (based on language) to be used. The scaling was only done so it is clearly visible that the wrong font specs are used.
    – Itai
    Aug 21, 2016 at 11:50
  • Please don't make example with unusual fonts. I don't want to have to adapt your code before being able to test it. Aug 21, 2016 at 19:00
  • Sorry, they are pretty common for Hebrew, you can get them at culmus.sourceforge.net, but I'll adapt the example to the DejaVu fonts.
    – Itai
    Aug 21, 2016 at 19:16
  • 1
    I edited the answer. Btw: you should notify me with @UlrikeFischer if you answer me. Aug 23, 2016 at 7:13

2 Answers 2

2

hebrew is your main language and so it would feel like a hack to set english to the default language only to get the fonts right.

Imho there is a bug in polyglossia, it doesn't seem to reset the fonts correctly.

The following seems to work to correct the problem. As I don't have your fonts I had to change them.

Edit

As noted in the comment my first solution broke the fonts in \section (and other places). The reason is that \xpg@set@normalfont redefines \normalfont globally. The following correct this, but it could have other side effects. Also it is more a work-around than a solution.

On the whole I think that the font changing commands in polyglossia are not symmetric. There is code for the case "latin=main, hebrew=other" but not the other way round.

\documentclass{article}
\usepackage[a4paper]{geometry}    
\usepackage{polyglossia}

\setmainfont{DejaVu Sans}
\setsansfont{DejaVu Sans}
\setmonofont{DejaVu Sans}
\newfontfamily\englishfont{TeX Gyre Termes}
\newfontfamily\englishfontsf{TeX Gyre Heros}
\newfontfamily\englishfonttt{TeX Gyre Cursor}

\setdefaultlanguage{hebrew}
\setotherlanguage{english}

\makeatletter
\def\xpg@set@normalfont#1{%
  \letcs{\rmfamily}{#1@font@rm}%
  \letcs{\sffamily}{#1@font@sf}%
  \letcs{\ttfamily}{#1@font@tt}%
  \def\normalfont{\protect\xpg@select@fontfamily{#1}}%def instead of gdef
  \gdef\reset@font{\protect\normalfont}%
}
\addto\inlineextras@english{\xpg@set@normalfont{english}}
\addto\blockextras@english{\xpg@set@normalfont{english}}
\makeatother



\begin{document}
\section{חלק ראשון}
נכתוב באנגלית \textenglish{Hello, World!}
ומעכשיו כל הכותרות לא יופיעו

\begin{english}
blub \sffamily blub \ttfamily blb
\end{english}


\section{חלק שני}
הכותרת לא מופיעה, אבל הטקסט כן.

\end{document}

enter image description here

3
  • Well, not perfect yet - this seems to make section titles set in Hebrew to try render with the English fonts, which just gives out blocks. I'll try to get a minimal example, but it may have something to do with hyperref.
    – Itai
    Aug 21, 2016 at 18:37
  • Hyperref is not to blame - the proposed fix makes it so after the first use of English text, all section, subsection etc. titles are set with the English fonts.
    – Itai
    Aug 21, 2016 at 18:44
  • Ok, I guess that's the best that can be reasonably done without fixing the bug(s) in polyglossia. I don't yet know enough about the inner working of (Xe)LaTeX to feel comfortable describing what the bug is, but I may try to look into it and file a bug report at a later occasion.
    – Itai
    Aug 23, 2016 at 8:11
1

Apparently \sffamilylatin and \ttfamilylatin will help a lot if the main language is non-Latin (page 6 of polyglossia documentation). We can then conditionally re-define \sffamily and \ttfamily to use these latin versions by checking if the active language is English, with \IfLanguageName{english}{true part}{false part}.

(I had previous used \iflanguage, but later found out that its use is discouraged, and \IfLanguageName from the iflang package is preferred.)

\documentclass[12pt]{article}
\usepackage{polyglossia}
\usepackage{iflang}
\setdefaultlanguage{hebrew}
\setotherlanguage{english}

% I don't actually which Hebrew fonts counts as serif or sans serif...
\newfontfamily\hebrewfont[Language=Hebrew,Script=Hebrew]{David CLM}
\newfontfamily\hebrewfontsf[Language=Hebrew,Script=Hebrew]{Miriam CLM}
\newfontfamily\hebrewfonttt[Language=Hebrew,Script=Hebrew]{Miriam Mono CLM}
\newfontfamily\englishfont{FreeSerif}
\newfontfamily\sffamilylatin{FreeSans}
\newfontfamily\ttfamilylatin{FreeMono}

% Conditionally redefine \sffamily, \ttfamily to use
% \sffamilylatin, \ttfamilylatin if active language is english
\AtBeginDocument{
  \let\oldttfamily\ttfamily
  \let\oldsffamily\sffamily
  \renewcommand{\ttfamily}{\IfLanguageName{english}{\ttfamilylatin}{\oldttfamily}}
  \renewcommand{\sffamily}{\IfLanguageName{english}{\sffamilylatin}{\oldsffamily}}
}

\begin{document}
בדיקה, באיזה פונט זה יוצג
\textsf{בדיקה, באיזה פונט זה יוצג}

\section{חלק ראשון}

נכתוב באנגלית \textenglish{Hello, World!}
ומעכשיו כל הכותרות לא יופיעו

\begin{english}
blub \sffamily blub \ttfamily blub \textsf{blub} \texttt{blub}
\end{english}


\section{חלק שני}
הכותרת לא מופיעה, אבל הטקסט כן.
\end{document}

Example project: https://www.overleaf.com/5971027jytqjd#/19831055/

7
  • Hmm... This is problematic when used in LyX, because LyX automatically uses \textsf, \texttt etc. Is there a simple way of telling it to use \xxfamilylatin only inside an English environment?
    – Itai
    Aug 25, 2016 at 9:55
  • Turns out we can use \iflanguage to check for the current active language and conditionally re-define \sffamily and \ttfamily. I've updated the code sample; this compiles for me fine in XeLaTeX but I don't have LyX to try it out.
    – imnothere
    Aug 25, 2016 at 13:05
  • Hmm... For some reason this makes xelatex go into an infinite loop for me.
    – Itai
    Aug 25, 2016 at 13:25
  • sorry I had a typo on the \setmonofont line. And this seems to work in TL2015 but not TL2016... I'll have another look later
    – imnothere
    Aug 25, 2016 at 13:59
  • 1
    This is now working for me in both TL2015 and 2016. (I'm using another set of Hebrew fonts different from your example)
    – imnothere
    Aug 25, 2016 at 14:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .