Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I understand that something like \SI{5}{\meter\per\second} defines a certain space between the number (5) and units (m/s). What should I use if instead of a numerical quantity I want a variable: R m/s? \SI{R}{\meter\per\second} fails because it expect a number in first parameter, and R \si{\meter\per\second} or $R$ \si{\meter\per\second} show different space.

Which is the correct use?

\documentclass{standalone}
\usepackage{siunitx}
\sisetup{per-mode=symbol}
\begin{document}
\begin{tabular}{r}
\SI{5}{\meter\per\second}\\
$R$ \si{\meter\per\second}\\
R \si{\meter\per\second}\\
\end{tabular}
\end{document}

enter image description here

share|improve this question
2  
Why would you want that? Combining variables with units makes the variables dependent on the unit system and thereby essentially meaningless, from a physics point of view. I suggest you rather use $R\cdot u$, where $u$ is \SI{1}{\meter\per\second}. –  leftaroundabout Oct 7 '11 at 16:46
    
May be the word variables is not correct in this case. I want to say something like 'a car goes at A m/s and the road has B Km ...' (not so easy problems and with a better style in my language). What should be the word in English? –  Ignasi Oct 8 '11 at 13:06
4  
No no, variable is quite the right word. My point is that it's not physically meaningful to say something like 'a car goes at A m/s and the road has B km ...'. Just say 'a car goes at speed v and the road has length l': v and l are physical quantities independent of any particular choice of units. Only when you're finally giving numerical values for these quantities it makes sense to say stuff like 'let now v = 37 m/s', or 'values of l in km: {60, 70, 89, 130}'. — Anyway, that's not a typesetting concern and therefore doesn't belong to this site, I just wanted to say it. –  leftaroundabout Oct 10 '11 at 12:14
    
@leftaroundabout: This is useful when using scaling variables, e.g., 25α dB, where α is the scaling variable. –  equaeghe 12 hours ago

5 Answers 5

up vote 16 down vote accepted

As others have commented, this is partly a style question. In terms of non-numerical input, you've got a couple of choices. You could add R as a 'symbol' to those that siunitx knows, but that will not always print in math mode. So I'd prefer the approach of turning off the number parser:

\documentclass{article}
\usepackage{siunitx}
\begin{document}
\SI[parse-numbers = false]{R}{\metre\per\second}
\end{document}

As egreg points out, to get italic output you'll need to set the math-rm option

\documentclass{article}
\usepackage{siunitx}
\begin{document}
\SI[mathrm = \mathnormal, parse-numbers = false]{R}{\metre\per\second}
\end{document}

Turning off the parser forces math mode, but retains font control and also consistent spacing (so if you alter the setting for number-unit-separator it will be updated).

share|improve this answer
    
Aren't we talking about \SI[math-rm = \ensuremath, parse-numbers = false]{R}{\metre\per\second} here instead? –  pluton Apr 29 at 15:54

I think you are raising two questions: First, how to set the spacing between the "R" and its unit (m/s) and second, whether to typeset the "R" in upright or italic shape.

Regarding the former: I believe what siunitx does in the \SI macro is to insert a thinspace width between the quantity and its unit; one can generate this spacing manually with the \, command. Regarding the latter question: I believe it is (near-)universal practice to typeset variables in italic shape. Hence, I'd recommend you use the following command:

$R$\,\si{meter\per\second}
share|improve this answer
\NewDocumentCommand{\varSI}{O{}}{\SI[math-rm=\mathnormal,parse-numbers=false,#1]}

\varSI{R}{\meter\per\second}
\varSI[math-rm=\mathbf]{R}{\meter\per\second}
share|improve this answer
    
I'd use \NewDocumentCommand here: it makes the new macro protected and also not 'long' (of course, \newcommand* would also do the later). –  Joseph Wright Oct 7 '11 at 12:10
    
@JosephWright I forgot that siunitx loads xparse. However the command is robust anyway, since it uses \newcommand with an optional argument. –  egreg Oct 7 '11 at 12:16
    
(Normally) A command taking an optional argument is not robust, as it uses \futurelet internally and so can't be \edefed. –  Joseph Wright Oct 7 '11 at 13:33
    
@JosephWright It used to be so years ago; see LaTeX News of December 1995. –  egreg Oct 7 '11 at 13:39
    
@egreg: Your suggestion of a NewDocumentCommand (or newcommand) which just declares some portion of a longer command it's new to me. Interesting! Thank you. –  Ignasi Oct 10 '11 at 7:43

As suggested above, you can declare the following:

\NewDocumentCommand\varSI{omm}{%
  \IfNoValueTF{#1}%
  {\SI{#2}{#3}}%
  {$#1$\,$=$\,\SI{#2}{#3}}%
}

The physical quantity comes as the optional argument and is displayed (if specified) in math mode, whereas the numerical value and the unit come as mandatory arguments. Hence, use wherever in the text \varSI[Quantity]{Value}{Unit with siunitx} or \varSI{Value}{Unit with siunitx}

For example, you can use either \varSI[E_K]{3}{\electronvolt} to display EK = 3 eV and only \varSI{3}{\electronvolt} to write 3 eV depending on the context.

share|improve this answer

What you could do for instance is to introduce the \si part into the mathmode part:

\documentclass{standalone}
\usepackage{siunitx}
\sisetup{per-mode=symbol}
\begin{document}
\begin{tabular}{r}
\SI{5}{\meter\per\second}\\
$R\,\si{\meter\per\second}$\\
\end{tabular}
\end{document}

Then you have control of the spacing and you can choose a different spacing than \, if you like.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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