4

I am having some trouble forming a game tree. My third layer appears to want to connect to the labels of the layer above, rather than the end nodes.

Here is an image

enter image description here

and the following is what is producing this:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{sgame}
\usepackage{pstricks}
\usepackage{egameps}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}
\begin{document}
\begin{figure}[!htb]
    \begin{center}
    %\newcommand{\payoff}[4][below]{\node[#1]at(#2){$(#1,#2)$};}
    \begin{tikzpicture}[scale=1.5,font=\footnotesize][!htb]
        \tikzstyle{solid node}=[circle,draw,inner sep=1.5,fill=black]
        \tikzstyle{hollow node}=[circle,draw,inner sep=1.5]
        \tikzstyle{level 1}=[level distance=15mm,sibling distance=2.5cm]
        \tikzstyle{level 2}=[level distance=15mm,sibling distance=1.5cm]
        \tikzstyle{level 3}=[level distance=15mm,sibling distance=1.5cm]

        \node[solid node,label=above:{$1$}]{}
            child{node[solid node,label=above left:{}]{}
                child{node[solid node,label=above left:{}]{} edge from parent node[left]{$L$}
                    child{node[solid node,label=above left:{}]{} edge from parent node[left]{$l$}}
                    child{node[solid node,label=above left:{}]{} edge from parent node[right]{$r$}}
                }
                child{node[solid node,label=above right:{}]{} edge from parent node[right]{$R$}
                    child{node[solid node,label=above left:{}]{} edge from parent node[left]{$l$}}
                    child{node[solid node,label=above left:{}]{} edge from parent node[right]{$r$}}
                }
            edge from parent node[left,xshift=-5]{$Left$}
            }
            child{node[solid node,label=above right:{}]{}
                child[missing]
            edge from parent node[right,xshift=5]{$Right$}
            };
    \end{tikzpicture}
    \end{center}
    \end{figure}
2
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format.
    – cfr
    Feb 13, 2015 at 4:14
  • By the way, you are missing an \end{document} in your MWE ;).
    – cfr
    Feb 13, 2015 at 4:14

3 Answers 3

3

Welcome to TeX SE! Personally, I would use forest for any tree. The code is much more compact and the package is a lot more powerful.

This is a fairly simplistic use:

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}

\begin{document}
  \tikzset{
    solid node/.style={circle,draw,inner sep=1.5,fill=black},
    hollow node/.style={circle,draw,inner sep=1.5},
  }
  \forestset{
    my left/.style = {
      edge label={node[midway, left, inner sep=5pt, font=\footnotesize] {$#1$}},
    },
    my right/.style = {
      edge label={node[midway, right, inner sep=5pt, font=\footnotesize] {$#1$}},
    },
    payoff/.style = {
      label={below:{[$#1$]}},
    }
  }
    \begin{forest}
      for tree={
        parent anchor=center,
        child anchor=center,
        l sep=15mm,
        if level=0{s=25mm}{s sep=15mm},
        font=\footnotesize,
      }
      [, solid node, label={above:1}
        [, solid node, my left=Left
          [, solid node, my left=L
            [, solid node, my left=l, payoff=8
            ]
            [, solid node, my right=r, payoff=9
            ]
          ]
          [, solid node, my right=R
            [, solid node, my left=l, payoff=3
            ]
            [, solid node, my right=r, payoff=6
            ]
          ]
        ]
        [, solid node, my right=Right
        ]
      ]
    \end{forest}
\end{document}

You could further reduce this. For example, if most nodes are solid, put that in for tree and just override where needed. phantom is probably what you want for missing although there is no example of this in your tree so it is hard to be sure.

forest tree

5
  • Thank you for this. A couple of things, can payoffs be added (brackets at the bottom) and can spacing be increased between the lines and letters like l, r, L, R etc?
    – Peggy
    Feb 13, 2015 at 4:18
  • @Peggy Yes and yes. However, I'm not quite sure what you mean about the payoffs. That is, I'm sure you can but I'm not sure exactly what it should look like.
    – cfr
    Feb 13, 2015 at 4:21
  • How would one go about the former? The latter is merely something like the following (3,1,2) under the final node. Reference here: sfu.ca/~haiyunc/notes/Game_Trees_with_TikZ.pdf page 16
    – Peggy
    Feb 13, 2015 at 4:23
  • @Peggy See edit. I defined a couple of styles for the edge labels and the payoffs. Obviously, you can adjust them to taste and need.
    – cfr
    Feb 13, 2015 at 4:29
  • @Peggy You're welcome. forest is great!
    – cfr
    Feb 13, 2015 at 4:35
3

The edge from parent node[left]{$L$} and the alike were put at the wrong positions. The new position indicated by <--are the correct positions. Also the sibling distance for level 3 was modified a little bit show the space.

Admittedly, @cfr's solution is recommended.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{sgame}
\usepackage{pstricks}
\usepackage{egameps}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}
\begin{document}
\begin{figure}[!htb]
    \begin{center}
    %\newcommand{\payoff}[4][below]{\node[#1]at(#2){$(#1,#2)$};}
    \begin{tikzpicture}[scale=1.5,font=\footnotesize][!htb]
        \tikzstyle{solid node}=[circle,draw,inner sep=1.5,fill=black]
        \tikzstyle{hollow node}=[circle,draw,inner sep=1.5]
        \tikzstyle{level 1}=[level distance=15mm,sibling distance=2.5cm]
        \tikzstyle{level 2}=[level distance=15mm,sibling distance=1.5cm]
        \tikzstyle{level 3}=[level distance=15mm,sibling distance=1cm]

        \node[solid node,label=above:{$1$}]{}
            child{node[solid node,label=above left:{}]{}
                child{node[solid node,label=above left:{}](){} 
                    child{node[solid node,label=above left:{}]{} edge from parent node[left]{$l$}}
                    child{node[solid node,label=above left:{}]{} edge from parent node[right]{$r$}}
                edge from parent node[left]{$L$}  % <--
                }
                child{node[solid node,label=above right:{}]{}
                    child{node[solid node,label=above left:{}]{} edge from parent node[left]{$l$}}
                    child{node[solid node,label=above left:{}]{} edge from parent node[right]{$r$}}
               edge from parent node[right]{$R$} %<--
                }
            edge from parent node[left,xshift=-5]{$Left$}
            } 
            child{node[solid node,label=above right:{}]{}
                child[missing]
            edge from parent node[right,xshift=5]{$Right$}
            };
    \end{tikzpicture}
    \end{center}
    \end{figure}
\end{document}
1
  • (+1) for finding the error. I took the easy option!
    – cfr
    Feb 13, 2015 at 4:36
1

Using the istgame package, you can draw a game tree. This is a Tikz-based latex package to draw game trees. You can use the istgame environment like the tikzpicture environment, which means that you can use Tikz options and macros within the istgame environment.

enter image description here

\documentclass{standalone}

\usepackage{istgame}

\begin{document}    
\begin{istgame}[scale=1.5,font=\footnotesize]
\xtShowEndPoints
\xtdistance{15mm}{30mm}
\istroot(0){1}
  \istb{Left}[l]
  \istb{Right}[r]{0,0}
  \endist
\istroot(1)(0-1){2}
  \istb{L}[l]
  \istb{R}[r]
  \endist
\xtdistance{15mm}{15mm}
\istroot(2)(1-1){1}
  \istb{\ell}[l]{1,2}
  \istb{r}[r]{3,4}
  \endist
\istroot(3)(1-2){1}
  \istb{\ell}[l]{5,6}
  \istb{r}[r]{7,8}
  \endist
\end{istgame}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .