# Automatically adjusting multirow height in tabular

I am trying to create a table which contains (multiple) multirow cells with a certain width, automatically wrapping the text. However, when the text is too long, it spills over to the next cell.

I know I can use the \\[1cm] to manually solve this problem (also see Auto resize tabular row height) but I want to do this automatically. Is this possible?

Here is a MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}

\begin{document}

\noindent\begin{tabular}{p{4cm}|c}
A & B  \\\hline
\multirow{2}{4cm}{My long text here is ok, not long enough to spill} & 1 \\\cline{2-2}
& 2 \\\hline
\multirow{2}{4cm}{Other text here} & 3 \\\cline{2-2}
& 4 \\\hline
\end{tabular}

\vspace{3cm}

\noindent\begin{tabular}{p{4cm}|c}
A & B  \\\hline
\multirow{2}{4cm}{My super long text here will spill over to the next line. How do I avoid this?} & 1 \\\cline{2-2}
& 2 \\\hline
\multirow{2}{4cm}{Other text here} & 3 \\\cline{2-2}
& 4 \\\hline
\end{tabular}

\end{document}


Output:

I will accept answers using other environments/packages.

• You could use the nested tabular approach instead of \multirow. Jul 23, 2020 at 12:22
• @leandriis Thanks. Does this mean that the text will not be automatically wrapped into newlines? Jul 23, 2020 at 12:26
• The text in the left column will keep its automatic line breaks. See the answer I just added. Jul 23, 2020 at 12:28

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}

\begin{document}

With multirow and a manually addd empty line:

\noindent\begin{tabular}{p{4cm}|c}
A & B  \\\hline
\multirow{2}{=}{My super long text here will spill over to the next line. How do I avoid this?} & 1 \\\cline{2-2}
& 2 \\
\\ \hline
\multirow{2}{=}{Other text here} & 3 \\\cline{2-2}
& 4 \\\hline
\end{tabular}

\vspace{1cm}

With nested tabulars:

\noindent\begin{tabular}{p{4cm}|@{}c@{}}
A & B  \\
\hline
My super long text here will spill over to the next line. How do I avoid this?
& \begin{tabular}[t]{c} 1 \\ \hline 2 \end{tabular} \\
\hline
Other text here & \begin{tabular}[t]{c} 3 \\ \hline 4 \end{tabular} \\
\hline
\end{tabular}

\end{document}


For vertically centerd cells you could use the cellspace package. IIf the lenght of the text in the first column or the number of row in the second column changes, the value of \cellspacetoplimit has to be adjusted accordingly.

\documentclass{article} \usepackage[utf8]{inputenc} \usepackage{array}

\usepackage[column=0]{cellspace} \setlength{\cellspacetoplimit}{0.5\baselineskip} \setlength{\cellspacebottomlimit}{\cellspacetoplimit} \begin{document}

\noindent\begin{tabular}{m{4cm}|@{}c@{}}
A & B  \\
\hline
My super long text here will spill over to the next line. How do I avoid this?
& \begin{tabular}{0c} 1 \\ \hline 2 \end{tabular} \\
\hline
Other text here & \begin{tabular}{c} 3 \\ \hline 4 \end{tabular} \\
\hline \end{tabular}

\end{document}

• This is good but creates a small issue. Is it possible to vertically center the first table like the second one? I.e. the divider between 1 and 2 should be in the middle of the cell. Jul 23, 2020 at 12:37
• Like so? i.stack.imgur.com/CvmNb.png Or do you alsso want to vertically center "1" and "2" in their respective cells? Jul 23, 2020 at 12:39
• Yes, ideally 1 & 2 should be aligned in their cells. Jul 23, 2020 at 12:41
• @orestisf: I am unfortunately not sure if this is achievable in an automatic way. If have however edited my anser to include a somewhat semi-manual way. Jul 23, 2020 at 17:41
• Thanks. I wrote my table using your original answer and I am pretty happy with it, so I will accept your answer. Thank you. Jul 23, 2020 at 18:09

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow, makecell, booktabs}

\begin{document}

\noindent
\begin{tabular}{m{4cm}|c}
A & B \\\hline
My super long text here will spill over to the next line. How do I avoid this? & \makecell{1 \\[0.5ex] \cmidrule(l{-7pt}r{-7pt}){1-1} 2}\\\hline
\multirow{2}{=}{Other text here} & 3 \\\cline{2-2}
& 4 \\\hline
\end{tabular}

\end{document}


• Thank you for your answer but I really need it to be automatic. The final table is big and is going to be generated by a script. If I could somehow infer & incorporate these numbers into my script (instead of manually adjusting them each time), this could work for me. Jul 23, 2020 at 14:30