TeX - LaTeX Asked by Daniel Ortiz on May 15, 2021
For a Game theory exercise I have been trying to recreate the following tree using the TiKz package. I don’t know if this package is the right approach but here is the tree I am attempting to replicate:
If someone could provide an example of the code needed to do this specific kind of tree it would be of great help, as I can copy it to multiple other examples I have to work.
I am particularly struggling with the labels in the branches, the dotted line and the horizontal line at the beginning. This is why I do not share any attempt of code as it is all looking off and it would be detrimental to the post.
In case my preamble is needed please find it below:
**EDIT: PREAMBLE CLEANED
documentclass[12pt, a4 paper]{article}
usepackage{multicol} %Needed for PDgame
usepackage{multirow}
usepackage{wrapfig}
usepackage[english]{babel}
usepackage[utf8]{inputenc}
usepackage{amsmath,amssymb}
usepackage{parskip}
usepackage{graphicx}
usepackage{mathtools}
usepackage{graphicx}
usepackage{verbatim}
usepackage{subcaption}
usepackage{indentfirst}
usepackage{tikz}
% Margins
usepackage[inner=2.0cm,outer=2.0cm,top=2.5cm,bottom=2.5cm]{geometry}
% Colour table cells
usepackage[table]{xcolor}
setlength{marginparwidth}{2cm}
begin{document}
end{document}
Answer adapted from this question. To connect nodes simply name them and then you can draw lines between them. This is how the dashed line is done. The top two lines are just hard coded. There's probably a way to do it more automatically but I don't know it.
documentclass[border=25]{standalone}
usepackage{tikz}
usetikzlibrary{calc}
begin{document}
tikzset{
solid node/.style={circle,draw,inner sep=1.2,fill=black},
}
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(top)[solid node,label=above left:{1},red]{}
child{[red]node[solid node,label=above:{1}]{}
child{[black]node[solid node,label=left:{2}]{}
child{[red]node[solid node,label=below:{((6, 0))}]{}edge from parent node[left]{(alpha_2)}}
child{[red]node[solid node,label=below:{((3, 1))}]{}edge from parent node[right]{(beta_2)}}
edge from parent node[left]{(A_1)}
}
child{node[solid node,label=below:{((4,1))}]{} % This is the branch that ends early
edge from parent node[right]{(B_1)}
}
edge from parent node[left,xshift=-10]{(a_1)}
}
child{node[solid node,label=above:{2}]{}
child{[red]node(A2)[solid node]{}
child{node[solid node,black,label=below:{((3, 3))}]{}edge from parent node[left]{(alpha_1)}}
child{[black]node[solid node,label=below:{((1,5))}]{}edge from parent node[right]{(beta_1)}}
edge from parent node[left]{(A_2)}
}
child{node(B2)[solid node]{}
child{[red]node[solid node,label=below:{((4, 2))}]{}edge from parent node[left]{(alpha_1)}}
child{node[solid node,label=below:{((5,1))}]{}edge from parent node[right]{(beta_1)}}
edge from parent node[right]{(B_2)}
}
edge from parent node[right,xshift=10]{(b_1)}
};
draw[dashed] (A2) -- (B2) node[midway, above] {1};
coordinate (right angle) at ($(top) + (0, 1.5)$);
draw[red] (top) -- (right angle) node[midway, right,black] {(C)};
node[solid node,red,label=above:{2}] at (right angle) {};
draw[red] (right angle) -- ($(right angle) + (1.5, 0)$) node[black, midway, above] {(S)} node[below,black] {((1, 2))};
end{tikzpicture}
end{document}
Correct answer by Willoughby on May 15, 2021
You might also use forest
.
documentclass{article}
usepackage[edges]{forest}
newcommand{ur}[2][red]{textcolor{#1}{underline{textcolor{black}{#2}}}}
begin{document}
begin{forest}
er/.style={edge=red},r/.style={color=red},rer/.style={r,er},
el/.style={edge label={node[midway,auto,color=black]{ensuremath{#1}}}},
el'/.style={edge label={node[midway,auto,color=black,swap]{ensuremath{#1}}}},
for tree={l sep+=2em,s sep+=2em,
if n children=0{math content}{circle,inner sep=1pt,fill},
if level=3{tier=bottom}{}}
[,r,label={90:1},alias=r,
tikz+={path[draw=red] ()-- node[auto]{$S$} ++ (2,0) node[below]{$(1,ur{2})$};}
[,rer,el={C},label={135:1}
[,er,el'={alpha},label={90:1}
[,el'={A_1},label={135:2}
[{(6,0)},el'={alpha_2}]
[{(3,1)},el={beta_2}]
]
[{(4,ur{1})},er,el={B_1}
]
]
[,label={90:2},el={b_1}
[,er,el'={A_2},tikz+={path[draw,dotted] () -- (!s);}
[{(3,3)},er,el'={alpha_1}]
[{(1,5)},el={beta_1}]
]
[,el={B_2}
[{(4,2)},er,el'={alpha_1}]
[{(1,5)},el={beta_1}]
]
]
]
]
end{forest}
end{document}
Answered by user237902 on May 15, 2021
Using the istgame package, you can do this:
documentclass[tikz]{standalone}
usepackage{istgame}
begin{document}
begin{istgame}
xtdistance{10mm}{20mm}
istroot[-45](0){2}
istb{C}[r] istb{S}[a]{(1,2)}[b] endist
xtdistance{15mm}{60mm}
istroot(1)(0-1)<135>{1}
istb{a_1}[al] istb{b_1}[ar] endist
xtdistance{15mm}{30mm}
istroot(2)(1-1){1}
istb{A_1}[al] istb{B_1}[ar]{(4,1)} endist
istroot(3)(1-2){2}
istb{A_2}[al] istb{B_2}[ar] endist
xtdistance{15mm}{15mm}
istroot(2a)(2-1)<135>{2}
istb{alpha_2}[al]{(6,0)} istb{beta_2}[ar]{(3,1)} endist
istroot(3a)(3-1)
istb{alpha_1}[al]{(3,3)} istb{beta_1}[ar]{(1,5)} endist
istroot(3b)(3-2)
istb{alpha_1}[al]{(4,2)} istb{beta_1}[ar]{(5,1)} endist
xtInfoset(3a)(3b){1}
end{istgame}
end{document}
documentclass[tikz]{standalone}
usepackage{istgame}
begin{document}
begin{istgame}
tikzset{KK/.style={draw=red,very thick}}
xtdistance{10mm}{20mm}
istroot[-45](0){2}
istb[KK]{C}[r]
istb[KK]{S}[a]{(1,2)}[b]
endist
xtdistance{15mm}{60mm}
istroot(1)(0-1)<135>{1}
istb[KK]{a_1}[al]
istb {b_1}[ar]
endist
xtdistance{15mm}{30mm}
istroot(2)(1-1){1}
istb {A_1}[al]
istb[KK]{B_1}[ar]{(4,1)}
endist
istroot(3)(1-2){2}
istb[KK]{A_2}[al]
istb {B_2}[ar]
endist
xtdistance{15mm}{15mm}
istroot(2a)(2-1)<135>{2}
istb {alpha_2}[al]{(6,0)}
istb[KK]{beta_2}[ar]{(3,1)}
endist
istroot(3a)(3-1)
istb[KK]{alpha_1}[al]{(3,3)}
istb {beta_1}[ar]{(1,5)}
endist
istroot(3b)(3-2)
istb[KK]{alpha_1}[al]{(4,2)}
istb {beta_1}[ar]{(5,1)}
endist
xtInfoset(3a)(3b){1}
end{istgame}
end{document}
Answered by InSung Cho on May 15, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP