![书本标签中的多列标题已旋转](https://linux22.com/image/406767/%E4%B9%A6%E6%9C%AC%E6%A0%87%E7%AD%BE%E4%B8%AD%E7%9A%84%E5%A4%9A%E5%88%97%E6%A0%87%E9%A2%98%E5%B7%B2%E6%97%8B%E8%BD%AC.png)
我想生成一个带有书本标签的交叉表。因此,我有 2 个数据集想要相互比较。
表格应该如下图所示,但我无法正确显示垂直标题。也许有人可以帮忙?
我想要一个从第 8 行到第 2 行的旋转标题,并在“Data2”之前换行。如果可能的话,另一个垂直 cmidrule 会很好。
这是 MWE:) 非常感谢!
\documentclass[a4paper, 12pt, headsepline]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{rotating}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\begin{document}
\begin{table}[!h]
\centering
\begin{tabular}{cccccccc}
\toprule
& & \multicolumn{6}{c}{\thead{Long text for Data1}} \\
\cmidrule(lr){3-8}
& & \thead{0} & \thead{1} & \thead{2} & \thead{3} & \thead{5} & \thead{10}\\
\midrule
\multirowcell{6}[0pt]{\rothead{Long text for\\Data2}}& \thead{0} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& \thead{1} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& \thead{2} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& \thead{3} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& \thead{5} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& \thead{10} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案1
在最后一行第一个单元格中使用 a\raisebox
和 a 。标题在 a内设置类型:\rotatebox
\parbox
\rotatebox
示例 1 – raisebox、rotatebox 和 parbox
\documentclass[a4paper, 12pt, headsepline]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{rotating}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\begin{document}
\begin{table}[!h]
\centering
\begin{tabular}{p{1cm}>{\bfseries}ccccccc}
\toprule
& & \multicolumn{6}{c}{\thead{Long text for Data1}} \\
\cmidrule(lr){3-8}
& & \thead{0} & \thead{1} & \thead{2} & \thead{3} & \thead{5} & \thead{10}\\
\midrule
& 0 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& 1 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& 2 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& 3 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& 5 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
\raisebox{35pt}[0pt][0pt]{\rotatebox[origin=c]{90}{\parbox{2.5cm}{\centering Long text for \\ Data2}}} & 10 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
示例 2 – raisebox、rotatebox 和嵌套表格
另一个可能的解决方案是在内使用嵌套的表格\rotatebox
。tabular
是通过定义新命令来设置的类型\tstack
。 垂直规则是\cmidrule
经过微调以延伸直到满足水平规则的。 百分号 (%) 我已经从单元格/列移动到标题。 您可以考虑减小标题中的字体大小以使其footnotesize
不那么显眼。
\documentclass[a4paper, 12pt, headsepline]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{booktabs, array}
\usepackage{rotating}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
% Multi-line left-aligned text with manual line breaks.
% The base line of the whole is at the top row.
\newcommand*{\tstack}[1]{%
\begingroup
\renewcommand*{\arraystretch}{1}%
\begin{tabular}[t]{@{}>{\bfseries}c@{}}#1\\\cmidrule(l{-0.65em}r{-0.65em}){1-1} \end{tabular}%
\endgroup
}
\begin{document}
\begin{table}[!h]
\centering
\begin{tabular}{@{}>{\centering\arraybackslash}p{1cm}>{\bfseries}rcccccc}
\toprule
& & \multicolumn{6}{c}{\thead{Long text for Data1}} \\
\cmidrule(lr){3-8}
& & \thead{0 (\%)} & \thead{1 (\%)} & \thead{2 (\%)} & \thead{3 (\%)} & \thead{5 (\%)} & \thead{10 (\%)}\\
\midrule
& 0 & ??? & ??? & ??? & ??? & ??? & ??? \\
& 1 & ??? & ??? & ??? & ??? & ??? & ??? \\
& 2 & ??? & ??? & ??? & ??? & ??? & ??? \\
& 3 & ??? & ??? & ??? & ??? & ??? & ??? \\
& 5 & ??? & ??? & ??? & ??? & ??? & ??? \\
\raisebox{47pt}[0pt][0pt]{\rotatebox[origin=c]{90}{\tstack{Long text for \\ Data2}}} & 10 & ??? & ??? & ??? & ??? & ??? & ??? \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
示例 3 – 嵌套表格和垂直规则
如果您接受第 1 列的垂直线和水平线之间的小间隙(我认为在本表格中是可以的),更自动化的解决方案是|
在第 1 列和第 2 列之间使用普通线(),然后在第 1 行和第 2 行中使用多列线取消该线。在下面的示例中,我还减小了字体大小,使用粗体时会更美观。
\documentclass[a4paper, 12pt, headsepline]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{booktabs, array}
\usepackage{rotating}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries\footnotesize}
% Multi-line left-aligned text with manual line breaks.
% The base line of the whole is at the top row.
\newcommand*{\tstack}[1]{%
\begingroup
\renewcommand*{\arraystretch}{1}%
\begin{tabular}[t]{@{}c@{}}#1\end{tabular}%
\endgroup
}
\begin{document}
\begin{table}[!h]
\centering
\begin{tabular}{@{}>{\footnotesize\bfseries\centering\arraybackslash}p{1.1cm}|>{\bfseries}rcccccc@{}}
\toprule
\multicolumn{1}{c}{} & & \multicolumn{6}{c}{\thead{Long text for Data1}} \\
\cmidrule(l){3-8}
\multicolumn{1}{c}{} & & \thead{0\,(\%)} & \thead{1\,(\%)} & \thead{2\,(\%)} & \thead{3\,(\%)} & \thead{5\,(\%)} & \thead{10\,(\%)}\\
\midrule
& 0 & ??? & ??? & ??? & ??? & ??? & ??? \\
& 1 & ??? & ??? & ??? & ??? & ??? & ??? \\
& 2 & ??? & ??? & ??? & ??? & ??? & ??? \\
& 3 & ??? & ??? & ??? & ??? & ??? & ??? \\
& 5 & ??? & ??? & ??? & ??? & ??? & ??? \\
\raisebox{44pt}[0pt][0pt]{\rotatebox[origin=c]{90}{\tstack{Long text for \\ Data2}}} & 10 & ??? & ??? & ??? & ??? & ??? & ??? \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
一个仅包含 的解决方案\thead
,其中的行数有作弊之处\multirow
:
\documentclass[a4paper, 12pt, headsepline]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{rotating}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\begin{document}
\begin{table}[!h]
\centering
\settowidth\rotheadsize{\theadfont Long text for}
\begin{tabular}{cccccccc}
\toprule
& & \multicolumn{6}{c}{\thead{Long text for Data1}} \\
\cmidrule(lr){3-8}
& & \thead{0} & \thead{1} & \thead{2} & \thead{3} & \thead{5} & \thead{10}\\
\midrule
\multirow{8}{*}{\rotatebox[origin=c]{90}{\thead{Long text for\\ Data2}}} & \thead{0} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& \thead{1} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& \thead{2} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& \thead{3} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& \thead{5} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& \thead{10} & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案3
{NiceTabular}
这是的解决方案nicematrix
。此环境与 类似{tabular}
,array
但提供了一些其他功能。
在 中{NiceTabular}
,您可以使用命令 水平和垂直合并单元格\Block
。对于行,您可以指定逻辑行数(而不是像 那样的物理行数\multicolumn
)。
提供了内置命令\rotate
来旋转块的内容。
\documentclass[a4paper, 12pt, headsepline]{scrreprt}
\usepackage{booktabs}
\usepackage{nicematrix}
\begin{document}
\begin{table}[!h]
\centering
\begin{NiceTabular}{>{\bfseries}c>{\bfseries}ccccccc}[cell-space-limits=3pt]
\toprule
\RowStyle[nb-rows=2]{\bfseries}
& & \Block{1-6}{Long text for Data1} \\
\cmidrule(lr){3-8}
& & 0 & 1 & 2 & 3 & 5 & 10 \\
\midrule
\Block{6-1}<\rotate>{Long text for\\ Data2}
& 0 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& 1 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& 2 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& 3 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& 5 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
& 10 & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% & ???\,\% \\
\bottomrule
\end{NiceTabular}
\end{table}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。