8

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}

enter image description here

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}

enter image description here

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}

enter image description here

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.
16
  • It seems that a workaround is to \newfontfamily\mymathfont{TeX Gyre Pagella} after \setmathfont and then add math-rm=\mymathfont, as well as detect-inline-family=math and detect-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.
    – Florian H.
    Jan 28, 2016 at 2:21
  • Also, why is this required for beamer, but not for article?
    – Florian H.
    Jan 28, 2016 at 2:33
  • 1
    Maybe because the beamer default fonts is sans serif?
    – Zarko
    Jan 28, 2016 at 3:03
  • 1
    Beamer does \def\familydefault{\sfdefault}. If you add that, you can reproduce in article. It also uses \mathfamilydefault. That can be set to \rmdefault or \sfdefault. Neither of which is what you want here....
    – cfr
    Jan 28, 2016 at 3:26
  • 1
    Use 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.
    – cfr
    Jan 28, 2016 at 3:32

1 Answer 1

6

This works for roughly the reasons explained in the comments:

\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}

Beamer with unicode maths

2
  • Mmh, I don't quite understand why the detect-display-math and detect-inline-* options are no longer required in \sisetup?
    – Florian H.
    Jan 28, 2016 at 4:06
  • Because setting serif stops, I think, the font switching associated with the default maths and text families when you switch in and out. And somehow, siunitx is sensitive to that even though unicode-math overrides it. I think. Like I said, I didn't trace this through Beamer's code. I had a grep and then guessed what might work, based on half an idea of what might be going wrong. Too sleepy now to thing carefully about it. Joseph will know, though ;). (Or you can ask for your money back.)
    – cfr
    Jan 28, 2016 at 4:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .