1

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.

What am I doing wrong? Thanks in advance for your help.

Minimum working example:

\documentclass[]{scrreprt}

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

\newcommand{\AddIf}[3]{\ifthenelse{\equal{#1}{y}}{#2}{#3}}

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

\begin{document}

The user has the following options:
\begin{itemize}
    \AddIf{\hasOptionOne}{\item Option One}{}
    \item Option Two
    \item Option Three
    \AddIf{\hasOptionFour}{\item Option Four}{}
\end{itemize}

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

\end{document}

Result:

Result

3
  • 1
    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, 2019 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, 2019 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, 2019 at 13:59

1 Answer 1

1

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}
\newcommand{\AddIf}[3]{\ifx#1\Yes%
#2%
\else
#3%
\fi}

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

\begin{document}

The user has the following options:
\begin{itemize}
    \AddIf{\hasOptionOne}{\item Option One}{}
    \item Option Two
    \item Option Three
    \AddIf{\hasOptionFour}{\item Option Four}{}
\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
            \AddIf{\hasOptionOne}{[Option One]}{}
            [Option Two]
            [Option Three]
            \AddIf{\hasOptionFour}{[Option Four]}{}
        ]
    \noexpand\end{forest}}
    \temp
\end{figure}

\end{document}

enter image description here

3
  • Yes, that really does the job, thanks for sharing. One additional remark/question: due to the expansion trick, forced line breaks within nodes (\\ or \newline) cause compilation errors - can this also be overcome somehow?
    – Michael
    Jan 31, 2019 at 9:00
  • @Michael Did you try to replace ` \` by `\noexpand\`?
    – user121799
    Jan 31, 2019 at 16:59
  • No, I hadn't tried that. Seems to work. Thanks again.
    – Michael
    Feb 1, 2019 at 9:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .