Merge cells in a LaTeX table: multirow and multicolumn
Help centre › LaTeX › Tables · Packages: multirow, booktabs
To merge cells across columns, write \multicolumn{3}{c}{Text} in place of three cells. To merge cells down rows, load \usepackage{multirow}, write \multirow{2}{*}{Text} in the first row and leave that cell empty in the rows below.
A complete example
A whole document: paste it into an empty project and compile it.
\documentclass{article}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{table}[htbp]
\centering
\caption{Accuracy by model and data set.}\label{tab:acc}
\begin{tabular}{llcc}
\toprule
& & \multicolumn{2}{c}{Data set} \\
\cmidrule(lr){3-4}
Family & Model & A & B \\
\midrule
\multirow{2}{*}{Linear} & Ridge & 0.81 & 0.77 \\
& Lasso & 0.79 & 0.75 \\
\midrule
\multirow{2}{*}{Tree} & Forest & 0.88 & 0.84 \\
& Boosted & 0.90 & 0.86 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}\multicolumn: merging across columns
It replaces that many cells, so the row has fewer &: in a four-column table, \multicolumn{2}{c}{…} plus two other cells makes three cells and two &. The alignment is a column specification of its own (c, l, r, p{3cm}), and it may add vertical lines, such as |c|.
\multicolumn{1}{c}{…} is the usual way to center one header cell above a column that is otherwise left- or right-aligned.
\multicolumn{number of columns}{alignment}{text}\multirow: merging down rows
Write it in the top row of the group; in the rows below, leave that column empty (keep the &). The width is * for the text's natural width, or a length (such as 3cm) to let a long text wrap over several lines. The text is centered vertically across the rows.
A \multirow with a negative number of rows (\multirow{-2}{*}{…}) is written in the bottom row of the group and reaches upward; it is used when rows are colored, because the color of later rows would otherwise paint over the text.
\multirow{number of rows}{width}{text}Both at once: a block of merged cells
Wrap a \multirow in a \multicolumn to merge a block both ways; in the rows below, write an empty \multicolumn of the same width so the column count stays right.
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|}
\hline
\multicolumn{2}{|c|}{\multirow{2}{*}{Merged block}} & C \\
\multicolumn{2}{|c|}{} & D \\
\hline
E & F & G \\
\hline
\end{tabular}
\end{document}Rules under part of a row
A full \hline (or \midrule) would cut through a \multirow. Use a partial rule instead: \cline{2-4} in a plain table, or \cmidrule(lr){2-4} with booktabs, where (lr) trims the rule ends slightly so adjacent partial rules do not touch.
Common mistakes
- Counting cells wrong: a
\multicolumn{2}counts as two cells, so the row needs one&fewer. Too many&give "Extra alignment tab has been changed to \cr". - Forgetting the empty cells under a
\multirow: the following rows shift one column to the left. - Using
\multirowwithout loading themultirowpackage: "Undefined control sequence". - A long
\multirowtext with width*makes the column too wide; give a width so it wraps, or ap{…}column. - Multirow text is not moved when the rows below it are taller than one line; nudge it with the optional
\multirow{2}{*}[2pt]{…}argument.
Errors you may meet
- Extra alignment tab has been changed to \cr.A table or matrix row has more & cells than the column specification allows. Add a column to {…}, remove an &, or add a missing \\.
- Misplaced alignment tab character &.An & appeared outside a table or align environment. In text write \&; in a bibliography fix the & in the .bib entry; in maths use align.
- Undefined control sequence.LaTeX met a command it does not know: usually a typo, or a command whose package is not loaded. The log's l. line ends with the culprit.
In Manulyx
Insert → Table puts a table environment in place, and pasting a range copied from a spreadsheet (two or more rows and columns) goes in as a LaTeX table, with booktabs rules when the paper loads booktabs and \hline otherwise; a notice offers the plain text back.
Write in Manulyx for free · Journal templates
Related
- Professional-looking tables in LaTeX with booktabsMake LaTeX tables look typeset: booktabs rules (\toprule, \midrule, \bottomrule, \cmidrule), no vertical lines, and aligned numbers.
- Make a LaTeX table fit the page widthFit a too-wide LaTeX table: tabularx X columns, p{} columns that wrap, a smaller font, less column spacing, and why \resizebox is a last resort.
- Table notes and footnotes in a LaTeX table (threeparttable)Add notes under a LaTeX table with markers in the cells, using threeparttable's \tnote and tablenotes, and why \footnote inside a table goes missing.
- A table across several pages in LaTeX (longtable)Break a long LaTeX table across pages with longtable, repeating the header on each page and adding a "continued" note, with booktabs rules.
- A landscape table or page in LaTeXTurn a wide table sideways: pdflscape's landscape environment rotates whole pages (upright on screen); rotating's sidewaystable rotates a float.
The examples on this page were compiled with pdfLaTeX to check that they work as shown. LaTeX quick reference · LaTeX errors explained