[Tex/LaTex] Top aligned tabular: \cline equivalent of \firsthline (or \firstcline)

rulestablesvertical alignment

When aligning tabular environments with the [t] option, one can use \firsthline (from the array package) instead of \hline when a line is required at the top of the table. Like so:

\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}

But what if I want only some cells of the first line to have a line above them, like this:

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}

And the compiled result:

Vertical alignment problem illustrated

Is there any easy way to achieve this effect without manually tweaking the vertical offset with \raisebox?

Best Answer

You can define a command \firscline similar to \firsthline but using \cline instead of \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}

enter image description here