# SIrange typeset different in text and math mode

I observe a difference in the dashes typesetting when using SIrange in text or math modes. Any idea of why this happens? Here's the MWE:

\documentclass{article}
\usepackage{siunitx}
\sisetup{
range-phrase= -}

\begin{document}
\SIrange{e-12}{e-10}{\second}
$\SIrange{e-12}{e-10}{\second}$
\end{document}


This is how it looks:

• I think the character inserted - is interpreted differently in relation to the environment, so to obtain the same you could substitute the - to \textendash, or leave a conditional (more complicated) – TheVal Sep 17 '13 at 8:42
• Great! Works fine, but the unbreakable space before and after the dash disappear. So one needs to write \sisetup{range-phrase= ~\endash~} to be consistent with the typography rules. – Maugan Sep 17 '13 at 9:12
• Your answer is right, now that is protected the spacing results equally distributed with sisetup{range-phrase= ~\texendash~}. Feel free to accept the answer if it has solved your issues. – TheVal Sep 17 '13 at 9:17
• All good for me. Thanks to all of you for your -so damn quick- answers! – Maugan Sep 17 '13 at 9:20
• Technical reasons and answers aside, I'd use (from) <first val> to <second val> in (running) text (or similar construction) and <first val> \cdots <second val> in math-mode so that the endash (or even your actual minus sign) doesn't get confused with a subtraction. Typographically, an en-dash without space around it is correct. – Qrrbrbirlbel Sep 17 '13 at 11:54

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:

## 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:

• Why not \sisetup{range-phrase=\ensuremath{-}}? – remus Sep 17 '13 at 8:58
• +1 for the diagnosis, but I wouldn't use \mathbin{-} in math mode (by the way, \mathbin is superfluous); maybe \text{\textendash} is better. If you add \thinspace on one side, it should be also on the other. – egreg Sep 17 '13 at 8:58
• @egreg, noticed that if one has to be coherent, then it has to do it on both sides, I'll have to edit now. Ah... using \text{\textendash}, as I tried, gives no space between the numbers, so should I add this scenario to my answer (for the sake of completeness)? – TheVal Sep 17 '13 at 9:05
• \text{\,\textendash\,}, if you want to add a thin space on both sides. The OP's problem is exactly that one gets different typesetting and with your proposed solution the result is different. – egreg Sep 17 '13 at 9:08
• @egreg All right, I'll make an addendum... – TheVal Sep 17 '13 at 9:09

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.

• but a hyphen, as shown in the question for text mode, isn't really correct; the "connector" should be an en-dash. – barbara beeton Sep 17 '13 at 12:20
• @barbarabeeton Well that's down to the input chosen by the person asking the question: you need range-phrase = -- to get an en-dash as always in TeX. Of course, the default is ' to ' as an en-dash looks very much like a minus sign. – Joseph Wright Sep 17 '13 at 12:28
• thanks -- i overlooked the range-phrase input. – barbara beeton Sep 17 '13 at 12:31
• @Joseph Wright: The fact is that the context in which it really is used needs the math-mode for correct spacing ($\tau_{32}=\SIrange{e-15}{e-11}{\second}$); without having to explicitely add the spaces (what is the use of LaTeX then). – Maugan Sep 23 '13 at 8:21
• So the solution to this problem is to generally end the math environment before giving a range, i.e. $\gamma =$ \numrange{1}{2} ? A warning for ranges inside math mode would be good. – Karl Feb 22 '16 at 10:23