[Tex/LaTex] Align numbers and separated uncertainties in tables with S columns

horizontal alignmentsiunitxtables

If I have a table like this

\documentclass{article}

\usepackage{booktabs}
\usepackage[separate-uncertainty=true]{siunitx}

\begin{document}
\begin{table}
  \centering
    \begin{tabular}{
                  l   
                  S[table-format=1.1(2)]
                  S[table-format=-1.1(2)]
                  S[table-format=2.1(3)]
                  %S[table-figures-uncertainty=2,
                  %  table-number-alignment=center]
                  }   
    \toprule
    Station   & {GHI}     & {DIF}      & {DHI}       \\  
    \midrule
    BS        & 2.4 +-3.7 & -5.3 +-1.6 & 11.6 +- 7.9 \\
    HB        & 3.0 +-4.3 & -3.2 +-3.3 & 10.0 +- 9.0 \\
    PD        & 2.4 +-2.8 & -3.1 +-1.3 &  8.4 +- 6.3 \\
    TR        & 1.3 +-4.8 & -4.5 +-2.8 &  7.5 +-10.6 \\
    WB        & 0.3 +-2.7 & -3.8 +-1.9 &  4.8 +- 6.4 \\[0.5em]
    Mean      & 1.6 +-3.0 & -3.9 +-1.3 &  7.6 +- 6.5 \\
    \bottomrule
    \end{tabular}
\end{table}
\end{document}

The numbers are aligned on the decimal sign, but the uncertainties are not.

What I would like to have is that in the last column space for two numbers for the uncertainty is reserved, so that it is formatted as given in the code (uncertainties aligned on the right and a space before for instance the 7.9 in the first row of the last column).

How can I achieve this with siunitx?

Best Answer

The siunitx package does not have any built-in facility for this. The only alignment provided in the package code is for the error to be left-aligned. Instead you can use a separate column for the uncertainties as follows:

Sample output

\documentclass{article}

\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}
\thispagestyle{empty}
\begin{table}
  \centering
  \begin{tabular}{
    l
    S[table-format=1.1]@{\,\( \pm \)\,}
    S[table-format=1.1]
    S[table-format=-1.1]@{\,\( \pm \)\,}
    S[table-format=1.1]
    S[table-format=2.1]@{\,\( \pm \)\,}
    S[table-format=2.1]
    }
    \toprule
    Station & \multicolumn{2}{c}{GHI} & \multicolumn{2}{c}{DIF}
    & \multicolumn{2}{c}{DHI}       \\
    \midrule
    BS & 2.4 &3.7 & -5.3 &1.6 & 11.6 & 7.9 \\
    HB & 3.0 &4.3 & -3.2 &3.3 & 10.0 & 9.0 \\
    PD & 2.4 &2.8 & -3.1 &1.3 &  8.4 & 6.3 \\
    TR & 1.3 &4.8 & -4.5 &2.8 &  7.5 &10.6 \\
    WB & 0.3 &2.7 & -3.8 &1.9 &  4.8 & 6.4 \\[0.5em]
    Mean & 1.6 &3.0 & -3.9 &1.3 &  7.6 & 6.5 \\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

Here we put the numbers and their uncertainties in to separate columns and place the plus/minus sign in the table format with appropriate spacing for convenience. The headings then need to span the two columns of the number and its uncertainty, so we input these inside \multicolumn commands.

If you wish to have an input format that is like 1.2+-3.6 then you can define a command \pmnum that takes such a number and splits it into the two column entries as follows. :

\documentclass{article}

\usepackage{booktabs}
\usepackage{siunitx}

\makeatletter
\newcommand{\pmnum}[1]{\@pmnum #1+}
\def\@pmnum#1+-#2+{#1&#2}
\makeatother

\begin{document}
\thispagestyle{empty}
\begin{table}
  \centering
  \begin{tabular}{
    l
    S[table-format=1.1]@{\,\( \pm \)\,}
    S[table-format=2.1]
    }
  \toprule
    Station   & \multicolumn{2}{c}{GHI} \\
    \midrule
    BS        & \pmnum{2.4+-3.7} \\
    HB        & \pmnum{3.0+-10.3} \\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

Thank you to lionade for pointing out an error in a previous version of this code, where #1 and #2 in @pmnum were enclosed in \num, breaking the alignment.