tikz.dev / forest Manual

Forest

Reference

3.18 Additional pgfmath functions

For every option and register, Forest defines a pgfmath function with the same name, with the proviso that the name might be mangled in order to conform to pgfmath’s naming rules. Specifically, all non-alphanumeric characters in the option/register name and the initial number, if the name starts with one, are replaced by an underscore __ in the pgfmath function name.

Pgfmath functions corresponding to options take one argument, a ⟨relative node name⟩ (see §3.15) expression, making it possible to refer to option values of non-current nodes. The ⟨relative node name⟩ expression must be enclosed in double quotes in order to prevent pgfmath evaluation: for example, to refer to the content of the parent, write content("!u"). To refer to the option of the current node, use empty parentheses: content().1

If the ⟨relative node name⟩ resolves to the invalid node, these functions will an return empty token list (for ⟨toks⟩ options), 0pt (for ⟨dimen⟩ options) or 0 (for ⟨count⟩ options).

Note that the nodewalk in the relative node name inherits its history from the outer nodewalk (if there is one), so strange but useful constructions like the following are possible.

(94)

Compiled output

\begin{forest}
for tree={no edge},
before typesetting nodes={
for nodewalk={
c,
every step={
tikz/.wrap pgfmath arg=
{\draw[<-] ()--(#1);}
{name("~!b~")}
},
21{up1},ancestors
}{}
},
[1[2[3]][4[5]]]
\end{forest}

Boolean function valid returns true if the node’s id\(\neq0\), i.e. if the node is a real, valid node; see §3.5.1 and §3.8. Boolean function invalid is a negation of valid.

  • min_l=(⟨nodewalk: node⟩,⟨nodewalk: context node⟩)(pgfmath function)

  • min_s=(⟨nodewalk: node⟩,⟨nodewalk: context node⟩)(pgfmath function)

  • max_l=(⟨nodewalk: node⟩,⟨nodewalk: context node⟩)(pgfmath function)

  • max_s=(⟨nodewalk: node⟩,⟨nodewalk: context node⟩)(pgfmath function)

  • These functions return the minimum/maximum value of l/s of node at the end of ⟨nodewalk: node⟩ in the context (i.e. growth direction) of node at the end of ⟨nodewalk: context node⟩.

Three string functions are also added to pgfmath: strequal tests the equality of its two arguments; instr tests if the first string is a substring of the second one; strcat joins an arbitrary number of strings.

Some random notes on pgfmath:

Compiled output

And some examples:

(95)

Compiled output

\begin{forest}
for tree={grow'=0,calign=first,l=0,l sep=2em,child anchor=west,anchor=base
west,fit=band,tier/.pgfmath=~level~()},
fullpath/.style={if n=0{}{content/.wrap 2
pgfmath args={##1/##2}{~content~("!u")}{~content~()}}},
delay={for tree=fullpath,content=/},
before typesetting nodes={for tree={content=\strut#1}}
[
[home
[joe
[\TeX]]
[saso
[\TeX]]
[a user with a long name
[\TeX]]]
[usr]]
\end{forest}
(96)

Compiled output

% mark non-phrasal terminal nodes
\begin{forest}
delay={for tree={if=
{!instr("P",~content~) && ~n_children~==0}
{fill=yellow}
{}
}}
[CP[DP][C'[C][TP[DP][T'[T][VP[DP][V'[V][DP]]]]]]]
\end{forest}
(97)

Compiled output

% roof terminal phrases
\useforestlibrary{linguistics}
% ...
\begin{forest}
delay={where n children=0{tier=word,
if={~instr~("P",~content~("!u"))}{roof}{}
}{}},
[VP[DP[Mary]][V'[V[loves]][DP[her cat]]]]
\end{forest}

1 In most cases, the parentheses are optional, so content is ok. A known case where this doesn’t work is preceding an operator: l+1cm will fail.