147

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.

7 Answers 7

175

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}
\usepackage[table]{xcolor}    % loads also »colortbl«

\begin{document}
  \rowcolors{2}{gray!25}{white}
  \begin{tabular}{cc}
    \rowcolor{gray!50}
    Table head & Table head\\
    Some values & Some values\\
    Some values & Some values\\
    Some values & Some values\\
    Some values & Some values\\
  \end{tabular}
\end{document}

enter image description here

The particular package manuals have the details.

6
  • If there are some other languages text in the table, please do not use the the order "\usepackage[latin1]{inputenc}"
    – ccy
    Commented Aug 27, 2016 at 8:52
  • 3
    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
    Commented 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'
    Commented Oct 18, 2019 at 15:21
  • 1
    Can the gray color start from the second row instead of first one ?
    – alper
    Commented Nov 19, 2020 at 22:27
  • 2
    @alper change the order of colors: \rowcolors{2}{white}{gray!25} Commented Nov 29, 2023 at 9:35
35

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

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

6
  • 43
    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 :-) Commented Jun 6, 2012 at 7:39
  • 1
    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
    Commented Apr 12, 2013 at 13:16
  • 1
    Another approach which might be worth trying in this case (xcolor already loaded by another package): just load colortbl instead of xcolor. Commented Feb 6, 2015 at 17:33
  • 2
    @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
    Commented May 17, 2017 at 14:45
  • 3
    @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
    Commented Mar 22, 2018 at 10:49
5

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
Table head & Table head\\
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).

Output of the firstcode

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}

Output of the second code

6
  • 1
    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
    Commented 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. Commented Aug 27, 2021 at 20:07
  • Ahhh, I see that now.
    – Werner
    Commented Aug 27, 2021 at 20:36
  • For the long lines can it add auto newline at the end of each line instead of continue
    – alper
    Commented 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). Commented Jan 23, 2022 at 16:07
2

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.

2

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     \\
\addlinespace[2pt]
      B & 1     & 2     & 3     \\
\addlinespace[2pt]
      C & 1     & 2     & 3     \\
\specialrule{2pt,teal5}{0pt}{2pt}
      D & 1     & 2     & 3     \\
\addlinespace[3pt]
      E & 1     & 2     & 3     \\
\bottomrule
\end{booktabs}

\end{document}

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .