2

I installed forest 2016/03/04 v2.0.2 and started experimenting with recent forest features by modifying the source code from https://tex.stackexchange.com/a/302713/82730.

With the following code,

\documentclass[convert={size=640}]{standalone}
\usepackage{microtype}
\usepackage[T1]{fontenc}
% forest
\usepackage{forest}
% parsing tree
\forestset{
  declare toks={wff}{},
  declare toks={connective}{},
  parsing tree/.style={
    declare dimen register={parsing tree sep},
    parsing tree sep=5pt,
    % Append the current root to a new phantom root.
    for root'={
      replace by={[,phantom,append=!last dynamic node]}
    },
    % `for tree` applies only to the subtree of the new phantom root.
    for tree={
      math content,
      parent anchor=children,
      child anchor=parent,
      inner sep=0pt,
      if n children=1{!first.calign with current edge}{},
      delay={
        content=\circ,
        insert before/.wrap pgfmath arg={
          [##1,no edge,math content,anchor=base east,
          before computing xy={
            s/.pgfmath={s("!n")-\forestregister{parsing tree sep}}
          }]
        }{wff},
        if connective={}{connective/.option=wff}{},
        insert after/.wrap pgfmath arg={
          [##1,no edge,math content,anchor=base west,
          before computing xy={
            s/.pgfmath={s("!p")+\forestregister{parsing tree sep}}
          }]
        }{connective},
      }
    }
  }
}
\begin{document}
\begin{forest}
  parsing tree
  [,wff=p_0\wedge q_0,connective=\wedge
  [,wff=p_0] [,wff=q_0 []]]
\end{forest}
\end{document}

comes

enter image description here

If I take if connective={}{connective/.option=wff}{} out of delay as below,

\documentclass[convert={size=640}]{standalone}
\usepackage{microtype}
\usepackage[T1]{fontenc}
% forest
\usepackage{forest}
% parsing tree
\forestset{
  declare toks={wff}{},
  declare toks={connective}{},
  parsing tree/.style={
    declare dimen register={parsing tree sep},
    parsing tree sep=5pt,
    % Append the current root to a new phantom root.
    for root'={
      replace by={[,phantom,append=!last dynamic node]}
    },
    % `for tree` applies only to the subtree of the new phantom root.
    for tree={
      math content,
      parent anchor=children,
      child anchor=parent,
      inner sep=0pt,
      if n children=1{!first.calign with current edge}{},
      if connective={}{connective/.option=wff}{},
      delay={
        content=\circ,
        insert before/.wrap pgfmath arg={
          [##1,no edge,math content,anchor=base east,
          before computing xy={
            s/.pgfmath={s("!n")-\forestregister{parsing tree sep}}
          }]
        }{wff},
        insert after/.wrap pgfmath arg={
          [##1,no edge,math content,anchor=base west,
          before computing xy={
            s/.pgfmath={s("!p")+\forestregister{parsing tree sep}}
          }]
        }{connective},
      }
    }
  }
}
\begin{document}
\begin{forest}
  parsing tree
  [,wff=p_0\wedge q_0,connective=\wedge
  [,wff=p_0] [,wff=q_0 []]]
\end{forest}
\end{document}

I see

enter image description here

It seems declared node options are available only after a delay. Am I right?

2 Answers2

1

You are right.

The tree preamble (in your case, parsing tree) is executed just before the keys given to individual nodes (more precisely, just before the keys of the root node). At that time, all options, including content and any used-declared options, still have the default value. So yes, to access the values they are given in the bracket specification, we need to use delay (assuming, of course, that the relevant option itself is not embedded in some temporal propagator.)

See also: example (11) in the tutorial, option content in reference, and intro to the Node keys section.

  • I couldn't find the explanation of the behavior in node keys section. –  May 04 '16 at 14:17
  • Sorry, my bad, I was looking at wrong version of the docs. Please look at 3.4.1 "Stages". – Sašo Živanović May 04 '16 at 15:30
  • It says The keys given in the bracket representation are processed. In detail, default preamble is processed first, then the given preamble (both in the context of the (formal) root node) and finally the keylists given to individual nodes. I couldn't imagine the implications of forest stages when I read it. –  May 04 '16 at 22:39
  • I don't think this is correct. The current value of the option is available without delay. It is just that's the default value. Just as the only values of other options (not user declared), node contents etc. are their defaults during the first cycle. – cfr May 04 '16 at 23:37
0

It seems declared node options are available only after a delay. Am I right?

It depends what you mean.

  1. If you mean that there is something special about declared node options - as opposed to Forest-provided options or as opposed to node content, for example - then, no. There is no difference.

  2. If you mean that values given to declared options can only be processed correctly when delayed, then, no. Not exactly. It depends on how you want to process them and when and where you want to specify them.

  3. If you mean that values which depend on Forest's parsing the bracket specification of the tree are unavailable until the tree is parsed i.e. during the first cycle, then, yes. This is true. But it is true of all values provided in the specification of the tree - not just options or declared options.

Since the OP has not attributed the source of the code, I'm only guessing that this is a descendant of code I posted here. In case that's right, however, I can say the following.

The reason I designed the style this way was that, for each node in the tree, the final value of connective (cyswllt) depends on the value given to connective, if any, in the bracket specification for that node. The value of connective is changed if, and only if, it is not specified in the tree specification. Thus, whether to change it or not requires the value specified in the tree specification to be available. And that requires the specification to have been parsed during the first cycle.

Note that the current value of a declared option is always available. During the first cycle, this is typically the default value.

\documentclass[border=5pt,tikz,multi]{standalone}
\usepackage{forest}
\forestset{%
  declare toks={cyswllt}{blue},
  dosbarthu/.style={%
    for tree={
      color/.option=cyswllt,
    },
  }
}
\begin{document}
\begin{forest}
  dosbarthu
  [a
    [b]
    [c]
  ]
\end{forest}
\end{document}

effect of default value

However, if you want to be able to change the value from the default - as will usually be the case, else why use an option at all? - then you need a delay. For example:

\documentclass[border=5pt,tikz,multi]{standalone}
\usepackage{forest}
\forestset{%
  declare toks={cyswllt}{blue},
  dosbarthu/.style={%
    for tree={
      color/.option=cyswllt,
    },
  }
}
\begin{document}
\begin{forest}
  dosbarthu,
  [a, cyswllt=green,
    [b]
    [c]
  ]
\end{forest}
\forestset{%
  dosbarthu/.style={%
    for tree={
      delay={color/.option=cyswllt,},
    },
  }
}
\begin{forest}
  dosbarthu,
  [a, cyswllt=green,
    [b]
    [c]
  ]
\end{forest}
\end{document}

yields

default and overriding values

As Sašo Živanović said, this is just like the behaviour of contents in version 1 (and 2) of Forest. If you want to change its value if, and only if, no contents is specified in the tree specification, or if you want a blue node if the contents is abc and a green one otherwise, then you must ensure that the conditional which tests this is executed only after the first cycle.

cfr
  • 198,882
  • I forgot to add the reference to this question as well. It is in this question, now. –  May 05 '16 at 00:11