tikz.dev / forest Manual

Forest

Reference

3.14 Aggregate functions

Aggregate functions walk a nodewalk and use the information found in the visited nodes to calculate something.

All aggregate functions are available both as key handlers and pgfmath functions.

  • .count=⟨nodewalk⟩,(aggregate)

  • aggregate_count("⟨nodewalk⟩")

    Store the number of nodes visited in the nodewalk into the handled option.

  • .max=⟨forestmath⟩⟨nodewalk⟩,(aggregate)

  • aggregate_max("⟨forestmath⟩","⟨nodewalk⟩")

    Calculate the value of the given ⟨forestmath⟩ expression at each visited node. Store the sum / average / product / minimum / maximum of these values into the handled option (handlers) or return it (pgfmath functions).

  • aggregate n=⟨count⟩(register)

  • the current step number

    In the every-step expression of an aggregate function, refers to the (real) step number in the aggregate’s ⟨nodewalk⟩. In the after-walk expression, refers to the total number of (real) steps made.

  • aggregate result=⟨toks⟩(register)

  • the current value of the result

    This register is where the intermediate results are stored.

  • aggregate value=⟨toks⟩(register)

  • the value of the expression at the current node

    This only applies to special aggregates like .sum, not to the generic .aggregate.

  • aggregate postparse=none|int|print|macro

  • Roughly speaking, how should the result be formatted? For details, see [Tantau 2013, §89]. Applies only to pgfmath versions of aggregate functions, i.e. not to the ' variants.

    none No formatting. int The result is an integer. print Use pgf’s number printing extension, see [Tantau 2013, §93]. macro Use a custom macro. Specify the macro using aggregate postparse macro=⟨cs⟩.

Example 1. Randomly generate the content of leaves. The content of a parent is the sum of its children’s content. Note how we use tree children-first to proceed in a bottom-up fashion.

(90)

Compiled output

\begin{forest}
delay={
aggregate postparse=int,
for tree children-first={
if n children=0
{content/.pgfmath={random(0,100)}}
{content/~.sum~={content}{children}}
}
}
[[[[][]][]][[][][[][][]]][[][[][]]]]
\end{forest}

Example 2: nested aggregate functions. We are given the black numbers. The inner aggregate, the sum of children, is applied at every blue node. (See how we actually display the blue numbers by storing aggregate value to content.) The outer aggregate stores the maximum blue number into the red root.

(91)

Compiled output

\begin{forest}
delay={
aggregate postparse=int,
content/~.max~=%
{~aggregate_sum~("content","children")}%
{every step={content/.register=~aggregate value~},children}%
} [ [[1][2][3]] [[3][4][5]] [[2][3][4]] ]
\end{forest}

Example 3: calculate root mean square of children using the generic .aggregate handler.

(92)

Compiled output

\begin{forest}
delay={
content/~.aggregate~=
{0}{~aggregate_result~()+content()^2}{sqrt(~aggregate_result~/~aggregate_n~)}
{children}
}
[[0][1][2][3][4]]
\end{forest}