alternate coloing rows with longtable and colortbl 

Date:          Mon, Dec 21 1998 12:00 am
Groups:        comp.text.tex

I'm trying to make a long table and i want to color rows alternatively to make the table more readable. I tryed to use the package ifthen and a new counter to determine in which row i am, but there's no color in the output. LaTeX doesn't complain either :-)

\newcounter{line}
\newcommand{\co}{\addtocounter{line}{1}}
\begin{longtable}{|>{\co\ifthenelse{\isodd{line}}{\rowcolor[gray]{0.95}}{}\bfseries
}l%
|l|l|>{\centering}p{1.5cm}|>{\sc}c|c|}

Where am i wrong? is there another possibility? I'd like to find a better solution than using \rowcolor in every 2 lines, especially if i have to add a row in the table later.

thanks in advance for your help…

alternate coloing rows with longtable and colortbl 

>I'm trying to make a long table and i want to color rows
>alternatively to make the table more readable.

The code we use in the tex-live docs does:

\definecolor{pale}{gray}{.9}
\newcount\colrow
\gdef\RowColor{pale}
\def\SetRowColor{%
 \rowcolor{\RowColor}%
 \global\advance\colrow by1\relax
 \ifodd\colrow
   \gdef\RowColor{pale}%
 \else
   \gdef\RowColor{white}%
 \fi
}

this is in essence identical to what you've done, except that it explicitly sets the colour for every line. the difference is that the table (which is auto-generated from a set of .bib files) has the \SetRowColor command explicitly inserted into each row (by the the \bibitem command, as it happens).

the above code is by seb rahtz; needless to say, david carlisle suggested a neater way still (which i've forgotten). perhaps david will explain why your technique (using a `>-clause') doesn't work and sebastian's does.

Robin Fairbairns

alternate coloing rows with longtable and colortbl 

As discussed at some length in a recent thread on this newsgroup you can't use \ifthenelse before a \multicolumn or similar command in a table. \rowcolor counts as similar to \multicolumn for this purpose.

of the counter rather than the name.

David Carlisle

\documentclass{article}

\def\yy{\\\rowcolor{red}}
\usepackage{colortbl,ifthen}
\newcounter{line}
\newcommand\xx{%
  \addtocounter{line}{1}%
  \ifthenelse{\isodd{\value{line}}}{\\\rowcolor[gray]{0.95}}{\\}}

\begin{document}

\begin{tabular}{lll}
aaa&bbb&ccc\xx
1&2&3\xx
one&two&three\xx
aaa&bbb&ccc\xx
1&2&3\xx
one&two&three\xx
aaa&bbb&ccc\xx
1&2&3\xx
one&two&three\xx
\end{tabular}

\end{document}