# Tabular align on decimal point with bold cells [duplicate]

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.

\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


• @DavidCarlisle I think it is not a duplicate as the bold contents are mixed up randomly. – Raaja Aug 8 '18 at 12:16
• @RaajaG well in fact each cell is styled separately so it comes to the small thing actually, they just happen to be in a row in that example, but perhaps you are right an an explicit answer here might be better – David Carlisle Aug 8 '18 at 12:18
• @DavidCarlisle that is what I think too. – Raaja Aug 8 '18 at 12:23
• @RaajaG actually I just linked to the wrong answer, I meant to link to tex.stackexchange.com/a/118463/1090 – David Carlisle Aug 8 '18 at 12:23
• @DavidCarlisle Then I agree with you! – Raaja Aug 8 '18 at 12:25

Is this what you're looking for?

\documentclass{article}

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

\begin{document}

\begin{table}[!t]
\caption{Example table}
\centering
\begin{tabular}{l r@{.}l r@{.}l r@{.}l}
\toprule
\multicolumn{2}{l}{\textbf{ID}}&\textbf{A}&\multicolumn{2}{c}{\textbf{B}}&\multicolumn{2}{c}{\textbf{C}}
\tabularnewline
\midrule
i1 (A) & \textbf{0}&\textbf{989} & 0&191 & 0&649 \\
i2 (B) & $-0$&137 & \textbf{0}&\textbf{884} & 0&001 \\
i3 (B) & 0&111 & 0&847 & 0&273\\
i4 (C) & 0&181 & 0&327 & \textbf{0}&\textbf{900}\\
\bottomrule
\end{tabular}
\end{table}

\end{document}


• note the spacing is worse if you use r@{.}l and also as you are using bold extended digits the alignment is lost, see tex.stackexchange.com/a/118463 – David Carlisle Aug 8 '18 at 12:24
• When you post an answer, please make it compilable so people don't have to guess the necessary packages. – Phelype Oleinik Aug 8 '18 at 12:32
• Thank you. I employed the solution David Carlisle linked to as I prefer to use dcolumn to align rather than using extra columns. – Ann Aug 8 '18 at 12:53