How to create alternating rows in a table?

Sometimes you can see in books tables, that have alternating background color for rows in a table to support readability. How I can achieve this with LaTeX?

And yes, I could put macros \odd and \even into each row, but in that case I would have to change all following rows, if I input later another row into the table. I want TeX to decide itself, which row is odd or even and put the appropiate style into it.

-

You can use the features of the colortbl package with additions of xcolor. See code below for a small example.

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

\begin{document}
\rowcolors{2}{gray!25}{white}
\begin{tabular}{cc}
\rowcolor{gray!50}
Some values & Some values \\
Some values & Some values \\
Some values & Some values \\
Some values & Some values
\end{tabular}
\end{document}


The particular package manuals have the details.

-
You beat me by 20 seconds. +1 – lockstep Nov 14 '10 at 13:24

This can easily be achieved using xcolor. It is explained at Wikibooks, you just add the table option to \usepackage{xcolor} and in the table itself you put

\rowcolors{1}{color-1}{color-2}

-

One thing that might be worth pointing out, is that apparently, many packages call on xcolor, and I had endless clashes first time I tried it, and it took me such a long time to figure this one out, so I felt it might help someone as slow as me to mention this: The xcolor package should be loaded last (or at least after the clashing packages).

-
Thanks, I could reproduce the clash, but had it not in first place, because I added it at the end anyways. But that was pure chance, so others might run into this problem. – Mnementh Nov 14 '10 at 18:14
I had that clash using beamer. Since beamer loads xcolor internally, you cannot add \usepackage[table]{xcolor} just to specify the table option. Therefore you have to put the line \PassOptionsToPackage{table}{xcolor} before the \documentclass{..}. Hope somebody else can use this :-) – Bertram Nudelbach Jun 6 '12 at 7:39
Thank you Bertram, this was exactly the solution I was looking for. I was using the tufte-book class. Now it works flawlessly ;) \PassOptionsToPackage{table}{xcolor} \documentclass{..} – user28917 Apr 12 '13 at 13:16
Another approach which might be worth trying in this case (xcolor already loaded by another package): just load colortbl instead of xcolor. – Paul Paulsen Feb 6 '15 at 17:33