2

I've started making a game tree, but I get stuck after the second level. Suggestions for extending to the desired output or cleaner code are much appreciated.

Desired output

enter image description here

Current output

enter image description here

Current code

\documentclass{article}
\usepackage{tikz}

\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]{text 1}
            }
        child{ node (n2) [SN]  {} 
            child{node[HN]  {} 
                edge from parent node[left]{text 3}}            
            child{node[HN]  {}
                edge from parent node[right]{text 4}}
                edge from parent node[right]{text 2}};
\draw[<-,shorten <=1pt]   
        (n0) -- + (2,1) node[right,align=left] {nodetext 1 };
\draw[<-,shorten <=1pt]   
        (n2) -- + (2,1) node[right,align=left] {nodetext 2};
\end{tikzpicture}
    \caption{Game tree}
    \label{fig:Game tree}
\end{figure}

\end{document}
  • forest or tikz-qtree would let you do this much more efficiently ;). – cfr Jul 22 '16 at 3:13
2

It may not be that easy to read, but it looks like it's possible to continue the way you started:

\documentclass{article}
\usepackage{tikz}

\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]{text 1}
            }
        child{ node (n2) [SN]  {} 
            child{node (n3) [HN] {} 
                child{node (n4) [SN] {}
                    child{node[HN] {} 
                        edge from parent node[left]{text 7}}
                    child{node[HN] {} 
                        edge from parent node[right]{text 8}}
                    edge from parent node[left]{text 5}}
                child{node[SN] {} 
                    edge from parent node[right]{text 6}}
                edge from parent node[left]{text 3}}            
            child{node[HN]  {}
                edge from parent node[right]{text 4}}
                edge from parent node[right]{text 2}};
\draw[<-,shorten <=1pt]   
        (n0) -- + (2,1) node[right,align=left] {nodetext 1};
\draw[<-,shorten <=1pt]   
        (n2) -- + (2,1) node[right,align=left] {nodetext 2};
\draw[<-,shorten <=1pt]   
        (n3) -- + (-2,1) node[left,align=right] {nodetext 3};
\draw[<-,shorten <=1pt]   
        (n4) -- + (-2,1) node[left,align=right] {nodetext 4};
\end{tikzpicture}
    \caption{Game tree}
    \label{fig:Game tree}
\end{figure}

\end{document}

enter image description here

I suggest you to also note on your drawing the names of your nodes (n1 etc) when you name them, this helps.

There are also other kind of tree systems that can be easier to use, but note that the examples in this link put the text where you put nodes: drawing tree using tikz, child overlap . See cfr's answer for an example more adapted to your needs.

  • @cfr Yes, what I wanted to say is that the examples where not answering the specific needs of this question, so need some tweaking as you did in your answer. I edited to try to make that a bit more clear. – Togh Jul 22 '16 at 7:38
3

It is not true that the text has to go where the nodes are if you use one of the easier tree specification syntaxes. forest allows you to put the text from the nodes somewhere else entirely.

For example,

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    parent anchor=children,
    anchor=center,
    inner sep=0pt,
    l sep*=5,
    s sep*=5,
    delay={
      edge label/.wrap value={node [above, midway, sloped] {#1}; },
      if content={}{}{
        if n=1{
          pin/.wrap pgfmath arg={135:#1}{content()},
        }{
          pin/.wrap pgfmath arg={45:#1}{content()},
        }
      },
      circle,
      minimum size=2.5pt,
      fill,
      content={},
    }
  }
  [node text 1
    [, edge label=text 1]
    [node text 2, edge label=text 2
      [node text 3, edge label=text 3
        [node text 4, edge label=text 5
          []
          []
        ]
        []
      ]
      [, edge label=text 4]
    ]
  ]
\end{forest}
\end{document}

It is much easier to specify many or large trees this way because the brackets reflect the structure, the trees can be formatted consistently and flexibly and parts of the construction (or all of it!) can even be done automatically.

moved text from nodes

2

Edit: istgame version 2.0

The istgame version 2 provides a new macro \xtCommentTo to put a comment to a node. The input mode changer \setistmathTF* is also a new macro available in the version 2.

enter image description here

\documentclass{standalone}

\usepackage{istgame}

\begin{document}

\begin{istgame}[sloped]
% action labels in text mode
\setistmathTF*000{textrm}
% tree part
\setistSolidNodeStyle[blue]
\setistHollowNodeStyle[blue]
\xtShowEndPoints[hollow node]
\xtdistance{30mm}{60mm}
\istroot(n0)[hollow node]
  \istb*{text 1}[above] \istb{text 2}[above]  \endist
\istroot(n1)(n0-2)
  \istb{text 3}[above]  \istb{text 4}[above]  \endist
\istroot(n2)(n1-1)[hollow node]
  \istb{text 5}[above]  \istb*{text 6}[above] \endist
\istroot(n3)(n2-1)
  \istb{text 7}[above]  \istb{text 8}[above]  \endist
% comment part
\xtCommentTo[bend right](n0)(2,1){nodetext 1}[r]
\xtCommentTo(n1)(2,1){nodetext 2}[r]
\xtCommentTo[bend left,dashed](n2)(-2,1){nodetext 3}[l]
\xtCommentTo[solid](n3)(-2,1){nodetext 4}[l]
\end{istgame}

\end{document}

Original answer

Here is a solution to use the istgame package. You can use the istgame package just like the tikzpicture environment, which means that you can use all the commands and options of tikzpicture together with macros provided with the istgame package within the istgame environment.

enter image description here

MWE:

\documentclass{standalone}
\usepackage{istgame}
%\usepackage{makecell}
\begin{document}

\begin{istgame}[sloped]
% tree part
\setistSolidNodeStyle[blue]
\setistHollowNodeStyle[blue]
\xtShowEndPoints[hollow node]
\xtdistance{30mm}{60mm}
\istroot(n0)[hollow node]
  \istb*{\mbox{text 1}}[above]
  \istb{\mbox{text 2}}[above]
  \endist
\istroot(n1)(n0-2)
  \istb{\mbox{text 3}}[above]
  \istb{\mbox{text 4}}[above]
  \endist
\istroot(n2)(n1-1)[hollow node]
  \istb{\mbox{text 5}}[above]
  \istb*{\mbox{text 6}}[above]
  \endist
\istroot(n3)(n2-1)
  \istb{\mbox{text 7}}[above]
  \istb{\mbox{text 8}}[above]
  \endist
% add some more
\begin{scope}[<-,shorten <=1pt]
\draw [->,shorten >=1pt,bend right=30] ($(n0)+(2,1)$) node [right] {nodetext 1} to (n0);
\draw (n1) -- +(2,1) node [right] {nodetext 2};
\draw (n2) -- +(-2,1) node [left] {nodetext 3};
\draw (n3) -- +(-2,1) node [left] {nodetext 4};
\end{scope}
\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.