# siunitx and XeLaTeX with OldStyle in text, but Lining in math

The attached example (not really an MWE) explains the problem better than I probably can here:

% !TeX program = xelatex
\documentclass{scrreprt}
\usepackage{siunitx}
\sisetup{%
%       detect-mode,
%       detect-all,
%       detect-display-math,
%       math-rm=\mathnormal,
}%
\usepackage{unicode-math}
\setmainfont[Numbers=OldStyle,Color=2244FF]{TeX Gyre Pagella}
\setmathfont[Numbers=Lining,Color=FF4422]{TeX Gyre Pagella Math}%lining as 'opposite' of OldStyle

\linespread{1.3}

\begin{document}
Wanted behaviour:

Text-Style numbers: 123456789.  Math-Style numbers: $$123456789$$.

In Display-Math, only the math font should be used. This works:
$$f(x) = 23.1(2) \times 10^{-2} x^2$$
However, \textit{siunitx} does not detect the (surrounding) math font and sets its digits using the text font. Triggering \textit{detect-all} does not change this either:

%   \sisetup{detect-all}

$$f(x) = \num{23.1(2)e-2} x^2$$
Even explicitly requesting math-font does not seem to work:
$$\num{10} \quad \SI{20}{\newton} \quad \SI[mode=text]{30}{\newton} \quad \SI[mode=math]{40}{\newton} \quad \SI[number-mode=math]{50}{\newton}.$$

Does not work outside of math mode either. These are expected to use math mode:

\SI[mode=math]{40}{\newton} \quad \SI[number-mode=math]{50}{\newton}.

This approach seems to work, since it will always use the math-font for its numbers and picks the text-font for its units, \textbf{as long as we are in math-mode}:

\sisetup{number-math-rm=\mathnormal,unit-math-rm=\mathrm,detect-mode}

$$\SI{40}{\newton}$$ \quad \SI{40}{\newton} \quad $$\num{45.2(1)e-2}$$ \quad \num{45.32(12)e-27}

However, if we try to replicate the same for text-mode, an error is thrown:

%   \sisetup{number-text-rm=\mathnormal,unit-text-rm=\textrm}

%   $$\SI{40}{\newton}$$ \quad \SI{40}{\newton} \quad $$\num{45.2(1)e-2}$$ \quad \num{45.32(12)e-27}

\vspace{2\baselineskip}

Also, if we use \textit{mathrm} instead of \textit{mathnormal}, it no longer works, and \textit{siunitx} picks the text-font for its numbers again. I do not know the difference here and why it does this:

\sisetup{number-math-rm=\mathrm}

$$\SI{40}{\newton}$$ \quad \SI{40}{\newton} \quad $$\num{45.2(1)e-2}$$ \quad \num{45.32(12)e-27}
\end{document}


These two (Number 1 and Number 2) questions are similiar, but mode=text does not do the trick, since then the numbers are set in OldStyle. Inversely, mode=math sets the units in (italic) math font.

This solution is looking for the same thing, but it seems different, since they don't use fontspec / unicode-math.

I can stick with

        \sisetup{number-math-rm=\mathnormal}


and it works for now, but it seems to be a very narrow solution that will fall apart quickly, once more sophisticated typesetting occurs. Is there a better solution?

## 1 Answer

You can define a new font family for the text mode (here in green):

\documentclass{scrreprt}
\usepackage{siunitx}
\sisetup{%
number-text-rm=\unitnumberfont,
detect-mode,
}%
\usepackage{unicode-math}
\setmainfont[Numbers=OldStyle,Color=2244FF]{TeX Gyre Pagella}
\newfontfamily\unitnumberfont{TeX Gyre Pagella Math}[Color=00FF00]
\setmathfont[Color=FF4422,mathrm=sym]{TeX Gyre Pagella Math}%

\linespread{1.3}

\begin{document}
Wanted behaviour:

Text-Style numbers: 123456789.  Math-Style numbers: $$123456789$$.

In Display-Math, only the math font should be used. This works:
$$f(x) = 23.1(2) \times 10^{-2} x^2$$

$$f(x) = \num{23.1(2)e-2} x^2$$

$$\num{10} \quad \SI{20}{\newton} \quad \SI{30}{\newton} \quad \SI{40}{\newton} \quad \SI{50}{\newton}.$$

\SI{40}{\newton} \quad \SI{50}{\newton}.
\end{document}


• Thank you, this is looking very clean. What keeps us from using the already existing math font family, and why is a new one required? Can't we (pseudo-command) say number-text-rm = \mathfont, which refers to the font set in \setmathfont? I also understand the meaning of mathrm=sym now. But since sisetup uses the newly created font family, where mathrm=sym is not set, what does it do for us now? Is it just a precaution / best practice? – Hans Lollo Dec 31 '18 at 13:57