[Tex/LaTex] Command \rowcolors from colortbl does not work as expected

colortables

Could someone please explain to me how to apply \rowcolors? I found an answer here but to be honest it does not work for me, not is it well explained in the answer! I could not find description for \rowcolors in manual as well. To some extend, input parameters give expected results as below

\rowcolors{<row no. to start from}{odd rows colour}{even rows colour}

for example: \row{3}{lightgray!30}{white} would define 30% of lightgray for all odd rows and white for all even rows starting from 3rd row. But there are examples below where this formula does not work.

I made a MWE below. I was playing with different parameters for \rowcolors and I am probably missing something because the results are not what I though they would be.

Let's go through couple of examples where I changed 4th line in the code:

\rowcolors{1}{lightgray!30}{} % working

enter image description here

\rowcolors{2}{}{lightgray!30} % working

enter image description here

\rowcolors{3}{lightgray!30}{} % working

enter image description here

% Not working. Colours expected to start from 5th row 
\rowcolors{5}{lightgray!30}{}

enter image description here

% Not working. Colours expected to start from 4th row
\rowcolors{4}{}{lightgray!30}

enter image description here

I have an impression \rowcolors works only up to third row. Is it a bug or something that I should expect?

Thanks

\documentclass{article}
\usepackage{booktabs}
\usepackage[usernames,dvipsnames,svgnames,table]{xcolor}
\rowcolors{4}{lightgray!30}{} % To be changed in examples

%\rowcolor{<Start from>}{odd}{even}

\begin{document}

\begin{tabular}{ r l r l r l r l r }

\toprule
 & \multicolumn{2}{c}{\textbf{File 1}}
 & \multicolumn{2}{c}{\textbf{File 2}}
 & \multicolumn{2}{c}{\textbf{File 3}}
 & \multicolumn{2}{c}{\textbf{File 4}} \\
\cmidrule(lr){2-3}
\cmidrule(lr){4-5}
\cmidrule(lr){6-7}
\cmidrule(lr){8-9}
 & \textbf{H1}
 & \textbf{H2}
 & \textbf{H3}
 & \textbf{H4}
 & \textbf{H5} 
 & \textbf{H6}
 & \textbf{H7}
 & \textbf{H8} \\
\midrule
1 & A & B & C & D & E & F & G & H \\
2 & A & B & C & D & E & F & G & H \\
3 & A & B & C & D & E & F & G & H \\
4 & A & B & C & D & E & F & G & H \\
5 & A & B & C & D & E & F & G & H \\
6 & A & B & C & D & E & F & G & H \\
7 & A & B & C & D & E & F & G & H \\
8 & A & B & C & D & E & F & G & H \\
\midrule[\heavyrulewidth]
\rowcolor[gray]{1}
\multicolumn{9}{l}{Extra information} \\
\bottomrule

\end{tabular}

\end{document}

Best Answer

After some tests, the problem seems to come from \cmidrule, but it is not specific to booktabs, since \cline has the same problem.

On the other hand, the row number to start from must be an odd number. This means that if you set the starting row number to be 2, the first grey row will be number 3. So if you really want to start at row number 2, you have to start at number 1 and swap the colours.

Second strange feature: it seems that an odd number of \cmidrule/\clines between two rows counts as a supplementary row. Thus when you want to start at row number 3, you do start at row 3 if you have 0, 2 or 4 \cmidrules, but start at row number 2 if you have 1 or 3.

Here is a code showing these problems:

\documentclass{article}
\usepackage[usernames,dvipsnames,svgnames,table]{xcolor}
\rowcolors{4}{lightgray!30}{white} % To be changed in examples

%\rowcolor{<Start from>}{odd}{even}

\rowcolors{3}{lightgray!30}{white} % To be changed in examples
\usepackage{booktabs}

\begin{document}
\rowcolors{3}{lightgray!30}{white}
\begin{tabular}{ r l r l r l r l r }

