6

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.

David Carlisle
  • 757,742
hpesoj626
  • 17,282

2 Answers2

5

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.

David Carlisle
  • 757,742
  • 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 09 '12 at 11:34
  • 1
    @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 09 '12 at 11:53
  • 1
    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 09 '12 at 12:22
  • @DavidCarlisle The problem with colorbtl seems to persist. Have you maybe had a chance to look into how one could fix/patch colortbl? – FlorianL May 06 '19 at 11:19
  • @FlorianL I'd forgotten completely about this, maybe an issue at https://github.com/davidcarlisle/dpctex/issues would stop it being lost... – David Carlisle May 06 '19 at 11:23
4

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:

enter image description here

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}

Peter Grill
  • 223,288