8

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.

9

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

  • Thanks for the clear explanation! I guess it's time for me to learn some plain TeX to really understand what is going on under the hood. – Rich Jul 16 '13 at 14:01
2

Here is Heiko's example using cals package as an alternative:

cals table with variable borders and color

\documentclass{article}
\usepackage{xcolor}
\usepackage{cals}
\begin{document}
\begin{calstable}
\colwidths{{10mm}{10mm}{10mm}}
\makeatletter
\brow
\cell{A}
\def\cals@borderL{2pt}\cell{B}\let\cals@borderL=\relax
\cell{C}
\erow
\brow
\def\cals@borderB{3pt}
\cell{X}
\def\cals@borderL{2pt}\def\cals@bgcolor{red}
 \cell{Y}\let\cals@borderL=\relax\def\cals@bgcolor{}
\cell{Z}
\erow
\let\cals@borderB=\relax
\brow
\def\cals@bgcolor{green}\cell{100}\def\cals@bgcolor{}
\def\cals@borderL{2pt}\cell{10}\let\cals@borderL=\relax
\cell{1}
\erow
\end{calstable}
\end{document}
  • This package looks hard to use... But it seems very flexible. Thanks! – Rich Aug 8 '13 at 17:00

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.