[Tex/LaTex] Make the labels on the edges of a decision tree centered/aligned

labelspositioningtikz-pgf

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}

enter image description here

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:

\documentclass{standalone}
\usepackage{forest}
\forestset{
    treenode/.style = {rounded corners, draw, base=top},
    payoff/.style   = {align=center, base=top}
    }
\begin{document}
  \begin{forest}
  %for tree={l sep=1cm, s sep=1cm},
  for tree={child anchor=north}
    [A,treenode
      [{2\\2\\0}, payoff, edge label={node[midway,left,font=\scriptsize]{$b=0$}}]
      [B,treenode,edge label={node[midway,right,font=\scriptsize]{$b\neq 0$}}
        [{0\\2\\0}, payoff, edge label={node[midway, left, font=\scriptsize]{R}}]
        [{2b\\2b\\-5b}, payoff, edge label={node[midway, right, font=\scriptsize]{A}}]
      ]
    ]
  \end{forest}
\end{document}

Tree with forest

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 the s sep value to mess with the parent-child distance and the l 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 no maximum width option, we use the text width one (I also took the liberty of creating a style for the left and right side edge nodes, which you can alter if needed be):

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{trees}
\tikzset{
  treenode/.style = {shape=rectangle, rounded corners,
draw, align=center,
%top color=white, bottom color=blue!20
  },
  payoff/.style    = {align=center, inner sep=0.1em, text width=1.5em},
  left side node/.style={above left, inner sep=0.1em},
  right side node/.style={above right, inner sep=0.1em}
}
\begin{document}
\begin{tikzpicture}
  [
%grow                    = right,
sibling distance        = 10em,
level distance          = 4em,
every node/.style       = {font=\footnotesize},
%sloped
  ]
  \node [treenode] {A} child{node[payoff] {2\\2\\0} edge from parent node[left side node] {$b=0$}}
                       child{node [treenode] {B} 
                                child{ node [payoff] {0\\2\\0} edge from parent node[left side node] {R}}
                                child{ node [payoff] {2b\\2b\\-5b} edge from parent node[right side node] {A}}
                                edge from parent node[right side node] {$b\neq0$}
                                };
\end{tikzpicture}
\end{document}

enter image description here

Related Question