Aligning Numbers in siunitx

The use of tables to communicate quantitative information is common in both academia and business but effective design sometimes eludes us. Readers must, at a glance, be able to get the information they need from the data you present. Fortunately, the tools for effective table design exist in LaTeX at least, if you know what you are doing. We are going to look at some simple ways to align numbers using the siunitx package.

The Table

We have the following table. While it is fairly presentable, we can't help but wonder can we do better? Yes, we see the numbers are fairly aligned in most cases but things look a little out of place where there are negative numbers.

Aligining numbers in siunitx

The numbers in the table are centered but the decimals are not aligned. We also need to italicize some of the numbers to highlight important data.

The code for our table looks something like this:

Though we can probably leave this table as is, we need to make some changes. Some of the numbers also need to be italicized to highlight some important data. This should be simple as we see below.

Aligining numbers in siunitx

Some of the numbers have been italicized but this throws off some of the decimal alignment.



So the question is, how can we make our table look better?

The siunitx package

The siunitx package not only allows authors to typeset quantities with ease and in a consistent way but also provides the tools to typeset tables. The package introduces another column type--the S column--to control alignment. We can then use the package's number parser to align our numbers. We are also going to use the [table-format=2.6] to tell the package something about our number. In our case, the number 2.6 tells the parser that the number has two integer digits and six decimal places. There are other options we can use for our S-columns that are outlined in the siunitx manual.

Aligning numbers in siunitx

The siunitx package allows for decimal alignment rather just centering numbers and hoping for the best.


which we can see from the code below.

Wait a second. Didn't we just say that the S-column allows us to align numbers? Well, it seems that some of the italicized numbers are no longer aligned and the dash is no longer centered. If anything, the siunitx package has made things worse! Does this mean we should go back to what we had before?

Fixing Decimal Alignment in siunitx

The problem we face is that the siunitx package's math parser doesn't see the italicized numbers as numbers but rather as text. It's the reason why the dash in the table is left aligned. Let's focus on each problem instead of worrying about both at the same time. Instead of the \textit macros, we will use \mathit to italicize numbers. This will tell siunitx's math parser its dealing with a number and not a word. Now our fix looks like:

Aligning numbers in siunitx

The \mathit macro tells the siunitx package that it is dealing with a number and not a word.


and the code looks something like:

There, that looks much better but we still have to deal with the left aligned dash. Now we must tell the siunitx parser to center the dash. We can do this by enclosing the cell's contents in curly braces:

aligning numbers in siunitx

By enclosing the \multirow command in curly braces we tell the sinuitx package's parser to center the text.


and our code looks like:

Conclusion

And there you have it. By using the siunitx package you can easily align numbers and text in your tables. Now your tables look great you can get back to stress-free writing.

  • David Gillooly

    Aren't the decimal points still not aligned?

    • Yes. Slightly but it seems it has more to do with the way italicized numbers are typeset in LaTeX; the package introduces some additional spaces for italics.

    • David Latchman

      Yes. Slightly but it seems it has more to do with the way italicized numbers are typeset in LaTeX; the package introduces some additional spaces for italics.

  • Sean Allred

    You know, you could apply bfseries to an entire column/row with the array package. It would encourage 'smart' use of LaTeX. 🙂