I am trying to create a decision tree and make the labels on the edges aligned. However, as shown in the following picture, the labels on the two edges are not on the same level. I tried to use pos=0.5
and midway
options to fix this, but apparently this does not work. Could someone help me with that?
\tikzset{
treenode/.style = {shape=rectangle, rounded corners,
draw, align=center,
%top color=white, bottom color=blue!20
},
dummy/.style = {circle,draw},
payoff/.style = {align=center}
}
\begin{tikzpicture}
[
%grow = right,
sibling distance = 10em,
level distance = 4em,
edge from parent/.style = {draw},
every node/.style = {font=\footnotesize},
%sloped
]
\node [treenode] {A}
child { node [payoff] {2\\2\\0}
edge from parent node [above,pos=0.5] {$b=0$} }
child { node [treenode] {B}
child { node [payoff] {0\\2\\0}
edge from parent node [above,pos=0.5] {R} }
child { node [payoff] {2b\\2b\\-5b}
edge from parent node [above,pos=0.5] {A}
}
edge from parent node [above] {$b \ne 0$}};
\end{tikzpicture}
Best Answer
EDIT: Currently, I've been seeing a few other questions about tree making with TikZ. Seems that there is a far more powerful package (but since it's based on TikZ could be made as a library IMHO) to do these kind of drawings: the
forest
package. And indeed, with it the syntax is a bit easier and the output consumes, nearly always, less space, take a look:As you can see, much less space. Although in this case shrinking the tree too much does not look too nice, but fear not as it is possible to control the space. Uncomment the line
for tree={l sep=1cm, s sep=1cm},
to get better results and Change thes sep
value to mess with the parent-child distance and thel sep
for sibling distance.Since the
payoff
nodes are not exactly the same size neither are the lines that connect them with the parent nodes. If you make them have the same width then all will be fine. To do so, as there is nomaximum width
option, we use thetext width
one (I also took the liberty of creating a style for the left and right sideedge
nodes, which you can alter if needed be):