# Creating proper automatic text spacing in a custom command using siunitx

I created two custom command such that I could get a properly formatted micro and nanometer suffixes without too much a hastle by using the siunitx package as follows:

\newcommand{\nm}{\si{\nano\meter} }
\newcommand{\um}{\si{\micro\meter} }


Normally the \si command incorporates spacing correctly when using it in a body of text, but when using my custom command I had to add a space at the end of it. Since it doesn't do that automatically when I use it in any bit of text. Which works, usually, but not when I use the command to end a sentence or put it before a comma. Maybe there is some kind of command structure which I am overlooking that takes care of this?

• Possible duplicate of Space after LaTeX commands – siracusa Nov 30 '16 at 14:04
• have a look at xspace package, and use it this way: \newcommand{\nm}{\si{\nano\meter}\xspace} ->space is added only when needed. – ebosi Nov 30 '16 at 14:14
• Please do not do this: if you really have to have 'short' commands use the free-standing-units option. (Given the loss of semantic meaning and the risk of clashes for such short names, I'd favour sticking to the standard interface.) – Joseph Wright Nov 30 '16 at 14:22

Whilst it's not the standard setting for a reason, one can create such free-standing units:

\documentclass{article}
\usepackage[free-standing-units, use-xspace]{siunitx}
\begin{document}

The distance~$d$ is given in \nm, so \nm are an SI unit of distance.

\end{document}


The xspace package is intended for this kind of use: it "smartly" adds a space at the end of your macro (i.e. not when you macro is followed by a punctuation character).
You use it this way:

\newcommand{\nm}{\si{\nano\meter}\xspace}


\documentclass{scrartcl}
\usepackage{siunitx}
\usepackage{xspace}

\newcommand{\nm}{\si{\nano\meter}\xspace}

\begin{document}
You can say that 13\nm is shorter than 17\nm.
\end{document}