2

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:

MWE output

I will accept answers using other environments/packages.

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

3 Answers 3

3

enter image description here

\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.

enter image description here

\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}
5
  • 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.
    – orestisf
    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?
    – leandriis
    Jul 23, 2020 at 12:39
  • Yes, ideally 1 & 2 should be aligned in their cells.
    – orestisf
    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.
    – leandriis
    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.
    – orestisf
    Jul 23, 2020 at 18:09
0

How about this (not really automatic) hack?

\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} 

enter image description here

1
  • 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.
    – orestisf
    Jul 23, 2020 at 14:30
0

Using tabularray package:

\documentclass{article}
\usepackage{tabularray}
\begin{document}


\SetTblrInner{rowsep=0pt}
\noindent\begin{tblr}{p{4cm}|c}
     A & B  \\\hline
     \SetCell[r=2]{} My super long text here will spill over to the next line. How do I avoid this? & 1 \\\cline{2-2}
      & 2 \\\hline
     \SetCell[r=2]{} Other text here & 3 \\\cline{2-2}
      & 4 \\\hline
\end{tblr}

\end{document}

output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .