TransWikia.com

Redefined array environment can't be nested

TeX - LaTeX Asked by Pablo Bustillo on July 31, 2021

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

One Answer

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

Correct answer by egreg on July 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP