Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
In general, we like to keep to 'one issue per question'. That means we can give general answers, which are potentially valuable to lots of people, rather than answering . For example, parentheses have come up before: tex.stackexchange.com/questions/39215/… –  Joseph Wright Jun 16 '12 at 11:26
    
The \rowcolor part is really entirely independent of siunitx, so you may want to ask that separately. –  Joseph Wright Jun 16 '12 at 12:23
    
Thanks, Joseph, I realized I need colortbl (as mentioned in the manual). –  Marius Hofert Jun 16 '12 at 12:51

1 Answer 1

up vote 10 down vote accepted

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.

share|improve this answer
    
Dear Joseph, many thanks for helping. I updated my post and incorporated final questions. –  Marius Hofert Jun 16 '12 at 9:37
    
Very good, thanks a lot. –  Marius Hofert Jun 16 '12 at 12:52
    
Use detect-all if you also want to retain the typeface and sizing. –  beerbajay Jun 10 at 20:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.