Color table rows, columns and cells in LaTeX (rowcolor, cellcolor)

Load xcolor with the table option. \rowcolor then colors a row, \columncolor a column and \cellcolor a single cell, and \rowcolors alternates two colors over all rows.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage[table]{xcolor}

Color one row

\rowcolor{gray!20} stands at the start of the row, before the first cell. gray!20 mixes 20 % gray with 80 % white.

\begin{tabular}{lr}
\hline
\rowcolor{gray!20}
Item   & Price \\
\hline
Apple  & 0.50 \\
Banana & 0.25 \\
\hline
\end{tabular}
Result
The compiled example

Open in the editor

Alternate the row colors

\rowcolors{2}{gray!15}{white} stands before the tabular. The arguments are the row where the pattern starts, the color of the odd rows and the color of the even rows. The braces around the example end the pattern after this table.

{\rowcolors{2}{gray!15}{white}
\begin{tabular}{lr}
\hline
Item   & Price \\
\hline
Apple  & 0.50 \\
Banana & 0.25 \\
Cherry & 0.10 \\
Date   & 0.30 \\
\hline
\end{tabular}}
Result
The compiled example

Open in the editor

Color a column

>{\columncolor{blue!10}} in front of a column letter colors every cell of that column.

\begin{tabular}{l>{\columncolor{blue!10}}r}
\hline
Item   & Price \\
\hline
Apple  & 0.50 \\
Banana & 0.25 \\
\hline
\end{tabular}
Result
The compiled example

Open in the editor

Color a single cell

\cellcolor{yellow!40} stands inside the cell. It wins over the color of the row and of the column.

\begin{tabular}{lr}
\hline
Item   & Price \\
\hline
Apple  & \cellcolor{yellow!40}0.50 \\
Banana & 0.25 \\
\hline
\end{tabular}
Result
The compiled example

Open in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages