1

For reference, this is what I am trying to achieve:

Desired outcome

This is what my current table looks like:

Current table

This is the problem I am trying to solve (making the class names longer breaks the table):

Cursed table

Here are the the required packages to reproduce the table:

\usepackage{rotating}
\usepackage{multirow}
\usepackage{booktabs}

And here is the code for the table that we generated with my butler ChatGPT:

\begin{table*}[ht]
  \centering
  \footnotesize
  \begin{tabular}{lp{4cm}ccp{5cm}}
    \toprule
    \multirow{2}{*}{\rotatebox[origin=c]{90}{}} & \multicolumn{1}{c}{\textbf{Test TYPE and description}} & \multicolumn{2}{c}{\textbf{Failure Rate (\%)}} & \multicolumn{1}{c}{\textbf{Example test cases \& \colorbox{gray}{expected behavior}}} \\
    & & \textbf{BERT} & \textbf{BiLSTM} & \\
    \midrule
    \multirow{3}{*}{\rotatebox[origin=c]{90}{Predicate Identification}} & Test 1 description & 10 & 30 & Expected behavior for Test 1 \\
    & Test 2 description & 15 & 35 & Expected behavior for Test 2 \\
    & Test 3 description & 20 & 40 & Expected behavior for Test 3 \\
    \midrule
    \multirow{3}{*}{\rotatebox[origin=c]{90}{Class B}} & Test 4 description & 5 & 15 & Expected behavior for Test 4 \\
    & Test 5 description & 10 & 20 & Expected behavior for Test 5 \\
    & Test 6 description & 15 & 25 & Expected behavior for Test 6 \\
    \bottomrule
  \end{tabular}
  \caption{Example of a table with rotated row headings, separated rows, one value per group, and a description of the test for each row.}
  \label{tab:example}
\end{table*}

I tried adding '\\' to the vertical row headers, but it did not work (still stayed as one long line)

Is there a way to ensure that the table resizes dynamically? And how about keeping some of the Classes with fewer tests horizontal to save space?

1 Answers1

2

Regarding rotated content, there simply hasn't been allocated enough space for the longer text, which causing everything to break. On top of that, \rotatebox creates a simple one-line box without line breaks. A remedy to your issues is (1) a paragraph box s.a. \parbox which automatically add linebreaks and (2) anything that intriduce extra spaces.

For the latter case (2), one can add extra empty rows before and after a group and increase a number of merged rows in \multirow. Another way is to add struts in the first and in the last row of the group. However, long texts would still cause a problem. This can be mitigated by using a trick with a zero-length box (e.g. \makebox[0pt]...)--see the code. As to the struts, I find \xmathstrut of mathtools a very convenient way to create extra spaces in tables. You could use bigstruts from multirow or play with gapes from makecell or use invisible rules etc.

Depending on how long is the rotated text, you might consider stretching the whole table by increasing a factor of arraystretch, such as

\renewcommand\arraytretch{1.5}   % X = 1.0 is the default value

in order to balance spacing in other rows, as well.

I'd suggest to define a custom colour instead of using absolute values to avoid repetition every time you want to highlight something. I also found that \colorbox slightly increases height in a header, which can be cancelled by \smash{}. Remove it if you find text too tight.

\documentclass[margin=2.5mm]{standalone}
\usepackage{mathtools}   % for \xmathstrut
\usepackage{rotating}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{makecell}    % for \thead

\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{\Gape[0pt][0pt]}
\colorlet{highlight}{gray!30}

\NewCommandCopy\oldxmathstrut\xmathstrut
\RenewDocumentCommand\xmathstrut{O{#2}m}{%
    \ensuremath{\oldxmathstrut[#1]{#2}}}


\begin{document}
  \footnotesize
  \renewcommand\arraystretch{1.15}
  \begin{tabular}{lp{4.5cm}ccp{4cm}}
    \toprule
    & \thead{Test TYPE and description}
    & \multicolumn{2}{c}{\thead{Failure Rate (\%)}}
    & \thead{Example test cases \&} \\
    & & \thead{BERT} & \thead{BiLSTM} & \thead{\smash{\colorbox{highlight}{expected behavior}}}%
    \xmathstrut[0.5]{0} \\
    \midrule
    \multirow[c]{3}{*}{\rotatebox[origin=c]{90}{\clap{%
        \parbox{1.5cm}{\centering Predicate\\Identification}}}}%
    \xmathstrut[0]{0.5}
    & Test 1 description & 10 & 30 & Expected behavior for Test 1 \\
    & Test 2 description & 15 & 35 & Expected behavior for Test 2 \\
    & Test 3 description & 20 & 40 & Expected behavior for Test 3 \xmathstrut[0.5]{0 }\\
    \midrule
    \multirow{3}{*}{%
        \rotatebox[origin=c]{90}{Class B}} & Test 4 description & 5 & 15 & Expected behavior for Test 4 \\
    & Test 5 description & 10 & 20 & Expected behavior for Test 5 \\
    & Test 6 description & 15 & 25 & Expected behavior for Test 6 \\
    \bottomrule
  \end{tabular}
  % \caption{Example of a table with rotated row headings, separated rows, one value per group, and a description of the test for each row.}
\end{document}

enter image description here

Celdor
  • 2,437
  • 2
  • 23
  • 44