我用来\hrulefill\hfill
创建水平线。问题是这些线没有接触单元格的边框。我怎样才能让它们接触单元格的垂直边框线?
ASCII 艺术示例:
现在我明白了
| Class 1 | Class 2 |
| --------------- |
我想得到这个
| Class 1 | Class 2 |
|-------------------|
优先使用tabular
替代环境的解决方案。tabularx
以下是 MWE:
\documentclass{article}
\usepackage[english]{babel}
\usepackage{array}
\begin{document}
\begin{tabular}{|c|>{\centering}p{2em}|>{\centering}p{2em}|>{\centering}p{2em}|>{\centering}p{2em}|}
\hline
& \multicolumn{2}{c|}{Class 1} & \multicolumn{2}{c|}{Class 2}\tabularnewline
\hline
A & \multicolumn{4}{c|}{\hrulefill\hfill}\tabularnewline
\hline
B & & \hrulefill\hfill & \hrulefill\hfill & \tabularnewline
\hline
\end{tabular}
\end{document}
答案1
您需要\tabcolsep
通过以下几种方式之一来解释:
\documentclass{article}
\usepackage[english]{babel}
\usepackage{array}
\begin{document}
\begin{tabular}{|c|>{\centering}p{2em}|>{\centering}p{2em}|>{\centering}p{2em}|>{\centering}p{2em}|}
\hline
& \multicolumn{2}{c|}{Class 1} & \multicolumn{2}{c|}{Class 2}\tabularnewline
\hline
A & \multicolumn{4}{c|}{\hrulefill\hfill}\tabularnewline
\hline
B & & \hrulefill\hfill & \hrulefill\hfill & \tabularnewline
\hline
\end{tabular}
\bigskip
\begin{tabular}{|c|>{\centering}p{2em}|>{\centering}p{2em}|>{\centering}p{2em}|>{\centering}p{2em}|}
\hline
& \multicolumn{2}{c|}{Class 1} & \multicolumn{2}{c|}{Class 2}\tabularnewline
\hline
A & \multicolumn{4}{@{}c@{}|}{\hrulefill}\tabularnewline
\hline
B & & \multicolumn{1}{@{}c@{}|}{\hrulefill}&
\multicolumn{1}{@{}c@{}|}{\hrulefill} & \tabularnewline
\hline
1&2&3&4\tabularnewline
\hline
\end{tabular}
\bigskip
{\setlength\tabcolsep{0pt}
\begin{tabular}{|c|>{\centering}p{2em}|>{\centering}p{2em}|>{\centering}p{2em}|>{\centering}p{2em}|}
\hline
& \multicolumn{2}{c|}{Class 1} & \multicolumn{2}{c|}{Class 2}\tabularnewline
\hline
A & \multicolumn{4}{c|}{\hrulefill}\tabularnewline
\hline
B & & \hrulefill & \hrulefill & \tabularnewline
\hline
\end{tabular}}
\bigskip
\begin{tabular}{|c|>{\centering}p{2em}|>{\centering}p{2em}|>{\centering}p{2em}|>{\centering}p{2em}|}
\hline
& \multicolumn{2}{c|}{Class 1} & \multicolumn{2}{c|}{Class 2}\tabularnewline
\hline
A & \multicolumn{4}{c|}{\mbox{}\kern-\tabcolsep\hrulefill\kern-\tabcolsep}\tabularnewline
\hline
B & &\mbox{}\kern-\tabcolsep\hrulefill\kern-\tabcolsep &
\mbox{}\kern-\tabcolsep\hrulefill\kern-\tabcolsep & \tabularnewline
\hline
\end{tabular}
\end{document}