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}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}}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}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}Pitfalls
- The
booktabsrules put white space above and below themselves, and the row color does not fill it. A colored row next to\midruletherefore shows a white stripe;\hlinedoes not have this effect. - Before version 3 of
xcolor(November 2023),\usepackage[table]{xcolor}stopped with Option clash for package geometry when another package such as TikZ or pgfplots had already loadedxcolor. Loadingxcolorwith the option before those packages works in every version. - A color name that is not defined stops with Undefined color `darkgreen'. Mixes such as
gray!20only work withxcolor, not with the oldercolorpackage.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
colortbl - Package
xcolor - How-to Colored text in LaTeX
- How-to Table borders and lines in LaTeX
- Error Option clash for package geometry