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.

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.

screenshot of two tables

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

share|improve this question

1 Answer 1

up vote 16 down vote accepted

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}
share|improve this answer
    
this works like a charm, thanks alot! –  vertoe Jun 3 '12 at 13:13

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.