如何使用 xcolor 和 longtable 重置交替行颜色?

如何使用 xcolor 和 longtable 重置交替行颜色?

我正在尝试在长表环境中自动交替行颜色。我的行在灰色和白色之间交替,并且交替在分页符之间持续。我的问题是,我希望分页符后的第一个非头行重置交替颜色并始终再次变为灰色。

这是一个(非常 M)WE。xelatex 对于我正在做的其他事情是必要的,以防它破坏任何解决方案。

% !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}

我希望 D 行和 F 行显示为灰色,E 行显示为白色;目前情况正好相反。我希望自动完成此操作,这样当我添加新行并且分页符出现在不同位置时,它会自动将顶部非标题行指定为灰色并从那里交替显示。

答案1

longtblr我将提供一个使用新 LaTeX3 包环境的替代解决方案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}

在此处输入图片描述

相关内容