77

I'm trying to do something that I thought would be relatively simple but seems to be quite hard. I have a table with a number of columns, and I'd like to make all of the titles appear in bold text. Obviously I can add \textbf to each of them in turn, but it strikes me that there must be a nicer way of doing this (particularly for tables with many columns).

As far as I can see there isn't a good way of doing this with plain LaTeX. Are there any packages that make this (and possibly other table-related issues) easier?

72

Taking apan's comment and turning it into an example:

\documentclass{article}
\usepackage{array}
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
}

\begin{document}
\begin{tabular}{$l^c^r}
\rowstyle{\bfseries}
a & a & a \\
b & b & b \\
c & c & c \\
\end{tabular}
\end{document}

This gives:

enter image description here

You might, of course, choose different markers for the column types.


To explain what is going on, the first 'column' is of type $ (could be any symbol not required in the preamble). This simply sets \currentrowstyle to do nothing, which means that in each row this command will be a no-op unless something else happens. The first real column (here l) will contain the command to make it bold (if required), but that is not true for the other columns. They therefore are preceded by ^, which is another fake column type used to apply \currentrowstyle.

In a normal row, \currentrowstyle therefore starts off as \relax and never changes, so the ^ do nothing and the row is unchanged. However, if the first column sets \rowstyle, this is saved as \currentrowstyle (for the later columns) and applied (for this column). The ^ then insert this at the start of each column in the row, so everything is bold.

(All of the operations are global as table cells form groups.)

  • 2
    I think I can explain how this works: $ is defined to "reset" the comment \currentrowstyle which is defined in \rowstyle. the rowstly command also sets the style for the column is currently is in (that's what the second #1 is for). The ^ simply puts \currentstyle in the beginning of the cell, so that if it is \relax, nothing happens, but if there are a \rowstyle command, the row gets styled. notice that a corollary of this is that if you put a \rowstly command in the middle of the row, you affect that column and the following ones. Exercise: what is the effect of {$lc^c$cc}? – Yossi Farjoun Nov 2 '10 at 11:05
  • @YossiFarjoun I've edited in an explanation. – Joseph Wright Feb 2 '13 at 7:53
  • 2
    I think $ isn't a particularly good choice of token, since many editors treat this as beginning a math region and the syntax highlighting is messed up. I'd used ^ and |. – kynan Oct 29 '14 at 7:16
  • @kynan I'm just using the symbols chosen by the author of the FAQ page linked to in a comment on the question: I didn't pick this particular set of symbols. – Joseph Wright Oct 29 '14 at 7:18
  • 1
    @macmadness86 The standard LaTeX tabular construct puts an \ingorespaces at the start of the cell: it's therefore expected in most peoples input. In the example in the above I don't need a % after the \rowcolor line due to the space skipping: if it was absent I'd need to worry about the line end. – Joseph Wright Nov 2 '16 at 9:20
16

This is one of the cases where I find the ConTeXt interface to be much better than LaTeX. To make the first row of a table bold, you just need

\setupTABLE[row][1][style=bold]

and not change anything in the table body. So, there is a clear separation of style and presentation. Minimal example:

\setupTABLE[row][1][style=bold]

\starttext
\startTABLE
  \NC A \NC B \NC C \NC \NR
  \NC A \NC B \NC C \NC \NR
  \NC A \NC B \NC C \NC \NR
\stopTABLE
\stoptext
  • 8
    No-one ever said LaTeX was perfect :-) I keep meaning to have a serious go at tables (for LaTeX3), but have not yet had the time just yet. – Joseph Wright Nov 2 '10 at 20:10
  • @Joseph: I'm looking forward to your table-package! – lockstep Dec 6 '10 at 22:20
  • 3
    @lockstep: Don't hold your breath - I have the output routine to think about yet :-) – Joseph Wright Dec 7 '10 at 6:34
  • 9
    @JosephWright: I hope you'll call it "carpenter" (as in "table-wright") – Brent.Longborough Feb 22 '12 at 9:02
14

For multi-page tables, besides lockstep's solution (from UK-FAQ), you can also use \rowfont command in longtabu environment from tabu package.

\documentclass{article}
\usepackage{longtable}
\usepackage{tabu}

\begin{document}

Some text.

\vspace{42\baselineskip}

\begin{longtabu}{lcr}
\rowfont{\bfseries}
a & a & a \\
b & b & b \\
c & c & c \\
\end{longtabu}

\end{document}
  • 7
    @lockstep: it's heavily bugged. See here, for example – egreg Jul 2 '11 at 17:46
  • @egreg: There're some bugs in tabu, but it is still usable. Another bug I know is that tabu cannot used together with tabularx and arydshln. (It isn't very important since tabu provides its own dash line syntax.) – Leo Liu Jul 2 '11 at 17:56
  • @Leo: well, just don't use p columns or add manually a final \strut to each p cell. – egreg Jul 2 '11 at 18:10
  • @egreg: I agree that the bug pointed out by you is a showstopper. – lockstep Jul 2 '11 at 18:29
  • Just to let you know that the original question of this answer got merged with this question now. – Martin Scharrer Jul 6 '11 at 8:31
10

For tables spanning several pages, simply combine array (as in Joseph Wright's answer) and longtable.

\documentclass{article}

\usepackage{array}
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
}
\usepackage{longtable}

\begin{document}

Some text.

\vspace{42\baselineskip}

\begin{longtable}{$l^c^r}
\rowstyle{\bfseries}
a & a & a \\
b & b & b \\
c & c & c \\
\end{longtable}

\end{document}

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.