# [Tex/LaTex] Tikzpicture inside forest node label

foresttikz-pgftrees

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.

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:

\documentclass[border=10pt,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{positioning}

\begin{document}

\Forest{
[A, tikz={\node[circle, draw, inner sep=.15ex, right=0pt of .east]  {1};} [B] [C]]
}

\end{document}


The advantage of this is that you can use relative node names. Here .east is the east anchor of the current node. We need to adjust the spacing to avoid having the nodes overlap. I've used positioning 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:

\Forest{
[A, tikz={\node[circle, draw, inner sep=0pt, right=0pt of .east]  {1};} [B] [C]]
}


or using a negative value:

\Forest{
[A, tikz={\node[circle, draw, inner sep=0pt, right=-2.5pt of .east]  {1};} [B] [C]]
}


# 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.

    label={[circle, draw, inner sep=.15ex, label distance=-2.5pt, anchor=west]east:<number to be circled>}


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 circled 89:

\forestset{
circ/.style={
label={[circle, draw, inner sep=.15ex, label distance=-2.5pt, anchor=west]east:#1}
},
}


Now, we can write

\Forest
{
[A, circ=3
[B
[D, circ=-4
]
]
[C, circ=4499]
]
}


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:

  circ/.style={
label={[circle, draw, inner sep=.15ex, label distance=-2.5pt, anchor=west]east:#1},
if level=0{}{
if n'=1{}{
if={equal(n_children("!u"),1)}{}{
!u.s sep+/.pgfmath={width("#1")+.3ex+.8pt},
}
},
},
}


which produces this

Complete code:

\documentclass[border=10pt,tikz]{standalone}
\usepackage{forest}
\forestset{
circ/.style={
label={[circle, draw, inner sep=.15ex, label distance=-2.5pt, anchor=west]east:#1},
if level=0{}{
if n'=1{}{
if={equal(n_children("!u"),1)}{}{
!u.s sep+/.pgfmath={width("#1")+.3ex+.8pt},
}
},
},
}
}
\begin{document}

\Forest
{
[A, circ=3
[B
[D, circ=-4
[E, circ=895]
[F]
]
]
[C, circ=4499]
]
}

\end{document}