xcolor and everycr-Problem

I'm trying to insert some \noalign-code in every row of a tabular by using colortbl and \everycr or \CT@everycr. In most cases it works fine, but not if there is a \rowcolors-command (from xcolor) after my command: \rowcolors uses this code:

\CT@everycr{\@rowc@lors\the\everycr}


This doesn't expand \the\everycr and so whatever I put in \everycr is always ignored. Is this a bug or did I overlook something?

\documentclass{article}
\usepackage{array}
\usepackage[table]{xcolor}

\begin{document}
\makeatletter
\newcommand\test{\noalign{\hrule height 0.1cm width 5cm}}
\rowcolors{0}{green}{blue}
%Works after/without \rowcolors:
\CT@everycr\expandafter{\expandafter\test\the\CT@everycr}
\begin{tabular}[t]{ll}
foo & foo\\
foo & foo\\
\multicolumn{2}{l}{blub}
\end{tabular}

\bigskip

%this fails:
\everycr\expandafter{\expandafter\test\the\everycr}
\rowcolors{0}{red}{green}
%\CT@everycr{\@rowc@lors\the\everycr}%inserted by @rowcolors

%it would work if \rowc@lors would use this:
%\CT@everycr\expandafter{\expandafter\@rowc@lors\the\everycr}%
\makeatother

\begin{tabular}[t]{ll}
foo & foo\\
foo & foo\\
\multicolumn{2}{l}{blub}
\end{tabular}
\end{document}

-
It needs to be unexpanded so that things that are added later to \everycr get used, otherwise it would only use things that were in \everycr at the time of the definition. I'll need to look later to see what's going wrong here. –  David Carlisle Jan 4 '13 at 16:36
@DavidCarlisle: The problem seems to be that tabular is emptying \everycr at the start. If I put \global\everycr{\test} at the start of the first row at least some of my lines reappears. –  Ulrike Fischer Jan 4 '13 at 17:13
yes latex kernel (and array package) use \ialign to initialise tabular. Which means I suppose that formally LaTeX doesn't support use of \everycr :-) However it also means that the code in colortbl (and then xcolor) that carefully re-inserts \the\everycr isn't doing anything useful. Hmmmm –  David Carlisle Jan 4 '13 at 17:25
@DavidCarlisle Yes that sums it up ;-). Btw: It wouldn't work either \rowcolors would use \CT@everycr\expandafter{\the\CT@everycr\@rowc@lors} instead (to reuse at least \CT@everycr): The content inserted by colortbl disturbs the alternating colors. I will have to find some other way to prevent rowcolors to kill my code. –  Ulrike Fischer Jan 5 '13 at 13:08
I ran out of time yesterday, hope to look at this later (but first I need to make bm work with unicode-math :-) –  David Carlisle Jan 5 '13 at 13:50

Based on David's comments, here's a hack for being able to use \everycr in tabular:

\documentclass{article}
\usepackage{array,etoolbox}
\usepackage[table]{xcolor}

\newenvironment{etabular}[1]
{\patchcmd{\ialign}{\everycr{}}{}{}{}%
\everycr{#1}%
\tabular}
{\endtabular}

\newcommand\test{\noalign{\hrule height 0.1cm width 5cm}}

\begin{document}
\rowcolors{0}{green}{blue}
\begin{etabular}{\test}[t]{ll}
foo & foo\\
foo & foo\\
\multicolumn{2}{c}{blub}
\end{etabular}
\end{document}


I wouldn't patch globally \ialign; however this would make all nested tabulars the same \everycr. Use \begin{etabular}{}[pos]{columns} for nested tabulars inside an etabular.

For longtable you can use a very similar patch:

\makeatletter
\newenvironment{elongtable}[1]
{\patchcmd{\LT@array}{\everycr{}}{}{}{}%
\everycr{#1}%
\longtable}
{\endlongtable}
\makeatother


since \LT@array is responsible of saying \everycr{}.

-
This doesn't work if I replace \tabular by longtable but I don't have currently the time to find out why. (The actual problem which led to the question was that I wanted to reset the rowcolors in a longtable at the start of a new page). –  Ulrike Fischer Feb 5 '13 at 13:34
@UlrikeFischer \LT@array says \everycr{}. The patch is just the same. –  egreg Feb 5 '13 at 14:09
Yes the patch works - and shows that longtable doesn't handle the \noalign as expected: The line are before the first row. Tabulars are complicated beasts ;-) –  Ulrike Fischer Feb 5 '13 at 14:32
@UlrikeFischer There is a rule before the table (and it's not too much of a surprise), but also an additional one a the end, which has possibly to do with what longtable has to do at breaks. –  egreg Feb 5 '13 at 14:41