Tx · LaTeX & TikZ

How to Prevent Text Overflow in a LaTeX `\multirow` Cell

Published By the 9to5Science editorial team

At a glance

  1. 1. Set a text width

    The asterisk width makes the text natural-width; use a fixed-width box to let long text wrap.

  2. 2. Format the paragraph

    Put the content in a parbox or give multirow an explicit width, and use ragged-right alignment if appropriate.

  3. 3. Check the row height

    Wrapping makes the cell taller; ensure the rows spanned by multirow have enough vertical space.

  4. 4. Compile and adjust

    Check the result around the horizontal rules and neighboring cells, then tune the width or row spacing.

A long label in a \multirow cell can run into the next column because LaTeX does not automatically choose a paragraph width for it. The usual fix is to put the text in a box with a specified width, so it can wrap. If the wrapped text is also too tall for the rows it spans, adjust the table’s vertical spacing separately.

Why text overflows

The \multirow command comes from the multirow package. Its basic syntax is:

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

The second argument controls the width of the text box. If you write * for this argument, the text is set at its natural width. That is convenient for short labels, but a long phrase may extend past the cell and overlap adjacent columns.

To make a paragraph wrap, give it a fixed width. You can either pass a width to \multirow or put the text in a \parbox of the desired width. A \parbox is especially useful when you want the multirow cell’s box width to be independent of its natural text width.

A minimal working example

This example wraps the multirow text to a width of 4 cm, gives the first column room for that box, and makes the rows tall enough for the paragraph. It uses the multirow package; \parbox is provided by LaTeX.

\documentclass{article}
\usepackage{multirow}

\begin{document}

\begin{tabular}{|p{5cm}|c|}
\hline
\multirow{2}{*}{\parbox[t]{4cm}{\raggedright
A long label that wraps onto a second line instead of overlapping the next column.}}
  & First row \rule{0pt}{2.5em} \\
  & Second row \rule{0pt}{2.5em} \\
\hline
\end{tabular}

\end{document}

Here, \parbox[t]{4cm}{...} creates a paragraph box that is 4 cm wide, so its contents wrap. The first column is wide enough to accommodate that box. The * in \multirow makes the multirow box take the natural width of that parbox—which is 4 cm. \raggedright avoids stretching spaces to justify the short lines. Remove it if you prefer justified text. The invisible rules increase the height of the rows so the wrapped paragraph has room.

You can also give \multirow the width directly:

\multirow{2}{4cm}{\raggedright A long label that needs to wrap.}

Use this form when it behaves as you want in your document. The explicit \parbox form makes the paragraph width particularly clear and is easy to adapt when the text needs other paragraph formatting.

Match the width to the table

Choose a width that fits the column and leaves enough room for the neighboring columns. A fixed width in a regular tabular is often simplest. For a table whose column width is already set with p{...}, keep the multirow content within that column’s intended width. If you use tabularx, the X column width is calculated from the table’s total width; a multirow paragraph may need its own box set to the calculated or otherwise appropriate width.

\multirow does not merge the widths of several columns. It places one text box across a specified number of rows in the column where the command appears. If the label should occupy multiple columns as well as multiple rows, that is a different layout problem and may require \multicolumn or a redesigned table.

Check vertical space too

Wrapping solves horizontal overflow, but it increases the text’s height. A multirow cell spanning two ordinary rows may not have enough vertical space for a paragraph that wraps to many lines. In that case, the text can collide with a horizontal rule or content in other rows.

Make sure the rows being spanned are tall enough. You can shorten the text, give it more width so it wraps onto fewer lines, or increase row height—for example, by adding a suitable invisible strut such as \rule{0pt}{2.5em} to cells in the affected rows. Choose the height based on the actual font and content, and inspect the compiled output.

Common mistakes

  • Keeping * and expecting wrapping: * asks for the natural text width. Put the text in a fixed-width paragraph box or use an explicit width.
  • Using the width of the whole table: The multirow text box should fit its own column, not the entire table width.
  • Fixing only the horizontal problem: Once text wraps, check that the rows have enough vertical space.
  • Counting the header row as part of the span: The first \multirow argument is the number of table rows the cell covers. Make sure the command starts in the first row of that span and that the count matches the layout.
  • Expecting \multirow to adjust every row automatically: It positions content across rows, but it does not always produce the row heights you intended. Use explicit row-height adjustments when needed.

If the content is short and must break at a particular point, a manual line break or a \shortstack can be simpler. For ordinary paragraph text, a fixed-width box is usually the more flexible solution.

References

  1. The multirow package documentation

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