13

This is a follow-up question for Aligning numbers by decimal points in table columns.

User lockstep provided this solution:

\usepackage{siunitx}
\begin{document}
\begin{tabular}{S[table-format=3.2]}
555 \\
7.77 \\
99.9
\end{tabular}

The accepted answer worked; but I subsequently found that it did not if the numbers are bolded in the table (i.e. within \textbf{}). In this case, everything is flushed left. I do need to use bold face in my case (to direct the user's eyes towards a particular column in a larger table).

To give an example, below is a test table and the output.

\begin{table}
   \begin{tabular}{@{}l S[table-format=3.2] S[table-format=3.2]@{}}
          \toprule
          \textbf{Foo} & \textbf{Normal} & \textbf{Bold}  \\
          \midrule
          foo1 & 111 & \textbf{111}\\
          foo2 & 222.2 & \textbf{222.2}\\
          foo3  & 3.33 & \textbf{3.33}\\
          foo4  & 4 & \textbf{4}\\
          foo5  & 5.5 & \textbf{5.5}\\
          \bottomrule
    \end{tabular}
\end{table}

Output:

alt text

I checked the manual but didn't find anything about this. I suppose the presence of the markup confuses the parser to believe there cell is not a number, but this is mere speculation and I could be wrong.

Thanks in advance for suggestions.

2 Answers2

15

You have to use the detect-all package option and to add \bfseries to the column declaration instead of using \textbf for every cell. (Sorry for not answering before - I wrongly assumed you wanted a single cell in bold.)

\documentclass{article}

\usepackage{booktabs}
\usepackage[detect-all]{siunitx}

\begin{document}

\begin{table}
  \begin{tabular}{@{}lS[table-format=3.2]>{\bfseries}S[table-format=3.2]@{}}
    \toprule
    \textbf{Foo} & \textbf{Normal} & \textbf{Bold} \\
    \midrule
    foo1 & 111 & 111 \\
    foo2 & 222.2 & 222.2 \\
    foo3 & 3.33 & 3.33 \\
    foo4 & 4 & 4 \\
    foo5 & 5.5 & 5.5 \\
    \bottomrule
  \end{tabular}
\end{table}

\end{document}
lockstep
  • 250,273
12

this came up recently in another forum and Joseph came up with a solution. I include his aswer. the key is to use the etoolbox package and \robustify\bfseries

Okay, the way that siunitx works with formatting is a little complex as it has to pick up which macros to expand and which not to. As e-TeX is required, I've taken the attitude that things 'not to expand' can be e-TeX \protected. For the case in hand, you therefore need to make `bfseries` robust. Something like

\documentclass{article}
\usepackage{colortbl,etoolbox,siunitx,xcolor}
\robustify\bfseries \sisetup{
detect-weight = true , locale = DE , }
\begin{document}

\begin{table} \sisetup{
group-separator = ., table-format = 5.0,
table-number-alignment = right , }
\begin{tabular}{S[table-format = 8.0,
table-number-alignment = right]SlSS} \rowcolor[gray]{0.9}
\bfseries 1234567 & \bfseries 12345 &
\bfseries Test & \bfseries 12345 &
\bfseries 12345 \\ 1234567 & 12345 &
Test & 12345 & 12345 \\
\end{tabular}
\end{table}

\end{document}

should do the job. I've got a bug fix of siunitx to do, so I'll add a note on this to the documentation. Let me know if the above works for you.

TH.
  • 62,639
Martin H
  • 18,164
  • Well, I actually had the question lockstep thought the OP had which is to only typeset single cells in bold. This works wonderfully, thanks! – Christian Jan 28 '13 at 17:57