# Vector notation in siunitx

I'm trying to use the siunitx package to format a number which is a vector (using \hat{x} to represent a vector in the x direction). Using \hat{x} leads to complaints about invalid tokens.

After seeing this question, I tried including

\sisetup{input-symbols=\hatx\haty}

\protected\def\hatx{\ensuremath{\hat{x}}}
\protected\def\haty{\ensuremath{\hat{y}}}


to my preamble. Then, I get siunitx error: "misplaced-sign-token" Misplaced sign token '+'.

For completeness, here's what I am trying now:

\documentclass{article}
\usepackage{siunitx}

\sisetup{input-symbols=\hatx\haty}

\protected\def\hatx{\ensuremath{\hat{x}}}
\protected\def\haty{\ensuremath{\hat{y}}}

\begin{document}

$$\vec{F} = \SI{-3.24e-20\hatx + 4.56e-18\haty}{\newton}$$

\end{document}


Any ideas how I can get this kind of thing to work properly?

-
add comment

## 2 Answers

One way to do this is to simply use:

\vec{F} = (\num{-3.24e-20}\hatx + \num{4.56e-18}\haty) \,\si{\newton}


## Code:

\documentclass{article}
\usepackage{siunitx}

\protected\def\hatx{\ensuremath{\hat{x}}}
\protected\def\haty{\ensuremath{\hat{y}}}

\begin{document}
\SI{1234}{\newton}

\num{1234}\,\si{\newton}

$$\vec{F} = (\num{-3.24e-20}\hatx + \num{4.56e-18}\haty) \,\si{\newton}$$
\end{document}

-
I'd add the parentheses around the vector, or the expression is mathematically different from what it should mean. –  egreg Jan 12 '13 at 21:45
@egreg: Yep. I had it originally but was experimenting to see how it looked without the parenthesis and copied the wrong one here. Thanks for noticing. –  Peter Grill Jan 12 '13 at 21:51
Thanks, I hadn't thought of that. But this eliminates the spacing that is normally provided by siunitx, I think? –  Tyler Jan 12 '13 at 21:54
@Tyler: Can you elaborate on which spacing you are referring to. AFAIK, The \SI macro is just a short form for of using \num for the number and \si for the units, both of which are part of the siunitx pacakge. –  Peter Grill Jan 12 '13 at 21:57
Take a look at this. The first is with \SI{1234}{\newton}. The second is with \num{1234}\si{\newton}. –  Tyler Jan 12 '13 at 22:06
show 2 more comments

May I suggest you try the mandi package? It is designed to handle this very thing. Furthermore, it is designed so that the user need never worry about a physical quantity's units because they are all baked into the package. For example, you can say \force{5} and get

You can also say \force{\mivector{2,4,-5}} and get the expected result

although it won't be formatted as in your example above. mandi was begun before siunitx came into existence and thus relies on the SIunits package for defining unit names, but in the next major release mandi will be independent of both SIunits and siunitx while, hopefully, not preventing use of the latter.

Disclaimer: I am the author of mandi.

-
Thanks, I was not aware of this package, but reading through the documentation, it looks very interesting. For now, I would prefer to keep using the vector notation from above, but your package does allow for a more readable source file. –  Tyler Jan 15 '13 at 2:16
add comment