# Incompatibility of siunitx and sansmath?

I seem to have stumbled upon an inconsistency when using both siunitx and sansmath - I need to have the same expressions in roman and sans-serif fonts.

Consider the following example:

\documentclass{article}
%\usepackage{sansmath}  %% first without sansmath
\usepackage{siunitx}
\sisetup{detect-all=true}
\begin{document}
Roman: $F_1=\SI{12.34}{kt/m^{3}}$ (outside math mode: \SI{12.34}{kt/m^{3}})
\bigskip

\sffamily  sffamily:
$F_1=\SI{12.34}{kt/m^{3}}$ (outside math mode: \SI{12.34}{kt/m^{3}})
\bigskip

%\sansmath plus sansmath:
%$F_1=\SI{12.34}{kt/m^{3}}$ (outside math mode: \SI{12.34}{kt/m^{3}})
\end{document}


I get the following result:

So indeed, siunitx detects the sf-ness (and there is no difference when using math mode). However, the F_1 is still roman.

But if I uncomment the sansmath package, like so:

\documentclass{article}
\usepackage{sansmath}  %% first without sansmath
\usepackage{siunitx}
\sisetup{detect-all=true}
\begin{document}
Roman: $F_1=\SI{12.34}{kt/m^{3}}$ (outside math mode: \SI{12.34}{kt/m^{3}})
\bigskip

\sffamily  sffamily:
$F_1=\SI{12.34}{kt/m^{3}}$ (outside math mode: \SI{12.34}{kt/m^{3}})
\bigskip

\sansmath plus sansmath:
$F_1=\SI{12.34}{kt/m^{3}}$ (outside math mode: \SI{12.34}{kt/m^{3}})
\end{document}


the result becomes So now siunitx typesets the numbers, but not the letters in the unit, in roman font, even before \sansmath is used (or if it is not used at all), but not when called without math mode! Redefining math-rm=\mathsf and the like did not work for me.

As a workaround I could use e.g. $F_1=12.34\,\si{kt/m^{3}}$, but that screws up some of the nice features of siunitx (e.g. spacing and automatic change of decimal sign), and the exponent 3 in the unit is still roman. Is there anything smarter, such as a suitable font redefinition?

• Welcome to TeX.SX! – Bobyandbob Aug 7 '17 at 14:03
• For the last example see here tex.stackexchange.com/questions/164249/… (\sansmath is internally simply \mathversion{sans}. – Ulrike Fischer Aug 7 '17 at 14:14
• @UlrikeFischer Yes, that solved it - thanks! (I didn't even know the \mathversion command.) – Toffomat Aug 7 '17 at 15:14
• @Toffomat This solves it for the example, but actually \sansmath does a bit more (try out some variables to see the problem). I will add an answer in a few minutes. – Ulrike Fischer Aug 7 '17 at 15:17
• @UlrikeFischer Indeed it seems to be more complicated -- I had to use \mymathversion{sans}\sansmath in my real document, but then it works... – Toffomat Aug 7 '17 at 15:25

sansmath changes the type of the numbers from "mathalpha" to "mathord". This means that numbers no longer change their font in \mathXX commands and so the internal \mathsf from siunitx has no effect on the numbers.

There is no easy work-around: changing the type back to mathalpha would help siunitx but break all other numbers in equations (they would be slanted now). Using a text font in siunitx doesn't work either -- one get problems with superscript. Imho the only thing one can do is that to load sansmath only if one is also willing to use it, that means to issue a \sansmath (with a small correction):

\documentclass[parskip=half-]{scrartcl}
\usepackage{sansmath}  %% first without sansmath

\usepackage{siunitx,amsmath}

\sisetup{detect-all}

\makeatletter
\newcommand{\mysansmath}{%
\def\unboldmath{%
\@nomath\unboldmath
\sansmath%
}%
}
\makeatother

\begin{document}
\minisec{math commands don't work on numbers}

$\mathsf{123456abc}$  $\mathbf{123456abc}$ $\mathit{123456abc}$

\minisec{sansmath}

\sansmath plus sansmath:
$F_1=\SI{12.34e2}{kt/m^{3}}$

\mysansmath plus mysansmath:
$F_1=\SI{12.34e2}{kt/m^{3}}$

outside math mode: \SI{12.34e2}{kt/m^{3}}

\sffamily

outside math mode: \SI{12.34e2}{kt/m^{3}}

\end{document}
`