# Why use a package to typeset units?

I've seen a lot of posts about different packages for typesetting units and their relative strengths and weaknesses. Most posts seem to agree that the siunitx package is the most up to date package, but I haven't really figured out the advantages of using a package at all. What is the advantage of writing something like:

\SI{10}{\kilo\gram\meter\per\second\squared}


when you could just write:

kg m s$^-2$


Or for numbers, what's the difference between

\num{10}


and

10

-
You can write \SI{10}{kg m s^{-2}}, which guarantees that the unit will be typeset in the correct way: the right space (or multiplication symbol if you so decide) between the various parts, no line break between them, the right glyph shape (upright even if the context is italics) and so on. –  egreg Apr 9 '13 at 23:30
@egreg You should write \SI{10}{\kg\m\per\square\s} instead of \SI{10}{kg m s^{-2}} to get the correct spacing and a consistent output. –  Svend Tveskæg Apr 9 '13 at 23:42
@SvendTveskæg I forgot the dots: \SI{10}{kg.m.s^{-2}} or, for the fraction form, \SI{10}{kg.m/s^{2}}. –  egreg Apr 9 '13 at 23:49
I would still say that the other way is "more" correct; check, e.g., the following: \documentclass{article} \usepackage{siunitx} \DeclareSIUnit\second{Horse} \begin{document} Should you write \SI{10}{\kg\m\per\square\s} or \SI{10}{kg.m.s^{-2}}? \end{document}. The formatting options is only avaliable with the approach I suggest. –  Svend Tveskæg Apr 9 '13 at 23:54
Correct spacing for larger numbers is also a plus. $\num{10000}$ will output 10 000. Writing $10\,000$, seems very hacky to me. –  N3buchadnezzar Apr 18 '13 at 7:40

I came to this question thinking "yeah! Why?" but upon reading the currently extant comments and answer I have already changed my tune and am now tending the other way. What I realized, thinking about it and about my small interactions with this package, is that it provides much the same service within LaTeX as LaTeX (or even just TeX itself) provides for writing as a whole. To understand that point, consider the alternatives in communicating some math in writing via a computer:

1. Write it phonetically, as it were. Words are words, of course, but Greek letters are spelled out, or perhaps drawn in some ad-hoc ASCII art, and more complicated formulas are hacked together in an unspecified manner that may or may not be clear from context.

2. Write it in TeX, in which Greek letters are spelled out in macros, more complicated formulas are hacked together in some possibly awful manner...and then the result is "compiled" into a document making it all look like you had in mind, so no harm done if your source was a little more schematic than you'd have liked.

The siunitx package does exactly this, but instead of math formulas, it does SI-style units. Sure, one could write the same symbols that normally appear in such units as kg or s$^{-1}$, but that can go wrong visually very easily depending on if the unit appears in math mode or not (just imagine how that affects the latter!), or if several units become crammed together, or (egreg's quite valid observation) broken across a line. Then you may decide that instead of s$^{-1}$ you really want /s for the unit of hertz, necessitating a global change of notation.

These are all the kinds of problems that are handled in TeX by defining macros containing your preferred styles for \kilogram and \hertz and so on. The siunitx just organizes the whole project of defining these macros and their relationships to each other, and gives a lot of aesthetic options on top of it.

Basically, it boils down to "Why would anyone want to program their document in the first place?", to which the answer is "It's easier to write well-structured documents that way."

-

Allow me to give an answer in the form of a practical anecdote:

When I wrote my PhD dissertation in chemistry, each chapter had either been published in or was soon to be submitted to a peer-reviewed journal. Each chapter was written at a different time over the course of several years, and of course each journal had its own standards as to whether there should be a space between the number and the unit, whether inverse units should be given as fractions or exponents, etc. Besides that, when I would just type out the units, sometime I would add a space and sometime I wouldn't, and so ended up doing a lot of proofreading to catch this until I hit on siunit, and then on siunitx.

By turning the units into macros, I had one less formatting issue that I had to worry about, and when I took the body of the chapter, I just had to write a new header using the journal's style package and the correct siunitx parameters (or, if the journal didn't support it, hard-code the macros in the preamble), and all the units and numbers were automagically formatted correctly. When I then needed to put them into my thesis, I just had to \input{} the body of the text after a \chapter{} tag, and all of my chapters were formatted to my university thesis standards with no fuss, no muss. Meanwhile, my friends who were working with Word were spending weeks and sometimes months doing nothing but reformatting and proofreading to get everything consistent (most of that spent on equation, figure, and reference numbering that I never gave a second thought to).

In fact, the file that had my macro definitions ended up being almost as long as my chapters, since every time I had a mathematical symbol (superscripts, subscripts, bold, etc.), I created a macro with a descriptive name so that not only was it easier to proofread, but if for some reason I had to change a symbol or variable, I only had to change it once. Same idea with units. If everything is a macro, if you need to change something, you only have to change it once and that propagates itself throughout the document without having to worry.

-
+1, Welcome to TeX.sx (sort-of belatedly), great first answer, and thumbs up for that combination of your first two badges. You know how to do things properly :). –  doncherry Apr 10 '13 at 0:47
@doncherry Thanks. I lurked around for quite a while before I found a question I actually knew the answer to :) –  craigim Apr 10 '13 at 0:53

As with all of the other components of TeX, LaTeX, and friends, the idea is to separate content from form

We wish to specify the changes globally in the preamble, and not worry about the details things in the document.

Similar questions:

• Why don't we write \textbf{1. Introduction} instead of \section{Introduction}?
• Why do we use the enumerate environment when we could just write (a), (b), (c) etc ?
• Why do we choose to make our graphics in LaTeX (using tikz and pstricks for example) when we could make them externally and just include them?
• Why do we use biblatex (and other bibliography packages) to organize our references- couldn't we just type them in the correct order ourselves?
-
I see your point in general, and I agree with the basic principle of separating style and content in LaTeX. I just never really considered units as something required that much formatting, although clearly there's a lot of small details I didn't consider such as italicization and spacing –  Vyas Apr 10 '13 at 2:02

Whether in math mode or not, I will

• always use \num from siunitx for any numerical literals,
• always use . for the decimal marker in the decimal literals,
• but later set the global setting in the preamble to specify the decimal marker for the rendered output.
\documentclass{article}

\usepackage{siunitx}
\sisetup{
%output-decimal-marker={,}% just uncomment if you want to use comma as the decimal marker!
}

\begin{document}
$\num{3.14}$ is not the value of $\pi$, \num{2.718281828} is not the value of $e$.
\end{document}


This mechanism makes your document portable!

-

The advantage of using a package is that you get set the numbers and units in a consistent typographycal way. Sometimes you have to use a spatium (\,) between number and unit, sometimes not. Package siunitx knows about this and does it automatically right for you.

-