当使用选项对齐tabular
环境时[t]
,可以使用\firsthline
(来自数组包)代替\hline
表格顶部需要一行的情况。像这样:
\documentclass{article}
\usepackage{array}
\begin{document}
Tabular with hline:
\begin{tabular}[t]{ll}
\hline A & B \\ C & D \\ \hline
\end{tabular}
and with firsthline:
\begin{tabular}[t]{ll}
\firsthline A & B \\ C & D \\ \lasthline
\end{tabular}
但是,如果我只希望第一行的一些单元格上方有一条线,该怎么办?如下所示:
Tabular with cline:
\begin{tabular}[t]{ll}
\cline{2-2} A & B \\ C & D \\ \hline
\end{tabular}
and what it should look like:
\raisebox{.8em}{
\begin{tabular}[t]{ll}
\cline{2-2} A & B \\ C & D \\ \hline
\end{tabular}
}
\end{document}
编译结果如下:
有没有什么简单的方法可以实现这种效果,而无需手动调整垂直偏移\raisebox
?
答案1
\firscline
您可以定义类似于\firsthline
但使用\cline
而不是的命令\hline
:
\documentclass{article}
\usepackage{array}
\makeatletter
\newcommand{\firstcline}[1]{%
\multicolumn1c{%
\global\backup@length\ht\@arstrutbox
\global\advance\backup@length\dp\@arstrutbox
\global\advance\backup@length\arrayrulewidth
\raise\extratabsurround\copy\@arstrutbox
}\\[-\backup@length]\cline{#1}
}
\makeatother
\begin{document}
Tabular with \verb+\hline+:
\begin{tabular}[t]{ll}
\hline A & B \\ C & D \\ \hline
\end{tabular}
and with \verb+\firstcline+:
\begin{tabular}[t]{ll}
\firstcline{1-1} A & B \\ C & D \\ \lasthline
\end{tabular}
\begin{tabular}[t]{ll}
\firstcline{2-2} A & B \\ C & D \\ \lasthline
\end{tabular}
\end{document}
答案2
使用,您可以直接使用{NiceTabular}
和获得预期输出。此外,您可以用代替。nicematrix
\hline
cline
\cline{2}
\cline{2-2}
\documentclass{article}
\usepackage{nicematrix}
\begin{document}
\verb|{NiceTabular}| with \verb|\hline|:\quad
\begin{NiceTabular}[t]{ll}
\hline A & B \\ C & D \\ \hline
\end{NiceTabular}
\bigskip
\verb|{NiceTabular}| with \verb|\cline|:\quad
\begin{NiceTabular}[t]{ll}
\cline{2} A & B \\ C & D \\ \hline
\end{NiceTabular}
\end{document}
您需要多次编译。