![具有多行和旋转内容的单元格大小](https://linux22.com/image/406275/%E5%85%B7%E6%9C%89%E5%A4%9A%E8%A1%8C%E5%92%8C%E6%97%8B%E8%BD%AC%E5%86%85%E5%AE%B9%E7%9A%84%E5%8D%95%E5%85%83%E6%A0%BC%E5%A4%A7%E5%B0%8F.png)
我遇到的问题是,旋转的多行单元格上的单元格大小无法适应其大小,因此内容无法容纳。我尝试按照以下解决方案进行操作旋转多行中的长文本,但单元格高度未适配。这可以在图片中看到
以下是我的示例:
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{multirow, makecell}
\usepackage{rotating}
\begin{document}
\begin{table}
\newcommand{\multirot}[1]{\multirow{2}{*}[1.5ex]{\rotcell{\rlap{#1}}}}
\begin{center}
\caption{Minimal Example Table Problem}
\label{tab:inputVector}
\begin{tabular}{|l|l|l|l|l|l|l|l|l|}
\cline{4-9}
\multicolumn{3}{c|}{} & \multicolumn{6}{c|}{Option}\\
\cline{4-9}
\multicolumn{3}{c|}{} &
\multirot{Short Name} &
\multirot{Another Name} &
\multirot{A very Long Name} &
\multirot{Longer name} &
\multirot{Short Name} &
\multirot{Short Name} \\
\cline{1-3}
Method & Column A & Column B & & & & & &\\
\hline
Method A & AAA & BBB & & & & & &\\
\hline
Method B & AAA & BBB & & & & & &\\
\hline
\end{tabular}
\end{center}
\end{table}
\end{document}
答案1
使用,以下代码将完成该工作({NiceTabular}
无论nicematrix
单元格的内容是什么)。
\documentclass{article}
\usepackage{nicematrix,tikz,caption}
\begin{document}
\begin{table}
\centering
\caption{Minimal Example Table Problem}
\label{tab:inputVector}
\begin{NiceTabular}{lll|l|l|l|l|l|l|}[cell-space-top-limit=2pt]
\cline{4-9}
&&& \Block{1-6}{Option} \\
\cline{4-9}
Method & Column A & Column B
& \RowStyle{\rotate} Short Name
& Another Name
& A Very Long Name
& Longer name
& Short Name
& Short Name \\
\hline
Method A & AAA & BBB & & & & & &\\
\hline
Method B & AAA & BBB & & & & & &\\
\hline
\CodeAfter
\tikz
\draw (2-1.north-|1) -- (5-|1)
(2-1.north-|2) -- (5-|2)
(2-1.north-|3) -- (5-|3)
(2-1.north-|1) -- (2-1.north-|4) ;
\end{NiceTabular}
\end{table}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。
答案2
尝试
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{rotating}
\usepackage{multirow, makecell}
\begin{document}
\begin{table}
\centering
\settowidth\rotheadsize{ very Long Name}
\caption{Minimal Example Table Problem}
\label{tab:inputVector}
\begin{tabular}{|l|l|l|l|l|l|l|l|l|}
\cline{4-9}
\multicolumn{3}{c|}{} & \multicolumn{6}{c|}{Option}\\
\cline{4-9}
\multicolumn{3}{c|}{} & & & & & &\\
\multicolumn{3}{c|}{} & & & & & &\\
\multicolumn{3}{c|}{} & & & & & &\\
\multicolumn{3}{c|}{} & & & & & &\\
\multicolumn{3}{c|}{} & & & & & &\\
\multicolumn{3}{c|}{} & & & & & &\\
\cline{1-3}
Method & Column A & Column B
& \multirow{-6.8}{*}{\rothead{Short Name}}
& \multirow{-6.8}{*}{\rothead{Another Name}}
& \multirow{-6.8}{*}{\rothead{A very Long Name}}
& \multirow{-6.8}{*}{\rothead{Longer name}}
& \multirow{-6.8}{*}{\rothead{Short Name}}
& \multirow{-6.8}{*}{\rothead{Short Name}} \\
\hline
Method A & AAA & BBB & & & & & &\\
\hline
Method B & AAA & BBB & & & & & &\\
\hline
\end{tabular}
\end{table}
\end{document}
附录: 最长的旋转文本(非常长的名称)大约为 8em 长。知道这一点后,表格的第二行应该为 6em 高,以便旋转文本可以位于旋转单元格内(2em 是从第三行(多行单元格的起始位置)和拉伸的行高获得的)。
\rule{0em}{6em}
我们可以通过在这一行的多列单元格中插入的隐形规则来确定第二行的高度:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{rotating}
\usepackage{multirow, makecell}
\usepackage[skip=1ex]{caption} % new
\begin{document}
\begin{table}
\centering
\renewcommand\arraystretch{1.1}
\setlength\rotheadsize{8em}
\caption{Minimal Example Table Problem}
\label{tab:inputVector}
\begin{tabular}{|l|l|l|l|l|l|l|l|l|}
\cline{4-9}
\multicolumn{3}{c|}{} & \multicolumn{6}{c|}{Option}\\
\cline{4-9}
\multicolumn{3}{c|}{\rule{0em}{6.4em}} & & & & & &\\
\cline{1-3}
Method & Column A & Column B
& \multirow{-7}{*}{\rothead{Short Name}}
& \multirow{-7}{*}{\rothead{Another Name}}
& \multirow{-7}{*}{\rothead{A very Long Name}}
& \multirow{-7}{*}{\rothead{Longer name}}
& \multirow{-7}{*}{\rothead{Short Name}}
& \multirow{-7}{*}{\rothead{Short Name}} \\
\hline
Method A & AAA & BBB & & & & & &\\
\hline
Method B & AAA & BBB & & & & & &\\
\hline
\end{tabular}
\end{table}
\end{document}
编译结果与答案的第一个版本相比略有改进: