colortbl and amsmath conflict

I have a problem with colortbl and amsmath. Is someone here aware of a conflict between these two packages?

\documentclass[10pt]{article}
\usepackage{amsmath}
%\usepackage{colortbl}

\begin{document}

\begin{tabular}[b]{cc}
Correct & Incorrect\\
\parbox[b][]{0.4\linewidth}{
\begin{align*}
3(2x-5)^2&=3(4x^2-20x+25)\\
&=12x^2-60x+75
\end{align*}
} &
\parbox[b][]{0.5\linewidth}{
\begin{align*}
3(2x-5)^2&=(6x-15)^2\\
&=36x^2-180x+225
\end{align*}
}\\
\end{tabular}

\end{document}


This compiles fine but when I uncomment \usepackage{colortbl} it throws errors.

Here is the error log.

! Misplaced \omit.
\math@cr@@@ ...@ \@ne \add@amps \maxfields@ \omit
\kern -\alignsep@ \iftag@ ...
l.14 }
&
I expect to see \omit only after tab marks or the \cr of
an alignment. Proceed, and I'll ignore this case.

! Misplaced \omit.
\math@cr@@@ ...@ \@ne \add@amps \maxfields@ \omit
\kern -\alignsep@ \iftag@ ...
l.14 }
&
I expect to see \omit only after tab marks or the \cr of
an alignment. Proceed, and I'll ignore this case.


I have to mention that the problem seems to crop up only when I use align inside the parbox in the table.

-
It seems fine if you use aligned instead of align. –  Peter Grill Dec 9 '12 at 8:54
Yes. Thanks for the info. Haven't tried that yet but I have noted that indeed it seems to have problems only with align. –  hpesoj626 Dec 9 '12 at 8:56

This is groping in the dark a bit, because I couldn't find anything significant by tracing, but then these alignment constructs are so fragile it really goes over my head.

By staring hard at the colortbl code, I cooked up the following patch. Please try the modified MWE:

\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{colortbl}

%%%%%%%%%% HERE COMES THE PATCH %%%%%%%%%%
\usepackage{etoolbox}

\makeatletter
\pretocmd\start@align
{%
\let\everycr\CT@everycr
\CT@start
}{}{}
\apptocmd{\endalign}{\CT@end}{}{}
\makeatother
%%%%%%%%%% END PATCH %%%%%%%%%%

\begin{document}

\begin{tabular}[b]{cc}
Correct & Incorrect\\
\parbox[b][]{0.4\linewidth}{
\begin{align*}
3(2x-5)^2&=3(4x^2-20x+25)\\
&=12x^2-60x+75
\end{align*}
} &
\parbox[b][]{0.5\linewidth}{
\begin{align*}
3(2x-5)^2&=(6x-15)^2\\
&=36x^2-180x+225
\end{align*}
}\\
\end{tabular}

\end{document}


I don't know whether this has any averse effects on the colortblness of the outer tabular though. Please test.

-
Thanks for taking the time to patching this. This works very well. And it doesn't mess with the part I am using colortbl for. I am accepting this as this answers my question but I have seen the wisdom of @egreg's advice about using aligned. Something's ought to break when things are not used properly. Just in this case, I did not know about it :) –  hpesoj626 Dec 9 '12 at 11:34
@hpesoj626 OTOH there is no reason to assume this shouldn't work. Everybody not experienced with the way LaTeX "programming" works would assume the \parbox is sufficient to "encapsulate" the align environment sufficiently from the influence of the tabular. It's a real disease of TeX's dynamic scoping and execution model that you get this kind of "overwrite" effect in almost every place where you nest things. –  Stephan Lehmke Dec 9 '12 at 11:53
Yes making colortbl mess with everycr was probably a mistake, it simplified the colortbl coding but makes it harder not to break nested alignments. I haven't really time to look today but the patch looks about right (or slower but possibly better) instead of patching align to reset everycr could get colortbl to reset it every cell so that any nested halign would work –  David Carlisle Dec 9 '12 at 12:22

I am not sure what the actual source of the problem is, but one way to work around this problem is to just use aligned instead:

Notes:

• As egreg pointed out, using a \parbox is no longer necessary when using the aligned environment.
• I would not recommend using an = when things are not equal as is the case for the incorrect column. I have not made that change here as then you need to decide what to do with the following equal.

Code:

\documentclass[10pt]{article}
\usepackage{colortbl}
\usepackage{amsmath}

\begin{document}
\begin{center}
\begin{tabular}[b]{cc}
Correct & Incorrect\\
\begin{aligned} 3(2x-5)^2&=3(4x^2-20x+25)\\ &=12x^2-60x+75 \end{aligned}
&
\begin{aligned} 3(2x-5)^2&=(6x-15)^2\\ &=36x^2-180x+225 \end{aligned}
\\
\end{tabular}
\end{center}
\end{document}

-
If I use \neq instead then I have to move the entry under the correct heading :) –  hpesoj626 Dec 9 '12 at 9:20
Moreover, aligned makes a box and accepts the options [t] or [b], so you can dispense with the \parbox –  egreg Dec 9 '12 at 10:52