6

When I write units in math mode, I separate them using \, eg: $3 \; kg \, m^2$

I want to create a macro that does this for me, so I used xparse like this:

\usepackage{xparse}

\NewDocumentCommand \units{O{}O{}O{}}{\;#1 \,#2 \,#3}

The problem is that if I only specify one argument, I still get 2 extra spaces because my macro skips the arguments but not the \,.

What I would like is that my macro skips the extra \, commands if I only specify 1 or 2 arguments.

3
  • do you really set kg in math italic? Commented Oct 13, 2016 at 14:18
  • 5
    Why don't you use siunitx? Commented Oct 13, 2016 at 14:45
  • Welcome to TeX.SE. For units you should defintely use siunitx package. But, in general to accomplish what you want you can either detect that an empty paramater was given and only add the \, spacing if the paramater is not empty, or switch to using the o or g paramter type (and then use \IfNoValueTF to detect missing parameters). Commented Oct 13, 2016 at 15:41

2 Answers 2

7

Fundamentally you want to condition on the existence of your optional arguments. In xparse this is done using \IfValueTF with the TF being optional on whether or not you need a True or False branch.

enter image description here

\documentclass{article}

\usepackage{xparse,amsmath}

\NewDocumentCommand\units{ o o o }{%
  \IfValueT{#1}{\;#1}%
  \IfValueT{#2}{\,#2}%
  \IfValueT{#3}{\,#3}%
}

\begin{document}

This pumpkin weighs $3\units[\text{kg}]$. That's heavy!

I can run $12\units[\text{m}][\text{s}^{-1}]$. That's fast!

Something ridiculous $1\units[a][b][c]$. That's ridiculous!

Something $5\units$ something else.

\end{document}

In general, units should be set like regular text.


A far better approach would be to use siunitx:

enter image description here

\documentclass{article}

\usepackage{siunitx}

\DeclareSIUnit{\aaa}{a}
\DeclareSIUnit{\bbb}{b}
\DeclareSIUnit{\ccc}{c}

\begin{document}

This pumpkin weighs $\SI{3}{\kilogram}$. That's heavy!

I can run $\SI{12}{\metre\per\second}$. That's fast!

Something ridiculous $\SI{1}{\aaa \bbb \ccc}$. That's ridiculous!

Something $5$ something else.

\end{document}
1
  • Thank you for the help with xparse, I'll definitely use siunitx from now on though.
    – Sebastian
    Commented Oct 14, 2016 at 9:08
5

What if you have more than three units? Here units are separated by spaces, then the macro takes care of using thin spaces.

\documentclass{article}

\usepackage{xparse,amsmath}

\NewDocumentCommand\units{>{\SplitList{ }}m}{%
  \ProcessList{#1}{\addunit}%
}
\NewDocumentCommand{\addunit}{m}{%
  \,\mathrm{#1}%
}

\begin{document}

This pumpkin weighs $3\units{kg}$. That's heavy!

I can run $12\units{m s^{-1}}$. That's fast!

Something ridiculous $1\units{a b c}$. That's ridiculous!

\end{document}

enter image description here

Of course, siunitx is far better.

1
  • I think I prefer your way of using xparse, but as has been mentioned by pretty much everyone siunitx is better so I'll use that instead.
    – Sebastian
    Commented Oct 14, 2016 at 9:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .