13

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:

enter image description here

6
  • 2
    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, 2013 at 8:42
  • 1
    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, 2013 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, 2013 at 9:17
  • All good for me. Thanks to all of you for your -so damn quick- answers!
    – Maugan
    Sep 17, 2013 at 9:20
  • 1
    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. Sep 17, 2013 at 11:54

2 Answers 2

11

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

5
  • 1
    Why not \sisetup{range-phrase=\ensuremath{-}}?
    – remus
    Sep 17, 2013 at 8:58
  • 1
    +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, 2013 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, 2013 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, 2013 at 9:08
  • @egreg All right, I'll make an addendum...
    – TheVal
    Sep 17, 2013 at 9:09
13

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.

7
  • but a hyphen, as shown in the question for text mode, isn't really correct; the "connector" should be an en-dash. Sep 17, 2013 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, 2013 at 12:28
  • thanks -- i overlooked the range-phrase input. Sep 17, 2013 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, 2013 at 8:21
  • 1
    I'd like that warning, too. I have spent quite some time now trying to figure out why \numrange behaves weird in math mode (yes, I could have read the doc). As an alternative, what about you making range commands exit math mode, if necessary, and reentering it again? Would that work?
    – bers
    Oct 6, 2016 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.