Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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

share|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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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