GeneralAutomatic table background colour

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
UJS
Posts: 6
Joined: Wed Nov 19, 2008 12:54 am

Automatic table background colour

Post by UJS »

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.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Automatic table background colour

Post by Juanjo »

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.
UJS
Posts: 6
Joined: Wed Nov 19, 2008 12:54 am

Re: Automatic table background colour

Post by UJS »

That's perfect, thanks Juanjo!
Post Reply