TransWikia.com

How do I change languages for text in math mode?

TeX - LaTeX Asked by cth on May 24, 2021

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.

2 Answers

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}

newcommandmathtext[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}

Correct answer by Davislor on May 24, 2021

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}

Answered by Mico on May 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP