# Tables: Colouring odd/even mixed up

I have a huge document (around 100 pages) and a lot of tables in it. I used

\usepackage[table]{xcolor}
\definecolor{lightblue}{rgb}{0.93,0.95,1.0}
\rowcolors{1}{white}{lightblue} % odd = white, even = lightblue


to highlight even rows with lightblue. This works pretty well and looks pretty as good as I need it to look like. But now I'm about to finish this document and I notice that some tables mix up the colours of odd and even rows. Some tables are displaying the even rows in lightblue and some the odd rows. Like in this screenshot below.

Minimal working example for the screenshot above:

\documentclass{report}
\usepackage[english]{babel}
\usepackage[table]{xcolor}
\definecolor{lightblue}{rgb}{0.93,0.95,1.0}
\rowcolors{1}{white}{lightblue}

\begin{document}
\begin{table}[htb]
\centering
\begin{tabular}{r|r|r|r}
N & R & C &   T \\ \hline % odd
5 & 8 & 5 & 640 \\        % even -- lightblue (OK)
6 & 8 & 4 & 640 \\        % odd
7 & 7 & 7 & 949 \\        % even -- lightblue (OK)
\end{tabular}
\end{table}
\begin{table}[htb]
\centering
\begin{tabular}{r|r|r|r}
N & R & C &   T \\ \hline % odd -- lightblue (wrong)
8 & 8 & 4 & 444 \\        % even
9 & 8 & 4 & 124 \\        % odd -- lightblue (wrong)
10 & 8 & 4 & 672 \\        % even
\end{tabular}
\end{table}
\end{document}


I don't get it, what's wrong with that code? The default behaviour should be that always the first row is white and the second row is lightblue (and that's working with 80% of my tables).

-

The reason is the counting of the rows. The command start counting the rows at the beginning of the command \rowcolors. So every row of your document take in count.

You can reset the counter with the following code which uses the command preto provided by etoolbox.

\usepackage{etoolbox}
\makeatletter
\preto\tabular{\global\rownum=\z@}
\makeatother


Instead of using etoolbox you can also use the package xpatch:

\usepackage{xpatch}
\makeatletter
\xpreto\tabular{\global\rownum=\z@}
\makeatother


Here the complete MWE:

\documentclass{report}
\usepackage[english]{babel}
\usepackage[table]{xcolor}
\definecolor{lightblue}{rgb}{0.93,0.95,1.0}
\rowcolors{1}{white}{lightblue}
\usepackage{etoolbox}
\makeatletter
\preto\tabular{\global\rownum=\z@}
\makeatother
\begin{document}
\begin{table}[htb]
\centering
\begin{tabular}{r|r|r|r}
N & R & C &   T \\ \hline % odd
5 & 8 & 5 & 640 \\        % even -- lightblue (OK)
6 & 8 & 4 & 640 \\        % odd
7 & 7 & 7 & 949 \\        % even -- lightblue (OK)
\end{tabular}
\end{table}
\begin{table}[htb]
\centering
\begin{tabular}{r|r|r|r}
N & R & C &   T \\ \hline % odd -- lightblue (wrong)
8 & 8 & 4 & 444 \\        % even
9 & 8 & 4 & 124 \\        % odd -- lightblue (wrong)
10 & 8 & 4 & 672 \\        % even
\end{tabular}
\end{table}
\end{document}

-
this works like a charm, thanks alot! –  donSchoe Jun 3 '12 at 13:13

I faced the same problem, followed your solution and it didn't work as might be expected. To be specific, the above solution still results in wrong row color started at certain row, e.g. with the command

\rowcolors{3}{blue}{red}


So I experimented to find the real working solution:

...
\usepackage{etoolbox}
\appto\endtabular{\global\rownum=0}
...


Have a try.

-
I'm curious why the other answer didn't work for you... –  Werner Sep 30 '14 at 19:17
I don't think that this is an answer –  Christian Hupfer Sep 30 '14 at 19:30
Try it and tell me if I'm doing a mistake. –  bydefault Sep 30 '14 at 20:11
Well the other solution worked well for me. What's the issue here? –  donSchoe Sep 30 '14 at 22:54