# Macro based flags for forest tree

I'm using simple y/n macros as flags in my document to include or exclude certain parts of the overall document (these normally are in a separate "config.tex" file for ease of use). With the forest package, I get strange results when using this approach (using pdftex). The inserted texts seem to be interpreted as text instead of added or non-added leaves.

Minimum working example:

\documentclass[]{scrreprt}

\usepackage{ifthen}
\usepackage[edges]{forest}

\newcommand{\hasOptionOne}{y}
\newcommand{\hasOptionFour}{y}

\begin{document}

The user has the following options:
\begin{itemize}
\item Option Two
\item Option Three
\end{itemize}

\begin{figure}
\centering
\caption{Available Options}
\label{fig-available-options}
\begin{forest}
for tree={draw,align=center},
forked edges,
[Main item
[Option Two]
[Option Three]
]
\end{forest}
\end{figure}

\end{document}


Result:

• forest has a highly sophisticated and powerful parser. I believe it is practically hopeless to try to trick it this way, You would have to makes sure that forest sees the expanded version of these \AddIfs, but the way you construct this command, already \edef\temp{\AddIf{\hasOptionOne}{\item Option One}{}} fails. So I would like to argue that you should resort to pure forest methods to achieve what you want. – user121799 Jan 29 '19 at 17:52
• Hi marmot. Can you suggest any pure forest methods that can be somehow connected to (possibly differently designed) flags outside the forest environment? I'd be happy to use them, but as I need these flags to be set centrally for the entire document, I'm not sure how to connect these two worlds. – Michael Jan 30 '19 at 8:34
• I added one example. Quite possible that there exist alternatives. (Just for the records, I usually try to avoid using the ifthen package because of such issues.) – user121799 Jan 30 '19 at 13:59

Since you asked for it... I replaced your command \AddIf by some version that can be appropriately expanded. And then a trick is used that has also some applications in pgfplots and so on. It makes sure that the content of the tree gets expanded first, and then it gets passed to forest.

\documentclass[]{scrreprt}

% \usepackage{ifthen} % no longer used here
\usepackage[edges]{forest}
\newcommand{\Yes}{y}
#2%
\else
#3%
\fi}

\newcommand{\hasOptionOne}{y}
\newcommand{\hasOptionFour}{y}

\begin{document}

The user has the following options:
\begin{itemize}
\item Option Two
\item Option Three
\end{itemize}

\begin{figure}
\centering
\caption{Available Options}
\label{fig-available-options}
\edef\temp{\noexpand\begin{forest}
for tree={draw,align=center},
forked edges,
[Main item
[Option Two]
[Option Three]

• @Michael Did you try to replace  \ by \noexpand\? – user121799 Jan 31 '19 at 16:59