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.

  • 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
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.

  • 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
  • 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 9 '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 9 '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 6 at 11:19
  • @FlorianL I'd forgotten completely about this, maybe an issue at github.com/davidcarlisle/dpctex/issues would stop it being lost... – David Carlisle May 6 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}
  • 1
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.