1

I am trying to span a row over two rows in a tabularx table with the following:

\begin{table}[h]
    \caption{Table caption}
    \hfill
    \begin{tabularx}{\linewidth}{|X|X|X|X|}
        \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 \\ \hline
    Row2 & Text2 & & 500\\ \hline 
    Row3 & Text3 & SomeOtherText & MoreText \\ \hline
    \end{tabularx}
\end{table}

However currently getting: enter image description here

  • The spanning text is overflowing
  • A horizontal line is being drawn between the spanning cells
  • Something strange with the lines around "Column2", "Column3", "Column4"

(compiling with pdflatex)

What is the simplest way to fix this...?

6

The following should get you started:

enter image description here

\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 the surrounding normal rows. In that case the multirow text will stick out of its block. We must now enlarge the other rows, and that is something \multirow cannot do." (section "3.8 Dealing with tall entries", currently on page 13-15 of the multirow 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 large height, therefore it always avoids vertical overflow." (from section "1.4 Multirow Cells", currently on page 4-5 of the tabularray manual)

enter image description here

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

enter image description here

\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}
3
  • Thanks, I think I understand! Little lost at This is due to the multirow text occupying 4 linew. Is there a more automatic method than adding empty rows ? – kaleidawave May 18 at 15:10
  • 1
    As far as I know, unfortunately not, at least not with multirow. The multirow documentation als explicitly states "The second case is when the \multirow entry is taller than the surrounding normal rows. In that case the multirow text will stick out of its block. We must now enlarge the other rows, and that is something \multirow cannot do." (page 13-15) For this particular table, I would recommend using \begin{tabularx}{\linewidth}{|l|l|X|l|}. With this alternative you should be able to get away with the \multirow occupying just 2 rows, hence no need for added, empty rows. – leandriis May 18 at 16:04
  • @kaleidawave: I have just updated my answer to also include an apporach based on the new tabularray package which attempts to avoid overflowing \multirow contents. Probably this approach is also interesting for you. – leandriis May 19 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.