# How to use siunitx with non numerical values?

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}


-
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
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 Aug 30 '14 at 20:02

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[math-rm = \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).

-
Aren't we talking about \SI[math-rm = \ensuremath, parse-numbers = false]{R}{\metre\per\second} here instead? – pluton Apr 29 '14 at 15:54
The option for \SI should be math-rm, not mathrm (I C&P-ed this snippet and got an error). I attempted to fix the typo, but TeX.SX said "Edits must be at least 6 characters"... – Merzong Nov 30 '15 at 2:27
@Merzong Indeed: fixed (I can do such edits) :-) – Joseph Wright Nov 30 '15 at 11:34

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}

-
\NewDocumentCommand{\varSI}{O{}}{\SI[math-rm=\mathnormal,parse-numbers=false,#1]}

\varSI{R}{\meter\per\second}
\varSI[math-rm=\mathbf]{R}{\meter\per\second}

-
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.

-

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.

-