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

Errors you may meet

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

The examples on this page were compiled with pdfLaTeX to check that they work as shown. LaTeX quick reference · LaTeX errors explained