[Tex/LaTex] How to print tabular confidence intervals as [x, y] with siunitx

siunitx

What would be the recommended way to print a confidence interval as [x, y] in an siunitx S column? At the moment I have a two S column solution:

\documentclass{article}
\usepackage{siunitx}
\begin{document}
    \begin{table}[h]
        \begin{tabular}{cS[table-format = 1.1]
                S[table-format = -1.2, table-space-text-pre=[ ]@{}
                S[table-format = -1.2, table-space-text-post=[, table-space-text-pre=[ ]
                }
            A & B & \multicolumn{2}{c}{CI} \\
            Values & 2.3 & {[}1.23{,} & 1.23{]} \\
            Values & 2.3 & {[}-3.42{,} & -2.43{]} \\
            Values & 2.3 & {[}4.12{,} & 7.33{]} \\
            Values & 2.3 & {[}-1.03{,} & -9.11{]} \\
        \end{tabular}
    \end{table}
\end{document}

Is there an easier siunitx way?

Best Answer

You can make use of table-space-text-pre and table-space-text-post to make space for the square brackets. To insert the brackets before and after the columns, use the >{...} and <{...} syntax. To avoid wrong spacing before the last square bracket at each line, you have to use the TeX primitive \cr instead og \\ to terminate the rows, as explained in section 7.13 of the siunitx manual.

To center the column heading B you can simply put it in a group such as {B}.

\documentclass[border=10pt]{standalone}
\usepackage{siunitx}
\begin{document}
  \begin{tabular}{
    c
    S[table-format = 1.1]
    >{{[}} % Add square bracket before column
    S[table-format = -1.2,table-space-text-pre={[}]
    @{,\,} % Add comma and thin-space between the columns
    S[table-format = -1.2,table-space-text-post={]}]
    <{{]}} % Add square bracket after column
  }
    A      & {B} & \multicolumn{2}{c}{CI} \cr
    Values & 2.3 &  1.23 &  1.23 \cr
    Values & 2.3 & -3.42 & -2.43 \cr
    Values & 2.3 &  4.12 &  7.33 \cr
    Values & 2.3 & -1.03 & -9.11 \cr
  \end{tabular}
\end{document}

Output