# How do I make forest work within another environment?

I'm trying to write an environment that passes it's insides to forest. MWE as follows:

\documentclass{article}

\usepackage{forest}
\usepackage{environ}

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

\begin{document}

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


The expected output is this:

Instead, I get a broken pdf, and the following error messages:

! 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.
\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.

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$}'.


For reference, replacing testenvironment with forest creates the correct output:

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


If I leave the line breaks out in the use of the environment and put them into the definition of the environment, I get the same error message:

\documentclass{article}

\usepackage{forest}
\usepackage{environ}

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

\begin{document}

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


Well, you can always just use \newenvironment.

\documentclass{article}

\usepackage{forest}
%\usepackage{environ}

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

\begin{document}

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

• Huh. I tried something like that at first, but ran into problems because Latex didn't like the idea of ending the outer environment before the inner or something. I'll try to reproduce the issue and ask it as a separate question. This works in any case. – sgf Jan 9 '18 at 19:04

You need to use a different name for the content of the environment because forest uses \BODY itself.

\environbodyname\MYBODY


Then you can set an action character so that the forest environment knows to expand \MYBODY normally first.

\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 already loads environ.

Alternatively, you can use the command version in the same way.

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


But John's solution is simpler if this is all you need to do with it.