1

I'm trying to redefine an "array"-like environment with xparse and it creates a lot of errors whenever I try to nest my new environment within itself:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{xparse}

%%begin novalidate
\NewDocumentEnvironment{newarray}{ O{rcl} +b }
{%
    \begin{array}{#1}%
        #2%
    \end{array}%
}{}
%%end novalidate

\begin{document}

I can nest \texttt{array} environments:
\[\begin{array}{rcl}
    1&=&\left\{\begin{array}{rcl}
            2 &=& 3\\
            4&=&5
        \end{array}\right.
\end{array}\]

I can nest an \texttt{array} environment within a \texttt{newarray} environment:
\[\begin{newarray}
    1&=&\left\{\begin{array}{rcl}
            2 &=& 3\\
            4&=&5
        \end{array}\right.
\end{newarray}\]

But I can't nest a \texttt{newarray} environment within an \texttt{array} environment:
\[\begin{array}{rcl}
    1&=&\left\{\begin{newarray}
            2 &=& 3\\
            4&=&5
        \end{newarray}\right.
\end{array}\]

And I can't nest \texttt{newarray} environments:
\[\begin{newarray}
    1&=&\left\{\begin{newarray}
            2 &=& 3\\
            4&=&5
        \end{newarray}\right.
\end{newarray}\]

\end{document}

It appears that the "&" symbols within the smaller environment are detected as new columns for the larger environment, and hence leading to many problems. Can you explain to me where this comes from? (I'm new to defining environments) Is there a way to correct this behavior? I used xparse because I want to be able to do this manipulation to the environment "IEEEeqnarraybox" of the package "IEEEtrantools", and because this environment has many optional arguments (and xparse seems well suited to defining several optional arguments). Thank you for your help!

Here is how it behaves in the pdf

1
  • Welcome! Is there any particular reason for using the b argument type? I see none.
    – egreg
    Jun 29, 2021 at 12:50

1 Answer 1

0

The b argument type to \NewDocumentEnvironment should only be used when some processing has to be done to the environment's contents.

In your case there is no processing done and you should really do

\NewDocumentEnvironment{newarray}{ O{rcl} }
 {\begin{array}{#1}}
 {\end{array}}

If this is just an example and you actually need to process the contents before delivering it, you have to use the “command form”:

\NewDocumentEnvironment{newarray}{ O{rcl} +b }
{%
 \array{#1}%
 #2%
 \endarray
}{}
2
  • Thank you very much! It worked perfectly! Just out of curiosity, what is the reason for the problem I observed? To my naive eyes, all those approaches seemed equivalent... (even though they clearly are not!) Jun 30, 2021 at 10:33
  • @PabloBustillo With the b argument type, LaTeX enters quite a complicated state in order to match environments start and end. The nesting will not work with the code you use and the “inner” form are the way to go.
    – egreg
    Jun 30, 2021 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .