7

I also want to align the second column. The problem is, that there can't be two numbers in one cell Is there a way to align the second column via digits even if there are more decimals? I also used the input-ignore option to ignore the commas

\usepackage{tabular}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{@{}
  S[table-format=1.2]
  l
  @{}}
{Radius} & {Luminosity}\\
0.30 &     [-195.728, 213.571] \\
0.000 &  [-2465.825, -1234.722] \\
0.648 &       [-35.587, 22.432] \\
\end{tabular}
\end{document}

3 Answers 3

7

You can split into two columns and inject the brackets:

\documentclass{article}

\usepackage{siunitx,booktabs}

\NewDocumentCommand{\lbr}{}{[\thinspace}
\NewDocumentCommand{\rbr}{}{]}

\begin{document}

\begin{tabular}{
  S[table-format=1.3]
  >{\lbr}S[table-format=-4.3,table-space-text-pre=\lbr]
  @{, }
  S[table-format=-4.3,table-space-text-post=\rbr]<{\rbr}
}
\toprule
{Radius} & \multicolumn{2}{c}{Luminosity}\\
\midrule
0.30  &  -195.728 &   213.571 \\
0.000 & -2465.825 & -1234.722 \\
0.648 &   -35.587 &    22.432 \\
\bottomrule
\end{tabular}
\end{document}

enter image description here

A small final note: \NewDocumentCommand is provided by xparse, which is loaded by siunitx. Using it, \lbr becomes a “protected” command, so it can be used as a “text-pre” in an S column.

2
  • I rejected the edit adding xparse to the list of loaded packages on the grounds that I verified that the example compiles without it since siunitx already loads xparse. There might be merit in mentioning that \NewDocumentCommand is a xparse command and that siunitx loads xparse, but this should probably happen in a comment and not in an uncommented edit.
    – moewe
    Commented Apr 24, 2019 at 13:26
  • 1
    @moewe Thanks; I added a final comment with a short explanation.
    – egreg
    Commented Apr 24, 2019 at 13:53
5

Split it into two columns:

\documentclass{article}

\usepackage{siunitx}

\begin{document}
\begin{tabular}{@{}
  S[table-format=1.3]
    @{\space[\thinspace}
  S[table-format=-4.3]
    @{,\space}
    S[table-format=-4.3]
        @{\thinspace]}
        @{}}
\multicolumn{1}{c}{Radius} & \multicolumn{2}{c}{Luminosity}\\
0.30 &     -195.728 & 213.571 \\
0.000 &  -2465.825 & -1234.722 \\
0.648 &       -35.587 & 22.432 \\
\end{tabular}
\end{document}

enter image description here

5
  • Rather than \space[\thinspace, use \hspace{2\tabcolsep}[\thinspace so as to restore the standard intercolumn spacing.
    – egreg
    Commented Apr 24, 2019 at 11:27
  • @egreg Thanks for your suggestion, but this would misalign the header
    – user186389
    Commented Apr 24, 2019 at 11:28
  • Yes, you need to add the same material in \multicolumn{1}{@{}c@{\hspace*{2\tabcolsep}\phantom[\thinspace}{Radius}. Possibly better is to inject the material with >{[\thinspace}
    – egreg
    Commented Apr 24, 2019 at 11:30
  • @egreg File ended while scanning use of \multicolumn.
    – user186389
    Commented Apr 24, 2019 at 11:33
  • @egreg >{[\thinspace} will mess with siunitx alignment
    – user186389
    Commented Apr 24, 2019 at 11:33
1

A variant layout:

\documentclass{article}

\usepackage{siunitx}

\begin{document}

\setlength{\extrarowheight}{2pt}
\begin{tabular}{@{}
  S[table-format=1.3]
     >{[\,}S[table-format=-4.3, table-space-text-pre = {[}, table-align-text-pre=false]<{{,}}
@{\enspace}
    S[table-format=-4.3, table-space-text-post ={]}]
         <{$\!$]}
        @{}}
{Radius} & \multicolumn{2}{c}{Luminosity}\\
0.30 & -195.728 & 213.571 \\
0.000 & -2465.825 & -1234.722 \\
0.648 & -35.587 & 22.432 \\
\end{tabular}

\end{document} 

enter image description here

You must log in to answer this question.

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