[Tex/LaTex] siunitx: How to get bold, aligned entries and how to align when units differ in length

horizontal alignmentsiunitxtablestabularx

I tried my first steps with siunitx, but I have trouble achieving the following in the minimal example below:

1) aligning the entries in the first column with respect to the decimal dot

2) getting a bold entry in the second column

3) getting the entries in the second column aligned according to the decimal dot.

The code is currently not running due to the \rowcolor command. I adapted this from the manual siunitx.pdf, why is it not working?

\documentclass{article}

\usepackage{siunitx, tabularx, etoolbox, xcolor}

\begin{document}
\begin{table}
\robustify\bfseries% see 7.15 in siunitx.pdf
\centering
\begin{tabular}{lS[table-format=2.3]S[table-format=1.4]}
    \multicolumn{1}{c}{$a\ (b)$}&\multicolumn{1}{c}{col 1}&\multicolumn{1}{c}{col 2}\\\hline
    0.05\ (0.1)&\SI{20.64}{s}&\rowcolor[gray]\bfseries\SI{3.0601}{s}\\
    0.1\ \phantom{5}(0.2)&\SI{16.82}{s}&\SI{1.59}{s}\\
    0.3\ \phantom{5}(0.4)&\SI{22.48}{s$^\ast$}&\SI{0.64}{s}\\
    0.4\ \phantom{5}(0.5)&\multicolumn{1}{c}{$-$}&\SI{0.52}{s}\\
  \end{tabular}
\end{table}
\end{document}

Update
After Joseph's first suggestion, I changed the table like this:

\documentclass{article}

\usepackage{siunitx, tabularx, etoolbox, xcolor, booktabs}

\begin{document}
\begin{table}
\robustify\bfseries
\centering
\begin{tabular}{ScS[table-format=2.2]lS[table-format=1.4, detect-weight]}
  \toprule
  $a$ & $(b)$ & \multicolumn{2}{c}{col 1, unit} & {col 2/\si{\s}}\\
  \midrule
  0.05 & (0.123) & 20.64 & s & \bfseries 3.0601\\
  0.1 & 0.2 & 16.82 & s2 & 1.59\\
  0.3 & 0.4 & 22.48 & s$^\ast$ & 0.64\\
  0.4 & 0.5 & {--} & kg & 0.52\\
  \bottomrule
\end{tabular}
\end{table}
\end{document}

My remaining questions are:

1) How can I align the entries of the second column when one of the entries has surrounding parenthesis? [(0.123)]

2) Putting the unit in a different column creates a too large spaces between the units and the numbers (especially if some numbers have more digits such as 2.3 kg vs 1.232525 kg). Is there a solution which aligns the entries of the col 1 according to the decimal point but allows entries to be specified by \SI{}{} to get proper spacing between numbers and units?

3) \rowcolor still does not work, I obtain an Undefined control sequence error. Do I need additional packages?

Best Answer

The content of the S column should be a number, not an \SI or \num command. You should then either move the unit to the column header (by division) or use a separate column. The latter approach is only recommended if the units vary down the column, which is not the case here. You also do not need the \multicolumn commands as siunitx will automatically deal with braced content in S columns:

\documentclass{article}

\usepackage{siunitx, tabularx, etoolbox, xcolor,booktabs}

\begin{document}
\begin{table}
\robustify\bfseries% see 7.15 in siunitx.pdf
\centering
\begin{tabular}{lS[table-format=2.2,table-space-text-post={*}]
  S[table-format=1.4,detect-weight]}
  \toprule
    \multicolumn{1}{c}{$a\ (b)$}
      &{col 1/\si{\s}}
      &{col 2/\si{\s}}\\
    \midrule  
    0.05\ (0.1)&20.64 & \bfseries 3.0601\\
    0.1\ \phantom{5}(0.2)&16.82&1.59\\
    0.3\ \phantom{5}(0.4)&22.48*&0.64\\
    0.4\ \phantom{5}(0.5)&{--}&0.52\\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

Note that I've used the booktabs package to draw proper rules in the table: these are superior to the LaTeX kernel's \hline.


Adding in the other requirements would take me to something like

\documentclass{article}

\usepackage{siunitx,tabularx, etoolbox, xcolor, booktabs}

\begin{document}
\begin{table}
\robustify\bfseries
\centering
\begin{tabular}
  {
    S
    S[table-format = 2.4,input-open-uncertainty=,input-close-uncertainty=,
      input-symbols=()]
    S[table-format=2.2,table-number-alignment = right]
    @{\,}
    s[table-alignment=left]
    S[table-format=1.4, detect-weight]
  }
  \toprule
  $a$ & {$(b)$} & \multicolumn{2}{c}{col 1, unit} & {col 2/\si{\s}}\\
  \midrule
  0.05 & (0.123) & 20.64 & \s & \bfseries 3.0601\\
  0.1 & 0.2 & 16.82 & \s\squared & 1.59\\
  0.3 & 0.4 & 22.48 & \s$^\ast$ & 0.64\\
  0.4 & 0.5 & {--} & \kg & 0.52\\
  \bottomrule
\end{tabular}
\end{table}
\end{document}

To include ( ... ) as part of the number, I've altered the settings such that the two tokens are not read as an uncertainty part but instead as symbols. I've also reserved space for this extra tokens by treating them as 'digits' from the point of view of table-format.

To deal with the spacing for a unit, I have used an s column (which parses units), and used @{\,} to remove the normal LaTeX inter-column spacing and use instead \, (a thin space). I've then altered the alignment so that the two parts of the quantity are pushed together.

Related Question