如何使 LaTex 表格垂直行标题适合?

问题描述 投票:0回答:1

作为参考,这就是我想要实现的目标:

Desired outcome

这是我现在的桌子的样子:

Current table

这就是我要解决的问题(让类名变长打破了表格):

Cursed table

这里是复制表格所需的包:

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

这是我们用我的管家 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*}

我尝试在垂直行标题中添加“\”,但它没有用(仍然是一长行)

有没有办法确保表格动态调整大小?将一些测试较少的类水平放置以节省空间如何?

latex tabular overleaf
1个回答
1
投票

关于旋转的内容,您只是没有为较长的文本确保足够的空间,导致一旦超过可用的垂直空间就会破坏所有内容。最重要的是,

\rotatebox
创建了一个没有换行符的简单单行框。解决您的问题的方法是 (1) 段落框 s.a.
\parbox
自动添加换行符和 (2) 任何引入额外空格的内容。

对于后一种情况(2),可以在一组前后添加额外的空行,并在

\multirow
中增加合并行的数量。另一种方法是在组的第一行和最后一行添加 struts。但是,长文本仍然会导致问题。这可以通过使用零长度框的技巧来缓解(例如
\makebox[0pt]...
)——见代码。至于支柱,我发现
\xmathstrut
mathtools
是在表格中创建额外空间的一种非常方便的方法。你可以使用
multirow
的 bigstruts 或玩
makecell
的间隙或使用隐形规则等

根据旋转文本的长度,可以考虑通过增加

arraystretch
的系数来拉伸整个表格,例如

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

为了平衡其他行的间距。

我建议定义自定义颜色而不是使用绝对值,以避免每次要突出显示某些内容时重复。我还发现

\colorbox
稍微增加了标题的高度,这可以被
\smash{}
取消。如果您发现文本太紧,请将其删除。

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

© www.soinside.com 2019 - 2024. All rights reserved.