Tables – How to Format Table with Aligned Values, Errors, and Units

siunitxtables

I want to have a table with numbers ± errors and units (or a percent sign). Ideally, the numbers are aligned at the decimal separators and the ± sign. Here is my best solution so far using siunitx that does not have units:

\sisetup{
table-number-alignment=center,
separate-uncertainty=true,
table-figures-integer = 1,
table-figures-decimal = 2}
\begin{table}
\centering
\caption{Fancy caption describing the table}
\label{tab:fancy_table}
\begin{tabular}{l
                S[separate-uncertainty,table-figures-uncertainty=1]
                S[separate-uncertainty,table-figures-uncertainty=1]
                S[separate-uncertainty,table-figures-uncertainty=1]
                S[separate-uncertainty,table-figures-uncertainty=1]}
\toprule
              & \multicolumn{2}{c}{I} & \multicolumn{2}{c}{II} \\ 
{Category}    & {a} & {b} & {c} & {d} \\
\midrule
{A}           & 2.51 \pm 0.15   & 2.49 \pm 0.11 & 2.28 \pm 0.05 & 2.23 \pm 0.05 \\
{B}           & 2.51 \pm 0.15   & 4.20 \pm 0.05 & 4.20 \pm 0.05 & 4.20 \pm 0.05 \\
{C}           & 0.22 \pm 0.05   & 4.20 \pm 0.05 & 4.20 \pm 0.05 & 4.20 \pm 0.05 \\
\midrule
{Total}       & 4.20 \pm 0.05   & 4.20 \pm 0.05 & 4.20 \pm 0.05 & 4.20 \pm 0.05 \\
\bottomrule
\end{tabular}
\end{table}

Which produces this table:

resulting table output

So far, so good. Now all these numbers have units. Or, in this example are percentages. The formatting rules I am bound to request to format these values as

(0.22 ± 0.05)% 

or

(0.22 ± 0.05) mm

This can be achieved with siunitx:

\sisetup{separate-uncertainty=true}
\SI{100 \pm 12}{\percent}

However, putting both together and adding \SI{x.xx \pm x.xx}{\percent} in the table breaks the alignment.

Can I achieve both, somehow?

Best Answer

The main issue appears to be the placement of the units -- here, percent (%). Since the information about the units of measurement is important and is (hopefully) the same for every entry in a given column, it's convenient to place the information about the units in the table's header. I suggest you place it right above \midrule.

Some minor points: (i) Since the left-hand column has type l, there's no need to encase the cell contents in curly braces. (ii) I added a couple of \cmidrule lines to make it entirely clear that "I" goes with "a" and "b" while "II" goes with "c" and "d".

enter image description here

\documentclass{article}
\usepackage{siunitx,booktabs}
\sisetup{ table-number-alignment=center,
          separate-uncertainty=true,
          table-figures-integer = 1,
          table-figures-decimal = 2}
\begin{document}
\begin{table}
\caption{Fancy caption describing the table}
\label{tab:fancy_table}
\sisetup{table-figures-uncertainty=1}
\centering
\begin{tabular}{@{} l *{4}{S} @{}}
\toprule
Category & \multicolumn{2}{c}{I} & \multicolumn{2}{c@{}}{II} \\ 
\cmidrule(lr){2-3} \cmidrule(l){4-5}
& {a} & {b} & {c} & {d} \\
& {(\%)} & {(\%)} & {(\%)} & {(\%)}  \\
\midrule
A  & 2.51 \pm 0.15   & 2.49 \pm 0.11 & 2.28 \pm 0.05 & 2.23 \pm 0.05 \\
B  & 2.51 \pm 0.15   & 4.20 \pm 0.05 & 4.20 \pm 0.05 & 4.20 \pm 0.05 \\
C  & 0.22 \pm 0.05   & 4.20 \pm 0.05 & 4.20 \pm 0.05 & 4.20 \pm 0.05 \\[1ex]
Total & 4.20 \pm 0.05   & 4.20 \pm 0.05 & 4.20 \pm 0.05 & 4.20 \pm 0.05 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}