In an attempt to separate the formatting of tables, and in particular their headers, from the content, I have produced the following code:
\documentclass{article}
\usepackage{array}
\usepackage{colortbl}
\usepackage{xcolor}
\newcolumntype{L}{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}}
\newcolumntype{C}{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}}
\newcommand{\vheader}[1]{\multicolumn{1}{>{\bfseries}c}{#1}}
\newcommand{\fheader}[2]{\multicolumn{1}{>{\bfseries}Cb{#1}}{#2}}
\begin{document}
\begin{tabular}{cLp{50mm}lr}
\rowcolor{black!20}
\vheader{Rank} &
\fheader{50mm}{Venerable Institution of Higher Education} &
\vheader{Location} &
\vheader{Score} \\
1 & California Institute of Technology (Caltech) & United States & 94.9 \\
\multicolumn{4}{c}{\ldots}\\
12 & University of California, Los Angeles (UCLA) & United States & 86.3 \\
13 & Columbia University & United States & 85.2 \\
14 & ETH Z\"urich -- Swiss Federal Institute of Technology Z\"urich & Switzerland & 84.5 \\
\end{tabular}
\end{document}
(data taken from Times Higher Education, universities chosen to have some with long names)
The idea is that table headers should be
- bold face
- centered
- bottom aligned
but that these choices should be easy to change globally. Therefore I have marked up the header cells using macros \vheader
for variable-width columns, like c
, l
, r
, and \fheader
for fixed-width columns like p
, m
, b
. It works:
But there are three things I don't like:
- The need to repeat the width of the column for fixed-width columns. I have found that
\hsize
will be set to the column width. But all my attempts to retrieve it and use it in the multicolumn preamble failed, nagging about misplaced\omit
and the likes. - The need to say explicitly whether this is a fixed or variable width column. Again, I tried code that checks whether
\hsize
was changed from its value outside the tabular, and uses different multicolumn preambles depending on that, but it disagreed with multicolumn processing. - The need to say this for every cell, instead of once for the whole row. The
tabu
package has a\rowfont
macro for instance, that can take care of the bold face and centering at least, though not the bottom alignment. Buttabu
is not maintained, and looking at the source code, this is a really brittle business that involves code in the preamble to remove the previous glue of columns, which is different depending on whether or notcolortbl
is used.
Suggestions for each and any of these would be welcome—sorry for asking three questions at once!
Edit: replaced booktabs
by colortbl
and a grey background for the header row. Coloured backgrounds are frequently asked for by designers, so it's important that this works. And colortbl
can be difficult, with the background not filling the whole cell.
\newcommand\fheader[1]{\centering\arraybackslash\bfseries #1}
? Ah it doesn't work, sorry