10

I have a really long \longtable - some 150 rows. I need to make certain rows (~80 of them) boldface and leave the other ones normal. But just putting {\bf a & b & ... & z} \\ causes an error. Is there an elegant, simple way to do this? I've searched, but can't find anything.

The following does not seem to work, at least not on my machine:

\documentclass[12pt]{article}

\usepackage{array,longtable}

\newcolumntype{C}{>{\rowfont}c}
\newcommand\setrowfont[1]{\noalign{\gdef\rowfont{#1}}}
\gdef\rowfont{}

\begin{document}

\begin{longtable}{l||c|c}
\setrowfont{\bfseries}
let's & try & this\\ \hline
\setrowfont{\bfseries\itshape}
and & see & if\\
it & might & work
\end{longtable}

\end{document}
ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149

2 Answers2

11

enter image description here

\documentclass{article}
\usepackage{array,longtable}
\newcolumntype{C}{>{\rowfont}c}
\newcommand\setrowfont[1]{\noalign{\gdef\rowfont{#1}}}
\gdef\rowfont{}

\begin{document}

\begin{longtable}{CCC}
\setrowfont{\bfseries}
a&bb&ccc\\
aa&bbbb&cccccc\\
\setrowfont{\bfseries\itshape}
a&bb&ccc\\
\setrowfont{}
a&bb&ccc\\
a&bbbb&cccccc\\
a&bbbb&cccccc\\
\setrowfont{\bfseries}
a&bb&ccc\\
\setrowfont{}
a&bb&ccc
\end{longtable}

\end{document}
David Carlisle
  • 757,742
8

Package array allows to insert macros into the cells. This macro can be a font setting command. At the start of a bold row, it is globally defined as \bfseries, at the end of the row it is reset:

\documentclass{article}
\usepackage{longtable}
\usepackage{array}

\newcommand*{\boldrow}{%
  \global\let\rowfont\bfseries
  \bfseries
}
\newcommand*{\unboldrow}{%
  \global\let\rowfont\relax
  \mdseries
}
\newcommand*{\rowfont}{}

\begin{document}

\begin{longtable}{>{\rowfont}l>{\rowfont}l<{\unboldrow}}
1 & a\\
\boldrow 2 & b\\
3 & c\\
\boldrow 4 & d\\
\boldrow 5 & e\\
6 & f\\
7 & g\\
\boldrow 8\\
\unboldrow 9 & h\\
\end{longtable}

\end{document}

Result

Remarks:

  • In the example, the bold font is automatically reset, if the last cell is reached. Otherwise (line 8), in the next line \unboldfont can be used.
Heiko Oberdiek
  • 271,626