# How to get the structure tree diagram

I want to obtain a structure tree diagram displayed above in Latex, but I don't know how to edit it. I am trying to construct this diagram in the following way, and the outcome is not the same as the original picture. The problems with which I am confronted are:

1. I do not know how to adjust the distance between two nodes in the same level;
2. I do not know how to decorate the circles as it's done in the original picture.

Any help will be appreciated.

\documentclass{article}
\usepackage{forest, color}
\begin{document}
% Globally set style for your trees
\forestset{circles/.style={for tree={circle,draw, text width =1em,text centered,edge=->}}}
\begin{forest}
circles
[{$f$}
[{$e$}]
[{$f$}
[{$x$}]
[{$i$}
[{$x$}]
]
]
]
\end{forest}

• What do you mean by 'decorate'?
– cfr
May 31, 2017 at 22:54

It isn't clear which aspects of the image you mean by 'decorations'. I assume the labels above left/right of the nodes. The code is annotated with comments explaining how the distances between levels and siblings are increased, how the nodes are made uniform in size, and how the labels are added in the relevant places.

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{
circles/.style={
for tree={
math content,
circle,
draw,
text width=1em,
text centered,
edge=->,
s sep'+=5pt, % increase distance between siblings by 5pt
l sep'+=5pt, % increase distance between levels by 5pt
},
before typesetting nodes={
for tree={% make circles uniform in size
content/.wrap value=\strut ##1,
% split content of nodes into content and value for my label
split option={content}{:}{content,my label},
},
},
},
my label/.style={% put the label left or right, depending on which child and level we have
label/.process={On=On=|? {n'}{1} {level}{0} {45:$#1$}{135:$#1$}},
},
}
\begin{document}
\begin{forest}
circles
[f:\epsilon
[e:1]
[f:2
[x:21]
[i:22
[x:221]
]
]
]
\end{forest}
\end{document}


If you are essentially counting down the tree (except for the root), you don't even need to specify the labels. You can achieve the same result with something like

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{
circles/.style={
for tree={
math content,
circle,
draw,
text width=1em,
text centered,
edge=->,
s sep'+=5pt, % increase distance between siblings by 5pt
l sep'+=5pt, % increase distance between levels by 5pt
},
before typesetting nodes={
for tree={% make circles uniform in size
content/.wrap value=\strut ##1,
if level=0{
my label=\epsilon,
}{
temptoksa=,
for nodewalk={filter={reverse={current and ancestors}}{>On>{level}{0}}}{temptoksa+/.option=n},
my label/.register=temptoksa,
},
},
},
},
my label/.style={% put the label left or right, depending on which child and level we have
label/.process={On=On=|? {n'}{1} {level}{0} {45:$#1$}{135:$#1$}},
},
}
\begin{document}
\begin{forest}
circles
[f
[e]
[f
[x]
[i
[x]
]
]
]
\end{forest}
\end{document}


For comparison:

• Firstly, thank you very much for your solution to my problems @cfr. In addition, I mean the numerical sequence (for example, 21, 22, etc.) above the left/right circle by the word decoration. When I am compiling the two Tex documents given above there are two mistakes to occur indicated as follows: for the first Tex, the error is Package pgfkeys Error: I do not know the key '/tikz/split option', to which you passed '{content}{:}{content,my label}', and I am going to ignore it. Perhaps you misspelled it. [\end{forest}];
– GL_n
Jun 1, 2017 at 9:55
• for the second Tex, the error isPackage pgfkeys Error: I do not know the key '/forest/label/.process', to which you passed 'On=On=|? {n'}{1} {level}{0} {45:$\epsilon$}{135:$\epsilon$}', and I am going to ignore it. Perhaps you misspelled it. [\end{forest}]. I do not know how to fix these mistakes.
– GL_n
Jun 1, 2017 at 9:55
• @GL_n You have an old version of Forest and need to update your TeX installation. (Updating just Forest may work, but a general update is recommended.)
– cfr
Jun 1, 2017 at 12:40
• @cfrThank you so much for your answer! Your Tex document do work after I update my Tex engine and the Forest package. Now I have another question: how to adjust the size of the circles and the width of the arrows in the tree? Actually, I want to get the circles with smaller size, and the arrows with fatter stroke.
– GL_n
Jun 1, 2017 at 14:53
• @GL_n Change the value of text width to change the size of the circles. You can say edge+={->, thick} or whatever you prefer to change the thickness of the arrows.
– cfr
Jun 2, 2017 at 11:05