17

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

18

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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