General ⇒ Automatic table background colour
Automatic table background colour
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.
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
Automatic table background colour
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}