# Mathematical Operators such as \lim are set in wrong font after changing mainfont

In my document I need to use Constantia as main font and Arial for different parts of the document. Therefore I use LuaLaTeX to compile the document. Furthermore I need siunitx and unicode-math. The actual problem arises when I use \SI{}{} for the first time in my document, since afterwards mathematical operators are set in Arial again (even though the main font is Constantia).

MWE:

\documentclass{scrreprt}
\usepackage{fontspec}
\setmainfont{Constantia}
\usepackage[output-decimal-marker=.,exponent-product=\cdot,per-mode=symbol]{siunitx}
\sisetup{detect-all}
\AtBeginDocument{\sisetup{math-rm = \symup}}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}
\begin{document}

\setmainfont{Arial}

\setmainfont{Constantia}

$\lim\limits_{x\rightarrow \infty}$ \SI{25}{\percent} $\lim\limits_{x\rightarrow \infty}$
\end{document}


yields:

As can be seen, "lim" is set in Arial. Commenting \setmainfont{Arial} helps:

Does anybody know where this comes from and how it can be fixed? Unfortunatelly I need to use the different fonts.

Regards Julian

• Welcome to TeX.SX! Why are you changing \setmainfont after \begin{document}? Jul 19, 2018 at 15:11
• Mhhh, it looks as though siunitx' \SI command influences the math mode after it, that should definitely not happen and I guess that should be reported at github.com/josephwright/siunitx/issues. At the same time I have the feeling that changing the main font with \setmainfont as often as you do does not constitute best practice. You should probably declare Arial as \newfontfamily and use that if appropriate. Jul 19, 2018 at 15:22
• \setmathrm{Constantia}? Jul 20, 2018 at 1:41
• @egreg This comes from the template I am using Jul 20, 2018 at 7:40
• @moewe I will try this, thank you for the hint Jul 20, 2018 at 7:41

The unicode-math package is supposed to set the operator font to the main font of the document. I’ve found that to be buggy sometimes. Here, it appears to be related to an interaction between fontspec and siunitx, but you knew that.

I haven’t attempted to debug this, but I do know a workaround: \setoperatorfont{\rmfamily}. Could be very handy if you’re changing the document font around or want to load a different font family for operator names.

\documentclass[varwidth, preview]{standalone}
\usepackage{fontspec}
\setmainfont{Constantia}
\defaultfontfeatures{Scale=MatchLowercase}
\usepackage[output-decimal-marker=.,exponent-product=\cdot,per-mode=symbol]{siunitx}
\sisetup{detect-all}
\AtBeginDocument{\sisetup{math-rm = \symup}}
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}
\setoperatorfont{\rmfamily}

\begin{document}

% These confuse unicode-math:
\setmainfont{Arial}
\setmainfont{Constantia}

$\lim\limits_{x\rightarrow \infty}$ \SI{25}{\percent} $\lim\limits_{x\rightarrow \infty}$
\end{document}


Aside from that, I added two other lines: setting Scale= as a default font feature so all the fonts you load will match, and explicitly including amsmath.

I would strongly advise against using \setmainfont inside the document body, however. It would be better to load the families you need in your preamble, such as \setsansfont{Arial} or \newfontfamily\yourfavoritefont{Comic Sans}, and select a font family inside the document. You can also set these as your operator font and use the range= option of \setmathfont to change your math symbols to match the text font.

• Thank you for this hint, it works very well to just use {\sffamily ... } with Arial set as sans font. I will thus mark this answer as solution. Jul 20, 2018 at 9:29