How to write 10k for 10000 with the siunitx package?

(too long for a comment, hence posted as an answer)

When people use expressions such as

I ran a 5k race last week.

or

After 21k iterations, the loop was terminated.

their intent is in no way to imbue the terms 5k and 21k with any kind of scientific precision and exactitude. Instead, the intent is informal and situational. In the former case, it's shorthand for saying, "I ran a five kilometer race last week"; listeners will hopefully be in no doubt about the unstated length parameter. In the latter case, it's shorthand for "After 21000 iterations, the loop was terminated."

Either way, trying to adduce the heavy machinery of the siunitx package -- specifically, the package's \num and \SI macros -- to typeset such informal expressions seems misguided. For sure, if one had to spell out the shorthand locution in the first sentence, one would write either "I ran a five kilometer race last week" or "I ran a 5 kilometer race last week" -- but surely not "I ran a \SI{5}{\kilo\meter} race last week".

I suppose one can quibble about whether or not it's desirable to insert a thinspace between the number and the letter k in the terms 5k and 21k. (I agree that a interword space seems excessive.) I'm pretty sure that with or without the thinspace, nobody will be confused about the meaning of the terms. Because the expressions are informal shorthands and because of the lack of potential for confusion, I'd choose the simpler solution and omit the thinspaces.


One possible solution would be to define k as a unit:

\documentclass{article}

\usepackage{siunitx}
\DeclareSIUnit\k{k}

\begin{document}
After \SI{21}{\k} iterations, the loop was terminated.

\end{document}

However, this seems a bit hacky and I'm not sure, whether the spacing between the number and the k is correct.

EDIT: Since it seems to be preferable, here is an alternative with no space between the number and the k. I also added a macro (thanks to @Fran), to increase readability:

\documentclass{article}

\usepackage{siunitx}
\DeclareSIUnit[number-unit-product = ]\k{k}
\def\n#1k{\SI{#1}{\k}}

\begin{document}
After \SI{10}{\k} iterations, the loop was terminated.

After \n10k iterations, the loop was terminated.
\end{document}

Ultimately, it would be nice to have this functionality included in siunitx as a variant of the \num command. Therefore, I opened an issue with this feature request at Github (link). We'll see how that works out. If it is actually implemented, I will update this answer.


\documentclass{article}
\usepackage{siunitx}
\begin{document}
After \SI{21}{\kilo{}} iterations, the loop was terminated.
\end{document}

Note, however, that the result is exactly the same that use 21\,k (...as \SI{10}{\meter} is also equal that 10\,m), that is easy to type, and more readable in the source document.

I use siunitx often, but is it not a mandatory way to type units in LaTeX, it is only a tool to be used only if it helps.

Tags:

Siunitx