Skip to content
Topos Uranos

Summary

This lesson extends the readable table of the previous lesson to the cases that a simple grid does not handle. It teaches how to merge cells across columns with \multicolumn, to group headings under a partial rule and to change the alignment of a single cell, and works out where the surplus width of a grouped heading goes; it teaches how to merge cells across rows with \multirow and how to decide when it is better avoided; it compares three ways of giving a table the width of the line (tabularx with its X column, tabular* with \extracolsep, and scaling with \resizebox, which is discouraged); it typesets tables that span several pages with longtable, with their repeated heading and the passes they need; it presents the rotation of pages for very wide tables; it generates tables from a data file with csvsimple and pgfplotstable, creating the file with filecontents; it shows tabularray as a modern alternative; and it ends with the typical errors, read in real logs.

Learning objectives

  1. Build grouped headings with \multicolumn and \cmidrule, count the cells of each row and predict the result when the count does not match.
  2. Choose between \multirow and the empty cell of the booktabs tradition, justifying the choice by how the table is read.
  3. Fit a table to the width of the line with tabularx or with tabular*, and explain why a table is not scaled with \resizebox.
  4. Typeset with longtable a table of several pages with its own heading and footer, interpret its warnings and know why it cannot go inside a float.
  5. Generate a table from a data file and explain what is gained by keeping a single source for the data.

Cells that span columns

The command \multicolumn{n}{alignment}{text} takes the place of n adjacent cells of a row and replaces them with a single one, with the alignment given in its second argument, written as in the table preamble (l, c, r, p{…}, and also @{}). From this follows a counting rule that the author must check in every row: the number of simple cells plus the widths declared in each \multicolumn must be, at most, the number of columns in the preamble.

Changing the alignment of a single cell

The humblest use of \multicolumn spans a single column: \multicolumn{1}{c}{…} merges nothing, but it gives that cell an alignment different from that of its column. This is the way to center the heading of a column of right-aligned figures, or to right-align the label of a total row, which should stand next to its figure.

ExampleThe budget of a project
LaTeX code
\documentclass{article}\usepackage{fontspec}\usepackage[english]{babel}\usepackage{booktabs}\begin{document}\begin{tabular}{llr}\topruleItem     & Details                & \multicolumn{1}{c}{Amount} \\\midruleTravel   & Two visits to the archive & 1\,200 \\Supplies & Paper and external drives &    300 \\Services & Digitization of holdings  &    850 \\\midrule\multicolumn{2}{r}{Total} & 2\,350 \\\bottomrule\end{tabular}\end{document}
Result
Result

The heading “Amount” is centered over a column that remains right-aligned, and the label “Total”, which spans the first two columns, is right-aligned, next to its figure.

Grouped headings

When several columns share a category, the table gains from a two-level heading: above, the name of the group, spanning its columns; below, the name of each column. Between both levels, booktabs offers the partial rule \cmidrule{2-3}, which underlines only columns 2 to 3; with the options (lr), \cmidrule(lr){2-3}, the rule is trimmed slightly on the left and on the right, so that two adjacent rules do not touch and the eye sees two groups.

ExampleResults of a study by group
LaTeX code
\documentclass{article}\usepackage{fontspec}\usepackage[english]{babel}\usepackage{booktabs}\begin{document}\begin{tabular}{lrrrr}\toprule& \multicolumn{2}{c}{Control group}& \multicolumn{2}{c}{Experimental group} \\\cmidrule(lr){2-3} \cmidrule(lr){4-5}Measurement & Mean & SD & Mean & SD \\\midruleBaseline & 52.1 & 8.3 & 51.7 & 7.9 \\Final    & 54.0 & 8.1 & 61.2 & 7.4 \\\bottomrule\end{tabular}\end{document}
Result
Result

The first row has an empty cell (the initial &) and two cells of two columns each: one plus two plus two, five, as in the preamble. Notice that the standard deviation column of the experimental group is wider than that of the control group.

Where the surplus width goes

The difference in widths in the previous example is no accident. If the text of a \multicolumn is wider than the sum of the columns it spans, the table must widen, and the engine gives all the excess to the last of those columns; the others keep their natural width. “Experimental group” is wider than “Mean” and “SD” together, and the second one receives the difference. With a heading much longer than its data, the effect is no longer discreet.

ExampleA heading that is too long
LaTeX code
\documentclass{article}\usepackage{fontspec}\usepackage[english]{babel}\usepackage{booktabs}\begin{document}\begin{tabular}{lrr}\toprule& \multicolumn{2}{c}{Experimental group after the intervention} \\\cmidrule(lr){2-3}Measurement & Mean & SD \\\midruleBaseline & 51.7 & 7.9 \\Final    & 61.2 & 7.4 \\\bottomrule\end{tabular}\end{document}
Result
Result

The mean lies far from its standard deviation, separated by a blank that means nothing. The correction is not typographic but editorial: a short heading (“Experimental”) and the explanation in the caption of the table or in the text.