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?

1

6 Answers 6

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

10
  • 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}? Commented Nov 2, 2010 at 11:05
  • @YossiFarjoun I've edited in an explanation.
    – Joseph Wright
    Commented Feb 2, 2013 at 7: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
    Commented Oct 29, 2014 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
    Commented Oct 29, 2014 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
    Commented Nov 2, 2016 at 9:20
17

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
4
  • 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
    Commented Nov 2, 2010 at 20:10
  • @Joseph: I'm looking forward to your table-package!
    – lockstep
    Commented Dec 6, 2010 at 22:20
  • 3
    @lockstep: Don't hold your breath - I have the output routine to think about yet :-)
    – Joseph Wright
    Commented Dec 7, 2010 at 6:34
  • 9
    @JosephWright: I hope you'll call it "carpenter" (as in "table-wright") Commented Feb 22, 2012 at 9:02
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}
5
  • 8
    @lockstep: it's heavily bugged. See here, for example
    – egreg
    Commented Jul 2, 2011 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
    Commented Jul 2, 2011 at 17:56
  • @Leo: well, just don't use p columns or add manually a final \strut to each p cell.
    – egreg
    Commented Jul 2, 2011 at 18:10
  • @egreg: I agree that the bug pointed out by you is a showstopper.
    – lockstep
    Commented Jul 2, 2011 at 18:29
  • Just to let you know that the original question of this answer got merged with this question now. Commented Jul 6, 2011 at 8:31
12

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}
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}
1
  • 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? Commented Dec 5, 2020 at 16:33
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

You must log in to answer this question.

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