Latex 三层包装\colorbox→\新环境→\新环境失败

Latex 三层包装\colorbox→\新环境→\新环境失败,latex,wrapper,latex-environment,Latex,Wrapper,Latex Environment,我正在尝试将使用\newenvironment(包“environment”)创建的环境包装到旧的\newenvironment: \NewEnviron{test}{\colorbox[gray]{0.7}{\BODY}} \newenvironment{wrapper}{\begin{test}}{\end{test}} \begin{wrapper} debug me \end{wrapper} 然而,这给了我一个奇怪的错误: LaTeX错误:\begin{test}在输入行15上,以

我正在尝试将使用
\newenvironment
(包“environment”)创建的环境包装到旧的
\newenvironment

\NewEnviron{test}{\colorbox[gray]{0.7}{\BODY}}
\newenvironment{wrapper}{\begin{test}}{\end{test}}

\begin{wrapper}
debug me
\end{wrapper}
然而,这给了我一个奇怪的错误:

LaTeX错误:\begin{test}在输入行15上,以\end{wrapper}结束。 LaTeX错误:\begin{wrapper}在输入行15上,以\end{document}结束

如果我将
\newenvironment{test}{aaa(\BODY)bbb}
替换为
\newenvironment{test}{aaa(}{)bbb}
,一切正常!似乎由于某种原因,
\NewEnviron
未能找到它的终点


我正在尝试将“floatfig”包装到一个
\colorbox
中,这样我需要一种方法将
\colorbox
转换到一个环境中,并将其包装到另一个环境中。我可以定义一个新命令,但这不是一个好主意。

问题是
\NewEviron
\newenvironment
的工作方式不同

1)
\newenvironment{test}{aaa(}{)bbb}
定义了两个命令:
\test
aaa(
\endtest
)bbb

\begin{test}
扩展为
\test

\end{test}
扩展为
\endtest
,并检查您的范围是否以
开始{test}
而不是
\begin{something}
,例如
\begin{wrapper}

2)
\NewEviron{test}{aaa(\BODY)bbb}
以不同的方式定义了
\test
。首先,
\test
使用以下技巧捕捉
\BODY

\def\test#1\end{\def\BODY{#1}aaa(\BODY)bbb\testcontinue}
(名称
\testcontinue
可能不同)和插入
aaa(\BODY)bbb
。然后
\testcontinue
检查
\end
在某个输入行上是否以
\end{test}
而不是
\end{something}
结束。宏
\endtest
不需要,因为它从未执行过

查看您的代码:

\begin{wrapper}
debug me               
\end{wrapper} 
\begin{wrapper}
扩展为
\begin{test}
。然后
\begin{test}
扩展为
\test
<代码>\testcatch
\BODY
。 注意
\BODY
等于
调试me
。现在
\testcontionue
检查 在
\BODY
结束于
\end{test}
之后的
\end
。这不是真的<代码>\end{test}不存在。 有
\end{wrapper}

您想说的是,
\end{wrapper}
必须扩展到
\end{test}
。但是
\end
在包装纸被人吃掉之前

macro \test: #1\end{\def\BODY{#1}aaa(\BODY)bbb\testcontinue}
不能执行


我希望我能成功地解释清楚。

我发现了一个黑客技巧,可以创建一个可以封装在另一个环境中的环境。您应该使用这样的存储框:

\newenvironment{example}[2][]{%
    \newsavebox{\exampleStore} % Box storage
    \begin{lrbox}{\exampleStore} % Start capturing the input
    }{%
        \end{lrbox} % Stop capturing the input
    \colorbox[gray]{0.7}{%
            \usebox{\NBstorage} % Load the box's contents
            }%
        }%
    }%

你的解释很好,谢谢!我想这意味着我不能将它包装到另一个环境中?或者可能有一个我不知道的窍门?你为什么不想使用
\newenvironment
?我需要创建一个
\colorbox
的环境版本,但不知道如何做:)接受:如果没有你的解释,我会失败。Спасибо, земляк! :)+很好的解释。我看过NewEnviron的代码,无法理解Will对其论点的奇怪表述。