所附示例(不是真正的 MWE)比我在这里更好地解释了该问题:
% !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}
这两个 (1号和2号) 问题类似,但mode=text
不起作用,因为数字设置在 中OldStyle
。相反,mode=math
将单位设置为 (斜体) 数学字体。
此解决方案正在寻找相同的东西,但它似乎不同,因为他们不使用fontspec
/ unicode-math
。
我可以坚持
\sisetup{number-math-rm=\mathnormal}
目前看来,这种方法是可行的,但似乎是一种非常狭隘的解决方案,一旦出现更复杂的排版,它就会很快失效。有没有更好的解决方案?
答案1
您可以为文本模式定义一个新的字体系列(此处为绿色):
\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}