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 Nov 14, 2010 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, 2016 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 ? Oct 12, 2016 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, 2019 at 15:21
• Can the gray color start from the second row instead of first one ? Nov 19, 2020 at 22:27

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 :-) Jun 6, 2012 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, 2013 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. Feb 6, 2015 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. May 17, 2017 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. Mar 22, 2018 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.

The environment {NiceTabular} of nicematrix has tools in order to color cells, rows and columns in a convenient way.

Morevorer, with nicematrix you won't have the thin white lines you see in some PDF viewers.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
\begin{NiceTabular}{cc}
\CodeBefore
\rowcolor{gray!50}{1}
\rowcolors{2}{gray!25}{white}
\Body
Some values & Some values\\
Some values & Some values\\
Some values & Some values\\
Some values & Some values\\
\end{NiceTabular}
\end{document}


You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

You can also color the rows with any cycle of colors:

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{c}
\CodeBefore
\rowlistcolors{1}{red!15,blue!15,green!15}
\Body
Mathilde \\
Pierre \\
Paul \\
Juliette \\
Jacques \\
Antoine \\
Emmanuelle \\
\end{NiceTabular}

\end{document}


• You could elaborate on why you won't see the "thin white lines" (because the row colours are drawn across the entire row rather than on a cell-by-cell basis), but then also mention that at the first go-around, you need to compile three times. Subsequent compilations may require fewer runs if nothing changes in terms of the tabular position. However, just mentioning {NiceTabular} and boasting about the positive things only is being biased...
– Werner
Aug 27, 2021 at 19:16
• @Werner: In fact, with nicematrix, if you use \cellcolor{...} in two adjacent cells (with the same color), you won't see thin white lines. The package has been designed to avoid those thin lines even if it is on a cell-by-cell basis. For the compilations, I have said that several (several) compilations may be needed. Aug 27, 2021 at 20:07
• Ahhh, I see that now.
– Werner
Aug 27, 2021 at 20:36
• For the long lines can it add auto newline at the end of each line instead of continue Jan 23, 2022 at 14:26
• @alper: In {NiceTabular}, you can use columns of type p, b and m as in standard {tabular} (of the package array). Jan 23, 2022 at 16:07

An alternative solution with tblr and booktabs environments of tabularray package: alternating rows work well even if you have changed the vertical space between them.

\documentclass{article}

\usepackage{xcolor}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{tblr}{
colspec = {rccc},
row{odd} = {blue8},
row{even} = {azure8},
row{1} = {purple8},
}
\hline[2pt]
Thing & Value & Value & Value \\
\hline[1pt]
A & 1     & 2     & 3     \\
B & 1     & 2     & 3     \\
C & 1     & 2     & 3     \\
D & 1     & 2     & 3     \\
E & 1     & 2     & 3     \\
\hline[2pt]
\end{tblr}

\bigskip

\begin{booktabs}{
colspec = {rccc},
row{odd} = {blue8},
row{even} = {azure8},
row{1} = {purple8},
}
\toprule
Thing & Value & Value & Value \\
\midrule
A & 1     & 2     & 3     \\
B & 1     & 2     & 3     \\
C & 1     & 2     & 3     \\
\specialrule{2pt,teal5}{0pt}{2pt}
D & 1     & 2     & 3     \\
E & 1     & 2     & 3     \\
\bottomrule
\end{booktabs}

\end{document}


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