TransWikia.com

Draw tree in tikz

TeX - LaTeX Asked by Hazards on December 2, 2020

I would like to replicate this powerpoint sketch in LaTeX. I tried it with tikz, but didn’t come to a result.

Any help appreciated!
enter image description here

4 Answers

Apart from the istgame package, which has been suggested in Alan Munn's comment, there is also the forest package which is based on TikZ and can be used in order to draw general trees. I base my answer on this answer by @cfr, who will be able to provide a much more elegant version of this if she sees this. However, I think that already this answer illustrates some of the strengths of forest, namely that you can do things programmatically like e.g. the placement of the edge labels, i.e. you do not have to do the repetitions by hand as in other approaches, and, what is perhaps more important, if you decide to change the typesetting of, say acc to be upright (what I want to recommend), you do it once and not over and over.

documentclass{article}
usepackage{forest}

begin{document}
  begin{forest}
    /tikz/my edge label/.style={inner sep=5pt, midway},
    /tikz/c/.style n args={2}{circle,inner sep=3pt,draw,fill=#1,label=above
    right:$#2$},
    trian/.style={tikz={draw[dashed]() -- ++(1,-1) --++({-2cm-3*sqrt(2)*1pt},0) -- ();}},
    for tree={
      l sep=4em,
      s sep=8em,
      anchor=center,
      before typesetting nodes={
        if n'=1{
          if n=1{}{edge label/.wrap value={noexpand node [my edge label,right]
          {$not$} }}
        }{
          edge label/.wrap value={noexpand node [my edge label,left] {$acc$} }
        },
      },
      if n children=0{}{
        circle,
        draw,
      }
    }
    [,c={black}{x_1},trian
     [,c={cyan}{{(a^0,1-a^0)}},label=below:{$x_2$}
      [{$(a^0,1-a^0)$}]
       [,c={cyan}{{x_3}},trian
        [,c={black}{{(a^1,1-a^1)}},label=below:{$x_4$}
         [{$bigl(delta_1a^1,delta_2(1-a^1)bigr)$}]
         [,c={black}{{x_5}},trian
          [,c={cyan}{{(a^2,1-a^2)}},label=below:{$x_6$}
           [{$bigl((delta_1)^2a^1,(delta_2)^2(1-a^1)bigr)$}]
           [{$(0,0)$}]
          ]
         ]
        ]
       ]
      ]
     ]
 end{forest}
end{document}

enter image description here

Correct answer by user121799 on December 2, 2020

Here is a starter. Run with xelatex or use auto-pst-pdf:

