TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to create a table where the background color of the rows changes every n>1 rows. I have seen in wikibooks how alternate rows can have different colors and I have looked at one xcolor documentation I found online at math.washginton.edu but I can't figure out how I can have the rowcolor to change every n rows, say every 5 rows.

Also, would it possible to define some sequence, say {4,9,11..} which will tell LaTeX at which row # to switch colors? That way if my observations are unbalanced then I can still highlight the backgrounds differently. What I mean by this is that if I have 3 rows of data from model-1 and 5 rows of data from Model-2 and 2 rows of data from Model-3 then the first 3 rows of my table can be white, the next 5 can be gray and the next 2 rows can be white again and so on..for improved readability, which I need because I have a ton of numbers, which I guess most tables have. :)

Right now I am laboriously writing \rowcolor{} in front of any row that I'd like colored.

Any help will be greatly appreciated.

share|improve this question
up vote 16 down vote accepted

This shows one way of adding a counter that increments every row and then having an arbitrary set of tests to switch colours on some condition. This starts off with no background switches to green on row 4 stays green until row 7 when it switches to blue, goes back to no background at row 13, then cycles round again every 20 rows.

enter image description here




share|improve this answer
Sweet!! Thanks a lot David, this is awesome!! – Amatya Apr 1 '12 at 23:16

If you load the xcolor package with the table option you also have access to the rowcolors command.

the syntax is \rowcolors{<starting row index>}{<odd row color>}{<even row color>}


if you use the tabu package you can use the \taburowcolors command.


\taburowcolors {green!40..green!40}
\taburowcolors {blue!40..blue!40}


which gives:

enter image description here

share|improve this answer
HI ArTourter, Thanks. I already know how to use that command and the wikibooks link I gave in my original post has an example using precisely that command. That command switches colors every n=1 rows but I am looking to be able to switch colors for n>1 and possibly "n" not constant. – Amatya Apr 1 '12 at 21:23
Hi ArTourter, This is great! Thanks a lot! – Amatya Apr 1 '12 at 23:17

Your Answer


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.