[Tex/LaTex] Variable-width horizontal rules with \cline intrude cell text

rulestablesvertical alignment

I have come to learn how to make a variable-width horizontal rule with \hline from the LaTeX companion and with \cline from Horizontal rule with adjustable height behaving like \cline{n-m}. But the problem with the horizontal \cline is that the rule intrudes on the texts in the cells below the \cline. However, \hline doesn't seem to have this intruding problem. The minimal working example below shows the differences.

\documentclass{article}
\usepackage{array}

\begin{document}

\newcolumntype{I}{!{\vrule width 3pt}}
\newlength\savedwidth
\newcommand\whline{\noalign{\global\savedwidth\arrayrulewidth\global\arrayrulewidth 6pt}%
\hline
\noalign{\global\arrayrulewidth\savedwidth}}
\begin{tabular}{|cIc|c|}\hline
A & B & C \\ \hline
X & Y & Z \\ \whline
100 & 10 & 1 \\ \hline
\end{tabular}

\renewcommand\whline[1]{\noalign{\global\savedwidth\arrayrulewidth\global\arrayrulewidth 6pt}%
\cline{#1}
\noalign{\global\arrayrulewidth\savedwidth}}
\begin{tabular}{|cIc|c|}\hline
 A & B & C \\ \hline
 X & Y & Z \\ \whline{1-3}
 100 & 10 & 1 \\ \hline
\end{tabular}

\end{document}

Top picture essentially used \hline; bottom one used \cline.

Another problem with using \cline in the definition of the thick horizontal rule (whline above) is that a \cellcolor command will wipe out the portion of the horizontal rule that is above the cell, no matter how thick I defined the whline to be. Using \hline in the definition of whline does not cause this problem with using colors in a cell.

I suspect that the vertical position, starting below which is the territory of a cell, for \hline and for \cline differs. For \hline, a cell starts below the bottom boundary of the horizontal rule, while for \cline, a cell starts below the top boundary of the rule? Anyone enlighten me please!

NOTE: I do want to use variable-width \cline (either thick or thin like the example showed), but I want to avoid these intruding problems.

Best Answer

Both problems are related. \cline has to support several \clines in a row, e.g.:

\documentclass{article}
\begin{document}
  \begin{tabular}{*{5}{c}}
    1 & 2 & 3 & 4 & 5\\
    \cline{1-1}\cline{3-3}\cline{5-5}
    A & B & C & D & E
  \end{tabular}
\end{document}

Example \cline

Therefore \cline cannot move down like \hline, the definition of \cline is (latex.ltx):

\def\cline#1{\@cline#1\@nil}
\def\@cline#1-#2\@nil{%
  \omit
  \@multicnt#1%
  \advance\@multispan\m@ne
  \ifnum\@multicnt=\@ne\@firstofone{&\omit}\fi
  \@multicnt#2%
  \advance\@multicnt-#1%
  \advance\@multispan\@ne
  \leaders\hrule\@height\arrayrulewidth\hfill
  \cr
  \noalign{\vskip-\arrayrulewidth}}

In the last step \cline moves back to the original vertical position.

But the vertical space for the line can be added after the \clines, e.g.:

\cline{...}\cline{...}\noalign{\vskip\arrayrulewidth}

Applied to your example, it can be added to macro \whline. This fixes also the problem with \cellcolor:

\documentclass{article}
\usepackage{array}
\usepackage{colortbl}

\begin{document}

\newcolumntype{I}{!{\vrule width 3pt}}
\newlength\savedwidth

\newcommand\whline[1]{%
  \noalign{%
    \global\savedwidth\arrayrulewidth\global\arrayrulewidth 6pt%
  }%
  \cline{#1}%
  \noalign{\vskip\arrayrulewidth}%
  \noalign{\global\arrayrulewidth\savedwidth}%
}
\begin{tabular}{|cIc|c|}\hline
 A & B & C \\ \hline
 X & \cellcolor{red}Y & Z \\ \whline{1-3}
 \cellcolor{green}100 & 10 & 1 \\ \hline
\end{tabular}

\end{document}

Result

Related Question