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}
2
  • 4
    one of the first users of longtable, last century, had tables that were several thousand rows long (and over 1100 tables in his document) so 150 rows isn't really long. Jul 1, 2013 at 16:34
  • @DavidCarlisle Rather good benchmark.
    – egreg
    Jul 1, 2013 at 16:45

2 Answers 2

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}
5
  • You've done exactly what I wanted, but I can't get this to work on my machine...it's not giving me any errors; it's just not boldfacing the rows! Jul 1, 2013 at 17:00
  • @user11693 You mean if you copy the code above it does not produce the image shown? (That seems unlikely) or do you mean it doesn't work when you apply to your real document? Jul 1, 2013 at 17:02
  • Hi David. Please see my edit in the OP! I'm beginning to think that the "|" in the argument of longtable are causing the problem! Jul 1, 2013 at 17:04
  • 2
    @user11693 you've used c not the C column defined to pick up the font Jul 1, 2013 at 17:06
  • D'oh! Wow...THAT was stupid. Curse you, case-sensitivity. Thanks, David! Works like a charm :) Jul 1, 2013 at 18:22
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .