0

I am using forest to typeset some trees that contain many repetitive structures. For example:

\begin{forest}
  [ [ NP [ N : \textit{John} ] ]
    [ VP
      [ V : \textit{ate} ]
      [ NP
        [ NP [ N : \textit{apples} ] ]
        [ Conj : \textit{and} ]
        [ NP [ N : \textit{bananas} ] ]
      ]
    ]
  ]
\end{forest}

enter image description here

In particular, note the [ NP [ N : \textit{...} ] ] construction that appears three times (John, apples, bananas).

I am hoping to use some abstraction (a macro, or forest's own options engine, etc.) to avoid having to type out the whole [ NP [ N : \textit{...} ] ] chunk each time, to the effect of something similar to (pseudo-example that doesn't actually work):

\newcommand{\NPN}[1]{[ NP [ N : \textit{#1} ] ]}
\begin{forest}
  [ \NPN{John}
    [ VP
      [ V : \textit{ate} ]
      [ NP
        \NPN{apples}
        [ Conj : \textit{and} ]
        \NPN{bananas}
      ]
    ]
  ]
\end{forest}

Is this possible? Ideally, I'd like to use forest here, but if other tree-drawing packages offer this capability, I could be interested in seeing how to do that as well.

Kye W Shi
  • 205
  • 2
  • 5

1 Answers1

1

The OP's attempt does not work because the macro \NPN is expanded when the node is typeset (thereby producing a node containing the brackets etc.), but the intention was that the macro would be expanded before the tree structure (brackets) is parsed (which happens much earlier).

Now, Forest actually makes it possible to expand the macros before the tree structure is parsed. The trick is to define an action character (below, @), and use it before every macro that needs to be expanded (there will be a single expansion). For details, see section 3.3 of the manual.

\documentclass{article}

\usepackage{forest}

\bracketset{ action character=@, }

\begin{document}

\newcommand{\NPN}[1]{[ NP [ N : \textit{#1} ] ]}

\begin{forest} [ @\NPN{John} [ VP [ V : \textit{ate} ] [ NP @\NPN{apples} [ Conj : \textit{and} ] @\NPN{bananas} ] ] ] \end{forest}

\end{document}

However, these days my preferred way to achive the same effect would be using a dynamic tree (see section 3.11 of the manual). Below, I define NPN as a style which sets the content of the node to NP and inserts an N node (with content N : noun) into it. (Note the comma before the NPN! Without it, Forest would interpret NPN as the content, and due to the internals of pgfkeys, the intended value (e.g. John) would be disregarged.)

\documentclass{article}

\usepackage{forest}

\forestset{ NPN/.style={ content=NP, append={[N : \textit{#1}]}, }, }

\begin{document}

\begin{forest} [ [,NPN=John,] [ VP [ V : \textit{ate} ] [ NP [,NPN=apples] [ Conj : \textit{and} ] [,NPN=bananas] ] ] ] \end{forest}

\end{document}

(One could also define NPN to append (to the current node) an NP node containing an N node (append={[NP[N : #1]]}). However, for such a style to be actually useful in OP's situation, where the [NP[N]] template appears both before and after "regular" nodes (like ``Conf), one would need to define both the append and prepend variant of the style.)