tikz.dev / forest Manual

Forest

Reference

3.6 Options and registers

Forest introduces two types of data storage: node options (or just options for short) and registers.

Options store data related to particular nodes. Each node has its own set of option values, i.e. the value of an option at some node is independent of its value at other nodes: in particular, setting an option of a node does not set this option for the node’s descendants. Register values are not associated to nodes.

Note that option and register keys share the same “namespace” (pgfkeys path and pgfmath function names) so it is not possible to have an option and a register of the same name!

3.6.1 Setting

The simplest way to set the value of an option or a register is to use the key of the same name.

  • ⟨option⟩=⟨value⟩(assignment)

  • Sets the value of ⟨option⟩ of the current node to ⟨value⟩.

    Note that option types ⟨keylist⟩ and ⟨autowrapped toks⟩ redefine this basic key.

Options can also be set for the non-current node:

  • relative node name.⟨option⟩=⟨value⟩(assignment)

  • Sets the value of ⟨option⟩ of the node specified by ⟨relative node name⟩ to ⟨value⟩.

    Notes:

    Compiled output

Additional keys for setting and modifying the value of an option or a register exist, depending on its data type. Informally, you can think of these keys as augmented operators known from various programming languages.

  • ⟨toks⟩(type)

  • contains ’s ⟨balanced text⟩ [Knuth 1996, 275].

    A toks ⟨option⟩ additionally defines the following keys:

    • ⟨option⟩+=⟨toks⟩(augmented assignment)

    • appends the given ⟨toks⟩ to the current value of the option.

    • +⟨option⟩=⟨toks⟩(augmented assignment)

    • prepends the given ⟨toks⟩ to the current value of the option.

  • ⟨autowrapped toks⟩(type)

  • is a subtype of ⟨toks⟩ and contains ’s ⟨balanced text⟩ [Knuth 1996, 275].

    ⟨option⟩=⟨toks⟩ of an autowrapped ⟨option⟩ is redefined to ⟨option⟩/.wrap value=⟨toks⟩ of a normal ⟨toks⟩ option.

    Keyvals ⟨option⟩+=⟨toks⟩ and +⟨option⟩=⟨toks⟩ are redefined to ⟨option⟩+/.wrap value=⟨toks⟩ and +⟨option⟩/.wrap value=⟨toks⟩, respectively. The normal toks behaviour can be accessed via keys ⟨option⟩’, ⟨option⟩+’, and +⟨option⟩’.

  • ⟨keylist⟩(type)

  • is a subtype of ⟨toks⟩ and contains a comma-separated list of ⟨key⟩[=⟨value⟩] pairs.

    Augmented assignment operators ⟨option⟩+ and +⟨option⟩ automatically insert a comma before/after the appended/prepended material.

    Augmented assignment operator ⟨option⟩-=⟨keylist⟩ deletes the keys from keylist ⟨option⟩. ⟨keylist⟩ specifies which keys to delete. If a key is given no value, all occurrences of that key will be deleted. If a key is given a value, only occurrences with that value will be deleted. To delete occurrences without value, use special value \ forestnovalue. (Note: if you include a key in ⟨keylist⟩ more than once, only the last occurrence counts.)

    ⟨option⟩=⟨keylist⟩ of a keylist option is redefined to ⟨option⟩+=⟨keylist⟩. In other words, keylists behave additively by default. The rationale is that one usually wants to add keys to a keylist. The usual, non-additive behaviour can be accessed by ⟨option⟩’=⟨keylist⟩.

    Manipulating the keylist option using augmented assignments might have the side-effect of adding an empty key to the list.

  • ⟨dimen⟩(type)

  • contains a dimension.

    The value given to a dimension option is automatically evaluated by pgfmath. In other words, ⟨option⟩=⟨value⟩ is implicitly understood as ⟨option⟩/.pgfmath=⟨value⟩.

    For a ⟨dimen⟩ option ⟨option⟩, the following additional keys (“augmented assignments”) are defined:

    • ⟨option⟩+=⟨value⟩(augmented assignment)

    • is equivalent to ⟨option⟩=⟨option⟩()+⟨value⟩

    • ⟨option⟩-=⟨value⟩(augmented assignment)

    • is equivalent to ⟨option⟩=⟨option⟩()-⟨value⟩

    • ⟨option⟩*=⟨value⟩(augmented assignment)

    • is equivalent to ⟨option⟩=⟨option⟩()*⟨value⟩

    • ⟨option⟩:=⟨value⟩(augmented assignment)

    • is equivalent to ⟨option⟩=⟨option⟩()/⟨value⟩

    The evaluation of ⟨pgfmath⟩ can be quite slow. There are two tricks to speed things up if the ⟨pgfmath⟩ expression is simple, i.e. just a ⟨dimen⟩:

    1. pgfmath evaluation of simple values can be sped up by prepending + to the value [Tantau 2007, §62.1];

    2. use the key ⟨option⟩’=⟨value⟩ to invoke a normal assignment.

    The two above-mentioned speed-up tricks work for the augmented assignments as well. The keys for the second, -only trick are: ’+, ’-, ’*, ’: — note that for the latter two, the value should be an integer.

  • ⟨count⟩(type)

  • contains an integer.

    The additional keys and their behaviour are the same as for the ⟨dimen⟩ options.

  • ⟨boolean⟩(type)

  • contains \(0\) (false) or \(1\) (true).

    In the general case, the value given to a ⟨boolean⟩ option is automatically parsed by pgfmath (just as for ⟨count⟩ and ⟨dimen⟩): if the computed value is non-zero, \(1\) is stored; otherwise, \(0\) is stored. Note that pgfmath recognizes constants true and false, so it is possible to write ⟨option⟩=true and ⟨option⟩=false.

    If key ⟨option⟩ is given no argument, pgfmath evaluation does not apply and a true value is set. To quickly set a false value, use key not ⟨option⟩ (with no arguments).

3.6.2 Reading

Option and register values can be accessed using the four macros listed below, handlers .option and .register (§3.12) and pgfmath functions (3.18).

  • \ forestoption{⟨option⟩}(macro)

  • \ foresteoption{⟨option⟩}(macro)

  • \ forestregister{⟨register⟩}(macro)

  • \ foresteregister{⟨register⟩}(macro)

  • These macros expand to the value of the given option or register. Note that \ forestoption and \ foresteoption expand to the value of the given option of the current node; to access option values of a non-current node, use pgfmath functions.

    In the context of \edef, \forestoption and \forestregister expand precisely to the token list of the option value, while \foresteoption and \foresteregister fully expand the value.

    • These macros can be useful in code introduced by TeX or PGF’s handler .expanded [Tantau 2007, §55.4.6].

3.6.3 Declaring

Using the following keys, users can also declare their own options and registers. The new options and registers will behave exactly like the predefined ones.

Note that the declaration of an option must provide a default value, while the declaration of a register must not do that (registers are initialized to the empty string, 0pt or 0, as appropriate for the type). The default value of an option will be assigned to any newly created nodes; the existing nodes are not affected.

  • declare toks=⟨option name⟩⟨default value⟩

  • Declares a ⟨toks⟩ option.

  • declare autowrapped toks=⟨option name⟩⟨default value⟩

  • Declares an ⟨autowrapped toks⟩ option.

  • declare keylist=⟨option name⟩⟨default value⟩

  • Declares a ⟨keylist⟩ option.

  • declare dimen=⟨option name⟩⟨default value⟩

  • Declares a ⟨dimen⟩ option. The default value is processed by ⟨forestmath⟩.

  • declare count=⟨option name⟩⟨default value⟩

  • Declares a ⟨count⟩ option. The default value is processed by ⟨forestmath⟩.

  • declare boolean=⟨option name⟩⟨default value⟩

  • Declares a ⟨boolean⟩ option. The default value is processed by ⟨forestmath⟩.

  • declare toks register=⟨register name⟩

  • Declares a ⟨toks⟩ register.

  • declare autowrapped toks register=⟨register name⟩

  • Declares an ⟨autowrapped toks⟩ register.

  • declare keylist register=⟨register name⟩

  • Declares a ⟨keylist⟩ register.

  • declare dimen register=⟨register name⟩

  • Declares a ⟨dimen⟩ register.

  • declare count register=⟨register name⟩

  • Declares a ⟨count⟩ register.

  • declare boolean register=⟨register name⟩

  • Declares a ⟨boolean⟩ register.

Several scratch registers are predefined:

  • temptoksa,(register)

  • temptoksb, temptoksc, temptoksd Predefined ⟨toks⟩ registers.

  • tempkeylista,(register)

  • tempkeylistb, tempkeylistc, tempkeylistd Predefined ⟨keylist⟩ registers.

  • tempdima,(register)

  • tempdimb, tempdimc, tempdimd, tempdimx, tempdimy, tempdiml, tempdims, tempdimxa, tempdimya, tempdimla, tempdimsa, tempdimxb, tempdimyb, tempdimlb, tempdimsb Predefined ⟨dimen⟩ registers.

  • tempcounta,(register)

  • tempcountb, tempcountc, tempcountd Predefined ⟨count⟩ registers.

  • tempboola,(register)

  • tempboolb, tempboolc, tempboold Predefined ⟨boolean⟩ registers.