TransWikia.com

Automatically adjusting multirow height in tabular

TeX - LaTeX Asked by orestisf on September 25, 2020

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}

noindentbegin{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}

noindentbegin{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.

2 Answers

enter image description here

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

begin{document}

With multirow and a manually addd empty line: 


noindentbegin{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:

noindentbegin{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.5baselineskip} setlength{cellspacebottomlimit}{cellspacetoplimit} begin{document}

noindentbegin{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}

Correct answer by leandriis on September 25, 2020

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

Answered by Bernard on September 25, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP