![如何从 diagbox 中删除不需要的水平线](https://linux22.com/image/395953/%E5%A6%82%E4%BD%95%E4%BB%8E%20diagbox%20%E4%B8%AD%E5%88%A0%E9%99%A4%E4%B8%8D%E9%9C%80%E8%A6%81%E7%9A%84%E6%B0%B4%E5%B9%B3%E7%BA%BF.png)
我在表格中使用 \diagbox,但不知为何水平线仍然存在。表格最终看起来像这样:
另外,垂直线没有完全连接到顶部规则。如能得到任何帮助,我将不胜感激。
代码如下:
\begin{tabular}{|l|c|c?c?c|c?c?c|c?c?c|c?c?c|c?c?c|c?c?}
\hline
\multirow{2}{*}{\diagbox{\textbf{Model}}{\textbf{Test Data}}} & \multicolumn{3}{|c|}{\textbf{Level 0}} & \multicolumn{3}{|c|}{\textbf{Level 1}} & \multicolumn{3}{|c|}{\textbf{Level 2}} & \multicolumn{3}{|c|}{\textbf{Level 3}} & \multicolumn{3}{|c|}{\textbf{Level 4}} & \multicolumn{3}{|c|}{\textbf{Level 5}} \\ \hline
& \emph{R1} & \emph{R2} & \mu & \emph{R1} & \emph{R2} & \mu &\emph{R1} & \emph{R2} & \mu &\emph{R1} & \emph{R2} & \mu &\emph{R1} & \emph{R2} & \mu &\emph{R1} & \emph{R2} & \mu \\ \toprule
\textbf{Level 0} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\textbf{Level 1} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\textbf{Level 2} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\textbf{Level 3} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\textbf{Level 4} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\textbf{Level 5} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\end{tabular}}
答案1
为了删除穿过第一列第一个单元格的水平线,请将其替换\hline
为\cline{2-19}
。关于不与水平线相连的垂直线:将其替换\toprule
为\hline
。(booktabs
包中的线与垂直线不兼容)。
对于以下 MWE,我假设?
表中的列规范定义如下:(\usepackage{array} \newcolumntype{?}{!{\vrule width 1pt}}
参见:这里)。但是,垂直线与问题中的屏幕截图不匹配,因此可能需要更改它们。
\documentclass{article}
\usepackage{diagbox}
\usepackage{multirow}
\usepackage[margin=0.5cm]{geometry}
\usepackage{array}
\newcolumntype{?}{!{\vrule width 1pt}}
\begin{document}
\begin{tabular}{|l|c|c?c?c|c?c?c|c?c?c|c?c?c|c?c?c|c?c?}
\hline
\multirow{2}{*}{\diagbox{\textbf{Model}}{\textbf{Test Data}}} & \multicolumn{3}{|c|}{\textbf{Level 0}} & \multicolumn{3}{|c|}{\textbf{Level 1}} & \multicolumn{3}{|c|}{\textbf{Level 2}} & \multicolumn{3}{|c|}{\textbf{Level 3}} & \multicolumn{3}{|c|}{\textbf{Level 4}} & \multicolumn{3}{|c|}{\textbf{Level 5}} \\ \cline{2-19}
& \emph{R1} & \emph{R2} & $\mu$ & \emph{R1} & \emph{R2} & $\mu$ &\emph{R1} & \emph{R2} & $\mu$ &\emph{R1} & \emph{R2} & $\mu$ &\emph{R1} & \emph{R2} & $\mu$ &\emph{R1} & \emph{R2} & $\mu$ \\ \hline
\textbf{Level 0} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\textbf{Level 1} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\textbf{Level 2} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\textbf{Level 3} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\textbf{Level 4} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\textbf{Level 5} & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\ \hline
\end{tabular}
\end{document}
就我个人而言,我会按如下方式重新设计您的表格。(请注意,表格现在窄了很多,更有可能适合您文档的文本宽度)。
\documentclass{article}
\usepackage{booktabs}
\usepackage{geometry}
\begin{document}
\setlength{\tabcolsep}{5pt}
\begin{tabular}{lcccccccccccccccccc}
\toprule
Model & \multicolumn{18}{c}{Test Data} \\ \cmidrule{2-19}
& \multicolumn{3}{c}{Level 0} & \multicolumn{3}{c}{Level 1} & \multicolumn{3}{c}{Level 2} & \multicolumn{3}{c}{Level 3} & \multicolumn{3}{c}{Level 4} & \multicolumn{3}{c}{Level 5} \\ \cmidrule(r){2-4} \cmidrule(lr){5-7} \cmidrule(lr){8-10} \cmidrule(lr){11-13} \cmidrule(lr){14-16} \cmidrule(lr){17-19}
& \emph{R1} & \emph{R2} & $\mu$ & \emph{R1} & \emph{R2} & $\mu$ &\emph{R1} & \emph{R2} & $\mu$ &\emph{R1} & \emph{R2} & $\mu$ &\emph{R1} & \emph{R2} & $\mu$ &\emph{R1} & \emph{R2} & $\mu$ \\ \midrule
Level 0 & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\
Level 1 & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\
Level 2 & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\
Level 3 & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\
Level 4 & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\
Level 5 & u & b & a & u & b & a & u & b & a & u & b & a & u & b & a & \emph{u} & \emph{b} & \emph{a}\\
\bottomrule
\end{tabular}
\end{document}