22

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?

4
  • I don't know the answer, but even so you should use the table-formats 1.1(2) for the first column, -1.1(2) for the second and 2.1(3) for the third to avoid overful boxes.
    – cgnieder
    Dec 6, 2012 at 21:51
  • @cgnieder thank you. I didn't really understood table-format I think. For my question I would need something like 2.1(2.1) I think, but this doesn't work.
    – bmu
    Dec 9, 2012 at 10:10
  • What you have there in parentheses are not uncertainties but look more like estimated errors. Uncertainties result from reading errors and usually are in the magnitude of some decimal places. Dec 9, 2012 at 10:58
  • 1
    @ThorstenDonig The numbers are trends and resulting uncertainties of a linear linear regression. They are quite big, but they are uncertainties. However it doesn't help to answer my question. Or do you mean to give the uncertainties in a different way?
    – bmu
    Dec 9, 2012 at 11:06

1 Answer 1

25

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.

6
  • I'm aware that people want to do two-part columns using one piece of input. While I'm not personally keen on this (unnecessary repetition of \pm, mixing two pieces of data in one input columns, ...), I will try to look to extend the package at some stage. This may well have to wait for v3, where the idea is to further increase the 'modularisation' of the number processing and therefore to extend the range of processable numbers.
    – Joseph Wright
    Dec 9, 2012 at 11:12
  • @JosephWright What I would like to have is the ability to write something like S[table-format=2.1(2.1)] in this case. I don't really understand your comment. Is something wrong with is approach?
    – bmu
    Dec 9, 2012 at 11:30
  • @bmu table-format = 2.1(2.1) makes no sense, as the bracket form of an uncertainty is always read as applying to the least significant figures. My point is that the current code does not attempt to carry out two separate alignments (main part and uncertainty), not least because I strongly favour the more compact bracket approach over using \pm.
    – Joseph Wright
    Dec 9, 2012 at 11:44
  • 1
    But in my case I really have high uncertainties, so this would be 7.5(106) I think. From my experience this is not usual in my scientific field to give the uncertainties in that way. Further more I would like to be able to give uncertainties, which are not symmetric, something like +4/-2 which is not possible with the brackets. But you are right with the table format, something like S[table-format=2.1+-2.1] perhaps would be clearer. Is this an option?
    – bmu
    Dec 9, 2012 at 11:49
  • I had the same issue and just like the OP hoped that S[table-format=1.1(2)] or S[table-format=2.1+-2.1] would be the way to format this.
    – Sicco
    May 3, 2013 at 17:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .