I am trying to align tabular columns on a decimal point using dcolumn
. Some cells should be bold. If I do this with \bm
, the document does not compile, whereas if I do it as \textbf
it does, but is misaligned. (To make my example compile, I allow it to use either \textbf
or \bm
). It was my understanding that dcolumn
would put columns in math mode, but clearly the bold columns are not. If I try to manually put them in math mode with $...$
, I have the same result (doesn't compile with \bm
, uses \textbf
when given a choice).
How can I achieve the desired effect of making some cells bold while aligning on the decimal?
Minimal working example:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs,array,multirow}
\usepackage{dcolumn}
\usepackage{bm}
%% Column numbers aligned on the decimal - See https://tex.stackexchange.com/questions/34614/in-a-tabular-how-to-left-align-ignoring-minus-signs
\newcolumntype{L}{D{.}{.}{2,3}}
%% Standard way of using bold math or textbf, as needed - proposed at https://tex.stackexchange.com/questions/595/how-can-i-get-bold-math-symbols
\newcommand*{\B}[1]{\ifmmode\bm{#1}\else\textbf{#1}\fi}
\begin{document}
\begin{table}[!t]
\caption{Example table}
\centering
\begin{tabular}{lLLL}
\toprule
\multicolumn{1}{l}{\textbf{\textit{ID}}} &
\multicolumn{1}{l}{\textbf{A}} & \multicolumn{1}{l}{\textbf{B}} &
\multicolumn{1}{l}{\textbf{C}} \tabularnewline
\midrule
i1 (A) & \B{0.989} & 0.191 & 0.649 \tabularnewline
i2 (B) & -0.137 & \B{0.884} & 0.001 \tabularnewline
i3 (B) & 0.111 & \B{0.847} & 0.273\tabularnewline
i4 (C) & 0.181 & 0.327 & \B{0.900} \tabularnewline
\bottomrule
\end{tabular}
\end{table}
\end{document}
Update: Solution
I used the approach suggested by David Carlisle and present the modifications here for reference.
Added to the preamble:
\makeatletter
\newcolumntype{B}[3]{>{\boldmath\DC@{#1}{#2}{#3}}c<{\DC@end}}
\makeatother
Changes to the table rows:
i1 (A) & \multicolumn{1}{B{.}{.}{2,3}}{0.989} & 0.191 & 0.649 \tabularnewline
i2 (B) & -0.137 & \multicolumn{1}{B{.}{.}{2,3}}{0.884} & 0.001 \tabularnewline
i3 (B) & 0.111 & \multicolumn{1}{B{.}{.}{2,3}}{0.847} & 0.273\tabularnewline
i4 (C) & 0.181 & 0.327 & \multicolumn{1}{B{.}{.}{2,3}}{0.900} \tabularnewline