How to print tabular confidence intervals as [x, y] with siunitx?
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}
I try to minimise how much I type so I'd put the brackets, [
and ]
, into the table definition:
\documentclass{article}
\usepackage{siunitx}
\begin{document}
\begin{table}[h]
\begin{tabular}{cS[table-format = 1.1]
@{\quad[\,}S[table-format = -1.2]@{,\,}S[table-format = -1.2]@{\,]}
}
A & \multicolumn{1}{c@{\quad\space}}{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}
This produces:
Here I have used @{...}
to insert material in between the columns. Note the sight kludge to get B
in the right place. This is necessary because of the \quad
, but it tends to suggest that there ought to be a more elegant solution!