问题陈述
\sisetup{detect-all}
在 Beamer 演示文稿中,该包的选项无法siunitx
按预期发挥作用,我希望按如下方式排版:
- 拉丁现代无衬线字体(sans serif)文本
- TeX 中的数学 Gyre Pagella Math (serif)
由于超出此 MWE 的原因,我的设置需要lualatex
和\usepackage{siunitx}
。我希望siunitx
包能够检测它是在文本模式还是数学模式下使用,并相应地调整字体,据我所知,这通常是使用来完成的\sisetup{detect-all}
。
MWE 来说明问题
以下 MWE(使用 编译lualatex
)显示了如何siunitx
Beamer在数学模式下错误地使用 sans 字体来显示宏。
\documentclass{beamer}
\usefonttheme{professionalfonts}% Turn fonts over to 'fontspec'
%\usefonttheme[onlymath]{serif}% Does /not/ solve the problem
\usepackage{fontspec}%
\setmainfont{TeX Gyre Schola}%
\setsansfont{Latin Modern Sans}%
\usepackage{unicode-math}%
\setmathfont{TeX Gyre Pagella Math}%
\usepackage{siunitx}%
\sisetup{%
detect-all,%
}%
\begin{document}
\begin{frame}
\begin{itemize}
\item Regular numbers in text mode: sans font, as expected: 2469
\item SI numbers in text mode: sans font, as expected: \num{2469}
\item Numbers in math mode: math font, as expected: $2469$
\item SI numbers in math mode: sans font (not expected): $\num{2469}$ % <== !!!
\end{itemize}
\end{frame}
\end{document}
MWE 证明该问题特定于 Beamer
看来这个问题是特定于的\documentclass{beamer}
,因为以下 MWE \documentclass{article}
(再次用 编译lualatex
)完全按预期工作。
\documentclass{article}%
\usepackage{fontspec}%
\setmainfont{TeX Gyre Schola}%
\setsansfont{Latin Modern Sans}%
\usepackage{unicode-math}%
\setmathfont{TeX Gyre Pagella Math}%
\usepackage{siunitx}%
\sisetup{%
detect-all,%
}%
\begin{document}
\begin{itemize}
\item Regular numbers in text mode: main font, as expected: 2469
\item SI numbers in text mode: main font, as expected: \num{2469}
\item Numbers in math mode: math font, as expected: $2469$
\item SI numbers in math mode: math font, as expected: $\num{2469}$
\end{itemize}
\end{document}
不令人满意的解决方法
在以下的 MWE 中,灵感来自于这个帖子并且使用 进行编译后lualatex
,siunitx
命令会使用正确的数学字体。我添加了一个额外的equation*
环境来区分内联数学和显示数学(因为\sisetup
我添加的必需选项也是如此)。
\documentclass{beamer}
\usefonttheme{professionalfonts}% Turn fonts over to 'fontspec'
%\usefonttheme[onlymath]{serif}% Did /not/ solve the problem
\usepackage{fontspec}%
\setmainfont{TeX Gyre Schola}%
\setsansfont{Latin Modern Sans}%
\usepackage{unicode-math}%
\setmathfont{TeX Gyre Pagella Math}%
\newfontfamily\mymathfont{TeX Gyre Pagella Math}% <== NEWLY ADDED
\usepackage{siunitx}
\sisetup{%
math-rm=\mymathfont,% <== NEWLY ADDED
detect-all,%
detect-display-math=true,% <== NEWLY ADDED
detect-inline-family=math,% <== NEWLY ADDED
detect-inline-weight=math,% <== NEWLY ADDED
}%
\begin{document}
\begin{frame}
\begin{itemize}
\item Regular numbers in text mode: sans font, as expected: 2469
\item SI numbers in text mode: sans font, as expected: \num{2469}
\item Numbers in math mode: math font, as expected: $2469$
\item SI numbers in inline math: math font (now as expected): $\num{2469}$% <== NOW WORKS
\item SI numbers in display math: math font (as expected)
\begin{equation*}
\num{2469}
\end{equation*}
\end{itemize}
\end{frame}
\end{document}
为什么我不认为这个解决方法是一个令人满意的解决方案
- 在比上述 MWE 更复杂的示例中,该解决方法会破坏编译(我仍在分析为什么会出现这种情况)
- 我仍然不知道为什么该问题只出现在 Beamer 中,而不是文章中,这让我想到更令人满意的解决方案实际上可能必须以 Beamer 为基础,而不是
siunitx
。 - 我现在需要有效地定义我的数学字体两次,这使得代码更难维护。
答案1
这大致是由于评论中解释的原因:
\documentclass{beamer}
\usefonttheme{professionalfonts}
\usefonttheme{serif}
\usepackage{fontspec}
\setmainfont{Latin Modern Sans}%TeX Gyre Schola}
% \setsansfont{Latin Modern Sans}
\usepackage{unicode-math}
\setmathfont{TeX Gyre Pagella Math}
\usepackage{siunitx}
\sisetup{%
detect-all,%
}
\begin{document}
\begin{frame}
\begin{itemize}
\item Regular numbers in text mode: sans font, as expected: 24693
\item SI numbers in text mode: sans font, as expected: \num{24693}
\item Numbers in math mode: math font, as expected: $24693$
\item SI numbers in math mode: sans font (not expected): $\num{24693}$ % <== !!!
\end{itemize}
\[ \sin \theta^2 + \cos \theta^2 = 1 \num{2469} \]
$x^3 + f(y) = \sqrt[5]{z}$
1234567890
$1234567890$
$\num{1234567890}$
\end{frame}
\end{document}