0

I am trying to color every other row in a longtable using the xcolor package with the table option as shown in this MWE (landscape and 17pt options are only used to shorten the MWE):

\documentclass[oneside,landscape,17pt]{memoir}
\usepackage{longtable}
\usepackage[table]{xcolor}
\definecolor{tableRows1}{gray}{0.9}
\definecolor{tableRows2}{gray}{0.4}

\begin{document}

\rowcolors{4}{tableRows1}{tableRows2}
\begin{longtable}{cc}
    \hiderowcolors
    \toprule
    Column 1    &   Column 2    \\
    \midrule
    \endfirsthead
    %
    \multicolumn{2}{c}{\textsl{Continued from previous page}}\\
    \toprule
    Column 1    &   Column 2    \\
    \midrule
    \endhead
    %
    \bottomrule
    \multicolumn{2}{c}{\textsl{Continued on next page}}
    \endfoot
    %
    \bottomrule
    \multicolumn{2}{c}{\textsl{Last foot}}
    \endlastfoot
    %
    \showrowcolors
    11  &   12 (\number\rownum) \\
    21  &   22 \\
    31  &   32 \\
    41  &   42 \\
    51  &   52 \\
    11  &   12 \\
    21  &   22 \\
    31  &   32 \\
    41  &   42 \\
    51  &   52 \\
    11  &   12 \\
    21  &   22 \\
    31  &   32 \\
    41  &   42 \\
    51  &   52 \\
    11  &   12 \\
    21  &   22 \\
    31  &   32 \\
    41  &   42 \\
    51  &   52 \\
    11  &   12 \\
    21  &   22 \\
    31  &   32 \\
    41  &   42 \\
    51  &   52 \\
\end{longtable}

\end{document}

Output of MWE.

As you can see, the coloring begins on the first line (not including the column descriptions) although I have specified it to begin at row 4 (\rowcolors{4}{tableRows1}{tableRows2}). Clearly some calculations are wrong(?) since row 1 is viewed as row 20 as shown in the parenthesis in row 1, column 2.

Why is that? And how do I change it to get the "correct" behavior?

Note: The offset roughly corresponds to the number of rows in the .tex-document from the beginning of the longtable to the first line of data, i.e. header and footer specification might be counted as part of the table. Commenting for instance the \multicolumn lines will reduce the number (20) on the first row.

  • 2
    You can reset the rownum counter: \noalign{\global\rownum=1}\showrowcolors – Ulrike Fischer Mar 30 '17 at 13:08
  • the longtable head and foot are set at the start of the table internally and then just extracted and re-inserted as the pages are broken, so the easiest thing to do is just count the way you indicate. – David Carlisle Mar 30 '17 at 14:10
2

You can reset the counter after the processing of header rows:

\documentclass[oneside,landscape,17pt]{memoir}
\usepackage{longtable}
\usepackage[table]{xcolor}
\definecolor{tableRows1}{gray}{0.9}
\definecolor{tableRows2}{gray}{0.4}

\begin{document}

\rowcolors{4}{tableRows1}{tableRows2}
\begin{longtable}{cc}
    \hiderowcolors
    \toprule
    Column 1    &   Column 2    \\
    \midrule
    \endfirsthead
    %
    \multicolumn{2}{c}{\textsl{Continued from previous page}}\\
    \toprule
    Column 1    &   Column 2    \\
    \midrule
    \endhead
    %
    \bottomrule
    \multicolumn{2}{c}{\textsl{Continued on next page}}
    \endfoot
    %
    \bottomrule
    \multicolumn{2}{c}{\textsl{Last foot}}
    \endlastfoot
    %
    \noalign{\global\rownum=1}%<---
    \showrowcolors
    11  &   12 (\number\rownum) \\
    21  &   22 \\
    31  &   32 \\
    41  &   42 \\
    51  &   52 \\
    11  &   12 \\
    21  &   22 \\
    31  &   32 \\
    41  &   42 \\
    51  &   52 \\
    11  &   12 \\
    21  &   22 \\
    31  &   32 \\
    41  &   42 \\
    51  &   52 \\
    11  &   12 \\
    21  &   22 \\
    31  &   32 \\
    41  &   42 \\
    51  &   52 \\
    11  &   12 \\
    21  &   22 \\
    31  &   32 \\
    41  &   42 \\
    51  &   52 \\
\end{longtable}

\end{document}

enter image description here

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.