4

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

Example Description

% !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:
    \begin{equation}
        f(x) = 23.1(2) \times 10^{-2} x^2
    \end{equation}
    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}

    \begin{equation}
        f(x) = \num{23.1(2)e-2} x^2 
    \end{equation}
    Even explicitly requesting math-font does not seem to work:
    \begin{equation}
        \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}.   
    \end{equation}

    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 Answers1

5

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:
    \begin{equation}
        f(x) = 23.1(2) \times 10^{-2} x^2
    \end{equation}

    \begin{equation}
        f(x) = \num{23.1(2)e-2} x^2
    \end{equation}

    \begin{equation}
        \num{10} \quad \SI{20}{\newton} \quad \SI{30}{\newton} \quad \SI{40}{\newton} \quad \SI{50}{\newton}.
    \end{equation}

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

enter image description here

Ulrike Fischer
  • 327,261
  • 1
    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?

    – Alex Povel Dec 31 '18 at 13:57
  • Starting with siunitx major version 3, commands like text-rm, number-text-rm etc. are removed. I now use text-font-command to achieve this effect. – Alex Povel Aug 20 '21 at 12:59