Just a quick demonstration with Forest. The major advantages of Forest lie in its power, flexibility and concise syntax. Regularities in content and styling can be turned into automated configuration rules and trees themselves can be specified very concisely. Since Forest is based on TikZ, the power of the host package is also available.
For example,
\begin{forest}
bordered tree,
[
[x, border
[x1, acc, for descendants=border
[][][]
]
[x2, rej]
[x3, for children=border
[][][]
]
]
]
\end{forest}
specifies the target tree:
![target tree](https://i.stack.imgur.com/rvVeR.png)
while the following partly explains the first and partly shows one or two additional tricks,
\begin{forest}
bordered tree,
[
[x, border, normal
[x1, acc, for descendants=border, label=left:\texttt{for descendants}
[][.,label=left:bordered children [][]][]
]
[x2, rej]
[x3, normal, for children=border, label=right:\texttt{for children}
[][.,label=right:unbordered children [][]][, for current and ancestors={edge+=blue}]
]
]
]
\end{forest}
which produces:
![annotated tree](https://i.stack.imgur.com/UFqZS.png)
Complete code:
\documentclass[border=10pt]{standalone}
\usepackage{forest}
\forestset{
declare boolean={border}{0},
bordered tree/.style={
for tree={
minimum size=4mm,
inner sep=0.5mm,
edge+={semithick},
semithick,
},
before typesetting nodes={
where border={
edge+={dashed, draw},
}{},
where={isodd(n_children)}{
tempcounta/.process={
Ow+n {n children}{(##1+1)/2}
},
for n={
> R {tempcounta} % doesn't work to plug the above in directly ??'
}{calign with current edge},
}{},
where content={}{}{normal},
},
},
/tikz/.cd,
normal/.style={circle,draw},
invis/.style={draw=none},
acc/.style={circle,thick,draw=green!50,fill=green!2},
rej/.style={circle,thick,draw=red!50,fill=red!20},
}
\begin{document}
\begin{forest}
bordered tree,
[
[x, border
[x1, acc, for descendants=border
[][][]
]
[x2, rej]
[x3, for children=border
[][][]
]
]
]
\end{forest}
\begin{forest}
bordered tree,
[
[x, border, normal
[x1, acc, for descendants=border, label=left:\texttt{for descendants}
[][.,label=left:bordered children [][]][]
]
[x2, rej]
[x3, normal, for children=border, label=right:\texttt{for children}
[][.,label=right:unbordered children [][]][, for current and ancestors={edge+=blue}]
]
]
]
\end{forest}
\end{document}
Best Answer
If you are using graph syntax, directed edges are specified within the tree specification itself. For example,
indicates a directed edge from
r
toa
.The whole point of the graph drawing libraries is that you specify the relationships and let Lua figure out the detailed structure.
Using this syntax, the directed edges are no problem. And, thanks to Gonzalo Medina's comment, I'm now able to get mathematical content into the nodes as well.
So, for example, using the graph syntax, I can produce
using this specification:
If your graphs are all trees i.e. have a single root, you might like to use a specialised tree-drawing package such as
forest
,qtree
ortikz-qtree
instead. If they are not all trees, you can still do this but only by tricking the package into thinking you are really drawing a tree, so it gets more complicated.For your tree, for example, the
forest
specification looks like this:Here including maths in the nodes is a bit more straightforward, but obviously you have to specify the precise structure of the tree you want - you can't just specify the relationships as you can with the graph drawing libraries.
Complete Code
EDIT
If you prefer to stick to the old syntax for trees, you can do that so long as you don't mix it in problematic ways with the graph drawing environment.
I don't get the problems with overlapping nodes shown in your screenshot provided I do not load the
graphdrawing
TikZ library: