[Tex/LaTex] Nested Environ’s give “! Argument of … has an extra }” error

amsmathenvironmentsequationsnesting

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{equation}\begin{split}
\BODY
\end{split}\label{eq:#1}\end{equation}
}

\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}

Best Answer

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}%
}