1

In a sans serif document (with \renewcommand{\familydefault}{\sfdefault}) I'd like to have units and numbers in text mode set in sans serif, and numbers and units in math mode (inline or display) in roman as all other math is also in roman.

I've created a test document to try different options, but I've not found options in siunitx (version 3) that leads to the desired behavior (see code at the end). In the images I've marked the cases where the font family is not what I would expect.

Without any \sisetup (same as \sisetup{reset-text-family = false, reset-text-series = false, reset-text-shape = false}):

Example text without any sisetup

With \AtBeginDocument{\sisetup{unit-font-command = \mathrm}}:

Example text with unit-font-command = mathrm

With \sisetup{detect-all = true}, regardless of unit-font-command:

Example text with detect-all = true

With \sisetup{mode = match}, regardless of unit-font-command:

Example text with mode = match

\documentclass{article}
\usepackage[margin=3cm]{geometry} % just to fit 3 tables in one line
\usepackage{siunitx}
%\AtBeginDocument{\sisetup{unit-font-command = \mathrm}}
%\sisetup{detect-all = true}
%\sisetup{mode = match}
%\sisetup{reset-text-family = false, reset-text-series = false, reset-text-shape = false}
\renewcommand{\familydefault}{\sfdefault} % My document should be in sans serif

\begin{document}%
Some text where the voltage $u$ is \qty{5}{\kilo\volt},
also containing the equation \(u = \qty{5}{\kilo\volt}\).
And display math:
\[
    u = \qty{5}{\kilo\volt}
\]
\end{document}

Seemingly related questions that don't help:


I have to admit that I find the font configuration in siunitx v3 pretty complex and confusing, and I've not gone beyond trial and error. Although it is probably quite powerful once understood. It does not help though, that most search results now yield obsolete solutions for version 2, which don't apply to the new version 3. In my personal humble opinion it would have been better to use a new name for the rewritten package to avoid this confusion and breakage (of course at the cost of other drawbacks).

1

1 Answer 1

1

Solution

The following set of options will always match the font of the units to the surrounding material, whether text mode or math mode:

\AtBeginDocument{ % See Note below
  \sisetup{
    mode=match,
    unit-font-command = \mathrm, % See Note below
    reset-text-family=false,
    reset-text-series=false,
    reset-text-shape=false
  }
}

Text example showing the result of mode=match, unit-font-command = \mathrm

Table showing the effect of different surrounding fonts on siunitx

Note: In a future version of siunitx (when this commit makes it into a release), the setting unit-font-command = \mathrm and the \AtBeginDocument will not be necessary anymore. Currently, this is necessary because siunitx assumes that the math font is also sans serif when the default font (i.e. \familydefault) is sans serif. With the new version, this will only apply when the beamer document class is used.


Old answer

Okay, I've found a solution that is acceptable to me:

\AtBeginDocument{\sisetup{mode=match, unit-font-command = \mathrm}}

However, this case still yields inconsistent fonts when using \qty in a block of text formatted with \textrm (or \rmfamily). In this case, the sans font is used, instead of the roman font. For me this is does not matter because I don't have large roman blocks of text, but it might be relevant for others.

As above, I've marked those cases in yellow where the font family does not match the surrounding text:

Tables comparing several different font options of siunitx v3

\documentclass{article}
\usepackage[margin=3cm]{geometry} % just to fit 3 tables in one line
\usepackage{siunitx}
%\AtBeginDocument{\sisetup{unit-font-command = \mathrm}}
%\sisetup{detect-all = true}
%\sisetup{mode = match}
%\sisetup{reset-text-family = false, reset-text-series = false, reset-text-shape = false}
\renewcommand{\familydefault}{\sfdefault} % My document should be in sans serif

\newcommand{\sifonttable}[1]{%
    \fbox{\begingroup\small
    \sisetup{#1}
    \begin{tabular}{@{}lll@{}}
        \multicolumn{3}{@{}p{4.5cm}@{}}{\scriptsize\texttt{\detokenize{#1}}} \\
        & Text & Math \\
        (default) &           u is \qty{5}{\kilo\volt} &           \(u = \qty{5}{\kilo\volt}\) \\
        rmfamily  & \rmfamily u is \qty{5}{\kilo\volt} & \rmfamily \(u = \qty{5}{\kilo\volt}\) \\
        sffamily  & \sffamily u is \qty{5}{\kilo\volt} & \sffamily \(u = \qty{5}{\kilo\volt}\) \\
        bfseries  & \bfseries u is \qty{5}{\kilo\volt} & \bfseries \(u = \qty{5}{\kilo\volt}\) \\
        itshape   & \itshape  u is \qty{5}{\kilo\volt} & \itshape  \(u = \qty{5}{\kilo\volt}\) \\
        % slshape  & \slshape  u is \qty{5}{\kilo\volt} & \slshape  $u = \qty{5}{\kilo\volt}$ \\
        % mathrm   &                                    & $\mathrm{u = \qty{5}{\kilo\volt}}$ \\
        % mathsf   &                                    & $\mathsf{u = \qty{5}{\kilo\volt}}$ \\
        % mathbf   &                                    & $\mathbf{u = \qty{5}{\kilo\volt}}$ \\
        % mathit   &                                    & $\mathit{u = \qty{5}{\kilo\volt}}$ \\
    \end{tabular}
    \endgroup}
}

\begin{document}%
{\sisetup{mode=match, unit-font-command=\mathrm}
Some text where the voltage $u$ is \qty{5}{\kilo\volt},
also containing the equation \(u = \qty{5}{\kilo\volt}\).
And display math:
\[
    u = \qty{5}{\kilo\volt}
\]
}

\subsubsection*{Single options}
\sifonttable{}
\sifonttable{detect-all=true}
\sifonttable{detect-mode=true}
\sifonttable{mode=match}
\sifonttable{unit-font-command=\mathrm}
\sifonttable{reset-text-family=false}
\sifonttable{reset-text-series=false}
\sifonttable{reset-text-shape=false}
\sifonttable{propagate-math-font=true}
\sifonttable{reset-math-version=true}
\sifonttable{text-family-to-math=true}
\sifonttable{text-series-to-math=true}


\subsubsection*{Combinations}
\sifonttable{detect-all=true, mode=match}
\sifonttable{detect-all=true, unit-font-command=\mathrm}
\sifonttable{detect-all=true, mode=match, unit-font-command=\mathrm}
\sifonttable{mode=match, unit-font-command=\mathrm}
\sifonttable{mode=match, unit-font-command=\mathrm, reset-text-series=false}
\sifonttable{reset-text-family=false, reset-text-series=false, reset-text-shape=false}
\end{document}
1
  • I might adjust how I support beamer here: it's the main reason for setting unit-font-command = \mathsf when the document body is sanserif
    – Joseph Wright
    Apr 13, 2023 at 13:26

You must log in to answer this question.

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