because LaTeX matters

Color Table Series Part 2: Xcolor Package

Series overview

3. The xcolor package

While coloring tables, the xcolor package provides the same commands as colortbl. The reason is that loading the xcolor package with the table option loads the colortbl package in the background. On top to the functionality provided by colortbl (discussed here), xcolor predefines more colors and provides a macro to set the background color for even and odd rows.

Loading the package

To use xcolor to color tables, load the package with the table option:

\usepackage[table]{xcolor}

To use the package with beamer, you have to change your \documentclass declaration instead:

\documentclass[xcolor=table]{beamer}

Alternating row colors in a table

The \rowcolors macro alternates the background color of rows in a table. Make sure it is \rowcolors with an “s” and not \rowcolor, which is defined by colortbl.

\rowcolors[<commands>]{<row>}{<odd-row color>}{<even-row color>}

This command set the odd rows’ color to <odd-row color> and the even rows’ color to <even-row color>, starting at the row number <row>. An optional <commands> is executed before each row.

\cellcolor overwrites \rowcolor, which itself overwrites \columncolor in a \multicolumn cell. On the other hand, \multicolumn overwrites \rowcolors (note the “s”), which itself overwrites \columncolor in the preamble of your table.

Finally, the tabu package provides a more flexible command with \taburowcolors. See the package documentation for more details.