6

I've created the following game tree:

enter image description here

I want it to look like this and I'm stuck:

enter image description here

(Apologize in advance for the ugly drawing!)

This is my 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}

\begin{document}

 \newcommand\term[2]{\node[below]at(#1){$#2$};}
%
\begin{tikzpicture}[font=\footnotesize,edge from parent/.style={draw,thin}]
  % Two node styles: solid and hollow
    \tikzstyle{solid node}=[circle,draw,inner sep=1.2,fill=black];
    \tikzstyle{hollow node}=[circle,draw,inner sep=1.2];
  % Specify spacing for each level of the tree
    \tikzstyle{level 1}=[level distance=15mm,sibling distance=20mm]
    \tikzstyle{level 2}=[level distance=15mm,sibling distance=34mm]
    \tikzstyle{level 3}=[level distance=15mm,sibling distance=11mm]
  % The Tree
  \node(0)[hollow node]{}
    child{node[solid node]{}edge from parent node[left]{Not buy}}
    child{node[solid node]at +(\tikzsiblingdistance,0){}
      child{node[hollow node]{}
        edge from parent node[left]{not buy}
      }
      child{node[hollow node]{}
        edge from parent node[right]{$P_r$}
      }
      edge from parent node[right]{$P_c$}
}
    child[sibling distance=5*\tikzsiblingdistance]{node[solid node]{}
      child{node[hollow node]{}
        edge from parent node[left]{not buy}
      }
      child{node[hollow node]{}
        edge from parent node[right]{$P_r$}
}
      edge from parent node[right,xshift=15]{$P_p$}
    };
  % information sets
  \draw[rounded corners=7]($(0-2)+(-.2,.25)$)rectangle($(0-3)+(.2,-.25)$);
  % specifying movers
  \draw[draw,<-,>=latex](0)--(32:8mm)node[right,inner sep=0]{Consumers decide whether or not to buy};
  \node at ($.5*(0-2)+.5*(0-3)$) {Remaining consumers decide to buy or not};
\end{tikzpicture}
\end{document}
5

See, if the following MWE gives what you expected:

enter image description here Figure is slightly changed in comparison to your sketch. If you not liked this, just change sibling distance accordingly and text write in one column.

\documentclass[tikz,
               border=3mm,
               ]{standalone}
\usetikzlibrary{fit}% <-- new

    \begin{document}
\begin{tikzpicture}[
                   font = \footnotesize,
edge from parent/.style = {draw ,thin},
      SN/.style = {%solid node
                   circle, inner sep=1.2 ,fill=black},
      HN/.style = {%hollow node
                   circle, inner sep=1.2, draw=black,
                   },
 level distance = 15mm,
 level 1/.style = {sibling distance=42mm},
 level 2/.style = {sibling distance=21mm}
                    ]
%---
\linespread{0.8}
%-------
    \node (n0) [SN] {}
        child{ node (n1) [SN]  {}         
            child{node[HN]  {} 
                edge from parent node[left]{not buy}}
            child{node[HN]  {} 
                edge from parent node[right]{$P_r$}}
                edge from parent node[left]{$P_c$}
            }
        child{ node (n2) [SN]  {} 
            child{node[HN]  {} 
                edge from parent node[left]{not buy}}            
            child{node[HN]  {}
                edge from parent node[right]{$P_r$}}
                edge from parent node[right]{$P_r$}
            };
\node[draw, rounded corners=9pt,minimum height=7mm, align=center,
     fit=(n1) (n2),
     label={[align=center]center:Remaining consumers\\ decide to buy or not}] {};
\draw   (n0) -- node[above] {Not buy} (n1 |- n0) node[SN] {};
\draw[<-,shorten <=1pt]   
        (n0) -- + (1,1) node[right,align=left] {Consumers decide\\ 
                                                whether or not to buy};
\end{tikzpicture}
    \end{document}
  • Zarko, works. Only thing that is missing is the "Consumers decide whether or not to buy" text and arrow pointing on the beginning node. – Saud Apr 26 '16 at 10:09
  • 1
    uh, I forgot on it. now is added and also erased auxiliary label (needed in drawing for orientation where is node) . So, the MWE now should be complete :-) – Zarko Apr 26 '16 at 10:29
  • Zarko, if I were to include a caption and a flatfoat to indicate source underneath the tree, where would I implement it? I've tried adding \caption{Title} just before \end{tikzpicture} – Saud Apr 26 '16 at 11:51
  • 1
    You should include tikzpicture into figure environment: \begin{figure}\begin{tikzpicture} ... \end{tikzpicture}\caption{...}\label{...}\end{figure} (caption will appear below figure). – Zarko Apr 26 '16 at 12:23
3

Here is a Forest version of your tree. One of the nice things about Forest is that it uses a bracket syntax which allows you to specify trees extremely concisely. And since it is TikZ-based, you can use the full power of TikZ in addition to Forest's range of features.

In this case, the tree itself is specified with this code:

  [, my pin=Consumers decide whether or not to buy
    [, edge label={node [midway, above] {Not buy}}, move me]
    [, my label=$P_c$, nudge=10pt
      [, my label=not buy]
      [, my label=$P_r$]
    ]
    [Remaining consumers decide to buy or not, tikz+={%
      \node [fit=() (!p) (!n), rounded corners=1.75ex, inner ysep=0pt, draw] {};
    }]
    [, my label=$P_p$, nudge=10pt
      [, my label=not buy]
      [, my label=$P_r$]
    ]
  ]

The fit library is used to draw the round-cornered rectangle around three nodes in the tree. These are specified using Forest's relative node names !p is the previous sibling and !n is the next sibling.

my label is a Forest style which sets the edge label to the left if the node is its parent's first child and to the right otherwise. nudge is used where the tree's branches slope too gently and the edges would clash with the labels otherwise. This is specified as a custom Forest option which is used by the my label style.

my pin uses Forest's pin option to add a label with an arrow. This is customised using TikZ's options for pins.

move me is a Forest style which aligns a child vertically with its parent. This is done late in the tree construction to override the results of Forest's own packing algorithm and calculations.

Forest tree

Complete code:

\documentclass[border=10pt,tikz,multi]{standalone}
\usetikzlibrary{fit,arrows.meta}
\usepackage{forest}% version 2 or later required - tested with 2.0.3
\begin{document}
\tikzset{% \tikzstyle is deprecated
  solid node/.style={circle,draw,inner sep=1.2,fill=black},
  hollow node/.style={circle,draw,inner sep=1.2},
}
\forestset{%
  declare dimen={nudge}{0pt},
  my pin/.style={%
    pin={[inner sep=0pt, pin distance=15pt]30:#1}
  },
  my label/.style={%
    delay={%
      if n=1{%
        edge label/.wrap pgfmath arg={node [midway, left, xshift=##1] {#1} }{nudge()}
      }{%
        edge label/.wrap pgfmath arg={node [midway, right, xshift=##1] {#1} }{nudge()}
      }
    }
  },
  move me/.style={%
    before drawing tree={%
      y/.wrap pgfmath arg={##1}{y("!u")}
    }
  },
}
\begin{forest}
  /tikz/every pin edge/.style={{Stealth[]}-},
  where level=1{%
    s sep+=50pt,
    delay={if content={}{solid node}{anchor=center,no edge}},
  }{hollow node},
  for tree={%
    l sep+=25pt,
    s sep+=5pt
  },
  [, my pin=Consumers decide whether or not to buy
    [, edge label={node [midway, above] {Not buy}}, move me]
    [, my label=$P_c$, nudge=10pt
      [, my label=not buy]
      [, my label=$P_r$]
    ]
    [Remaining consumers decide to buy or not, tikz+={%
      \node [fit=() (!p) (!n), rounded corners=1.75ex, inner ysep=0pt, draw] {};
    }]
    [, my label=$P_p$, nudge=10pt
      [, my label=not buy]
      [, my label=$P_r$]
    ]
  ]
\end{forest}
\end{document}
  • didn't know about the forest package. Thx! – Saud Apr 26 '16 at 10:03
  • cfr, I've posted the same question to Zarko: If I were to include a caption and a flatfoat to indicate source underneath the tree, where would I implement it? I've tried adding \caption{Title} just before `\end{tikzpic – Saud Apr 26 '16 at 11:52
  • 1
    @Saud What's a flatfoat? The usual way to add a caption is to do something like \begin{figure}\begin{tikzpicture}...\end{tikzpicture}\caption{Caption text}\label{fig:thisfigurelabel}\end{figure}. If you don't want the figure to float, you can use the \captionof{}{} command from either the caption or capt-of package instead. – cfr Apr 26 '16 at 12:01
  • Thx. I will give it a try. Flatfoat should of course have been floatfoot. I use floatfoot to give the figure a second "title" underneath. I usually write the source of the book or website I got the figure from. – Saud Apr 26 '16 at 12:37
2

You can use the istgame package to draw game trees. The istgame environment is almost the same as tikzpicture environment, which means that you can use any tikzpicture commands within istgame environment.

enter image description here

\documentclass{standalone}

\usepackage{istgame}
\usepackage{makecell}

\begin{document}

\begin{istgame}[font=\footnotesize]
% tree part
\xtdistance{15mm}{42mm}
\istroot(n0)
  \istb*<grow=west,level distance=21mm>{Not\ buy}[a]
  \endist
\istroot(n0)
  \istb{P_c}[al]
  \istb{P_r}[ar]
  \endist
\xtShowEndPoints[hollow node]
\xtdistance{15mm}{21mm}
\istroot(n1)(n0-1)
  \istb{not\ buy}[l]
  \istb{P_r}[r]
  \endist
\istroot(n2)(n0-2)
  \istb{not\ buy}[l]
  \istb{P_r}[r]
  \endist
% add some more
\xtInfosetO[solid,thin](n1)(n2){\makecell[c]{Remaining consumers\\[-1pt] decide to buy or not}}(10pt)
\draw [<-,>=latex,shorten <=2pt] (0,0) to [bend left] (32:8mm) node [right,inner sep=0] {\makecell[l]{Consumers decide\\ whether or not to buy}};
\end{istgame}

\end{document}

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.