# Using Forest Package in Newenvironment

I'm trying to use the forest package to draw circuits, so I want to define a "circuit" environment that is a wrapper around the forest environment. But defining a newenvironment runs into problems. Here is a minimal working example:

\documentclass{article}
\usepackage{forest}

\newenvironment{cc}{\begin{forest}}{\end{forest}}

\begin{document}

\begin{cc}[AND]\end{cc}

\end{document}


And the error produced says "\begin{forest} on input line 8 ended by \end{cc}." What's the problem?

I have seen errors of this sort where some packages don't define their environments nicely, which causes problems when trying to wrap around them in newenvironment. There is sometimes a workaround to those problems; is there one here?

• Did you try \newenvironment{cc}{\forest}{\endforest}? – egreg Feb 22 '15 at 23:21
• I strongly suspect there is a better way of approaching this but it is difficult to say on the basis of the information given.... – cfr Feb 23 '15 at 0:43

The forest package grabs the environment contents with the help of the environ package.

In these cases, the workaround is very simple:

\newenvironment{cc}{\forest}{\endforest}


Or you can use environ to define your environment in a similar way. For example:

\environbodyname\ccbody
\NewEnviron{cc}{% \forest/\endforest from egreg's answer at https://tex.stackexchange.com/a/229608/
\forest
\ccbody
\endforest}


The main reason to do this is obviously when it is required. For simple cases, egreg's method works fine and is, therefore, preferable because it is simpler.

However, the alternative approach above works in cases where the simpler method cannot be used. This is the case if, for example, we want to add a new root node to the tree.

Suppose we want to align several trees. Obviously there are lots of ways to do this, but it is just intended to be a simplistic example. Using the technique above, we can define an environment ccs which allows us to specify all four trees in one go and which will automatically align those trees by ensuring that each is the child of a phantom root.

That is, we can write

\environbodyname\ccsbody
\bracketset{action character=@}
\NewEnviron{ccs}[1]{% \forest/\endforest from egreg's answer at https://tex.stackexchange.com/a/229608/
\forest
#1
[,phantom, for children={no edge} @\ccsbody]
\endforest}


This allows us to write

\begin{ccs}
{
for tree={font=\sffamily, align=center},
}
[AND [one\\two]]
[OR [one][two]]
[NAND [NOT one] [NOT two]]
[NOR [NOT one\\NOT two]]
\end{ccs}


to produce

and is equivalent to

\begin{forest}
for tree={
font=\sffamily, align=center,
if level=0{
phantom, for children={no edge}
}{}
},
[
[AND [one\\two]]
[OR [one][two]]
[NAND [NOT one] [NOT two]]
[NOR [NOT one\\NOT two]]
]
\end{forest}


Clearly, we wouldn't bother defining the new environment for a one-off. But if we wanted to typeset many such tree groups and if we wanted to align them using a phantom root, we might find it convenient not to need to add that additional node each time. And, as I say, this is only an intentionally simplistic example to demonstrate the point of the technique. It is not intended as a compelling application of that technique.

Complete code:

\documentclass[tikz,multi,border=5pt]{standalone}
\usepackage{forest}
\environbodyname\ccsbody
\bracketset{action character=@}
\NewEnviron{ccs}[1]{% \forest/\endforest from egreg's answer at https://tex.stackexchange.com/a/229608/
\forest
#1
[,phantom, for children={no edge} @\ccsbody]
\endforest}
\begin{document}
\begin{ccs}
{
for tree={font=\sffamily, align=center},
}
[AND [one\\two]]
[OR [one][two]]
[NAND [NOT one] [NOT two]]
[NOR [NOT one\\NOT two]]
\end{ccs}
\end{document}

• Why adding a layer? With this, \begin{cc}<commands>\end{cc} would be translated into \forest\ccbody\endforest so \forest would set \BODY to \ccbody, which would then be expanded to <commands>. – egreg Feb 17 '16 at 18:29
• \newenvironment{cc}{\forest<customsomething>}{\endforest} – egreg Feb 17 '16 at 20:30
• Please see edit. @egreg I cleaned up my comments a bit. – cfr Feb 17 '16 at 22:22