4

I am generating a bunch of TeX-tables (with Stata), in which one particular row has to be highlighted (bold). I can however only modify the first column in there, hence I want to place a command in the first cell of column to make it bold(or not)

So far I've been using this "Make first row of table all bold", which does exactly what I want.

\documentclass[12pt]{standalone}
\usepackage{dcolumn}    
\newcolumntype{X}{>{\rowstyle{\relax}}l}
\newcolumntype{Y}{>{\currentrowstyle}c}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}}
\begin{document}
\begin{tabular}{XYY}
normal row & 1.1 & 2.2\\
bold row \rowstyle{\bfseries} & 1.1 & 2.2\\
\end{tabular}
\end{document}

Now, I decided to switch to dcolumn, which is great, but breaks the bold-workaround, as it wraps cells in mathmode. The solution David provided here (Decimals in table don't align with dcolumn when bolded) does not really work either, as it would require a \multicolumn{1}{B}{...} in every bold cell.

Simply using mathbf instead doesnt seem to do the trick. Any suggestions are greatly appreciated.

sheß
  • 3,622
  • 1
    What do you mean with "outside of LaTeX"? Please show us some code of yours to play around with. Just one of the tables in a compilable environment. – LaRiFaRi Mar 21 '14 at 12:57
  • You should take a look at the numprint package (§6 of the documentation: Printing aligned numbers in tabulars). It can manage bold numbers (in text and math modes) and non-numeric extra content of cells. – Bernard Mar 21 '14 at 13:17
  • As David Carlisle explains in the answer you link to, \bfseries can't work because dcolumn uses math mode. – egreg Mar 21 '14 at 14:23
  • This is why I wrote "which is great, but breaks the bold-workaround, as it wraps cells in mathmode" just after the link. I am searching for away to solve this. I am sorry if that was not clear – sheß Mar 21 '14 at 14:38

3 Answers3

5

A solution with package siunitx:

\documentclass[12pt]{standalone}

\usepackage{array}
\usepackage{siunitx}

\newcolumntype{X}{%
  >{\rowstyle{\relax}}l%
}
\newcolumntype{Y}{%
  >{\currentrowstyle}S[detect-weight]%
}
\newcommand{\rowstyle}[1]{%
  \protected\gdef\currentrowstyle{#1}%
}

\begin{document}
  \begin{tabular}{XYY}
    normal row & 1.1 & 2.2\\
    normal row & 12.34 & 56.78\\
    bold row \rowstyle{\bfseries} & 1.1 & 2.2\\
    bold row \rowstyle{\bfseries} & 12.34 & 56.78\\
  \end{tabular}
\end{document}

Result

Heiko Oberdiek
  • 271,626
  • great, i use this for now. i will just not (for now) mark it as accepted answer since it is technically not an answer to the question(-title). – sheß Mar 21 '14 at 15:08
3
\documentclass[12pt]{standalone}
\usepackage{dcolumn}  

\makeatletter  
\newcolumntype{X}{>{\rowstyle{\relax}}l}
\newcolumntype{D}[3]{>{\currentrowstyle\DC@{#1}{#2}{#3}}c<{\DC@end}}
\makeatother
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}}
\begin{document}
\begin{tabular}{XD..{-1}D..{-1}}
normal row & 1.1 & 2.2\\
bold row \rowstyle{\bfseries\boldmath} & 1.1 & 2.2\\
\end{tabular}
\end{document}

enter image description here

David Carlisle
  • 757,742
1

I use this:

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

And then in the table, just \best{1.1} or wherever you want to have some number in bold.

This is specifically for math mode and works well together with siunitx.

Inspired from here. Also see here.

Albert
  • 2,707