# Different fonts inside and outside math-mode

When processing the simple file:

\documentclass{report}

\begin{document}
1234567890

$1234567890$
\end{document}


with pdflatex one get a PDF with one font only (CMR10), but when processing with either xelatex or lualatex the produced PDF use Latin Modern Roman 10 outside math-mode and CMR10 inside.

What are the reasons behind this?

• Because LaTeX now supports UTF-8 by default, the font should also be able to to reflect this. Therefore, when using Xe/LuaTeX, the LaTeX format automatically switches to TU font encoding. The CMR10 font is not available in TU encoding (and never will be), therefore its closest relative Latin Modern Roman 10 is loaded instead. In math mode this is a little more tricky that is why standard OML/OMS encoding is retained. I don't know whether there exist any plans to make UTF-8 available in math as well. – Henri Menke Feb 23 at 1:09
• What is the tricky part of getting the TU encoding in math-mode? – Paulo Ney Feb 23 at 1:19
• The traditional setup was to have different families, i.e. \mathbf, \mathcal, etc. switched to a different family. This is no longer possible in Unicode, because all the symbols come from a single font. Therefore the family switching approach doesn't work anymore and has to be replaced by a much more complex system which switches mathcodes back and forth. This is implemented in the unicode-math package. You'll also notice why this is problematic because with the unicode-math package TeX is suddenly much slower due to having to reassign thousands of mathcodes all the time. – Henri Menke Feb 23 at 1:28
• @HenriMenke Whoops, thanks for pointing out my misreading of the example. Want to post your answer? – Davislor Feb 23 at 1:35
• To complement to @HenriMenke’s comment, \mathbf switches to bold upright text font while \mathcal switches to OMS math font. Math font encoding was never really actively developed (see LaTeX font encoding guide p. 3, the last paragraphs before Section 1.3). – Ruixi Zhang Feb 23 at 2:10

Since TeX Live 2018 LaTeX now UTF-8 by default, because the “Unicode revolution” has been completed1. This makes it superfluous to add \usepackage[utf8]{inputenc} when using pdfLaTeX.

The UTF-8 support in pdfLaTeX is fake however and you cannot expect to get a meaningful output when you paste arbitrary UTF-8 characters into the input, because the underlying pdfTeX engine is inherently limited to 8-bit encoding.

This is quite different with Xe/LuaTeX. These engines support UTF-8 by default2. Traditional TeX fonts are very limited in the glyphs they offer3. This is definitely not enough to describe UTF-8, so the font also has to cover the full UTF-8 range4. To this end the TU encoding was introduced.

By default LaTeX loads the Computer Modern (CM) family of fonts. The CMR10 font is not available in TU encoding (and never will be), therefore its closest relative Latin Modern Roman 10 is loaded instead.

Now the question remains, why is Latin Modern not used in math mode? That is because in math mode things are a little more tricky. The traditional setup was to have different families and commands like \mathbf, \mathcal, etc. switched to a different family. This is no longer possible in Unicode, because all the symbols come from a single font. Therefore the family switching approach doesn't work anymore and has to be replaced by a much more complex system which swaps mathcodes back and forth. This is implemented in the unicode-math package. You'll also notice why this is problematic because with the unicode-math package TeX is suddenly much slower due to having to reassign thousands of mathcodes all the time. That is why currently standard OML/OMS encoding is retained.

1 Everyone stores their files in UTF-8 nowadays and most software understands UTF-8. Just think of Emojies (they are everywhere). They are part of UTF-8, so everything that can display Emojies has to support UTF-8.
2 in fact LuaTeX will reject your file if it is not UTF-8
3 T1 encoding can only encode 256 glyphs
4 whether the font has the required glyph is another question

• The change was upstream, in the April 2018 release of LaTex, if you want to get specific about when and where. It’s therefore true of all distributions. – Davislor Feb 23 at 5:52
• I still personally include \usepackage[utf8]{inputenc} in case someone tries to compile on an older distro. – Davislor Feb 23 at 5:53
• The switch to TU for the unicode engines was at the begin of 2017, it is rather unrelated to the UTF8 default (added in 2018) which affects only pdftex and the other 8bit engines. – Ulrike Fischer Feb 23 at 8:09
• @UlrikeFischer Only the team can answer this, but I'm rather sure that the TU encoding was a prerequisite for the UTF-8 default. I'm relatively sure that there is a clear relation between them. – Henri Menke Feb 23 at 8:27
• The "UTF8 default" as you call it does what inputenc does in the document and inputenc is not relevant for the unicode engines. – Ulrike Fischer Feb 23 at 8:32

The font setup for text and math are quite separated tasks in TeX -- text fonts has to care about things like accented chars and hyphenation, math fonts about lots of symbols and correct placement of sup- and superscripts.

Your example uses only a few glyph which in pdftex can be taken from the same font. But if you add e.g. a minus (-), or chars (abc) the used fonts will be different inside and outside math. And if you switch to the recommended T1-encoding for the text with \usepackage[T1]{fontenc} there will be even differences for the numbers.

For the unicode engines there is a difference even in the default setup as the use of a unicode font as text font is more or less mandatory to get accented chars and hyphenation right (see e.g. https://tex.stackexchange.com/a/470987/2388). So at the begin of 2017 the LaTeX-kernel changed the default for this engines so that users weren't forced to load fontspec anymore.

In math hyphenation and accented chars are not really relevant, so the legacy math setup works find in the unicode engine. But even if the math setup is changed to use an unicode font it wouldn't be the same as the textfont but e.g. latin modern math.

• I do understand most of your argument, but I do NOT understand why the alphabet inside math-mode NEEDS to be any different from the one used in text-mode. In fact MTProII is a counter-example to that -- it uses numerals, roman and italics that are copy-cats of Time News Roman, and even though they are different fonts you end up with uniform text and formulas. Latin Modern is not Computer Modern (and never will be) and mixing the two offends the senses of almost anyone - it is that visible. Observe that we are not talking about a lot of character - is just alphabet and numbers mostly. – Paulo Ney Feb 23 at 16:45
• A math font is not only a bunch of glyphs. Much more important are values needed for the placement. A standard text font in tex has normally only 7 fontdimens (e.g. the em and ex values), math fonts much more, e.g. the amount a subscript need to be shifted. Check e.g. "C Legacy TEX font dimensions" in the unicode-math documentation. – Ulrike Fischer Feb 23 at 16:55
• You can quite easily get latin modern also inside math, both with legacy fonts and with unicode-math. That this is not the default is not due to some necessity but only due to the conservative nature of latex - defaults are not easily changed. – Ulrike Fischer Feb 23 at 17:00
• I do understand now and unicode-math is part of the equation. Many thanks for the details. – Paulo Ney Feb 23 at 17:06