# [Tex/LaTex] Use column-separator & (ampersand) inside \newenvironment

ampersandenvironmentsmacrossyntaxtables

I'd like to define a grammar environment which basically wraps a tabular environment. Inside the grammar environment I'd like to use an environment called production to insert rows. Example:

\documentclass[a4paper,10pt]{article}

\newenvironment{production}[1]
{\noindent\ignorespaces#1 ::= &}
{\\}

\newenvironment{grammar}
{\begin{tabular}{p{3cm}l}}
{\end{tabular}}

\begin{document}
\begin{grammar}
\begin{production}{XmlStartTag}
...
\end{production}
\begin{production}{XmlEndTag}
...
\end{production}
\end{grammar}

\end{document}


The goal of this code is to put the name of the non-terminal that is being defined in the left column and the actual production into the right column of the tabular.

Unfortunately this fails due to the & and \\ within the production environment. I guess it has something to do with macro expansion. I guess I have to escape the & and \\ with something that is only expanded after the macro was used?

Thanks!

The problem is that the \begin{...} and \end{...} pair commands automatically create a "group" so that, in effect, the & and \\ are "out of scope" for the tabular, while inside the "production" environment they just show up at a place where the compiler is not expecting them.

A second problem with your definitions is that, even if they would work, they would be adding an extra \\ at the end of the tabular, adding an unwanted space at the end. Perhaps some more appropriate definitions would be

\documentclass[a4paper,10pt]{article}

\newcommand{\production}[1]{#1 ::= &}
\newenvironment{grammar}{\tabular{p{3cm}l}}{\endtabular}

\begin{document}
\begin{grammar}
\production{XmlStartTag} ... \\
\production{XmlOtherTag} ... \\
\production{XmlEndTag} ...
\end{grammar}
\end{document}


Note no \\ at the end of the last production. Also in the definition of the grammar you don't need to repeat the work of \begin/\end, and you can instead directly use \tabular and \endtabular.