SIrange typeset different in text and math mode

The range commands (\numrange and \SIrange) are only intended for use in text mode: this is covered in the documentation. The reason is that it's not possible to ensure in all cases that spaces are correctly dropped at the end of a line if you start off in math mode, so for example

$\SIrange{10}{20}$

would have the wrong spacing with the standard settings if the line break occurs at the to. As such, the code does not attempt to handle the case where it is used in math mode. I guess I can add a warning for this situation.


As written in my comment, you can tell LaTeX to behave differently if you are inside or outside the math mode using the TeX command \if<>\else<>\fi; specifically using the conditional form:

\ifmmode<expression inside math>
\else
 <expression outside math>
\fi

So if this code is applied to your MWE:

\documentclass{article}
%
\usepackage{siunitx}
\sisetup{range-phrase=%
 \ifmmode\mathbin{-}
 \else
  \thinspace\textendash\thinspace
 \fi%
}
%
\begin{document}
%
\centering
\SIrange{e-12}{e-10}{\second}
\[
\SIrange{e-12}{e-10}{\second}
\]
%
\end{document}

The result is:

Result of the MWE with two identical outputs


Update

There is another way of having the correct dash, as egreg suggested, by considering the inclusion of text inside the math mode, replacing \mathbin{-} with:

\sisetup{range-phrase=%
 \ifmmode\text{\,\textendash\,}
 \else
  \thinspace\textendash\thinspace
 \fi%
}
%

Obtaining:

Result of the MWE with two identical outputs, visually identical to the earlier one

Tags:

Siunitx