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.

The code for our table looks something like this:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
\begin{tabular}{lcccc} \toprule & \textbf{Half-Cauchy} & \textbf{Gamma} & \textbf{Half-Normal} & \textbf{Uniform} \\ \midrule \multirow{2}{*}{\textbf{Half-Cauchy}} & \multirow{2}{*}{-} & 19.747170 & 40.779140 & -20.999020 \\ & & 64.445890 & 73.057920 & 81.842710 \\ \midrule \multirow{2}{*}{\textbf{Gamma}} & -19.747170 & \multirow{2}{*}{-} & -9.691131 & -94.381620 \\ & 64.445890 & & 34.809810 & 68.477040 \\ \midrule \multirow{2}{*}{\textbf{Half-Normal}} & -40.779140 & 9.691131 & \multirow{2}{*}{-} & -43.942080 \\ & 73.057920 & 34.809810 & & 26.003810 \\ \midrule \multirow{2}{*}{\textbf{Uniform}} & 20.999020 & 94.381620 & 43.942080 & \multirow{2}{*}{-} \\ & 81.842710 & 68.477040 & 26.003810 & \\ \bottomrule \end{tabular} |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
\begin{tabular}{lcccc} \toprule & \textbf{Half-Cauchy} & \textbf{Gamma} & \textbf{Half-Normal} & \textbf{Uniform} \\ \midrule \multirow{2}{*}{\textbf{Half-Cauchy}} & \multirow{2}{*}{-} & 19.747170 & 40.779140 & -20.999020 \\ & & \textit{64.445890} & \textit{73.057920} & \textit{81.842710} \\ \midrule \multirow{2}{*}{\textbf{Gamma}} & -19.747170 & \multirow{2}{*}{-} & -9.691131 & -94.381620 \\ & \textit{64.445890} & & \textit{34.809810} & \textit{68.477040} \\ \midrule \multirow{2}{*}{\textbf{Half-Normal}} & -40.779140 & 9.691131 & \multirow{2}{*}{-} & -43.942080 \\ & \textit{73.057920} & \textit{34.809810} & & \textit{26.003810} \\ \midrule \multirow{2}{*}{\textbf{Uniform}} & 20.999020 & 94.381620 & 43.942080 & \multirow{2}{*}{-} \\ & \textit{81.842710} & \textit{68.477040} & \textit{26.003810} & \\ \bottomrule \end{tabular} |

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.

which we can see from the code below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
\begin{tabular}{ l S[table-format=2.6] S[table-format=2.6] S[table-format=2.6] S[table-format=2.6]} \toprule & \textbf{Half-Cauchy} & \textbf{Gamma} & \textbf{Half-Normal} & \textbf{Uniform} \\ \midrule \multirow{2}{*}{\textbf{Half-Cauchy}} & \multirow{2}{*}{-} & 19.747170 & 40.779140 & -20.999020 \\ & & \textit{64.445890} & \textit{73.057920} & \textit{81.842710} \\ \midrule \multirow{2}{*}{\textbf{Gamma}} & -19.747170 & \multirow{2}{*}{-} & -9.691131 & -94.381620 \\ & \textit{64.445890} & & \textit{34.809810} & \textit{68.477040} \\ \midrule \multirow{2}{*}{\textbf{Half-Normal}} & -40.779140 & 9.691131 & \multirow{2}{*}{-} & -43.942080 \\ & \textit{73.057920} & \textit{34.809810} & & \textit{26.003810} \\ \midrule \multirow{2}{*}{\textbf{Uniform}} & 20.999020 & 94.381620 & 43.942080 & \multirow{2}{*}{-} \\ & \textit{81.842710} & \textit{68.477040} & \textit{26.003810} & \\ \bottomrule \end{tabular}% |

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:

and the code looks something like:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
\begin{tabular}{ l S[table-format=2.5] S[table-format=2.5] S[table-format=2.5] S[table-format=2.5]} \toprule & \textbf{Half-Cauchy} & \textbf{Gamma} & \textbf{Half-Normal} & \textbf{Uniform} \\ \midrule \multirow{2}{*}{\textbf{Half-Cauchy}} & \multirow{2}{*}{-} & 19.747170 & 40.779140 & -20.999020 \\ & & $\mathit{64.445890}$ & $\mathit{73.057920}$ & $\mathit{81.842710}$ \\ \midrule \multirow{-2}{*}{\textbf{Gamma}} & -19.747170 & \multirow{2}{*}{-} & -9.691131 & -94.381620 \\ & $\mathit{64.445890}$ & & $\mathit{34.809810}$ & $\mathit{68.477040}$ \\ \midrule \multirow{2}{*}{\textbf{Half-Normal}} & -40.779140 & 9.691131 & \multirow{2}{*}{-} & -43.942080 \\ & $\mathit{73.057920}$ & $\mathit{34.809810}$ & & $\mathit{26.003810}$ \\ \midrule \multirow{2}{*}{\textbf{Uniform}} & 20.999020 & 94.381620 & 43.942080 & \multirow{2}{*}{-} \\ & $\mathit{81.842710}$ & $\mathit{68.477040}$ & $\mathit{26.003810}$ & \\ \bottomrule \end{tabular}% |

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:

and our code looks like:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
\begin{tabular}{ l S[table-format=2.5] S[table-format=2.5] S[table-format=2.5] S[table-format=2.5]} \toprule & \textbf{Half-Cauchy} & \textbf{Gamma} & \textbf{Half-Normal} & \textbf{Uniform} \\ \midrule \multirow{2}{*}{\textbf{Half-Cauchy}} & {\multirow{2}{*}{-}} & 19.747170 & 40.779140 & -20.999020 \\ & & $\mathit{64.445890}$ & $\mathit{73.057920}$ & $\mathit{81.842710}$ \\ \midrule & -19.747170 & {\multirow{2}{*}{-}} & -9.691131 & -94.381620 \\ \multirow{-2}{*}{\textbf{Gamma}} & $\mathit{64.445890}$ & & $\mathit{34.809810}$ & $\mathit{68.477040}$ \\ \midrule & -40.779140 & 9.691131 & {\multirow{2}{*}{-}} & -43.942080 \\ \multirow{-2}{*}{\textbf{Half-Normal}} & $\mathit{73.057920}$ & $\mathit{34.809810}$ & & $\mathit{26.003810}$ \\ \midrule & 20.999020 & 94.381620 & 43.942080 & {\multirow{2}{*}{-}} \\ \multirow{-2}{*}{\textbf{Uniform}} & $\mathit{81.842710}$ & $\mathit{68.477040}$ & $\mathit{26.003810}$ & \\ \bottomrule \end{tabular}% |

## 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 GilloolyAren't the decimal points still not aligned?

David LatchmanPost authorYes. 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 LatchmanYes. 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 AllredYou know, you could apply bfseries to an entire column/row with the

`array`

package. It would encourage 'smart' use of LaTeX. 🙂