tikz.dev / forest Manual

Forest

Reference

3.9 Conditionals

All conditionals take arguments ⟨true keylist⟩ and ⟨false keylist⟩. The interpretation of the keys in these keylists depends on the environment the conditional appears in. If it is a part of a nodewalk specification, the keys are taken to be nodewalk keys (§3.8), otherwise node keys (§3.5).

All the conditionals can be nested safely.

  • if=⟨forestmath: condition⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • If ⟨forestmath: condition⟩ evaluates to true (non-zero), ⟨true keylist⟩ is processed (in the context of the current node); otherwise, ⟨false keylist⟩ is processed.

    For a detailed description of pgfmath expressions, see [Tantau 2007, part VI]. (In short: write the usual mathematical expressions.)

    In the following example, if is used to orient the arrows from the smaller number to the greater, and to color the odd and even numbers differently. (Style random_tree is defined in the front page example.)

    (82)

    Compiled output

    \pgfmathsetseed{314159}
    \begin{forest}
    before typesetting nodes={
    for descendants={
    if={content()>content("!u")}{edge=->}{
    if={content()<content("!u")}{edge=<-}{}},
    edge label/.wrap pgfmath arg=
    {node[midway,above,sloped,font=\scriptsize]{+#1}}
    {int(abs(content()-content("!u")))}
    },
    for tree={circle,if={mod(content(),2)==0}
    {fill=yellow}{fill=green}}
    }
    [,random tree={3}{3}{100}]
    \end{forest}
  • if ⟨option⟩=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • This simple conditional is defined for every ⟨option⟩ (except boolean options, see below): if ⟨value⟩ equals the value of the option at the current node, ⟨true keylist⟩ is executed; otherwise, ⟨false keylist⟩.

  • if ⟨boolean option⟩=⟨true keylist⟩⟨false keylist⟩(conditional)

  • Execute ⟨true keylist⟩ if ⟨boolean option⟩ is true; otherwise, execute ⟨false keylist⟩.

  • if in ⟨toks option⟩=⟨toks⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • Checks if ⟨toks⟩ occurs in the option value; if it does, ⟨true keylist⟩ are executed, otherwise ⟨false keylist⟩.

    This conditional is defined only for ⟨toks⟩ options, see §3.6.1.

  • if ⟨dimen option⟩>=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • if ⟨dimen option⟩<=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • if ⟨count option⟩>=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • if ⟨count option⟩<=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • if ⟨dimen register⟩>=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • if ⟨dimen register⟩<=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • if ⟨count register⟩>=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • if ⟨count register⟩<=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • If the current value of the dimen/count option/register is greater/less than or equal to ⟨value⟩, execute ⟨true keylist⟩; else, execute ⟨false keylist⟩.

  • if nodewalk valid=⟨keylist: test nodewalk⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • If the test nodewalk finished on a valid node, ⟨true keylist⟩ is processed (in the context of the current node); otherwise, ⟨false keylist⟩ is processed.

  • if nodewalk empty=⟨keylist: test nodewalk⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • If the test nodewalk contains no (real) steps, ⟨true keylist⟩ is processed (in the context of the current node); otherwise, ⟨false keylist⟩ is processed.

  • if current nodewalk empty=⟨true keylist⟩⟨false keylist⟩(conditional)

  • If the current nodewalk contains no (real) steps, ⟨true keylist⟩ is processed (in the context of the current node); otherwise, ⟨false keylist⟩ is processed.

  • if in saved nodewalk=⟨nodewalk⟩⟨toks: nodewalk name⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • If the node at the end of ⟨nodewalk⟩ occurs in the saved nodewalk, ⟨true keylist⟩ is processed (in the context of the current node); otherwise, ⟨false keylist⟩ is processed.

  • if have delayed=⟨true keylist⟩⟨false keylist⟩(propagator)

  • If any options were delayed in the current cycle (more precisely, up to the point of the execution of this key), process ⟨true keylist⟩, otherwise process ⟨false keylist⟩. (delay n will trigger “true” for the intermediate cycles.)

    This key assumes that the processing order of the innermost invocation of process keylist or process keylist’ is given by processing order. If this is not the case, explicitely supply the processing order using if have delayed’.

  • if have delayed’=⟨nodewalk⟩⟨true keylist⟩⟨false keylist⟩(propagator)

  • Like if have delayed, but assume the processing order given by ⟨nodewalk⟩.

The following keys are shortcuts: they execute their corresponding if ... conditional for every node in the subtree of the current node (including the node itself). In other words:

where ...⟨arg\(_1\)⟩…⟨arg\(_n\)⟩/.style={for tree={if ...=⟨arg\(_1\)⟩…⟨arg\(_n\)⟩}}

  • Except in special circumstances, you probably don’t want to embed keys from the where family within a for tree, as this results in two nested loops. It is more usual to use an if family key there. For an example where using where actually does the wrong thing, see question Smaller roofs for forest on Stackexchange.

  • where=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where ⟨option⟩=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where ⟨boolean option⟩=⟨true keylist⟩⟨false keylist⟩(conditional)

  • where in ⟨toks option⟩=⟨toks⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where ⟨dimen option⟩>=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where ⟨dimen option⟩<=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where ⟨count option⟩>=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where ⟨count option⟩<=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where ⟨dimen register⟩>=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where ⟨dimen register⟩<=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where ⟨count register⟩>=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where ⟨count register⟩<=⟨value⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where nodewalk valid=⟨toks: nodewalk name⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where nodewalk empty=⟨toks: nodewalk name⟩⟨true keylist⟩⟨false keylist⟩(conditional)

  • where in saved nodewalk=⟨nodewalk⟩⟨toks: nodewalk name⟩⟨true keylist⟩⟨false keylist⟩(conditional)