1

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?

  • 1
    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
1

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}
0

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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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