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.

up vote 130 down vote accepted

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.

  • 1
    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

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

  • Thanks, I could reproduce the clash, but had it not in first place, because I added it at the end anyways. But that was pure chance, so others might run into this problem. – Mnementh Nov 14 '10 at 18:14
  • 27
    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
  • 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 Apr 12 '13 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. – Paul Paulsen Feb 6 '15 at 17:33
  • 1
    @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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.