I would like to embed a tikzpicture
in a forest
node label:
\documentclass[
,crop=true
,varwidth=\maxdimen
]{standalone}
\usepackage{forest}
\begin{document}
\Forest{
[A\tikz{\node[circle,draw,inner sep=.15ex]{1};} [B] [C]]
}
\end{document}
However, the \tikz
part is simply ignored:
Instead the root node label should look like this:
I bet it's very easy to accomplish, but I just don't get it.
Best Answer
Nesting
tikzpicture
environments should be avoided. It guarantees unpredictability ;).However, there is a straightforward way to do this using forest which provides a
tikz
key for adding annotations to nodes after the tree is drawn:The advantage of this is that you can use relative node names. Here
.east
is theeast
anchor of the current node. We need to adjust the spacing to avoid having the nodes overlap. I've usedpositioning
which is recommended, though you could do without it if you had to for some reason.If you need the nodes closer, consider reducing or eliminating the
inner sep
:or using a negative value:
EDIT
The only problem with the above solution is that it is rather tedious if required frequently and the circled numbers may overlap with sibling nodes if the numbers get large.
Moreover, it is also possible - and perhaps slightly simpler - to use forest's
label
option to create the circled number as a label for the node.However, this is hardly less tedious to type. So, it would be convenient to create a style which allowed us to write, for example,
circ=89
to label the node with a circled89
:Now, we can write
to produce
which is more convenient.
This works fine if the numbers are all on the border of the tree, but less well if they occur in between nodes within the tree:
To solve this, we can increase the distance between the siblings for that particular node. We would like to do this automatically, but we only want to do it when necessary, so we add some conditional code to
circ
:which produces this
Complete code: