顶部对齐表格:\cline 相当于 \firsthline (或 \firstcline)

顶部对齐表格:\cline 相当于 \firsthline (或 \firstcline)

当使用选项对齐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\hlinecline\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}

您需要多次编译。

上述代码的输出

相关内容