# Nested Environ's give “! Argument of … has an extra }” error

I tried using two Environ's in a nested fashion, but I get the

! Argument of \marray has an extra }


error. Any idea why this happens?

\documentclass{minimal}
\usepackage{amsmath,environ}

\NewEnviron{meqmultinum}[1]{
$$\begin{split} \BODY \end{split}\label{eq:#1}$$
}

\NewEnviron{marray}[2]{
\renewcommand*{\arraystretch}{#2}
\begin{array}{#1}
\BODY
\end{array}
}

\begin{document}

\begin{meqmultinum}{test}
a&=\begin{marray}{l l}{1.2}
\alpha & \beta\\
\gamma & \kappa
\end{marray}\\
b&=\begin{array}{l l}
\alpha & \beta\\
\gamma & \kappa
\end{array}
\end{meqmultinum}

\end{document}

-
You would avoid the problem (and the code would be a lot more efficient if you use \newenvironment{marray}[2]{\renewcommand*{\arraystretch}{#2}\begin{array}{#1}}{‌​\end{array} you only need the \BODY construct in cases where you need to pass the enviornment body as a comamnd argument. –  David Carlisle Nov 19 '12 at 15:35
Excellent, thanks! –  Wox Nov 19 '12 at 15:42
This question may also be helpful. –  Andrew Uzzell Nov 19 '12 at 15:47

Environment split inside meqmultinum provides an alignment. That means that the contents of the environment is processed cell by cell:

Cell 1: a
Cell 2: =\begin{marray}{l l}{1.2} \alpha

The rest & \beta \\ \gamma & \kappa \end{marray} does not belong to cell 2. Thus the code of \begin{marray} complains, because it cannot find \end of \end{marray}.

This can be fixed by adding curly braces around \begin{marray}...\end{marray}:

\begin{meqmultinum}{test}
a&={\begin{marray}{l l}{1.2}
\alpha & \beta\\
\gamma & \kappa
\end{marray}}\\
...
\end{meqmultinum}


Why does it work with array instead of marray? Environment array does not need to see at the beginning \end{array}, instead it sets up a tabular and the contents of array with its & is processed in this inner tabular.

## Other definition of marray

Another way to fix this issue is given by David Carlisle in his comment. There is no need for the special environment processing of package environ for array:

\newenvironment*{marray}[2]{%
\renewcommand*{\arraystretch}{#2}%
\begin{array}{#1}%
}{‌%
​\end{array}%
}

-
Thanks for the clear and elaborate answer Heiko. –  Wox Nov 19 '12 at 16:31

There is no need to use \NewEnviron for these environments; in this case split can give problems, but aligned can be used instead.

\documentclass{article}
\usepackage{amsmath}

\newenvironment{meqmultinum}[1]
{\equation\label{eq:#1}\aligned}
{\endaligned\endequation}

\newenvironment{marray}[2]
{\renewcommand*{\arraystretch}{#2}\array{#1}}
{\endarray}

\begin{document}

\begin{meqmultinum}{test}
a&=\begin{marray}{l l}{1.2}
\alpha & \beta\\
\gamma & \kappa
\end{marray}\\
b&=\begin{array}{l l}
\alpha & \beta\\
\gamma & \kappa
\end{array}
\end{meqmultinum}

\end{document}

-
I wasn't aware of the \something...\endsomething notation instead of \begin{something}...\end{something}. Do you have a reference that explains this? –  Wox Nov 20 '12 at 8:32
@Wox The usage of \foo and \endfoo instead of \begin{foo} and \end{foo} is sometimes handier; it has pros and cons: it doesn't form a group and doesn't set the environment's name (so error messages may be more helpful), but for some environments, notably lrbox it mustn't be used. –  egreg Nov 20 '12 at 8:42