Does something like \rowcolors exist for groups of rows?

I have taken a look at the code for \rowcolors and adapted it to allow groups greater one. The new command \groupedRowColors takes four mandatory and one optional argument:

  • The first mandatory argument is the group size, the number of consecutive lines to have the same color.
  • The second mandatory argument is the start line, all lines above will not be colored. This is useful to exclude a header.
  • The third and fourth mandatory arguments are the colors to be used.

If you do not like where the grouping starts or would like to start with the other color you can use the optional argument to shift the colors. Please note that integer division in \numexpr rounds the result so the required shift may be different from what one might expect. (See this question.)

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{booktabs}

\makeatletter
\newcommand{\groupedRowColors}[5][0]{% [#1: offset], #2: group size, #3: start line, #4: color 1, #5: color 2
    % copied from xcolor.sty
    \global\rownum=\z@
    \global\@rowcolorstrue
    \@ifxempty{#4}%
        {\def\@oddrowcolor{\@norowcolor}}%
        {\def\@oddrowcolor{\gdef\CT@row@color{\CT@color{#4}}}}%
    \@ifxempty{#5}%
        {\def\@evenrowcolor{\@norowcolor}}%
        {\def\@evenrowcolor{\gdef\CT@row@color{\CT@color{#5}}}}%
    % simplified (no check for \if@rowcmd)
    \def\@rowcolors{%
        \if@rowcolors
            \noalign{%
                \relax
                \ifnum\rownum<#3
                    \@norowcolor
                % I have changed this check:
                \else \ifodd \numexpr (\rownum-#1)/#2\relax
                    \@oddrowcolor
                \else
                    \@evenrowcolor
                \fi \fi
            }%
        \fi
    }%
    \CT@everycr{\@rowc@lors\the\everycr}%
    \ignorespaces
}
\makeatother


\begin{document}
\groupedRowColors{3}{2}{green!10}{blue!10}
\begin{tabular}{cc}
    \toprule
        Column~1 & Column~2 \\
    \midrule
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
    \bottomrule
\end{tabular}
\end{document}

enter image description here


EDIT: example with longtable

\documentclass{article}
\usepackage[a6paper, landscape]{geometry}

\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{longtable}

\makeatletter
\newcommand{\groupedRowColors}[5][0]{% [#1: offset], #2: group size, #3: start line, #4: color 1, #5: color 2
    % copied from xcolor.sty
    \global\rownum=\z@
    \global\@rowcolorstrue
    \@ifxempty{#4}%
        {\def\@oddrowcolor{\@norowcolor}}%
        {\def\@oddrowcolor{\gdef\CT@row@color{\CT@color{#4}}}}%
    \@ifxempty{#5}%
        {\def\@evenrowcolor{\@norowcolor}}%
        {\def\@evenrowcolor{\gdef\CT@row@color{\CT@color{#5}}}}%
    % simplified (no check for \if@rowcmd)
    \def\@rowcolors{%
        \if@rowcolors
            \noalign{%
                \relax
                \ifnum\rownum<#3
                    \@norowcolor
                % I have changed this check:
                \else \ifodd \numexpr (\rownum-#1)/#2\relax
                    \@oddrowcolor
                \else
                    \@evenrowcolor
                \fi \fi
            }%
        \fi
    }%
    \CT@everycr{\@rowc@lors\the\everycr}%
    \ignorespaces
}
\makeatother


\begin{document}
\groupedRowColors{3}{0}{green!10}{blue!10}
\begin{longtable}{ c c }
    \hiderowcolors
    \toprule
        Column~1 & Column~2 \\
    \midrule
\endfirsthead
        Column~1 & Column~2 \\
    \midrule
\endhead
    \midrule
        \multicolumn{2}{r@{}}{\footnotesize(to be continued on next page)}
\endfoot
    \bottomrule
\endlastfoot
    \showrowcolors
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
        (\the\rownum,0) & (\the\rownum,1) \\
\end{longtable}
\end{document}

(The definition of \groupedRowColors is unchanged.)

enter image description here


The environment {NiceTabular} of nicematrix provides tools to color cells, rows and columns. The resultat is very good in all the PDF viewers, at all levels of zoom.

\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{nicematrix}

\begin{document}
\begin{NiceTabular}{cc}%
  [ 
    code-before = \rowcolor{green!10}{2-4,8-10,14-15}
                  \rowcolor{blue!10}{5-7,11-13}
  ]
    \toprule
        Column~1 & Column~2 \\
    \midrule
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
    \bottomrule
\end{NiceTabular}
\end{document}

However you need several compilations (because nicematrix uses PGF/Tikz nodes) and nicematrix is not compatible with longtable.

Output of the above code


It's also possible to write a command \MyColorRows to do the job automatically.

The following command \MyColorRows takes in four arguments:

  • the number of the first colored row (in the example 2) ;
  • the number of successives row to color with the same color (in the example 3) ;
  • the first color ;
  • the second color.

The command \MyColorRows must be used in the code-before (as does \rowcolor of nicematrix).

\documentclass{article}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{nicematrix}

\ExplSyntaxOn
\NewDocumentCommand \MyRowColors { m m m m }
  {
    \int_step_inline:nnn { #1 } { \value { iRow } } 
      {
        \int_if_even:nTF { \int_div_truncate:nn { ##1 - #1 } { #2 } }
          { \rowcolor { #3 } { ##1 } }
          { \rowcolor { #4 } { ##1 } }
      }
  }
\ExplSyntaxOff

\begin{document}
\begin{NiceTabular}{cc}[code-before = \MyRowColors{2}{3}{green!10}{blue!10}]
    \toprule
        Column~1 & Column~2 \\
    \midrule
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
        (\arabic{iRow},0) & (\arabic{iRow},1) \\
    \bottomrule
\end{NiceTabular}
\end{document}

The output is the same.