Reset alternating row colors using xcolor and longtable

automationcolorlongtablerowcolortables

I am attempting to automate alternating row colors in a longtable environment. My rows alternate between gray and white, and the alternating persists across pagebreaks. My issue is that I would like the first non-head row after a pagebreak to reset the alternating colors and always be gray again.

Here is a (very M)WE. xelatex is necessary for other things I am doing in case it breaks any solutions.

% !TeX program = xelatex
\documentclass{article}
\usepackage{longtable}
\usepackage[table]{xcolor}
\begin{document}
{\rowcolors{5}{white}{gray!25}
 \begin{longtable}{ccc}
  Talks.&&\\\endfirsthead
  Talks, continued.&&\\\endhead
  \multicolumn{3}{r}{Continued on next page.}\\\endfoot
  \endlastfoot

  Talk A&Place A&Date A\\
  Talk B&Place B&Date B\\
  Talk C&Place C&Date C\\
  \newpage %Only a contrivance to see a pagebreak; in practice the pagebreak will be automatically determined.
  Talk D&Place D&Date D\\
  Talk E&Place E&Date E\\
  Talk F&Place F&Date F\\
 \end{longtable}}
\end{document}

I'd like rows D and F to be gray, and row E to be white; currently the opposite is true. I'd like to have this done automatically, so that as I add new rows and the pagebreak occurs in different locations, it automatically assigns the top non-head row to be gray and alternates from there.

Best Answer

I will give an alternative solution with longtblr environment of the new LaTeX3 package tabularray:

\documentclass{article}
\usepackage[a6paper,margin=10mm]{geometry}
\usepackage{xcolor}
\usepackage{tabularray}
\DefTblrTemplate{firsthead}{default}{%
  \centering\InsertTblrText{caption}.\par
}
\DefTblrTemplate{middlehead,lasthead}{default}{%
  \centering\InsertTblrText{caption}, continued.\par
}
\colorlet{realoddcolor}{gray!25}
\colorlet{realevencolor}{white}
\ExplSyntaxOn
\prg_generate_conditional_variant:Nnn \str_if_eq:nn { en } { TF }
\cs_gset_eq:NN \SavedNewPage \newpage
\cs_gset_eq:NN \SavedColor \color
\NewDocumentCommand \MyNewPage { }
  {
    \SavedNewPage
    \cs_set_eq:NN \color \MyColor
  }
\NewDocumentCommand \MySavedColor { o m }
  {
    \IfNoValueTF {#1} { \SavedColor{#2} } { \SavedColor[#1]{#2} }
  }
\NewDocumentCommand \MyColor { o m } 
  {
     \str_if_eq:enTF {#2} {oddcolor}
       {
         \xglobal \colorlet { oddcolor } { realoddcolor }
         \xglobal \colorlet { evencolor } { realevencolor }
         \MySavedColor [#1] { #2 }
         \cs_gset_eq:NN \color \SavedColor
       }
       {
         \str_if_eq:enTF {#2} {evencolor}
           {
             \xglobal \colorlet { evencolor } { realoddcolor }
             \xglobal \colorlet { oddcolor } { realevencolor }
             \MySavedColor [#1] { #2 }
             \cs_gset_eq:NN \color \SavedColor
           }
           {
             \MySavedColor [#1] { #2 }
           }     
       } 
  }  
\NewDocumentCommand \ResetRowColor {}
  {
    \cs_set_eq:NN \newpage \MyNewPage
    \cs_set_eq:NN \color \MyColor
  }
\ExplSyntaxOff

\begin{document}

\begingroup
\ResetRowColor
 \begin{longtblr}[
   caption = {Talks},
   entry = none,
   label = none,
 ]{
   colspec = {ccc},
   row{odd} = {oddcolor},
   row{even} = {evencolor},
 }
  Talk A&Place A&Date A\\
  Talk B&Place B&Date B\\
  Talk C&Place C&Date C\\
  Talk D&Place D&Date D\\
  Talk E&Place E&Date E\\
  Talk F&Place F&Date F\\
  Talk A&Place A&Date A\\
  Talk B&Place B&Date B\\
  Talk C&Place C&Date C\\
  Talk D&Place D&Date D\\
  Talk E&Place E&Date E\\
  Talk F&Place F&Date F\\
  Talk A&Place A&Date A\\
  Talk B&Place B&Date B\\
  Talk C&Place C&Date C\\
  Talk D&Place D&Date D\\
  Talk E&Place E&Date E\\
  Talk F&Place F&Date F\\
  Talk A&Place A&Date A\\
  Talk B&Place B&Date B\\
  Talk C&Place C&Date C\\
  Talk D&Place D&Date D\\
  Talk E&Place E&Date E\\
  Talk F&Place F&Date F\\
  Talk A&Place A&Date A\\
  Talk B&Place B&Date B\\
  Talk C&Place C&Date C\\
  Talk D&Place D&Date D\\
  Talk E&Place E&Date E\\
  Talk F&Place F&Date F\\
 \end{longtblr}
\endgroup

\end{document}

enter image description here

Related Question