Problem statement
The \sisetup{detect-all}
option of the siunitx
package does not work for me as expected in a Beamer presentation, which I would like to typeset as follows:
- Text in Latin Modern Sans (sans serif)
- Math in TeX Gyre Pagella Math (serif)
For reasons beyond this MWE, my setup requires lualatex
and \usepackage{siunitx}
. I would like the siunitx
package to detect whether it is being used in text or math mode and adjust the font accordingly, which I understand is typically done using \sisetup{detect-all}
.
MWE to illustrate the problem
The following MWE (compiled with lualatex
) shows how Beamer incorrectly uses the sans font for siunitx
macros in math mode.
\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 to demonstrate that this problem is specific to Beamer
It appears that this problem is specific to \documentclass{beamer}
, since the following MWE for \documentclass{article}
(again compiled with lualatex
) works entirely as expected.
\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}
Non-satisfactory workaround
In the following MWE, which was inspired by this post and compiled with lualatex
, siunitx
commands use the correct math font. I've added an additional equation*
environment to distinguish between inline and display math (as the required \sisetup
options that I have added also do).
\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}
Why I do not consider this workaround a satisfactory solution
- In an example more complex than the above MWEs, the workaround breaks compilation (I'm still analyzing why that might be the case)
- I still do not know why the problem only appears in Beamer, but not article, which makes me think that a more satisfactory solution might actually have to be grounded in Beamer, not
siunitx
. - I now need to effectively define my math font twice, which makes the code harder to maintain.
\newfontfamily\mymathfont{TeX Gyre Pagella}
after\setmathfont
and then addmath-rm=\mymathfont,
as well asdetect-inline-family=math
anddetect-inline-weight=math
to the\sisetup
block (inspired by tex.stackexchange.com/a/241086/38212). However, I don't quite understand whether this is actually the best solution, and more importantly why it's necessary to explicitly tell siunitx about the math-rm font.beamer
default fonts is sans serif?\def\familydefault{\sfdefault}
. If you add that, you can reproduce inarticle
. It also uses\mathfamilydefault
. That can be set to\rmdefault
or\sfdefault
. Neither of which is what you want here....serif
for the font theme across the board and set the main font to Latin Modern Sans. Do you need Schola for anything? Probably not. If you do, maybe define a special font family for that.