\toprule
 & \multicolumn{2}{c}{\textbf{File 1}}
 & \multicolumn{2}{c}{\textbf{File 2}}
 & \multicolumn{2}{c}{\textbf{File 3}}
 & \multicolumn{2}{c}{\textbf{File 4}} \\
\cmidrule(lr){2-3}%
%\cmidrule(lr){4-5}
%\cmidrule(lr){6-7}
%\cmidrule(lr){8-9}
 & \textbf{H1}
 & \textbf{H2}
 & \textbf{H3}
 & \textbf{H4}
 & \textbf{H5}
 & \textbf{H6}
 & \textbf{H7}
 & \textbf{H8} \\
\midrule
1 & A & B & C & D & E & F & G & H \\
2 & A & B & C & D & E & F & G & H \\
3 & A & B & C & D & E & F & G & H \\
4 & A & B & C & D & E & F & G & H \\
5 & A & B & C & D & E & F & G & H \\
6 & A & B & C & D & E & F & G & H \\
7 & A & B & C & D & E & F & G & H \\
8 & A & B & C & D & E & F & G & H \\
\midrule[\heavyrulewidth]
\rowcolor[gray]{1}
\multicolumn{9}{l}{Extra information} \\
\bottomrule

\end{tabular}
\vspace{1cm}

\rowcolors{3}{lightgray!30}{white}
\begin{tabular}{ r l r l r l r l r }

\toprule
 & \multicolumn{2}{c}{\textbf{File 1}}
 & \multicolumn{2}{c}{\textbf{File 2}}
 & \multicolumn{2}{c}{\textbf{File 3}}
 & \multicolumn{2}{c}{\textbf{File 4}} \\
\cmidrule(lr){2-3}%
\cmidrule(lr){4-5}
%\cmidrule(lr){6-7}
%\cmidrule(lr){8-9}
 & \textbf{H1}
 & \textbf{H2}
 & \textbf{H3}
 & \textbf{H4}
 & \textbf{H5}
 & \textbf{H6}
 & \textbf{H7}
 & \textbf{H8} \\
\midrule
1 & A & B & C & D & E & F & G & H \\
2 & A & B & C & D & E & F & G & H \\
3 & A & B & C & D & E & F & G & H \\
4 & A & B & C & D & E & F & G & H \\
5 & A & B & C & D & E & F & G & H \\
6 & A & B & C & D & E & F & G & H \\
7 & A & B & C & D & E & F & G & H \\
8 & A & B & C & D & E & F & G & H \\
\midrule[\heavyrulewidth]
\rowcolor[gray]{1}
\multicolumn{9}{l}{Extra information} \\
\bottomrule

\end{tabular}
\vspace{1cm}

\rowcolors{3}{lightgray!30}{white}
\begin{tabular}{ r l r l r l r l r }

\toprule
 & \multicolumn{2}{c}{\textbf{File 1}}
 & \multicolumn{2}{c}{\textbf{File 2}}
 & \multicolumn{2}{c}{\textbf{File 3}}
 & \multicolumn{2}{c}{\textbf{File 4}} \\
\cmidrule(lr){2-3}%
\cmidrule(lr){4-5}
\cmidrule(lr){6-7}
%\cmidrule(lr){8-9}
 & \textbf{H1}
 & \textbf{H2}
 & \textbf{H3}
 & \textbf{H4}
 & \textbf{H5}
 & \textbf{H6}
 & \textbf{H7}
 & \textbf{H8} \\
\midrule
1 & A & B & C & D & E & F & G & H \\
2 & A & B & C & D & E & F & G & H \\
3 & A & B & C & D & E & F & G & H \\
4 & A & B & C & D & E & F & G & H \\
5 & A & B & C & D & E & F & G & H \\
6 & A & B & C & D & E & F & G & H \\
7 & A & B & C & D & E & F & G & H \\
8 & A & B & C & D & E & F & G & H \\
\midrule[\heavyrulewidth]
\rowcolor[gray]{1}
\multicolumn{9}{l}{Extra information} \\
\bottomrule

\end{tabular}

\end{document} 

enter image description here