Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 42 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.

share|improve this answer
1  
You beat me by 20 seconds. +1 –  lockstep Nov 14 '10 at 13:24
add comment

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}
share|improve this answer
add comment

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

share|improve this answer
    
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
6  
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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