Grouped table headers in LaTeX with \multicolumn and \cmidrule
A grouped header is one cell that spans several columns, such as "Dataset A" above "Top-1" and "Top-5". In LaTeX you build it with \multicolumn{n}{c}{text}, draw the line beneath it with \cmidrule(lr){a-b} from booktabs, and span rows with \multirow from the multirow package. Count columns first: every row must still add up to the same total.
Convert a table from a PDF or screenshot →By TableToLatex · · Examples tested with pdfLaTeX, TeX Live 2023, booktabs, multirow
How grouped headers are built
A grouped header has two levels. The top row holds cells that span several columns, and the row below names the individual columns. \multicolumn{n}{align}{text} merges n columns into one cell with its own alignment. \cmidrule(lr){a-b} from booktabs draws a rule under columns a to b only, and \multirow{n}{*}{text} from the multirow package lets a label such as Method sit across both header rows.
Count before you type. The specification lcccc has five columns, so the group row is one cell for the label plus two groups of two: 1 + 2 + 2 = 5. Each group's partial rule uses the same numbers, columns 2 to 3 and 4 to 5.
% preamble: \usepackage{booktabs} \usepackage{multirow}
\begin{table}[htbp]
\centering
\caption{Top-1 and top-5 accuracy (\%) on two datasets.}
\label{tab:grouped}
\begin{tabular}{lcccc}
\toprule
\multirow{2}{*}{Method} & \multicolumn{2}{c}{Dataset A} & \multicolumn{2}{c}{Dataset B} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& Top-1 & Top-5 & Top-1 & Top-5 \\
\midrule
Baseline & 71.2 & 90.1 & 64.8 & 85.3 \\
Ours & 74.8 & 92.0 & 67.1 & 87.9 \\
\bottomrule
\end{tabular}
\end{table}The (lr) option shortens each rule by 0.5em at both ends, so two neighbouring rules show a gap instead of merging into one line. The rule weights themselves are covered in the guide to booktabs versus hline, and aligning the numbers under such a header is the subject of siunitx S columns.
Build a grouped header in seven steps
- 1
Count the columns
Write down the total number of real columns and which of them belong to each group. Grouped headers never change that total.
- 2
Write the column specification
One letter per real column: {lcccc} for a label plus two groups of two. The header cells that span columns do not appear here.
- 3
Add the group row
Use \multicolumn{2}{c}{Dataset A} for each group. If the label column should span both header rows, start the row with \multirow{2}{*}{Method}.
- 4
Add the partial rules
Immediately after the row's closing \\, add \cmidrule(lr){2-3} \cmidrule(lr){4-5}. The numbers are the columns each group covers.
- 5
Add the sub-header row
Start with an empty cell (the multirow label above already covers it), then one cell for each real column.
- 6
Finish the table
Follow the sub-header with \midrule, the data rows and \bottomrule.
- 7
Compile and recount
"Extra alignment tab has been changed to \cr" means a row has more cells than the specification. Recount the & signs and the spans in that row.
Header commands at a glance
| Piece | Syntax | Rule to remember |
|---|---|---|
| Span columns | \multicolumn{2}{c}{Dataset A} | n is how many columns it replaces; the row total must stay the same |
| Span rows | \multirow{2}{*}{Method} | Needs the multirow package; leave the cell below empty |
| Partial rule | \cmidrule(lr){2-3} | Column numbers count from 1; (lr) trims both ends |
| One-cell header | \multicolumn{1}{c}{Top-1} | Centres a header over a right-aligned number column |
| Two-line header | \makecell{Top-1\\acc.} | Needs the makecell package; or use two sub-header rows |
What goes wrong
- Extra alignment tab. A row has more cells than columns. The usual cause is forgetting that
\multicolumn{2}{c}{...}already uses two of them. - Undefined control sequence on
\multirow.\multicolumnis built in, but\multirowis not; add the multirow package. - Rules that join up. Neighbouring
\cmidrulecommands without(lr)merge into one line across the groups. - Rules under the wrong columns. The numbers count columns, not groups, so
{2-3}means the second and third column of the whole table. - Misplaced noalign. A
\cmidruletyped before the row's closing\\instead of after it. - Header text mangled in S columns. Plain text in a siunitx column is parsed as a number; brace it, as shown in the S-column guide.
How TableToLatex helps
TableToLatex reads the table from a PDF page or a screenshot and writes the rows as a booktabs tabular. Merged header cells are flattened into a single header row in the current output, which always compiles, so the group row and the \cmidrule lines are yours to add with the steps above. The LaTeX table generator gives you the data rows and column count to build on. The table tool is free and runs in your browser. Paste a tab-separated range from a spreadsheet. See pricing.
Questions about multicolumn and multirow
How do I span two columns in a LaTeX table?+
Use \multicolumn{2}{c}{Text} in place of two cells. The first argument is how many columns it covers, the second is the alignment (l, c or r) and the third is the content. The row still has to add up to the full number of columns.
What does the (lr) in \cmidrule(lr){2-3} do?+
It trims the rule at the left and right ends, by 0.5em unless you change it. Without it, two neighbouring partial rules touch and look like one long line.
Which packages do I need?+
\multicolumn is part of LaTeX itself. \cmidrule needs booktabs, and \multirow needs the multirow package. Load them with \usepackage{booktabs} and \usepackage{multirow}.
How do I merge cells vertically?+
Put \multirow{2}{*}{Label} in the first row and leave the cell below it empty. The star means the cell takes the natural width of its text.
Can I use \multicolumn with siunitx S columns?+
Yes, for headers. A group header over two S columns is \multicolumn{2}{c}{Group}. Header text for a single S column goes in braces or in \multicolumn{1}{c}{...}, otherwise siunitx tries to read it as a number.
Why does my cmidrule appear on the wrong row?+
The rule belongs to the row that ends just before it, so \cmidrule must come after that row's \\. Placing it before the \\ gives "Misplaced \noalign".
Does \cmidrule work without booktabs?+
No. The standard \cline{2-3} draws a similar rule but joins up with its neighbours and does not match the booktabs spacing, so use \cmidrule if the rest of the table uses booktabs.
Keep reading
- booktabs vs hline
Why journals and templates favour booktabs, with a before and after snippet.
- Decimal alignment
S columns, table-format, signs and ± without misaligned decimals.
- Table too wide
resizebox, adjustbox, landscape and sidewaystable, and when each is right.
Have a table in a spreadsheet? Paste it into the table tool, or see all LaTeX table guides.