# Prevent multirow cell overflow with dynamic cell content

I have the following table layout:

The table consists of 3 columns - one column which is 25% wide, and two columns that take up equal amounts of the space left (thus 37.5% each).

The content for these columns is obtained from programmatic variables, which can vary in length quite a bit. Thus, each table cell should be able to process manual line breaks in the variable strings that get passed in, as well as automatic line breaks based on content length.

I've tried a couple different approaches, and the closest thing I've managed to a working example is included below, using multirow and nested tabular environments inside a tabularx for the width: MWE:

\documentclass[11pt,a4paper]{report}
\usepackage[margin=0.5cm]{geometry}
\usepackage{array,multirow,tabularx,lipsum}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|p{4cm}|X|X|}
\hline
\multicolumn{1}{|l|}{
\begin{tabular}[l]{@{}l@{}}
One Line\\
Two Lines
\end{tabular}
} &
\multirow{3}{*}{
\begin{tabular}[l]{@{}l@{}}
Lots of text goes here\\
Lots of text here indeed lel lel lol aaa ss bzxbg
\end{tabular}
} &
\multirow{3}{*}{
\begin{tabular}[l]{@{}l@{}}
Lots of text go here\\
In fact, tons of text go here\\
So much text, in fact, that you'd have to do weird cell padding things to fix it\\
AAAA\\
BBBB\\
CCCC\\
DDDD\\
EEEE
\end{tabular}
} \\
\cline{1-1}
\begin{tabular}[l]{@{}c@{}}
This line here will overlap \\
AA
\end{tabular} & & \\
\cline{1-1}
\begin{tabular}[l]{@{}c@{}}
One Line\\
Two Lines\\
Three Lines
\end{tabular} & &
\\ \hline
\end{tabularx}
\end{document}


This layout has some issues, as can be seen below:

While the manual line breaks work, longer lines don't automatically break once they fill out the cells, instead overflowing on the side.

The two cells on the right also don't cause the multirow cells to resize, instead overflowing at the bottom. (the cells inside the multirow do cause a resize - if they are longer, the rightmost two cells do correctly resize downward)

The alignment + padding isn't 100% either.

Are there any simple mistakes one can easily fix in my MWE, or different approaches to multirow content that can dynamically resize? The usual answer is to match your multirow content to the number of lines in the largest cell. I can't do that here, as I do not know in advance which cell that will end up being, since the fields contain dynamic content.

• Try to replace tabulars with tabularx environments so that you have nested tabularx's. Other than the l,r,c columns, the X column supports automatic linebreaking. – user1146332 Apr 15 at 22:48

Is this is what you like to have?

Your problem is nested tables and multirows in tabularx columns, which override defined columns types. Removing nested tabulars and use new syntax for multirow (see code below) MWE with your table is:

\documentclass[11pt,a4paper]{report}
\usepackage[margin=0.5cm]{geometry}
\usepackage{array,multirow,tabularx,lipsum}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|p{4cm}|X|X|}
\hline
One Line

Two Lines
&   \multirow[t]{8}{=}{Lots of text goes here
Lots of text here indeed lel lel lol aaa ss bzxbg
}
&   \multirow[t]{8}{=}{Lots of text go here In fact, tons of text go here
So much text, in fact, that you'd have to do weird cell padding things to fix it\newline
AAAA\newline
BBBB\newline
CCCC\newline
DDDD\newline
EEEE\newline
}   \\
\cline{1-1}
This line here will overlap

AA
&   &               \\
\cline{1-1}
One Line

Two Lines

Three Lines
&   &               \\ \hline
\end{tabularx}
\end{document}


Addendum: Another possibility is not to use \multirow cells and instead it nest tabular table in the first column:

\documentclass[11pt,a4paper]{report}
\usepackage[margin=0.5cm]{geometry}
\usepackage{array,multirow,tabularx,lipsum}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|@{} p{4cm}  |X|X|}
\hline
\begin{tabular}[t]{ >{\raggedright\arraybackslash}p{\dimexpr\hsize-\tabcolsep} }
One Line    \\
Two Lines   \\  \hline
This line here will overlap (no longer) \\
AA          \\ \hline
One Line    \\
Two Lines   \\
Three Lines
\end{tabular}
&   Lots of text goes here
Lots of text here indeed lel lel lol aaa ss bzxbg
&   Lots of text go here In fact, tons of text go here So much text, in fact, that you'd have to
do weird cell padding things to fix it

AAAA

BBBB

CCCC

DDDD

EEEE    \\ \hline
\end{tabularx}
\end{document}


• That's close, but there are 2 issues - The two columns on the right should be top-left aligned, and they're instead center-left aligned - The content is dynamic, it can't be assumed that the multirow will be 8 lines long – Wynand Marais Apr 15 at 22:56
• @WynandMarais, corrected. – Zarko Apr 15 at 22:57
• I accidentally hit enter instead of shift-enter, my apologies - the comment has been edited. For posterity, it is copy-pasted below: ----- That's close, but there are 2 issues - The two columns on the right should be top-left aligned, and they're instead center-left aligned - The content is dynamic, it can't be assumed that the multirow will be 8 lines long – Wynand Marais Apr 15 at 23:00
• If text will be longer than 8 lines, than will spill out of table. The cures are (i) insert fake rows, which span multirow cells or make rows in the first column higher, or (ii) not use multirow cells and in the first column nest table with correct defined column. – Zarko Apr 15 at 23:05
• @WynandMarais, see addendum to answer. – Zarko Apr 15 at 23:24