3

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?

2

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

| improve this answer | |
  • Wow, that's a whole lot of @s in there. I think that definition would make a sensible addition to the array package. – Fritz Mar 8 '13 at 19:12
0

With {NiceTabular} of nicematrix, you use \hline and cline directly with the expected output.

\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-2} A & B \\ C & D \\ \hline
\end{NiceTabular}

\end{document}

You need several compilations.

Ouput of the above code

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.