# Small corrections needed for game tree

I've drawn what I would like the result to be:

The current game tree looks like this:

With the current code:

\documentclass[12pt,twoside]{book}%{book}
\usepackage[a4paper, hmargin={2.5cm, 2.5cm}, vmargin={2.5cm, 2.5cm},bindingoffset=6mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[dvipsnames]{xcolor}
\usetikzlibrary{fit}% <-- new

\begin{document}

\begin{figure}
\begin{tikzpicture}[
font = \footnotesize,
edge from parent/.style = {draw ,thin},
SN/.style = {%solid node
circle, inner sep=1.2 ,fill=blue},
HN/.style = {%hollow node
circle, inner sep=1.2, draw=blue,
},
level distance = 25mm,
level 1/.style = {sibling distance=50mm},
level 2/.style = {sibling distance=40mm}
]
%---
%-------
\node (n0) [HN] {}
child{ node (n1) [SN]  {}
edge from parent node[left]{$n_c$ buy}
}
child{ node (n2) [SN]  {}
child{node[HN]  {}
edge from parent node[left]{$1-n_c$ buy}}
child{node[HN]  {}
};
\draw[<-,shorten <=1pt]
(n0) -- + (2,1) node[right,align=left] {Entrepreneur\\
sets $P_c$ };
\draw[<-,shorten <=1pt]
(n2) -- + (2,1) node[right,align=left] {Entrepreneur\\
sets $P_r$ };
\end{tikzpicture}
\caption{Timing of game}
\label{Testing label}
\floatfoot{\textit{Source}: Own illustration}
\end{figure}

\end{tikzpicture}
\end{document}


I don't know how to add the timelines -----t=0,1,2 on each stage and how to implement the profit and utility functions after each node. Guidance much appreciated :)

• Anyone that can help? :/ – Saud May 26 '16 at 19:00
• Perhaps, but it would really be appreciated if you could fix the code so that it compiles without errors. – Torbjørn T. May 27 '16 at 19:52

The missing text has been added as labels to the nodes. I defined a style addlabel for convenience. The amsmath package is added for the \text macro.

For the missing lines, I use the syntax of the calc library to draw a line relative to the n0 node, and I add a coordinate at each end of that line (l1 and l2). The next two lines are drawn with perpendicular coordinates, e.g. (l1 |- n2) is the point that has the x-coordinate of l1 and the y-coordinate of n2. This makes it easy to draw the lines of the same length.

I also removed the xcolor package, as that is already loaded by tikz, and moved the dvipsnames option to the documentclass.

I don't know where \floatfoot comes from, so that is commented out, and the extra \end{tikzpicture} you had is removed. I also commented the \linespread, do you really want that? (Personally I'd remove it.)

\documentclass[12pt,twoside,dvipsnames]{book}%{book}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[
font = \footnotesize,
edge from parent/.style = {draw ,thin},
SN/.style = {%solid node
circle, inner sep=1.2 ,fill=blue,
},
HN/.style = {%hollow node
circle, inner sep=1.2, draw=blue,
},
level distance = 25mm,
level 1/.style = {sibling distance=50mm},
level 2/.style = {sibling distance=40mm}
]
%---
%-------
\node (n0) [HN] {}
child{ node (n1) [SN,addlabel={$\pi_1=\text{equation 1}$\\$u_1=\text{utility 1}$}]  {}
edge from parent node[left]{$n_c$ buy}
}
child{ node (n2) [SN]  {}
child{node[HN,addlabel={$\pi_2=\text{equation 2}$\\$u_2=\text{utility 2}$}]  {}
edge from parent node[left]{$1-n_c$ buy}}
};
\draw[<-,shorten <=1pt]
(n0) -- + (2,1) node[right,align=left] {Entrepreneur\\
sets $P_c$ };
\draw[<-,shorten <=1pt]
(n2) -- + (2,1) node[right,align=left] {Entrepreneur\\
sets $P_r$ };

\draw [dotted] ($(n0.center)+(-4cm,0)$) coordinate (l1) -- ($(n0.center) + (6cm,0)$) coordinate (l2) node[right] {$t=0$};
\draw [dotted] (l1 |- n2) -- (l2 |- n2) node[right] {$t=1$};
\draw [dotted] (l1 |- n3) -- (l2 |- n3) node[right] {$t=3$};
\end{tikzpicture}
\caption{Timing of game}
\label{Testing label}
%    \floatfoot{\textit{Source}: Own illustration}
\end{figure}
\end{document}


A solution with forest

# Code

\documentclass[12pt,twoside]{book}%{book}
\usepackage[a4paper, hmargin={2.5cm, 2.5cm}, vmargin={2.5cm, 2.5cm},bindingoffset=6mm]{geometry}
\usepackage{forest,mathtools}
\forestset{
declare toks={elo}{}, % Edge Label Options
anchors/.style={anchor=#1,child anchor=#1,parent anchor=#1},
decision edge label/.style n args=3{
edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut\unexpanded{#3}}}
},
decision/.style={if n=1
{decision edge label={left}{east}{#1}}
{decision edge label={right}{west}{#1}}
},
game tree/.style={
for tree={
s sep=30mm,l=25mm,
if n children=0{anchors=north}{
if n=1{anchors=south east}{anchors=south west}},
math content,
},
anchors=south,
dot={fill=white},for descendants={dot={fill=blue}},
delay={for descendants={split option={content}{;}{decision,content}}},
},
}
\newcommand\payoff[1]{\begin{matrix}#1\end{matrix}}

\begin{document}

\begin{figure}
\centering
\begin{forest}
game tree,
[
[$n_c$ buy;\payoff{\pi_1=\text{equation 1} \\ u_1=\text{utility 1}}]
[$1-n_c$ buy;\payoff{\pi_2=\text{equation 2} \\ u_2=\text{utility 2}},dot={fill=white}]
]
]
\draw[<-,shorten <=3pt]
(!r.anchor) -- + (2,1) node[right,align=left] {Entrepreneur\\
sets $P_c$ };
\draw[<-,shorten <=3pt]
(!2.anchor) -- + (2,1) node[right,align=left] {Entrepreneur\\
sets $P_r$ };
\draw[dotted](current bounding box.west|-!r.anchor)--(current bounding box.east|-!r.anchor)node[right,overlay]{$t=0$};
\draw[dotted](current bounding box.west|-!2.anchor)--(current bounding box.east|-!2.anchor)node[right,overlay]{$t=1$};
\draw[dotted](current bounding box.west|-!21.anchor)--(current bounding box.east|-!22.anchor)node[right,overlay]{$t=2$};
\end{forest}
\caption{Timing of game}
\label{Testing label}
% \floatfoot{\textit{Source}: Own illustration}
\end{figure}

\end{document}


# Output

A shorter but inflexible forest implementation:

## Code

\documentclass[tikz]{standalone}
\usepackage{forest}\usetikzlibrary{arrows.meta}
\begin{document}
\begin{forest}
Pin/.style={pin={[pin edge={thin,shorten <=4\pgflinewidth,black,{Latex[round]}-,
to path={-- (\tikztotarget.west)}}, pin distance=1.5cm, align=left]
10:Entrepreneur\\ sets $P_#1$}},
buy/.style={edge label={node[inner sep=.1667em, /forest/if level=2{near end}{midway},
/forest/if n=1{above left}{above right}] {#1 buy}}},
c/.style={node options={circle,fill,outer sep=0pt,inner sep=.4mm},anchor=center},
foo/.style={anchor=parent, child anchor=parent, node options={align=left,
append after command={(.parent) edge[fill,to path={circle[radius=.4mm]}] ()}},
content={$\pi_{\pgfmathprint{level}} = {}$equation \pgfmathprint{level}\\
$u_{\pgfmathprint{level}} = {}$utility \pgfmathprint{level}}},
for tree={l sep=2.5cm, s sep*=5,if n=1{alias/.wrap pgfmath arg={level-#1}{level()}}{}}
[, c, Pin=c, alias=level-0
[, foo, buy=$n_c$]
[, foo, buy=$1-n_c$]
[, foo, content={0\\0}, buy=not] ] ]
\path(current bounding box.west)coordinate(cbbw)(current bounding box.east)coordinate(cbbe);
\foreach \l[evaluate={\a={\l==0?"center":"north"};}] in {0,...,2}
\draw[dashed] (cbbw |- level-\l.\a) -- (cbbe |- level-\l.\a) node[right] {$t=\l$};
\end{forest}
\end{document}

• Nice ;) Do you need node options for c? – cfr May 28 '16 at 2:11

This is a demonstration using an experimental package justtrees. (Code available on request.)

justtrees is a forest-based package designed to make it easy to align labels horizontally with the different levels of the tree. Labels may be aligned to the left and/or right of the tree and formatting may be configured globally. just={<label>} adds a label, by default, to the right as shown here.

The backgrounds library is used to ensure the dotted lines are drawn behind the tree and arrows.meta is used for the circles which are filled with white or blue to obscure the dots. justtrees automatically figures out where to place the labels on the right. Since the package is based on forest, forest's facilities are used to automate the addition of these labels, the placement and format of edge labels and the overall style of the tree.

Once configured, the tree itself is specified quite concisely with

  [, pin=45:{Entrepreneur\\sets $P_c$}
[{$\pi_1 = \text{equation 1}$\\$u_1 = \text{utility 1}$}, edge label={$n_c$ buy}
]
[, edge label=not buy, pin=45:{Entrepreneur\\sets $P_r$}
[{$\pi_2 = \text{equation 2}$\\$u_2 = \text{utility 2}$}, edge label={$1 - n_c$ buy}
]
[$0$\\$0$, edge label=not buy
]
]
]


\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{justtrees,amsmath}% justtrees rev 4959
\usetikzlibrary{arrows.meta,backgrounds}
\begin{document}
\begin{justtree}
{
/tikz/every pin/.append style={pin distance=40pt, align=left},
/tikz/every pin edge/.append style={shorten <=2pt, {Stealth[]}-, draw=black},
delay={
where n children=0{%
label/.wrap pgfmath arg={[align=center, label distance=5pt]below:#1}{content()},
content={},
}{},
for tree={
if n=1{
edge label/.wrap value={node [midway, left, anchor=mid east] {#1}},
}{
edge label/.wrap value={node [midway, right, anchor=mid west] {#1}},
},
}
},
for tree={%
l sep'+=70pt,
s sep'+=80pt,
+edge={%
shorten >=-2pt,
shorten <=-2pt,
},
if level=3{%
+edge={%
{Circle[width=4pt,length=4pt,blue,fill=blue]}-{Circle[width=4pt,length=4pt,blue,fill=white]},
},
}{%
+edge={%
{Circle[width=4pt,length=4pt,blue,fill=white]}-{Circle[width=4pt,length=4pt,blue,fill=white]},
},
},
},
before typesetting nodes={%
for nodewalk={fake=r,L,ancestors}{%
if level=0{}{%
just/.wrap pgfmath arg={$t=#1$}{int(level()-1)},
tikz+/.wrap pgfmath arg={%
\scoped[on background layer]{%
\draw [densely dotted] (right just #1) -- (right just #1 -| just tree west);
}
}{level()}
}
}
},
just format={anchor=west},
}
[, pin=45:{Entrepreneur\\sets $P_c$}
[{$\pi_1 = \text{equation 1}$\\$u_1 = \text{utility 1}$}, edge label={$n_c$ buy}
]
[, edge label=not buy, pin=45:{Entrepreneur\\sets $P_r$}
[{$\pi_2 = \text{equation 2}$\\$u_2 = \text{utility 2}$}, edge label={$1 - n_c$ buy}
]
[$0$\\$0$, edge label=not buy
]
]
]
\end{justtree}
\end{document}


The istgame package (version 2) provides macros for time-lines: \xtTimeLineH and \xtTimeLineV. It also provides macros for comments: \xtCommentTo and \xtCommentFrom.

Here is an example of using \xtTimeLineH and \xtCommentTo.

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{makecell}
\usepackage{istgame}

\begin{document}

\begin{istgame}[font=\footnotesize]
% tree
\setistmathTF001
\xtShowEndPoints
\xtdistance{25mm}{50mm}
\istroot(0)
\istb{$n_c$ buy}[al]{\makecell[l]{\pi_1=\text{equation 1}\\u_1=\text{utility 1}}}
\endist
\istroot(1)(0-2)
\istb{$1-n_c$ buy}[al]{\makecell[l]{\pi_2=\text{equation 2}\\u_2=\text{utility 2}}}
\xtTimeLineH(0){-4cm}{6cm}{$t=0$}
\xtTimeLineH(1){-4cm}{6cm}{$t=1$}
\xtTimeLineH(1-1){-4cm}{6cm}{$t=2$}
\xtCommentTo[solid](0)(2,1){\makecell[l]{Entrepreneur\\sets $P_c$}}[r]
\xtCommentTo[solid](1)(2,1){\makecell[l]{Entrepreneur\\sets $P_r$}}[r]