Quantcast
Channel: tabular – texblog
Viewing all articles
Browse latest Browse all 22

Color Table Series Part 2: Xcolor Package

$
0
0

Overview

  1. Introduction to colors
  2. The colortbl package
  3. The xcolor package

 

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.

Alternating background color with xcolor

% Preamble
\usepackage[table]{xcolor}

% Table code
{
	\rowcolors{2}{green!25}{green!75}
	\begin{tabular}{ccc}
		\hline
		\rowcolor{gray}Header & Header            & Header \\
		1                     & 2                 & 3      \\
		4                     & \cellcolor{gray}5 & 6      \\
		7                     & 8                 & 9      \\
		\hline
	\end{tabular}
}

\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.


The author of this article is the main developer behind the LaTeX Complex Tables Editor.


Viewing all articles
Browse latest Browse all 22

Trending Articles