5

I have drawn the following tree in TikZ. What I don't like about it is

  1. that I have to manually adjust the sibling distance for each level; and
  2. the syntax is too cumbersome.

It has been suggested to me by @percusse and @AlanMunn that using tikz-qtree and/or forest may be a more efficient way to produce the same result. But I don't have experience in using either package.

It would be nice if someone can reproduce my example using either of these packages, so that I can have quick example to learn from instead of reading a 150-page manual trying to find an answer.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}

\begin{document}

\tikzset{
  % Two node styles for game trees: solid and hollow
  solid node/.style={circle,draw,inner sep=1.2,fill=black},
  hollow node/.style={circle,draw,inner sep=1.2},
}

% macro for entering payoffs
\newcommand\payoff[1]{
  $\begin{pmatrix} #1 \end{pmatrix}$
}

\begin{tikzpicture}[font=\footnotesize]
  \tikzset{
    level 1/.style={level distance=15mm,sibling distance=65mm},
    level 2/.style={level distance=15mm,sibling distance=30mm},
    level 3/.style={level distance=15mm,sibling distance=15mm},
    level 4/.style={level distance=15mm,sibling distance=10mm},
  }

  \node[hollow node,label=above:{Nature}]{}
    child{node[solid node,label=left:{P1}]{}
      child{node(l1)[solid node]{}
        child{node[solid node,label=below:{\payoff{1\\-1}}]{}edge from parent node[left]{L}}
        child{node[solid node,label=below:{\payoff{0\\0}}]{}edge from parent node[right]{R}}
        edge from parent node[left]{U}
      }
      child{node(l2)[solid node]{}
        child{node[solid node,label=below:{\payoff{0\\0}}]{}edge from parent node[left]{L}}
        child{node[solid node,label=below:{\payoff{2\\-2}}]{}edge from parent node[right]{R}}
        edge from parent node[right]{D}
      }
      edge from parent node[left,xshift=-10]{A}
    }
    child{node[solid node,label=right:{P1}]{}
      child{node(r1)[solid node]{}
        child{node[solid node,label=below:{\payoff{-2\\2}}]{}edge from parent node[left]{L}}
        child{node[solid node,label=below:{\payoff{0\\0}}]{}edge from parent node[right]{R}}
        edge from parent node[left]{U}
      }
      child{node(r2)[solid node]{}
        child{node[solid node,label=below:{\payoff{0\\0}}]{}edge from parent node[left]{L}}
        child{node[solid node,label=below:{\payoff{-1\\1}}]{}edge from parent node[right]{R}}
        edge from parent node[right]{D}
      }
      edge from parent node[right,xshift=10]{B}
    }
  ;

  \draw[dashed](l1)--(l2)--node[midway,above]{P2}(r1)--(r2);
\end{tikzpicture}

\end{document}
  • Have you at least tried to do something with tikz-qtree and/or forest. Converting the example to the [/] syntax might be the first thing to do … – Qrrbrbirlbel May 8 '13 at 23:32
  • @Qrrbrbirlbel: Yes I've tried. My difficulty with tikz-qtree is adding the labels to the nodes and edges. I've never used forest before, and when I tried using \tikzset to set my node styles, I got an error. Still reading the forest manual trying to find out... – Herr K. May 8 '13 at 23:36
10

Since this answer involves a different package, I've added it separately. Here's a solution using the forest package. The principles are similar, but I think that once you get used to the syntax, it might be a little easier to draw your trees using this package.

\documentclass{article}
\usepackage{forest}
\usepackage{amsmath}

\tikzset{
  % Two node styles for game trees: solid and hollow
  solid node/.style={circle,draw,inner sep=1.2,fill=black},
  hollow node/.style={circle,draw,inner sep=1.2},
  % styles for long branch labels
  left label/.style={above left,midway},
  right label/.style={above right,midway}
}

% macro for entering payoffs
\newcommand\payoff[1]{
  $\begin{pmatrix} #1 \end{pmatrix}$
}
\begin{document}

\begin{forest}
[ ,for tree={s sep=.5in},label={Nature},hollow node
  [ ,for tree={solid node},edge label={node[left label]{A}}
        [ ,name=P2left,edge label={node[left label] {U}}
          [ ,label={below:\payoff{1\\-1}},edge label={node[left,midway] {L}} ]
          [ ,label={below:\payoff{0\\0}},edge label={node[right,midway] {R}} ] ]
        [ ,edge label={node[right label] {D}}
          [ ,label={below:\payoff{0\\0}},edge label={node[left,midway] {L}} ]
          [ ,label={below:\payoff{2\\-2}},edge label={node[right,midway] {R}} ] ]   
  ]
  [ ,for tree={solid node},edge label={node[right label]{B}}
  [ ,edge label={node[left label] {U}}
        [ ,label={below:\payoff{-2\\2}},,edge label={node[left,midway] {L}} ]
        [ ,label={below:\payoff{0\\0}},,edge label={node[right,midway] {R}} ]    
    ]
  [ ,name=P2right,edge label={node[right label] {D}}
    [ ,label={below:\payoff{0\\0}}, edge label={node[left,midway] {L}} ]
        [ ,label={below:\payoff{-1\\1}},,edge label={node[right,midway] {R}} ] ]
  ]
]
\draw[dashed] (P2left) --  node[above] {P2} (P2right);
\end{forest}
\end{document}

