2

So I have a document with Hebrew as its main language and English as the secondary language. Hebrew is used for regular text and English is used for both regular text and text inside math formulas. However, it turns out that the command \text{} uses the Hebrew (main) font as opposed to the font used for English text. Here's an example:

\documentclass[a4paper,english,hebrew]{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{David CLM}

\newfontfamily{\englishfont}[Ligatures=TeX]{Latin Modern Roman}
\setmathrm[Ligatures=TeX]{Latin Modern Roman}

\makeatother

\usepackage{polyglossia}
\setdefaultlanguage{hebrew}
\setotherlanguage{english}
\begin{document}
\noindent שלום \textenglish{Hello} \LRE{
\[
y=\underbrace{x+z}_{\text{const}}=\text{abc}=\mathrm{abc}
\]
}
\end{document}

And here's what it looks like:

Output

Notice the difference in fonts between \text{abc} (uses David CLM) and \mathrm{abc} (uses Latin Modern Roman, which is the desired font). Currently I use \mathrm{} as a workaround, but it's inconvenient for many reasons (e.g. math mode spacing). Is there any way to force \text{} to use the \englishfont variant?

By the way, \text{} also messes up the order of words when the main language is RTL. For example, the command

\text{abc def}=\mathrm{abc\,def}

gives

Second example

i.e. it's not just the font that is problematic here.

1
  • I'd say \text is the wrong command to use here in the first place. It is not a device to providing upright text (add \itshape before your math and see why).
    – daleif
    Commented Dec 18, 2020 at 10:26

2 Answers 2

3

First, note that the command you want is \textnormal, not \text—especially with DavidCLM and amsthm. If you use \text inside a theorem statement, which is italicized, it will inherit the italic formatting of the text around it. And your English and Hebrew italics slant in opposite directions! Using \textnormal resets all text formatting, which is what you want here.

Babel in LuaTeX can detect which language you’re typing in and change fonts automatically. With this set-up, \textenglish, \begin{hebrew}, etc. still work, but they won’t usually do anything because the script you’re typing in will change languages automatically and override them.

If you need to alternate complete paragraphs in English and Hebrew, this won’t be adequate: the layout will stay right-to-left even in English sections. You would need to remove the onchar= options and go back to \textenglish and \begin{english}.

Again, this MWE requires LuaLaTeX. (Since you use Mapping=, you appear to be compiling in XeTeX.)

\documentclass[a4paper]{article}
\tracinglostchars=2
\usepackage{amsmath,amsthm}
\usepackage[bidi=basic,
            layout=sectioning.tabular,
            nil
           ]{babel}
\usepackage{unicode-math}

\defaultfontfeatures{ Scale=MatchUppercase,
                      Ligatures=TeX,
                      Renderer=HarfBuzz }

% The Culmus fonts are free for download at
% https://sourceforge.net/projects/culmus/
\defaultfontfeatures[DavidCLM]{
   UprightFont = *-Medium,
   ItalicFont = *-MediumItalic,
   BoldFont = *-Bold,
   BoldItalicFont = *-BoldItalic,
   Extension = .otf }

\defaultfontfeatures[MiriamCLM]{
  UprightFont = *-Book,
  BoldFont = *-Bold,
  Extension = .ttf }

\babelprovide[import, onchar=ids fonts]{english}
\babelfont{rm}
          [Scale=1.0, Language=Default]{NewComputerModernBook}
\babelfont{sf}
          [Language=Default]{NewComputerModernSansBook}
\babelfont{tt}
          [Language=Default]{NewComputerModernMonoBook}

\babelprovide[import, main, onchar=ids fonts]{hebrew}
\babelfont[hebrew]{rm}
          [Language=Default]{DavidCLM}
\babelfont[hebrew]{sf}
          [Language=Default]{MiriamCLM}
\babelfont[hebrew]{tt}
          [Language=Default]{MiriamMonoCLM}

\setmathfont{NewCMMath-Book}
\setmathrm{NewComputerModernBook}
\setmathsf{NewComputerModernSansBook}
\setmathtt{NewComputerModernMonoBook}

\babeltags{english=english} % Enable \textenglish, \begin{english}, etc.
\babeltags{hebrew=hebrew} % Enable \texthebrew, \begin{hebrew}, etc.

\begin{document}
\noindent שלום Hello
\[
y=\underbrace{x+z}_{\textnormal{const}}=\textnormal{abc}=\mathrm{abc}
\]

\end{document}

David CLM + New Computer Modern Book sample

If you are going back to explicit language tagging, or you want to stick with Polyglossia, there are two quick fixes. One is to define a new command like \mathtext or \mathen as a shortcut for \textnormal{\textenglish{...}}. The other is to wrap your equations in \begin{english} and \end{english} so that the \text commands default to your English font. At least use \textup instead of \text in this case, so you don’t have a head-on collision of italics that slant left and right.

\documentclass[a4paper]{article}
\tracinglostchars=2
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{polyglossia}

\setdefaultlanguage{hebrew}
\setotherlanguage{english}

\defaultfontfeatures{ Scale=MatchUppercase,
                      Ligatures=TeX }

\setmainfont[Script=Hebrew, Scale=1.0]{David CLM}

\newfontfamily{\englishfont}{Latin Modern Roman}
\setmathrm{Latin Modern Roman}

\newcommand\mathtext[1]{\textnormal{\textenglish{#1}}}

\begin{document}
\noindent שלום \textenglish{Hello}

\begin{english}\[
y=\underbrace{x+z}_{\mathtext{const}}=\textup{abc}=\mathrm{abc}
\]\end{english}

\end{document}
8
  • First of all, thank you. I actually work in a LyX environment, and XeTeX is a standard choice there, which is why I prefer the second solution. From what I understand, \textup and \mathrm are recommended for single-word notations (e.g. textual indices), whereas \text is used specifically for textual comments (i.e. more than one word) in math mode, which is what I need (in that regard the last example of "abc def" is better). The solution of declaring a new command is likely easier than wrapping the commands with \begin{english}, which can be pretty messy in LyX.
    – cth
    Commented Dec 18, 2020 at 16:18
  • 1
    @cth The \text, \textup and \textnormal commands are all appropriate for short passages of multiple words in math mode.
    – Davislor
    Commented Dec 18, 2020 at 16:44
  • 2
    @cth Traditionally, \mathrm was to switch to the upright font and keep math spacing, but it was also used for words in math mode, and there’s often no way to tell which use was intended. (Is \mathrm{iff} a word that should have a ligature, or the product of three quantities?) The unicode-math package introduces \symup for single-letter upright math symbols, and using either \symup or \textnormal removes all ambiguity. You can configure \mathrm to mean something close to either. In this set-up, \textup and \mathrm will give you the same font.
    – Davislor
    Commented Dec 18, 2020 at 16:59
  • 1
    @cth And I guess I should mention that \textup would not be appropriate if you change the font family to \sffamily or \ttdamily, as those would still bleed through. You’re better off using \mathtext then.
    – Davislor
    Commented Dec 18, 2020 at 17:40
  • 1
    @cth I made it a little more confusing than it has to be. Short answer: \text or \textnormal puts stuff inside a text box in math mode. Inside that box, you can use all text-mode commands, including changing the language. The \textnormal command resets all font formatting, and \text resets none of it.
    – Davislor
    Commented Dec 18, 2020 at 19:36
1

Since you're using XeLaTeX, you could load the unicode-math package (which automatically loads the fontspec package) and set the global math font via a \setmathfont instruction.

That said, you should use \mathrm and \mathit to assure that the arguments are typeset using the math font's glyphs rather than the text font's glyphs.

For the following screenshot, I employed the Cardo font, as my TeX distribution does not feature David CLM. (I chose Cardo because it looks very different from Latin Modern, making it easy to spot any differences.)

enter image description here

% !TEX TS-program = xelatex
\documentclass[a4paper]{article}
\usepackage{amsmath,amsthm}

\usepackage{unicode-math}
\setmainfont[Ligatures=TeX]{Cardo} % or some other suitable font
\newfontfamily{\englishfont}{Latin Modern Roman}[Ligatures=TeX, Scale=MatchLowercase]
\setmathfont{Latin Modern Math}[Scale=MatchLowercase]
\setmathrm[Ligatures=TeX]{Latin Modern Roman}

\usepackage{polyglossia}
\setdefaultlanguage{hebrew}
\setotherlanguage{english}

\begin{document}
\noindent שלום \quad \textenglish{Hello} \quad Hello

\[ % no need to encase the displayed equation in an \LRE "wrapper"
y=\underbrace{x+z}_{\mathrm{const}}=Hello=\mathit{Hello}=\mathrm{Hello}=\text{Hello}
\]

\LRE{% % of course, \LRE still works
\[
y=\underbrace{x+z}_{\mathrm{const}}=Hello=\mathit{Hello}=\mathrm{Hello}=\text{Hello}
\]
}
\end{document}
3
  • Perhaps I'm missing something, but it seems like unicode-math does not affect \text in your example - the latter still uses the main font (Cardo) as opposed to the standard math font (Latin Modern Roman). I understand that \mathrm is generally preferred over \text but my question was specifically about the \text command. At any rate, according to other answers \textnormal is more appropriate in this case. Thank you!
    – cth
    Commented Dec 18, 2020 at 16:34
  • @cth - The purpose of loading the unicode-math package and of running \setmathfont is to let you type math-y stuff without having to use \LRE directives in a XeLaTeX setup. I thought my code made it clear that you should not be using \text (or for that matter, \textnormal) directives; it looks I was much too subtle. My bad.
    – Mico
    Commented Dec 18, 2020 at 17:16
  • Actually, \LRE was placed there automatically by LyX. I wouldn't normally type it in XeLaTeX. But thank you for explaining it in detail, I appreciate it.
    – cth
    Commented Dec 18, 2020 at 19:25

You must log in to answer this question.

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