Tx · LaTeX & TikZ

How to Make a LaTeX Table Cell Span a Variable Number of Rows

Published By the 9to5Science editorial team

At a glance

  1. 1. Count the rows

    Determine how many table rows belong to each group; LaTeX does not infer this from the following rows.

  2. 2. Set the span

    Pass that count as the first argument to \multirow, and use * for the natural cell width.

  3. 3. Add remaining rows

    Put the cell content in the first row of the group, then leave its column empty in the rows beneath.

  4. 4. Adjust the rules

    Use partial horizontal rules to separate groups without drawing through a cell that spans multiple rows.

A cell that labels a group of table entries can span a different number of rows in each group. In LaTeX, the usual solution is the multirow package. You provide the span explicitly; the package does not count later rows or infer the group size from the table contents.

The basic syntax

Load multirow in the preamble, then write:

\multirow{number of rows}{width}{cell text}

The first argument is the number of rows to span. The second sets the cell width: * uses the natural width of the content. Put the command in the first row of the group. In that column, leave the corresponding cells in the following rows empty.

For example, \multirow{3}{*}{Group A} makes a cell spanning three table rows. Each group can use a different count.

Worked example with different row counts

This table has a group of three entries and a group of two. The partial rules separate entries within each group, while the full rule separates the groups.

\documentclass{article}
\usepackage{multirow}

\begin{document}

\begin{tabular}{llr}
\hline
Group & Item & Value \\
\hline
\multirow{3}{*}{Group A} & A1 & 10 \\
                        & A2 & 12 \\
\cline{2-3}
                        & A3 & 15 \\
\hline
\multirow{2}{*}{Group B} & B1 & 8 \\
\cline{2-3}
                        & B2 & 11 \\
\hline
\end{tabular}

\end{document}

The first \multirow spans the three rows containing A1, A2, and A3. The second spans the two rows containing B1 and B2. The blank first-column entries are intentional: the spanning cell occupies those positions.

Supplying a variable count

If the row count changes between tables, put the count in a macro and use that macro as the first argument. For example:

\newcommand{\GroupARows}{3}
\newcommand{\GroupBRows}{2}

Then write \multirow{\GroupARows}{*}{Group A} and \multirow{\GroupBRows}{*}{Group B} in the table. Change the macro definitions when the group sizes change. The macro should expand to a valid integer.

This is useful when table code is generated from data or when you want the row counts defined in one place. It does not, by itself, calculate the number of data rows. Your document or generating script still needs to provide the correct count for each group.

If you calculate counts with a TeX count register, you can pass its printed value. For example, after \newcount\GroupRows and \GroupRows=3, use \multirow{\the\GroupRows}{*}{Group A}. Make sure the count is set before the table uses it.

Horizontal rules and alignment

A normal \hline crosses every column, including the column occupied by a multirow cell. When you want a rule between entries inside a group, use \cline{2-3} to draw it only across columns two and three. This avoids drawing a line through the spanning cell in column one. Use \cline ranges that match the columns you intend to rule.

By default, \multirow centers the cell contents vertically across the span. Its width argument can be * for natural width or a fixed dimension, such as 2.5cm, if you need a controlled column-like text width. Long content may need a fixed width to wrap; see the package documentation for the optional vertical-position and adjustment arguments.

Common mistakes

  • Using the number of groups instead of the number of rows. The count is the number of table rows occupied by the cell, including the row where the \multirow command appears.
  • Leaving out the empty cells below. The spanning cell covers those positions. Do not put another cell entry in that column for the covered rows.
  • Using a wrong count. If a group has three data rows but the span is two, the label will not cover the group; if the span is too large, it can overlap the next group. Keep the count and the actual rows in sync.
  • Expecting automatic counting. \multirow receives a span; it does not inspect the rows that follow. For tables built from changing data, calculate or supply the span in the code that generates the table.
  • Drawing rules through the label. Replace a full-width \hline within a group with a partial \cline over the other columns when that is the intended design.

For very large or automatically generated tables, generating both the rows and their span counts from the same data source is the safest approach. That prevents the displayed table structure from drifting out of sync with the \multirow arguments.

References

  1. The multirow package documentation

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.