[Tex/LaTex] siunitx: aligning numbers by decimal points in tables doesn’t work for bolded or italicized numbers

formattinghorizontal alignmentpunctuationsiunitxtables

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.

Best Answer

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}