10

I am making a table using booktabs and dcolumn. It works perfectly until I tried to bold one of the numbers and then the decimals won't read properly.

Are there any workaround here?

\documentclass{article}
\usepackage{booktabs,dcolumn}
\begin{document}
\newcolumntype{.}{D{.}{.}{-1}}
\begin{tabular}{l .}
\toprule
M & \multicolumn{1}{c}{N} \\
\midrule
5 & 12.0 \\
10 & \textbf{24.0}\\
\bottomrule
\end{tabular}
\end{document}

Bolded numbers

1
  • I don't know with dcolumn, but in this answer there's a method with siunitx and its S-type column.
    – egreg
    Jun 10, 2013 at 12:44

3 Answers 3

12

\textbf takes you out of math mode and gives you a text mode setting, which in principle is not using the math fonts at all (although in the computer modern setup digits and . do in fact come from the text roman font)

You would lose the alignment anyway as dcolumn needs to see the . at the top level not inside a group.

D is defined via \newcolumntype so you need to define a similar column say B that also inserts \boldmath

(Note your example generates several unrelated errors when run, please test before posting!)

enter image description here

\documentclass{article}

\usepackage{booktabs,dcolumn}
\begin{document}

\newcolumntype{.}{D{.}{.}{-1}}
\makeatletter
\newcolumntype{B}[3]{>{\boldmath\DC@{#1}{#2}{#3}}c<{\DC@end}}
\makeatother
\begin{tabular}{l .}
\toprule
M & \multicolumn{1}{c}{N} \\
\midrule
5 & 12.0 \\
10 & \multicolumn{1}{B{.}{.}{-1}}{24.0}\\
\bottomrule
\end{tabular}
\end{document}

The . will be aligned exactly using the above, but the default bold digits are wider than the standard ones so alignment will be out. this may not matter if you only have one or two digits but to use the non-extended bold easiest is to define a version of \boldmath that uses b rather than bx.

enter image description here

\documentclass{article}

\usepackage{booktabs,dcolumn}
\DeclareMathVersion{nxbold}
\SetSymbolFont{operators}{nxbold}{OT1}{cmr} {b}{n}
\SetSymbolFont{letters}  {nxbold}{OML}{cmm} {b}{it}
\SetSymbolFont{symbols}  {nxbold}{OMS}{cmsy}{b}{n}

\begin{document}


\newcolumntype{.}{D{.}{.}{-1}}
\makeatletter
\newcolumntype{B}[3]{>{\boldmath\DC@{#1}{#2}{#3}}c<{\DC@end}}
\newcolumntype{Z}[3]{>{\mathversion{nxbold}\DC@{#1}{#2}{#3}}c<{\DC@end}}
\makeatother
\begin{tabular}{l .}
\toprule
M & \multicolumn{1}{c}{N} \\
\midrule
5 & 11112.0 \\
10 & \multicolumn{1}{B{.}{.}{-1}}{11124.0}\\
19 & \multicolumn{1}{Z{.}{.}{-1}}{11124.0}\\
\bottomrule
\end{tabular}

\end{document}
4
  • 1
    somehow i knew you'd be the one to answer this. why don't you add this situation to the dcolumn documentation? Jun 10, 2013 at 12:53
  • 1
    @barbarabeeton I really wish I hadn't used an @ in \DC@ and \DC@end but it was a long time ago:-) I suspect that people (and google) are more likely to find it here than in the package doc but I may add something... Jun 10, 2013 at 13:27
  • Thanks David Carlisle - that's really nice. Do you mind if I add it to the wikibook? I'd probably make it a non-booktabs version for simplicity - though personally I'm a (recent) convert to the booktabs style.
    – Chris H
    Jun 24, 2013 at 9:55
  • @ChrisH sure, see this link Jun 24, 2013 at 14:16
0

I know this is an old thread, but I'd like to offer an alternative solution that solves a recurring problem raised by the previous answers. The previous answers all require a restatement of the column format parameters (e.g., \multicolumn{1}{B{.}{.}{-1}}{...}) in each bold cell. This becomes extremely cumbersome in large data tables, since changing the max decimal places for a column then requires finding every bold cell in the column and changing them all accordingly.

The following alternative solution creates a simpler, no-parameter \boldcell macro that changes the font to bold in a cell without needing to restate any column format parameters.

\documentclass{article}
\usepackage{booktabs,dcolumn}

\newcommand\theDC{}
\newcommand\boldcell{\relax\ifmmode$\egroup\fi\bfseries\boldmath\theDC}
\makeatletter
\newcolumntype{E}[3]{>{\def\theDC{\DC@{#1}{#2}{#3}}\theDC}c<{\DC@end}}
\makeatother
\newcolumntype{.}{E{.}{.}{-1}}

\begin{document}
\begin{tabular}{l .}
\toprule
M & \multicolumn{1}{c}{N} \\
\midrule
5 & 12.0 \\
10 & \boldcell 24.0 \\
\bottomrule
\end{tabular}
\end{document}

This works by creating a new column type E that is just like D except that it saves the column format parameters, which can then be recalled by \boldcell.

Getting a \boldcell implementation that works is trickier than it may seem because we can't use \multicolumn, since it replaces the column formatting before the original format parameters can be saved. To avoid this, \boldcell instead aborts the in-progress \DC@ before it creates any output and then restarts it with bold font. This way the original parameters get saved and can be automatically reused during the restart.

One can replace \boldmath in the above to use a different bold math font if desired (see other answers).

0

I got here but I just wanted to have the text bold but not increasing the width. I was not actually using dcolumn. In that case, via, you can do:

\usepackage{graphicx}
\newsavebox\CBox
\def\textBF#1{\sbox\CBox{#1}\resizebox{\wd\CBox}{\ht\CBox}{\textbf{#1}}}

And then use \textBF{24.0} instead of \textbf{24.0}.

Or alternative for math mode specifically (works well with siunitx):

\newsavebox\CBox
\def\mathBF#1{\sbox\CBox{#1}\resizebox{\wd\CBox}{\ht\CBox}{\ensuremath{\mathbf{#1}}}}
\newcommand{\best}[1]{\mathBF{#1}}  % for table

Then use \mathBF{24.0} or \best{24.0}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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