如何让森林在另一个环境中发挥作用?

如何让森林在另一个环境中发挥作用?

我正在尝试编写一个将其内部传递到森林的环境。MWE 如下:

\documentclass{article}

\usepackage{forest}
\usepackage{environ}

\NewEnviron{testenvironment}{%
\begin{forest}\BODY\end{forest}}

\begin{document}

\begin{testenvironment}
[S[DP][VP]]
\end{testenvironment}
\end{document}

预期输出如下:

在此处输入图片描述

相反,我得到了一个损坏的 pdf,以及以下错误消息:

! Argument of \bracket@Parse@token has an extra }.
<inserted text> 
          \par 
l.14 \end{testenvironment}

I've run across a `}' that doesn't seem to match anything.
For example, `\def\a#1{...}' and `\a}' would produce
this error. If you simply proceed now, the `\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.

Runaway argument?
! Paragraph ended before \bracket@Parse@token was complete.
<to be read again> 
                   \par 
l.14 \end{testenvironment}

I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

! Extra }, or forgotten \endgroup.
<recently read> }

l.14 \end{testenvironment}

I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

作为参考,替换testenvironmentforest可创建正确的输出:

...
\begin{forest}
[S[DP][VP]]
\end{forest}
...

如果我把换行符留在环境的使用部分并将它们放入环境的定义部分,我会收到相同的错误消息:

\documentclass{article}

\usepackage{forest}
\usepackage{environ}

\NewEnviron{testenvironment}{%
 \begin{forest}
 \BODY
 \end{forest}}

\begin{document}

\begin{testenvironment}[S[DP][VP]]\end{testenvironment}
\end{document}

答案1

嗯,你随时都可以使用\newenvironment

\documentclass{article}

\usepackage{forest}
%\usepackage{environ}

\newenvironment{testenvironment}{\forest}{\endforest}

\begin{document}

\begin{testenvironment}
[S[DP][VP]]
\end{testenvironment}
\end{document}

答案2

您需要对环境的内容使用不同的名称,因为forest它使用\BODY它自己。

\environbodyname\MYBODY

然后你可以设置一个动作角色,以便forest环境知道\MYBODY先正常扩展。

\documentclass{article}
\usepackage{forest}
\environbodyname\MYBODY
\bracketset{action character=@}
\NewEnviron{testenvironment}{%
  \begin{forest}
    @\MYBODY
  \end{forest}}
\begin{document}

\begin{testenvironment}[S[DP][VP]]\end{testenvironment}
\end{document}

forest已经加载environ

预期输出

或者,您可以以相同的方式使用命令版本。

\NewEnviron{testenvironment}{%
  \Forest{@\MYBODY}}

约翰的解决方案如果您只需要这样做,那就更简单了。

相关内容