# 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
• If there are some other languages text in the table, please do not use the the order "\usepackage[latin1]{inputenc}" – ccy Aug 27 '16 at 8:52
• Actually, the \rowcolors command is not referenced anywhere in the manual. Only the \rowcolor without the s. Any idea on how to make color alternate every n row ? – Overdrivr Oct 12 '16 at 10:06
• Be careful with this approach: in my setup it colored the part of equations typeset with \begin{cases}\end{cases} – cw' Oct 18 '19 at 15:21

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

• 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
• @PaulPaulsen this does not work because the command \rowcolors is not defined by colortbl but by xcolor if the table option is used. Therefore your solution doesn't work in this case. – Skillmon May 17 '17 at 14:45
• @BertramNudelbach An alternative to the solution using \PassOptionsToPackage{table}{xcolor} is to load the beamer package with the option xcolor=table as specified in the Beamer user guide on p. 19. – applesoup Mar 22 '18 at 10:49

This post says if you want to apply alternating colors (white, then lightgray) to all your tables, you can use pure TeX to "redefine the tabular environment":

\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.94}
\let\oldtabular\tabular
\let\endoldtabular\endtabular
\renewenvironment{tabular}{\rowcolors{2}{lightgray}{white}\oldtabular}{\endoldtabular}


I edited the example slightly to make white first, and the lightgray lighter.

\documentclass{article}
\usepackage[table]{xcolor}

\usepackage{multirow}

\usepackage{float}
\usepackage{graphicx} %rotate text
\usepackage{colortbl}

\begin{document}

\begin{table}[ht]
\rowcolors{1}{}{lightgray}
\centering
\caption{Multirow table with continuous coloring.}\label{tab:multi row}
\begin{tabular}{p{5cm}p{5cm}}
\hline
Column 1 & Column 2\\
\hline
-&-\\
-&-\\
\cellcolor{green}&Single-row\\
\cellcolor{green}&Single-row\\
%\parbox[t]{2mm}{\multirow{-3}{*}{Rotating}} & working row \\
\cellcolor{green}\parbox[t]{2mm}{\multirow{-3}{*}{\rotatebox[origin=c]{90}{$job\_id$}}} & working row \\
-&-\\
-&-\\
\hline
\end{tabular}
\end{table}%

\begin{table}[h]
\centering
\begin{tabular}{|c|l|r|r|r|r|}\hline
~ & \multicolumn{1}{c|}{Text} & {\hfill Text\hfill} & \multicolumn{1}{c|}{Text} & \multicolumn{1}{c|}{Text} & \multicolumn{1}{c|}{Text} \\ \hline
\multirow{3}{*}{\rotatebox[origin=c]{90}{\parbox[c]{1cm}{\centering rotato-ata}}} & text &&&&\\
& text &testing A& testing B&&\\
& text &right aligned&&&\\ \hline
\end{tabular}
\end{table}

\begin{table}[ht]
\rowcolors{1}{}{lightgray}
\centering
\caption{Multirow table with all cells in the same color.}\label{tab:multi row}
\begin{tabular}{p{5cm}p{5cm}}
\hline
Column 1 & Column 2\\
\hline
-                       &       -       \\
-                       &       -       \\
\cellcolor{green} Text Green   &   Single-row  \\
\cellcolor{yellow} &   Single-row  \\
\multirow{-3}{*}{\cellcolor{red}Text of -3, Misplaced}&Single-row\\
-                       &       -       \\
-                       &       -       \\
\hline
\end{tabular}
\end{table}%

\begin{table}[ht]
\rowcolors{1}{}{lightgray}
\centering
\caption{Multirow table with all rows in the same color.}\label{tab:multi row}
\begin{tabular}{p{5cm}p{5cm}}
\hline
Column 1 & Column 2\\
\hline
-                       &       -       \\
-                       &       -       \\
\rowcolor{green}        &   Single-row  \\
\rowcolor{yellow}       &   Single-row  \\
\rowcolor{red}\multirow{-3}{*}{Text}&Single-row\\
-                       &       -       \\
-                       &       -       \\
\hline
\end{tabular}
\end{table}%

\end{document}