\colorbox → 的三重包装\新环境→ \newenvironment 失败

发布于 2024-09-03 10:58:17 字数 724 浏览 2 评论 0原文

我试图将使用 \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 转换为环境并将其包装到另一个环境中。我可以定义一个新命令,但这不是一个好主意。

I am trying to wrap an environment created with \NewEnviron (package 'environ') into an old good \newenvironment:

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

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

However, this gives me a strange error:

LaTeX Error: \begin{test} on input line 15 ended by \end{wrapper}.
LaTeX Error: \begin{wrapper} on input line 15 ended by \end{document}.

If I replace \NewEnviron{test}{aaa(\BODY)bbb} with \newenvironment{test}{aaa(}{)bbb} — everything works as expected! It seems like \NewEnviron fails to find its end for some reason.

I'm trying to do some magic with 'floatfig' wrapped into a \colorbox so I need a way to convert \colorbox to an environment and wrap it into another one. I can define a new command but it's not a very good idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-09-10 10:58:17

问题是 \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 使用以下技巧捕获 \BODY

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

(名称 \testcontinue 可能不同)并插入 aaa(\正文)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 等于调试我。现在 \testcontionue 检查
\BODY 之后的 \end\end{test} 结束。这不是真的。 \end{test} 不存在。
\end{wrapper}

您想说 \end{wrapper} 必须扩展为 \end{test}。但是wrapper之前的\end被吃掉了

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

,无法执行。

我希望我能成功解释。

The thing is that \NewEviron and \newenvironment works in different ways.

1) \newenvironment{test}{aaa(}{)bbb} defines two commands: \test is aaa( and \endtest is )bbb.

\begin{test} is expanded to \test.

\end{test} is expanded to \endtest and checks that your scope begins with begin{test} rather \begin{something else}, for example \begin{wrapper}.

2) \NewEviron{test}{aaa(\BODY)bbb} defines \test in different way. First of all \test catches the \BODY using the following trick

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

(name \testcontinue may be different) and inserts aaa(\BODY)bbb. Then \testcontinue checks that \end on some input line ended by \end{test} rather than \end{something else}. Macro \endtest
is not needed because it is never executed.

Look on your code:

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

\begin{wrapper} is expanded to \begin{test}. Then
\begin{test} is expanded to \test. \test catch \BODY.
Attention! \BODY is equal to debug me. And now \testcontionue checks
that \end after \BODY ended by \end{test}. It is not true. \end{test} is absent.
There is \end{wrapper}.

You want to say that \end{wrapper} must be expanded to \end{test}. But \end before wrapper was eaten by

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

and can not be executed.

I hope I success to explain.

过去的过去 2024-09-10 10:58:17

我发现了一个技巧来创建一个可以包含在另一个环境中的环境。人们应该像这样使用 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
            }%
        }%
    }%

I've found a hacky trick to create an environment that can be wrapped in another one. One should use saveBoxes like this:

\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
            }%
        }%
    }%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文