8

This question is about a particular feature of the forest package.

According to the manual (22):

\begin{forest}<tree>\end{forest} 
\Forest[*]{<tree>} 

The environment and the starless version of the macro introduce a group; the starred macro does not, so the created nodes can be used afterwards. (Note that this will leave a lot of temporary macros lying around. This shouldn’t be a problem, however, since all of them reside in the \forest namespace.)

By default nodes in the tree get a name of the form node@id. However, if you use name=<name> then a node is instead named <name>.

Given this, my possibly naïve understanding of this feature suggested that something like one of the variations in the following code:

\documentclass{article}
\usepackage{forest}
\begin{document}
  \Forest*{
    [A, name=a
      [B, name=b]
      [C, name=c]
    ]
  }
  \tikz[overlay,remember picture]\draw [red] (a) -- (b) -- (c) -- (a);
  \tikz[overlay]\draw [blue] (a) -- (b) -- (c) -- (a);
  \tikz[remember picture]\draw [green] (a) -- (b) -- (c) -- (a);
  \tikz\draw [magenta] (a) -- (b) -- (c) -- (a);
\end{document}

might produce something roughly similar to this:

triangle ABC with overlaid red connections

However, I cannot figure out how to get anything like this, although the nodes are, indeed, accessible outside the tree in the sense that I do not get errors and, moreover, some of the above variants draw something which looks roughly isomorphic to my target image:

isomorphic-ish connecting lines in magenta, green and blue

Note that I would also expect, as implied above, to get errors if I tried to reference a node created within an ordinary forest environment, after that environment ended. But, in fact, I get just the same behaviour in this case - and with the unstarred \Forest - as with \Forest*. That is:

\documentclass{article}
\usepackage{forest}
\begin{document}
  \Forest{
    [A, name=a1
      [B, name=b1]
      [C, name=c1]
    ]
  }
  \Forest*{
    [A, name=a
      [B, name=b]
      [C, name=c]
    ]
  }
  \begin{forest}
    [A, name=a2
      [B, name=b2]
      [C, name=c2]
    ]
  \end{forest}
  \tikz[overlay,remember picture]\draw [red] (a) -- (b) -- (c) -- (a);
  \tikz[overlay]\draw [blue] (a) -- (b) -- (c) -- (a);
  \tikz[remember picture]\draw [green] (a) -- (b) -- (c) -- (a);
  \tikz\draw [magenta] (a) -- (b) -- (c) -- (a);
  \tikz[overlay,remember picture]\draw [red] (a1) -- (b1) -- (c1) -- (a1);
  \tikz[overlay]\draw [blue] (a1) -- (b1) -- (c1) -- (a1);
  \tikz[remember picture]\draw [green] (a1) -- (b1) -- (c1) -- (a1);
  \tikz\draw [magenta] (a1) -- (b1) -- (c1) -- (a1);
  \tikz[overlay,remember picture]\draw [red] (a2) -- (b2) -- (c2) -- (a2);
  \tikz[overlay]\draw [blue] (a2) -- (b2) -- (c2) -- (a2);
  \tikz[remember picture]\draw [green] (a2) -- (b2) -- (c2) -- (a2);
  \tikz\draw [magenta] (a2) -- (b2) -- (c2) -- (a2);
\end{document}

produces the following without error:

isomorphic variations with different environments and macros

A two-part question: ;)

  • How should this feature be used?

  • What exactly is its intended purpose, if not the one suggested by my (possibly idiosyncratic) reading of the manual?

Note that I can perfectly easily produce the target image in other, more convenient ways. It is just an example to illustrate how I expected the feature to be used.

The following is not part of the question. It is an attempt to forestall further answers which treat the question as a 'how do I draw this?' rather than a 'what is this meant to be for and how am I meant to use it?'

Just for the record:

\Forest{[A[B][C, tikz={\draw [red] () -- (!s) -- (!u) -- ();}]]}

will produce a tree which looks just like the target i.e. in which the tree branches are darker because the red overlays the black, while:

\Forest{for tree={edge={draw=red}}[A[B][C, tikz={\draw [red] () -- (!s);}]]}

will produce a version which is not like the target image because the red lines are evenly coloured, but which would probably be what I actually wanted if this were a question about how to draw the target image, which it is not.

  • 1
    Why do you read packages documentation? That's just looking for trouble ;) – Gonzalo Medina Sep 8 '15 at 23:26
  • @GonzaloMedina Certainly it finds trouble often enough... ;). I was wondering if there was a way of using this to work around the problems caused by combining tikzmark, forest and XeLaTeX, raised in another question somebody asked. – cfr Sep 8 '15 at 23:40
  • @GonzaloMedina I guess that means you don't know the answer? – cfr Sep 8 '15 at 23:42
  • @GonzaloMedina I can't produce any different behaviour at all. Even if I reference nodes named in the regular environment, I don't get errors and I still get these shifted tree skeletons. – cfr Sep 9 '15 at 0:25
  • 1
    Yes. I did some experiments and got similar results. Hopefully Sašo will shed some light here. – Gonzalo Medina Sep 9 '15 at 0:46
6

The nodes remaining in memory \Forest* because introduces no group are not tikz's nodes, but forest's logical nodes, i.e. the node hierarchy.

The intended use of this feature is to be able to build a tree in several places in the document, or to reuse trees, like below. (I will include this example in the documentation.)

\documentclass{article}
\usepackage{forest}

\begin{document}
\Forest*{
  [VP,name=VP
    [V]
    [DP]
  ]
}
\forestset{for name={VP}{for tree={s=0}}}% workaround for the bug
\Forest*{
  [TP
    [T]
    [,replace by=VP]
  ]
}
\end{document}

reusing a tree

(While constructing this example, I have found a bug ... the VP in the second tree is not packed correctly ... thus the workaround. I will fix the bug in the upcoming version, which, I might announce, has progressed nicely in the past month.)

UPDATE. Answering cfr's comment: replace by' (which makes a copy of the replacement subtree) is legit in this example, and almost equivalent to replace by. The difference shows only when modifying the nodes, as shown below (or explicitely referring to their id.)

\documentclass{standalone}
\usepackage{forest}

\begin{document}
\Forest*{
  [VP,name=VP
    [V]
    [DP]
  ]
}
\forestset{for name={VP}{for tree={s=0}}}% workaround for the bug
\Forest*{
  copy name template={copy of #1},
  before typesetting nodes={
    for name={VP}{content+=?},
    for name={copy of VP}{content+=!}
  }
  [TP
    [T]
    [,replace by=VP]
    [,replace by'=VP]
  ]
}
\end{document}

illustrating <code>replace by'</code>

  • 2
    Thank you! I was very confused by this, I'm afraid. In this example, is replace by equivalent to replace by'. Or should the latter only be used within the scope of a single forest environment or command? Good to hear about the progress on the new version! – cfr Sep 9 '15 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.