output of code

  • I see. So \tikzset has to be used outside of the forest environment... Thanks again for the answers. They are very helpful! – Herr K. May 9 '13 at 5:10
  • \forestset{ left label/.style={ edge label={node[left label]{#1}} }, right label/.style={ edge label={node[right label]{#1}} }, where n children=0{ if n=1{ edge label={node[left,midway] {L}} }{ edge label={node[right,midway] {R}} } }{} } – cfr Dec 20 '15 at 2:47
7

This is not the answer you asked for but...

I (ab)use the automata package to write game trees because its structure and syntax are more intuitive than all the trees packages that are available.

This is how I would implement your example. (ps. later you can adapt it to label the information sets instead of the nodes by using option pos of node. Also you can fine tune the info. sets using the in and out options of edge.)

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{external,automata,trees,positioning,shadows,arrows,shapes.geometric}

\begin{document}

\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,semithick]
\tikzstyle{every state}=[fill,draw=none,green,text=white,circular drop shadow]
\tikzstyle{accepting}=[circle split, draw,text=black,inner sep=0.05cm]
\tikzstyle{initial}=[red,text=white]

 \node[state,initial]   (0)  {Nature};
 \node[state]           (A) [below left of=0]  {P1};
 \node[state]               (B) [below right of=0] {P1};
 \node[state]           (AU) [left of=A]  {P2};
 \node[state]           (AD) [below of=A] {P2};
 \node[state]           (BU) [below  of=B]  {P2};
 \node[state]           (BD) [right of=B] {P2};
 \node[accepting]    (AUL) [above left of=AU] {$1$\nodepart{lower}$-1$};
 \node[accepting]    (AUR) [below left of=AU] {$0$\nodepart{lower}$0$};
 \node[accepting]    (ADL) [left of=AD]  {2\nodepart{lower}-2};
 \node[accepting]    (ADR) [below of=AD] {0\nodepart{lower}0};
 \node[accepting]    (BUL) [below of=BU]   {2\nodepart{lower}-2};
 \node[accepting]    (BUR) [right of=BU] {0\nodepart{lower}0};
 \node[accepting]    (BDL) [below right of=BD]  {0\nodepart{lower}0};
 \node[accepting]    (BDR) [above right of=BD] {$1$\nodepart{lower}$-1$};

\path(0) edge node[above]{A}(A) edge node[above]{B}(B);
\path(A) edge node[above]{U}(AU) edge node[left]{D}(AD);
\path(B) edge node[right]{U}(BU) edge node[above]{D}(BD);
\path(AU) edge node[above]{L}(AUL) edge node[left]{R}(AUR);
\path(AD) edge node[above left]{L}(ADL) edge node[right]{R}(ADR);
\path(BU) edge node[left]{L}(BUL) edge node[above]{R}(BUR);
\path(BD) edge node[below left]{L}(BDL) edge node[above left]{R}(BDR);
\path(AU) edge [-,dotted,thick,bend right] (AD);
\path(AD) edge [-,dotted,thick] (BU);
\path(BU) edge [-,dotted,thick,bend right] (BD);

 \end{tikzpicture}

enter image description here

5

This type of tree isn't a lot easier to draw with tikz-qtree since it was designed for drawing linguistic trees, the branches of which are almost never labelled. To label an edge with tikz-qtree you have to draw each edge separately. So it's hard to tell whether the following example using tikz-qtree will help you or not, but at least it should show you how you could do such a tree with the package.

\documentclass{article}
\usepackage{tikz-qtree}
\usepackage{amsmath}

\tikzset{
  % Two node styles for game trees: solid and hollow
  solid node/.style={circle,draw,inner sep=1.2,fill=black},
  hollow node/.style={circle,draw,inner sep=1.2},
}

% macro for entering payoffs
\newcommand\payoff[1]{
  $\begin{pmatrix} #1 \end{pmatrix}$
}

\begin{document}

\begin{tikzpicture}[every level 0 node/.style={draw,hollow node},
                   every level 1 node/.style={draw,solid node},
                   every level 2 node/.style={draw,solid node},
                   every level 3 node/.style={draw,solid node},
                   level distance=.65in,
                   edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node[label={Nature}]{};  
        \edge node [auto=right ] {A}; [.\node[label=left:P1]{};
            \edge node [auto=right] {U}; [.\node (P2left) {};
                \edge node[auto=right] {L}; [.\node [label=below:{\payoff{1\\-1}}] {}; ]
                \edge node[auto=left] {R}; [.\node [label=below:{\payoff{0\\0}}] {}; ]]
            \edge node [auto=left] {D}; [.{} 
                \edge node[auto=right] {L}; [.\node [label=below:{\payoff{0\\0}}] {}; ]
                \edge node[auto=left] {R}; [.\node [label=below:{\payoff{2\\-2}}] {}; ]]] 
        \edge node [auto=left] {B}; [.\node[label=right:P2]{};
        \edge node [auto=right] {U}; [.{}
                \edge node[auto=right] {L}; [.\node [label=below:{\payoff{-2\\2}}] {}; ]
                \edge node[auto=left] {R}; [.\node [label=below:{\payoff{0\\0}}] {}; ]]
            \edge node [auto=left] {D}; [.\node (P2right) {}; 
                \edge node[auto=right] {L}; [.\node [label=below:{\payoff{0\\0}}] {}; ]
                \edge node[auto=left] {R}; [.\node [label=below:{\payoff{-1\\1}}] {}; ]]] ]
        ] 
\draw[dashed] (P2left) --  node[above] {P2} (P2right);
\end{tikzpicture}
\end{document}

output of code

  • Thank you very much! It looks like tikz-qtree wouldn't simplify the task too much. But the spacing is indeed much nicer now. – Herr K. May 9 '13 at 5:03
4

Here's a somewhat unwise version which is hard to read or generalize, but is (at least to me) satisfyingly minimal.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\tikz[auto label/.style args={at #1 with #2}{
        label={[label distance=#1]\the\tikznumberofcurrentchild*180:#2}
    },
    dot/.style={inner sep=0.0625cm, circle, draw},
    every child node/.style={dot, fill},
    level distance=1.5cm,
    level/.style={sibling distance=6cm/(\tikztreelevel^1.25)}
]{
\node [dot, label=above:Nature, ] (tree) {}
    child foreach \n/\l [count=\a] in {P1/A, P1/B}{ 
        node [auto label=at 0cm with \n] {} 
        child foreach \l [count=\b] in {U, D}{ node {}  
            child foreach \l [count=\c, evaluate={\p=int(-(\b==\c)*((\a-1)*3-\c));\q=int(-\p);}] in {L, R} { 
                node [label=below:{$\begin{pmatrix}\p\\\q\end{pmatrix}$}] {}     
                edge from parent  coordinate [auto label=at 0cm with \l] 
            } 
            edge from parent  coordinate [auto label=at 0cm with \l] 
        }
        edge from parent coordinate [auto label=at 0.25cm with \l]   
    };
    \draw [dashed] (tree-1-1) -- (tree-2-2) node [midway, above] {P2};
}
\end{document}

enter image description here

3

This is another solution using the istgame package, which is based on TikZ. Since the istgame environment is almost the same as the tikzpicture environment, you can use any commands and options available in the tikzpicture within the istgame environment.

enter image description here

\documentclass{standalone}

\usepackage{amsmath}
\usepackage{istgame}

\begin{document}

\begin{istgame}
\xtShowEndPoints
\xtdistance{15mm}{50mm}
\istroot(0)[initial node]{Nature}
  \istb{A}[al]
  \istb{B}[ar]
  \endist
\xtdistance{15mm}{24mm}
\istroot(l)(0-1)<135>{P1}
  \istb{U}[l]
  \istb{D}[r]
  \endist
\istroot(r)(0-2)<45>{P1}
  \istb{U}[l]
  \istb{D}[r]
  \endist
\xtdistance{15mm}{12mm}
\istroot(l1)(l-1)
  \istb{L}[l]{\binom{1}{-1}}
  \istb{R}[r]{\binom{0}{0}}
  \endist
\istroot(l2)(l-2)
  \istb{L}[l]{\binom{0}{0}}
  \istb{R}[r]{\binom{2}{-2}}
  \endist
\istroot(r1)(r-1)
  \istb{L}[l]{\binom{-2}{2}}
  \istb{R}[r]{\binom{0}{0}}
  \endist
\istroot(r2)(r-2)
  \istb{L}[l]{\binom{0}{0}}
  \istb{R}[r]{\binom{-1}{1}}
  \endist
\xtInfoset[dashed](l1)(r2){P2}
\end{istgame}

\end{document}
  • Thanks for the addition, InSung! Your package looks amazing. I'll have to spend some time studying it. – Herr K. Nov 20 '17 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.