\ colorbox→\ NewEnviron→\ newenvironment的三重包装失败

时间:2010-05-26 02:20:46

标签: latex wrapper latex-environment

我正在尝试将使用\NewEnviron创建的环境(包'environ')包装成旧的\newenvironment

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

\begin{wrapper}
debug me
\end{wrapper}

然而,这给了我一个奇怪的错误:

  

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

如果我将\NewEnviron{test}{aaa(\BODY)bbb}替换为\newenvironment{test}{aaa(}{)bbb} - 一切都按预期工作!似乎\NewEnviron由于某种原因未能找到它的终点。

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

2 个答案:

答案 0 :(得分:6)

问题在于\NewEviron\newenvironment以不同的方式运作。

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

\begin{test}已扩展为\test

\end{test}已扩展为\endtest并检查您的范围是以begin{test}而非\begin{something else}开头,例如\begin{wrapper}

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

\def\test#1\end{\def\BODY{#1}aaa(\BODY)bbb\testcontinue}

(名称\testcontinue可能不同)并插入aaa(\BODY)bbb。然后,\testcontinue会检查某个输入行上的\end \end{test}而不是\end{something else}。宏\endtest 不需要,因为它永远不会被执行。

查看您的代码:

\begin{wrapper}
debug me               
\end{wrapper} 

\begin{wrapper}已扩展为\begin{test}。然后 \begin{test}已扩展为\test\test抓住\BODY。 注意! \BODY等于debug me。现在\testcontionue检查 在\end结束\BODY之后\end{test}。这不是真的。 \end{test}缺席。 有\end{wrapper}

您想说\end{wrapper}必须扩展为\end{test}。但是包装器之前的\end

吃掉了
macro \test: #1\end{\def\BODY{#1}aaa(\BODY)bbb\testcontinue}

且无法执行。

我希望我成功解释。

答案 1 :(得分:1)

我发现了一个hacky技巧来创建一个可以包装在另一个环境中的环境。应该像这样使用saveBox:

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