documentclass{article}
usepackage{pst-tree}
newcommandTriangle[1][black]{TRIANGLE[#1]}
defTRIANGLE[#1](#2){rput[t](#2){%
        pspolygon[linestyle=dashed](0,0)(-1.3,-1.3)(1.3,-1.3)}%
        pscircle[fillstyle=solid,fillcolor=#1](#2){5pt}}

begin{document}
psset{treesep=2,levelsep=2}
psTree{Tdot[dotsize=10pt,name=x1]}%
    psTree{TC[radius=5pt,name=x2]_[tpos=0.8]{$(a^0,1-a^0)$}}
      TR[name=x2l]{$(a^0,1-a^0)$}^{$acc$} 
      psTree{TC[radius=5pt,name=x3]_{$not$}}
        Tdot[dotsize=10pt,name=x4]_[tpos=0.8]{$(a^1,1-a^1)$}
      endpsTree 
    endpsTree
endpsTree
uput{10pt}[20](x1){$x_1$}%
Triangle(x1)%
uput{10pt}[-90](x2){$x_2$}%
uput{10pt}[0](x3){$x_3$}%
Triangle[cyan!40](x3)%
end{document}

enter image description here

Answered by user2478 on December 2, 2020

Here's a version using the istgame package which is designed specifically for drawing game theory trees. The package doesn't have a dashed stye for the dashed continuum of branches, so I've created a new command modelled on the existing istgame continuum of branches macro istcntm macro. This macro is called istcntmx and allows you pass TikZ options to the triangle. Its syntax is otherwise identical.

documentclass{article}
usepackage{istgame}
makeatletter
ExplSyntaxOn
NewDocumentCommand istcntmx
{ t' O{istdefault@grow} r() D(){0,0} O{dashed} G{} D+.{cntmdefault@levdist}D.+{cntmdefault@sibdist} }
{ 
    IfBooleanTF {#1}
    {
        istCntmRootx' [ #2 ] ( #3 ) ( #4 ) [ #5 ] + #7 .. #8 +
    }
    {
        istCntmRootx [ #2 ] ( #3 ) ( #4 ) [ #5 ] + #7 .. #8 +
    }
}

NewDocumentCommandistCntmRootx{t'O{south}r()D(){0,0}O{dashed}G{.5}D+.{cntmdefault@levdist}D.+{cntmdefault@sibdist}}
{
    IfBooleanTF {#1}
    {
    cntmdistance{#7}{#8}
    tikzset{level~1/.style={level~distance=cntmlevdist,sibling~ distance=cntmsibdist]}}
    setistNullNodeStyle[]{istdefault@nullnodesize}[]
    node (#3) at (#4) [null~node] {} [xtgrowprime=#2] child[#5] child[#5];
    draw [#5] (#3) -- (#3-1) -- (#3-2);
    setistNullNodeStyle{istdefault@nullnodesize}
    }
    {
    cntmdistance{#7}{#8}
    tikzset{level~1/.style={level~distance=cntmlevdist,sibling~ distance=cntmsibdist]}}
    setistNullNodeStyle[]{istdefault@nullnodesize}[]
    node (#3) at (#4) [null~node] {} [xtgrow=#2] child[#5] child[#5];
    draw [#5] (#3) -- (#3-1) -- (#3-2);
    setistNullNodeStyle{istdefault@nullnodesize}
    }
}
ExplSyntaxOff
makeatother
setistDecisionNodeStyle[black]{6pt}
tikzset{blue node/.style={draw=blue,fill=cyan!50,circle=6pt}}
begin{document}
begin{istgame}
xtdistance{20mm}{35mm}
istcntmx(0)+15mm..25mm+
istroot(0)
istb*{}[]{(a^{0},1-a^{0})}[[xshift=10pt,yshift=-8pt]right]
endist
istroot(1)(0-1)[blue node]<[yshift=-3pt]below>{$x_{2}$}
istb{acc}[al]{(a^{0},1-a^{0})} istb{not}[ar]{x_{3}}[r]
endist
istroot(2)(1-2)[blue node]
istcntmx(0-1)(1-2)+15mm..25mm+
istb*{}[]{(a^{1},1-a^{1})}[[xshift=10pt,yshift=-8pt]right]
endist
istroot(3)(2-1)<[yshift=-3pt]below>{$x_{4}$}
istb{acc}[al]{(delta_{1}a^{1},delta_{2}(1-a^{1}))} istb*{not}[ar]
endist
end{istgame}
end{document}

output of code

Answered by Alan Munn on December 2, 2020

This is another example of using the istgame package.

istgame version v2.0

With the version 2, you can use a new macro istrootcntm, which is simpler (if used by defaults) and more powerful than using (obsolete) istcntm followed by istroot. You can control the features of a continuum triangle using cntmpreset(*).

documentclass{standalone}

usepackage{istgame}

begin{document}

begin{istgame}[font=scriptsize]
% presets
tikzset{odd node/.style={decision node,minimum size=6pt}}
tikzset{even node/.style={oval node,fill=cyan!50,minimum size=6pt}}
% game tree 
cntmdistance*{20mm}{35mm}
cntmpreset*[densely dashed]{.6}
istrootcntm(1a)[odd node]<15>{$x_1$}
  istb{(a^0,1-a^0)}[right,near end] endist
istroot(2)(1a-1)[even node]<-90>{$x_2$}
  istb{acc}[al]{left(a^0,1-a^0right)} 
  istb{not}[ar] 
  endist
istrootcntm(2a)(2-2)[even node]<0>{$x_3$}
  istb{(a^2,1-a^2)}[right,near end] endist
istroot(3)(2a-1)[odd node]<-90>{$x_4$}
  istb{acc}[al]{left(delta_1 a^1,delta_2(1-a^1)right)} 
  istb{not}[ar] 
  endist
istrootcntm(3a)(3-2)[odd node]<0>{$x_5$}
  istb{(a^2,1-a^2)}[right,near end] endist
istroot(4)(3a-1)[even node]<-90>{$x_6$}
  istb{acc}[al]{left((delta_1)^2a^2,(delta_2)^2(1-a^2)right)} 
  istb{not}[ar]{(0,0)} 
  endist
end{istgame}

end{document}

enter image description here

original answer (istgame v1.0)

I borrowed distances and colors from Alan's answer, which will work in every direction of tree growing.

(I am working on an update for the istgame package, hoping that drawing game trees will be a little more easier in various situations. I have also spent some time to new macros related to continua of branches.)

documentclass{standalone}

usepackage{istgame}

begin{document}

begin{istgame}[scale=1.2,font=scriptsize]
% presets
tikzset{odd node/.style={decision node,minimum size=6pt}}
tikzset{even node/.style={oval node,fill=cyan!50,minimum size=6pt}}
NewDocumentCommandDASHEDcntm{ r() r() G{} O{below right} }
{
    istroot(#1)(#2)[null node]+15mm..{(7/4)*15mm}+ 
      istb[dashed] istb[dashed] endist
    draw [dashed] (#1-1) -- (#1-2);
    xtPayoff($(#1-1)!.5!(#1-2)$){#3}[#4]
}
% game tree 
xtdistance{20mm}{35mm}
DASHEDcntm(1a)(0,0){(a^0,1-a^0)}
istroot(1a)[odd node]<15>{$x_1$}  istb endist
istroot(2)(1a-1)[even node]<-90>{$x_2$}
  istb{acc}[al]{left(a^0,1-a^0right)} 
  istb{not}[ar] 
  endist
DASHEDcntm(2a)(2-2){(a^1,1-a^1)}
istroot(2a)(2-2)[even node]<0>{$x_3$}  istb endist
istroot(3)(2a-1)[odd node]<-90>{$x_4$}
  istb{acc}[al]{left(delta_1 a^1,delta_2(1-a^1)right)} 
  istb{not}[ar] 
  endist
DASHEDcntm(3a)(3-2){(a^2,1-a^2)}
istroot(3a)(3-2)[odd node]<0>{$x_5$}  istb endist
istroot(4)(3a-1)[even node]<-90>{$x_6$}
  istb{acc}[al]{left((delta_1)^2a^2,(delta_2)^2(1-a^2)right)} 
  istb{not}[ar]{(0,0)} 
  endist
end{istgame}

end{document}

Answered by InSung Cho on December 2, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP