The following should get you started:
\multirow{*}
results in a cell that is as wide as its contents, hence the overflow. To get a multirow cell that is as wide as the surrounding column, use \multirow{=}
instead.
To get rid of the horizontal line, use \cline{1-2} \cline{4-4}
instead of the \hline
.
To fix the column headers, remove the \\
after the first \hline
command.
Once these changes are applied, you will notice the multirow text overflows into the third table row. This is due to the multirow text occupying 4 linew, while you only allowed it to span 2 table rows. To overcome this, I added &&&\\
to add two empty table rows.
"The second case is when the
\multirow
entry is taller than thesurrounding normal rows. In that case the multirow text will stick outof its block. We must now enlarge the other rows, and that issomething\multirow
cannot do." (section "3.8 Dealing with tallentries", currently on page 13-15 of themultirow
manual)
\documentclass{article}\usepackage{tabularx}\usepackage{multirow}\begin{document}\begin{table}[h] \caption{Table caption} \begin{tabularx}{\linewidth}{|X|X|X|X|} \hline \textbf{Column1} & \textbf{Column2} & \textbf{Column3} & \textbf{Column4} \\ \hline Row1 & Text1 & \multirow{4}{=}{Spanning text that may need to be wrapped in the column} & 500 \\ &&&\\ \cline{1-2} \cline{4-4} Row2 & Text2 & & 500\\ &&&\\\hline Row3 & Text3 & SomeOtherText & MoreText \\ \hline \end{tabularx}\end{table}\end{document}
Since you asked for a more automated way of making sure a tall \multirow
text does not overflow, even if it taller than the rows it spans, here is an approach based on the quite new tabularray
package:
"Second, it will enlarge row heights if the multirow cells have largeheight, therefore it always avoids vertical overflow." (from section"1.4 Multirow Cells", currently on page 4-5 of the
tabularray
manual)
\documentclass{article}\usepackage{tabularray}\begin{document}\begin{table}[h] \caption{Table caption} \begin{tblr}{hlines,vlines, colspec={XXXX}} \textbf{Column1} & \textbf{Column2} & \textbf{Column3} & \textbf{Column4} \\ Row1 & Text1 & \multirow{2}{=}{Spanning text that may need to be wrapped in the column} & 500 \\ Row2 & Text2 & & 500\\ Row3 & Text3 & SomeOtherText & MoreText \\ \end{tblr}\end{table}\end{document}
Personally, I would work around this issue by adjustig the column widths. Since only one column contains longer texts, while all others contain just single words, I would change teh column specifiers of columns 1, 2 and 4 to l
instead of X
. This would result in the following MWE and output, in which you don't have to worry about overflowing \multirow
text and ymore, since it only occupies two rows:
\documentclass{article}\usepackage{tabularx}\usepackage{multirow}\usepackage{tabularray}\begin{document}\begin{table}[h] \caption{Table caption} \begin{tabularx}{\linewidth}{|l|l|X|l|} \hline \textbf{Column1} & \textbf{Column2} & \textbf{Column3} & \textbf{Column4} \\ \hline Row1 & Text1 & \multirow{2}{=}{Spanning text that may need to be wrapped in the column} & 500 \\ \cline{1-2} \cline{4-4} Row2 & Text2 & & 500\\ \hline Row3 & Text3 & SomeOtherText & MoreText \\ \hline \end{tabularx}\end{table}\begin{table}[h] \caption{Table caption} \begin{tblr}{hlines,vlines, colspec={llXl}} \textbf{Column1} & \textbf{Column2} & \textbf{Column3} & \textbf{Column4} \\ Row1 & Text1 & \multirow{2}{=}{Spanning text that may need to be wrapped in the column} & 500 \\ Row2 & Text2 & & 500\\ Row3 & Text3 & SomeOtherText & MoreText \\ \end{tblr}\end{table}\end{document}