Hi guys,
I'm trying to create a table with a background colour that alternates between three white rows, and three gray rows. I noticed that you can manually specify the colour for each row using the colortbl package, but at this point I'm not sure how many rows I'll end up with, and new rows aren't just added at the end of the table. It wouldn't be very efficient to keep changing the code...
With the xcolor package it was quite simple to set different backgrounds for even and odd rows, but I still couldn't find exactly what I was looking for. Any help would be greatly appreciated.
Edit: I forgot to mention I'm using a longtable.
General ⇒ Automatic table background colour
NEW: TikZ book now 40% off at Amazon.com for a short time.

Automatic table background colour
Try the following example:
Code: Select all
\documentclass{article}
\usepackage{colortbl}
\usepackage{longtable}
\begin{document}
\newcounter{RowNum}
\newcommand{\SetRowColor}{%
\noalign{\stepcounter{RowNum}}\ifnum\value{RowNum}>3\rowcolor[gray]{0.8}\fi%
\noalign{\ifnum\value{RowNum}=6\setcounter{RowNum}{0}\fi}}
\begin{longtable}{|c|c|c|c|c|}
\hline
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\SetRowColor First column & Second column & Third column & Fourth column & Fifth column \\
\hline
\end{longtable}
\end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Re: Automatic table background colour
That's perfect, thanks Juanjo!