我正在尝试用 xparse 重新定义一个类似“数组”的环境,每当我尝试将新环境嵌套在其自身中时,它都会产生很多错误:
\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}
看起来,较小环境中的“&”符号被检测为较大环境中的新列,因此导致了许多问题。您能向我解释一下这是怎么回事吗?(我是定义环境的新手)有没有办法纠正这种行为?我使用 xparse 是因为我希望能够对包“IEEEtrantools”的环境“IEEEeqnarraybox”进行这种操作,并且因为这个环境有许多可选参数(xparse 似乎非常适合定义几个可选参数)。谢谢您的帮助!
答案1
仅当需要对环境内容进行某些处理时才应使用参数b
类型。\NewDocumentEnvironment
在你的情况下没有进行任何处理,你真的应该这样做
\NewDocumentEnvironment{newarray}{ O{rcl} }
{\begin{array}{#1}}
{\end{array}}
如果这只是一个例子,而您实际上需要在传递内容之前对其进行处理,则必须使用“命令形式”:
\NewDocumentEnvironment{newarray}{ O{rcl} +b }
{%
\array{#1}%
#2%
\endarray
}{}