# unicode-math: inline math characters get stretched horizontally when changing font size

I need to change font size mid document. The issue is that the characters in the inline math get stretched (get wider) compared to text mode no matter what method I tried and this looks bad (and wastes page real estate). How can I fix this?

Here's an MWE:

\documentclass{scrbook}
\usepackage{unicode-math}

\begin{document}
Same character width:\\
123456789012345678901234567890\\
$123456789012345678901234567890$\par

\KOMAoptions{fontsize=10}
% or other change in size like \footnotesize \LARGE etc

Different character width:\\
123456789012345678901234567890\\
$123456789012345678901234567890$\par

\end{document}


You could force unicode-math to use the normal latin modern text font for the numbers:

\documentclass{scrbook}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}
\setmathfont[range={"0030-"0039}]{Latin Modern Roman}
\setmathfont[range=\int]{Latin Modern Math} %last font should be a math font

\begin{document}
Same character width:\\
123456789012345678901234567890\\
$123456789012345678901234567890$\par

\KOMAoptions{fontsize=10}
Different character width:\\
123456789012345678901234567890\\
$123456789012345678901234567890 x^3$\par

\end{document}


# Edit

Actually the range command is not necessary. Simply setting the math font like egreg does in his answer solves the problem for me in a modern tex system:

\documentclass{scrbook}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}

\begin{document}
Same character width:\\
123456789012345678901234567890\\
$123456789012345678901234567890$\par

\KOMAoptions{fontsize=10}
Different character width:\\
123456789012345678901234567890\\
$123456789012345678901234567890 x^3$\par

\end{document}


# Edit 2

After looking a bit more closely (and testing other fontsizes) I found the source of the problem: unicode-math is setting up the math with different "math styles". Smaller fontsizes uses e.g. +ssty=0; and this leads to wider numberals (this is a feature, not a bug. Wider chars are more readable in small sizes).

So if one change the overall fontsize one has to setup the math fonts again:

\documentclass[fontsize=11pt]{scrbook}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}

\begin{document}
Same character width:\\
123456789012345678901234567890 abc\\
$123456789012345678901234567890 abc$

\KOMAoptions{fontsize=9}
\setmathfont{Latin Modern Math}
Different character width:\\
123456789012345678901234567890 abc\\
$123456789012345678901234567890 abc$

\end{document}


In the case of the Latin Modern Fonts where the text fonts have optical sizes there is still a difference.

• Can you please tell me the range for A-Z and a-z to add them as well? (as a comment, not in MWE). TY! Dec 23 '14 at 9:26
• Well, the edit is exactly the same as my answer. Dec 23 '14 at 10:15
• @egreg: yes, sorry. But I added a reference to your answer now. Btw: the fontsize=10pt is not the problem, you get a similar effect with e.g. \small. Somehow unicode-math doesn't setup the default font correctly. Dec 23 '14 at 10:21
• @UlrikeFischer Your 1st MWE seems to be the workaround. I just need to find a list with the rest of the characters. Dec 23 '14 at 12:20
• @UlrikeFischer Edit 2 is wonderful. I switched to it and it's flawless in my book. Your 1st MWE (please don't delete it) worked as well after extending the range to 007A plus some other codes. Thank you for your effort. Dec 23 '14 at 16:47

You simply need to specify the math font. Why this happens may be a bug in unicode-math (or a consequence of how fontsize=10 works).

\documentclass{scrbook}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}

\begin{document}

Same character width:\\
123456789012345678901234567890\\
$123456789012345678901234567890$\par

\KOMAoptions{fontsize=10}
% or other change in size like \footnotesize \LARGE etc

Different character width:\\
123456789012345678901234567890\\
$123456789012345678901234567890$\par

\end{document}


The small shift in the displays is due to what seems a bug in the Latin Modern Math font; if I do

\sbox0{78}\showbox0
\sbox0{$78$}\showbox0


with the normal scrbook setting (11pt), then I get

\hbox(7.40219+0.24089)x10.95
.\EU1/lmr/m/n/10.95 78


for the first box, while I get

\hbox(7.40219+0.2409)x11.09236
.\mathon
.\EU1/LatinModernMath(0)/m/n/10.95 glyph#24
.\kern0.14235
.\EU1/LatinModernMath(0)/m/n/10.95 glyph#25
.\mathoff


The difference in size is 11.09236 - 10.95 = 0.14236 (the difference is 1sp due to binary arithmetic, not relevant). The kern is added between 7 and any digit. No other kerning is inserted between digits.

• I actually need to use \setmainfont{TeX Gyre Termes} and \setmathfont{TeX Gyre Termes Math} which have the same [bad] result. Ulrike Fischer's workaround seem to fit my [extra] requirements. Looked like a bug to me as well. Thank you! Dec 23 '14 at 9:47
• @alwaysask Why not saying that in your question? If I add \setmainfont{TeX Gyre Termes} and \setmathfont{TeX Gyre Termes Math}, I have no problem with the original example. Are you sure you have an up-to-date TeX distribution? Dec 23 '14 at 9:49
• @egreg I found what's wrong (not the solution). Make the fontsize in KOMAoptions something other than 10 or 7 (my book uses 9) and your MWE will go wrong again (i.e set \KOMAoptions{fontsize=9}. Dec 23 '14 at 11:37