4

In some fields of science (e.g. computer science) large numbers without units are often abbreviated using the kilo prefix. Is there a way to do this using the siunitx package?

Examples:

After 21k iterations, the loop was terminated.

I tried:

\num{21\kilo}
\SI{21}{\kilo}

However, both give me errors.

Thanks.

  • 1
    @mico Thanks for the quick response. I'd like to achieve the latter. I tried to clarify my question. – Nos Mar 6 at 16:58
  • 1
    @Nos What's the problem in being clear and write 21000? – egreg Mar 6 at 17:10
  • 1
    It depends on how it is used. If the “k” immediately follows the number, just write $21\mathrm{k}$, it's not a job for siunitx. – egreg Mar 6 at 17:24
  • 1
    What's wrong with simply 21k? – Peter Wilson Mar 6 at 17:57
  • 1
    @PeterWilson Thanks for the suggestion. However, I have a rhetorical question: What is wrong with 10 m instead of \SI{10}{\meter} :-) ? In principal, nothing. Still, I would prefer to let siunitx take care of all the styling of my numbers. – Nos Mar 6 at 18:26
4

(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.

| improve this answer | |
  • Thank you for this detailed answer. I like the arguments that you make. However, this answer does not strictly answer the question. Also, you base your argumentation on the assumption that the k notation is only used in informal contexts, which is IMHO not the case (see e.g. this paper). Perhaps it should only be used in informal texts, but that's highly subjective. – Nos Mar 7 at 9:24
  • 1
    @Nos I read “5 fps” (with a normal space) and ”200nm” (no space) on page 2, “160k iterations” (no space) on page 4. They use “fps” as an acronym, which is fine, but it should be opened up at first usage in the document body, otherwise it is apparently considered a unit, so it should be separated from the number by a thin space. The unit nm must be separated with a thin space. The “k” is just a shorthand for “000”. Keep it attached to the number, no space. – egreg Mar 7 at 9:37
  • @egreg You're absolutely right. It is a little sloppy. However, I would still not call it informal. And I think 2669 citations agree. :-) Many comments and answers in this thread give advice on how to best write 21k. And I appreciate it, but frankly speaking: I don't want to know. :-) That's exactly the reason why I love Latex and packages like siunitx. Because they are so great at taking care of these questions, so that I don't have to deal with them. Hence my question about a siunitx way of writing 21k. – Nos Mar 7 at 9:47
  • 1
    @Mico No need to apologize! After all you, and everybody else here, are helping people like me. OT: I totally agree with you. And that is the downside to arxiv, there is no editor. :-) Still, the mentioned paper is a great publication. – Nos Mar 7 at 10:02
  • 1
    I agree with @mico here: I think that when you say 5k and 21k you mean a number, not a number with a unity, like "I run a five thousand yesterday". So my vote is to type it without any space, as part of the number. – Rmano Mar 7 at 15:13
2
\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.

| improve this answer | |
  • Thanks. This does actually answer my question. The reason why I always try to use siunitx for numbers and units is to enforce consistency. Readability is no concern for me. I can read it in the beautifully rendered PDF if I want to. TBH, I had hoped more for the existence of a \num version (e.g. \num{21k}). That way it would be readable and enforce consistency. – Nos Mar 7 at 9:31
  • 1
    @Nos Well, this is a macro language, nothing prevents you to define some as \def\n#1k{\SI{#1}{\kilo{}}}and then use \n21534536k or \n{21534536}k or \n 21534536 k or \n 21 534 536 k or even \n 21 53 45 36 k... In any case is readable, will produce the desired result whatever the spaces used (of course, except in \n{21534536} k ) and will use siunitx consistently. – Fran Mar 7 at 11:18
2

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.

| improve this answer | |

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.