93

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?

lockstep
  • 250,273
robintw
  • 7,362

6 Answers6

82

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.)

Werner
  • 603,163
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 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 02 '10 at 11:05
  • @YossiFarjoun I've edited in an explanation. – Joseph Wright Feb 02 '13 at 07:53
  • 3
    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 07: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 07:18
  • @JosephWright How can I use \multicolumns{2}{l}{col1 and col2} in the head column? – Jonathan Komar Apr 16 '15 at 08:53
  • How to apply this only to one table? – Léo Léopold Hertz 준영 Aug 10 '16 at 16:55
  • @JosephWright Why do you need \ignorespaces? Does it have anything to do with the spaces before #1? – Jonathan Komar Nov 02 '16 at 09:12
  • 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 02 '16 at 09:20
  • @kynan | also isn't great as it creates a vertical line. ~ works for me. – Toivo Säwén Mar 31 '22 at 10:53
18

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
Aditya
  • 62,301
15

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}

David Carlisle
  • 757,742
Leo Liu
  • 77,365
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}
David Carlisle
  • 757,742
lockstep
  • 250,273
  • I like this implementation. Do you know of a way to create a new tabular environment that automatically places the $ and ^ before the column declaration? – likethevegetable Dec 05 '20 at 16:33
10

It is easy with tabularray package to set row fonts. And you can change the environment from tblr to longtblr to get a long table.

\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{
   width = 0.5\textwidth, colspec = {X[1]X[2]X[3]}, hlines,
   row{1} = {font=\bfseries}, rowhead = 1,
}
   a & b & c \\
   1 & 2 & 3 \\
   1 & 2 & 3 \\
   1 & 2 & 3 \\
\end{tblr}
\end{document}
L.J.R.
  • 10,932
6

In {NiceTabular} of nicematrix, you have a built-in command \RowStyle to specify formatting commands to the (end of the) current row. So, it's possible to use \RowStyle{\bfseries}. However, the command \RowStyle has also a key bold which has the advantage to bold both text mode and math mode.

\documentclass{article}
\usepackage{nicematrix}

\begin{document} \begin{NiceTabular}{lcr} \RowStyle[bold]{} a & a & a \ b & b & b \ c & c & c \ \end{NiceTabular} \end{document}

Output of the above code

JamesT
  • 3,169
F. Pantigny
  • 40,250