Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to put a \begin{align*} and \end{align*} for every entry at columns 2 and 3 of my table. But I'm getting errors that seem to say my environment markers are mismatched. I'm sure I've got my braces properly matched for the > and < table preambles. What could be the problem? Here's a minimal working example http://pastebin.tlhiv.org/wPmuJulo

I'll quote it here:

\documentclass{memoir}

\usepackage{amsmath}
\usepackage{array}
\usepackage{longtable}
\usepackage{booktabs}
\newcounter{rowno}
\setcounter{rowno}{0}

\begin{document}
  \begin{longtable}[l]{%
    >{\stepcounter{rowno}\therowno.}c%
    >{\begin{align*}}%
      m{0.5\textwidth}%
      <{\end{align*}}%
    >{\begin{align*}}%
      m{0.5\textwidth}%
      <{\end{align*}}%
    }
    %m{0.5\textwidth}%
    %m{0.5\textwidth}%
    %\multicolumn{1}{r}{\textbf{No.}} & \textbf{Question} & \textbf{Answer} \\\hline
    & E=mc^2 & E=mc^2
  \end{longtable}
\end{document}

This results in the error:

error| \begin{align*} on input line 23 ended by \end{longtable}

What I expect from the > and < table preamble

For example, I have a table preamble that looks like this. This is a one-column table:

\begin{longtable}{>{\begin{foobar}} l <{\end{foobar}}}
  foo \\
  bar \\
  baz \\
  quux
\end{longtable}

I expect that LaTeX would interpret it as though I've typed:

\begin{longtable}{l}
  \begin{foobar}
    foo
  \end{foobar} \\
  \begin{foobar}
    bar
  \end{foobar} \\
  \begin{foobar}
    baz
  \end{foobar} \\
  \begin{foobar}
    quux
  \end{foobar}
\end{longtable}

But it isn't quite the case. How do I make LaTeX work that way?

share|improve this question
1  
Why do you need align*? You can do something else to obtain centred math content. It would be better if you could show exactly what you're after, since I assume E=mc^2 is not your eventual output. –  Werner Apr 30 '12 at 5:10
    
@Werner: I understand your request for the specifics, but I'm looking for a way to put environments inside table cells using the table preamble (see update). It just happens that the math stuff is the use case in which I stumbled on this requirement. –  Kit Apr 30 '12 at 5:28
    
In order to use something like align, you need to box the content in something like a minipage. So, use >{\begin{minipage}{.4\textwidth}\begin{align*}}l<{\end{align*}\end{minipage}}. However, the vertical alignment will be off and require correction. –  Werner Apr 30 '12 at 5:28
    
@Werner: Not quite. It still throws the same error about \begin{minipage} mismatched with \end{longtable} –  Kit Apr 30 '12 at 5:38
2  
Kit, @Werner: I think the question is specific enough and doesn't need to be made more general. The issue here is really the math environment, not environments in general. I suggest removing the "Update" part again and changing "Environments" to "Math environments" in the title. –  Martin Scharrer Apr 30 '12 at 7:51

1 Answer 1

The code you have will work fine with "normal" environments. However, the problem here is the way align* works. It is actually a pseudo-environment which collects its content using \collect@body by reading everything up to the \end{align*}. This end-macro is now hidden in the table preamble code and will not be found. Instead the \end{longtable} is found and a correct error is raised.

The conclusion is that you can't use such environments in a table. However, you can use my collcell package to collect the content of the table cell first and then add \begin{align*} .. \end{align*} around it. This way both the begin and end are on the same level and align* works.

\documentclass{memoir}

\usepackage{amsmath}
\usepackage{array}
\usepackage{collcell}
\usepackage{longtable}
\usepackage{booktabs}
\newcounter{rowno}
\setcounter{rowno}{0}

\newcommand\myalign[1]{\begin{align*}#1\end{align*}}
\begin{document}
  \begin{longtable}[l]{%
     >{\stepcounter{rowno}\therowno.}c%
     >{\collectcell\myalign}%
      m{0.5\textwidth}%
     <{\endcollectcell}%
     >{\collectcell\myalign}%
      m{0.5\textwidth}%
     <{\endcollectcell}%
    }
    &
    E=mc^2
    &
    E=mc^2
    \\
    \end{longtable}
\end{document}

If you want to use & and \\ inside the align* you need to wrap the whole cell in { } so that these are not confused with the cell and row separators of the table. If you actually only want to center the equation then you can do this much easier. See the definition of the third column below. With align* you won't get an alignment at the = anyway for equations of different rows.

\documentclass{memoir}

\usepackage{amsmath}
\usepackage{array}
\usepackage{collcell}
\usepackage{longtable}
\usepackage{booktabs}
\newcounter{rowno}
\setcounter{rowno}{0}

\newcommand\myalign[1]{\begin{align*}#1\end{align*}}
\begin{document}
  \begin{longtable}[l]{%
     >{\stepcounter{rowno}\therowno.}c%
     >{\collectcell\myalign}%
      m{0.5\textwidth}%
     <{\endcollectcell}%
     >{\centering\arraybackslash$\displaystyle}%
      m{0.5\textwidth}%
     <{$}%
    }
    &
    {
        e&=mc^2 \\
        e&=m g h
    }
    &
    e=mc^2
    \\
    &
    {
        e&=mc^2 \\
        e_\text{total}&=m g h + m v^2
    }
    &
     A = e \chi \alpha M p l e
    \\
    \end{longtable}
\end{document}
share|improve this answer
    
This addresses the "collect environment body" issue but it seems to me that any non trivial use of align in this context is going to fail as you need the & to end both the alignment fields and the table cell and it can't do both. –  David Carlisle Apr 30 '12 at 8:12
    
@DavidCarlisle: True. Using align* here doesn't make much sense. I was concentrating on the technical issue at hand, not the big picture. It should be easily possible to use & and \\ in the cell if you wrap the whole content in { }, though –  Martin Scharrer Apr 30 '12 at 8:31
    
@MartinScharrer Yes, the cell contents must be wrapped in {} if an alignment is needed. Of course, if alignments are not really needed, the solution is easier: use displaymath or even >{$$} and <{$$}. –  egreg Apr 30 '12 at 8:34
    
@egreg: Yes, I agree. I already added this in the meantime. Just saw you comment now. –  Martin Scharrer Apr 30 '12 at 8:45
    
explicit mandatory {} are a maintenance issue waiting to bite you though. using explict \begin{align} makes things a lot clearer (and simpler to program:-) –  David Carlisle Apr 30 '12 at 9:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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