0

I need to create a table in LaTeX that looks exactly like its counterpart in a Word document.

For this, I need a table that can span multiple pages with repeating headers (longtable, etc.) and contains merged cells with horizontally and vertically centered content (multirow, makecell, etc.). Some of the content needs to be rotated/vertical.

Currently, I'm struggling with overflowing content in a multirow environment. I have tried to find a solution for over a day now:

Demonstration of problem

\documentclass{article}

\usepackage{xltabular}
\usepackage{array}
\usepackage{makecell}
\usepackage{multirow}
\usepackage{graphicx}

\begin{document}
  \begin{xltabular}{\textwidth}{|X|X|}  
    \hline
    \thead{Two \\ lines} & \thead{One vertically centered line} \\
    \hline
    \multirowcell{2}{\rotatebox{90}{LONG CENTERED STRING }} & \makecell{Something} \\
    \cline{2-2}
    & \makecell{Something} \\            
    \hline
  \end{xltabular}
\end{document}

How can I make the table adjust all the row heights based on the highest row? That is, how can I make the table adjust the height of the two "Something" rows so that they're equal in height to the "LONG CENTERED STRING" multirow cell?

(I'd prefer a non-cheating, dynamic solution as the content of the table is also somewhat dynamically generated. Hence, adjusting row heights manually or inserting fake rows is not ideal.)

4
  • Combining \multirow with longtable/xltabular, especially when it comes to your long and rotated texts, is somewhat risky as a page break might occur at a subpar place in your table resulting in the contents of the \multirow protruding into the footer of the page.
    – leandriis
    Commented Jan 31, 2021 at 18:32
  • Also, please be aware, that enclosing a breakable xltabular in a non-breakable table environment does not really make sense. Remove the table environment.
    – leandriis
    Commented Jan 31, 2021 at 18:34
  • Thank you for highlighting this potential problem and for your suggestions. I've updated the MWE accordingly and removed the table environment. Regarding your concern: Yes, in some cases having a table that spans multiple pages will result in weird effects with such a table layout. However, I'm willing to accept this as the document will be reviewed by a human who can then tweak the document manually before sending it out to a customer.
    – ctitze
    Commented Feb 1, 2021 at 9:37
  • Your problem hasn't solution as you imagine. \multirow cells can not be higher than height of spanned adjacent cells (actually lines). If it is declared such, it contents spill out of bottom of cells. I see two possible solutions: (i) increase heights of adjacent cells, or (ii) break text in multirow cells in multiline text.
    – Zarko
    Commented Feb 1, 2021 at 10:24

2 Answers 2

1

Here is a fully automatic version. No guessing about the number of rows. It measures the length of the vertical text and uses that to put struts in the second column cells. I have no idea if this can be generalised to your use case, however.

\documentclass{article}
\usepackage{rotating}
\usepackage{xltabular}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{multirow}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}

\newcommand{\verticaltext}{ LONG CENTERED STRING }
\newlength{\verticalsize}
\settowidth{\verticalsize}{\verticaltext}
\newcommand{\halfstrut}{\rule[-0.25\verticalsize]{0pt}{0.5\verticalsize}}

\begin{xltabular}{.7\textwidth}{CC}\toprule
\thead{Two \\ lines} & \thead{One vertically centered line} \\\midrule
\multirow[c]{1}*[20pt]{\begin{turn}{90} \verticaltext \end{turn}}
                     & \halfstrut a \\ \cmidrule{2-2}
                     & \halfstrut b \\ \midrule
whatever \\\bottomrule
\end{xltabular}

\end{document}

enter image description here

1
  • Thank you, I think this will do the job for now! :-)
    – ctitze
    Commented Feb 16, 2021 at 16:20
0

My approach, already suggested by Zarko, would be take care of the number of rows used by the rotated cell and the cells at the right:

mwe

\documentclass{article}
\usepackage{rotating}
\usepackage{xltabular}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{multirow}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{xltabular}{.7\textwidth}{CC}\toprule
\thead{Two \\ lines} & \thead{One vertically centered line} \\\midrule
\multirow{12.5}{*}{\begin{turn}{90} LONG CENTERED STRING \end{turn}} 
& \multirow{7}{*}{a}\\\\\\\\\\\\\cmidrule{2-2}
& \multirow{7}{*}{b}\\\\\\\\\\\\\midrule
whatever
\\\bottomrule
\end{xltabular}
3
  • I wonder why OP like to have so awful looking table ... Well it might be that text in the cells adjacent to multirow has many lines.. Who knows. BTW, you abuse syntax for writing of tables. Also empty rows should have all ampersands needed delimiting of columns.
    – Zarko
    Commented Feb 2, 2021 at 7:55
  • @ Zarko, Yeah, but I'm too lazy to do it right when you cannot notice that in the PDF. :)
    – Fran
    Commented Feb 2, 2021 at 8:41
  • Thank you for your suggestions, Fran and Zarko. Do you think it would be possible to somehow calculate the height of the rotated string object and add it to the columns on the right? In my example it would add one fourth of the height as whitespace above "Something" and one fourth below. The same goes for the row below.
    – ctitze
    Commented Feb 3, 2021 at 12:43

You must log in to answer this question.

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