我很想拥有这个博弈论练习中用 Word 绘制的树的精美 tikz 版本。
到目前为止我已完成以下事项:
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3cm},
level 2/.style={sibling distance=1.5cm}]
\node {$1$}
child {node {$2$}
child {node {$(4,1)$}}
child {node {$(2,1)$}}
}
child {node {$2$}
child {node {$(1,0)$}}
child {node {$(0,4)$}}
};
\end{tikzpicture}
但是如何添加最后的细节?谢谢。
答案1
与标签主题无关:使用具有istgame
最佳对齐方式的包来对最后的树进行对齐:
\documentclass[a4paper,12pt]{article}
\usepackage{istgame}
\usepackage{amsmath}
\begin{document}
\begin{istgame}[scale=1,font=\footnotesize]
\setistDecisionNodeStyle{4pt}
\xtdistance{15mm}{30mm}
\istroot[0](0)[initial node]
<0>{}
\istb{R}[a]{\begin{matrix} 3 & \\ 1 \end{matrix}}[r]
\endist
\xtdistance
{15mm}{2.5cm}
\istroot(0){$1$}
\istb{L}[al]
\istb{M}[ar]
\endist
\xtdistance{15mm}
{1cm}
\istroot(1)(0-1)<100>{$2$}
\istb{l}[l]{\begin{matrix} \phantom{44}4 & \\ \phantom{44}1 \end{matrix}}
\istb{r}[r]{\begin{matrix} \phantom{44}2 & \\ \phantom{44}1 \end{matrix}}
\endist
\istroot(2)(0-2)<45>
{$2$}
\istb{l}[l]{\begin{matrix} \phantom{44}1 & \\ \phantom{44}0 \end{matrix}}
\istb{r}[r]{\begin{matrix}
\phantom{44}0 & \\ \phantom{44}4 \end{matrix}}
\endist
\end{istgame}
\end{document}
再试一次使用istgame
包,但没有\phantom
东西:
\documentclass{article}
\usepackage{amsmath}
\usepackage{istgame}
\NewDocumentCommand\vpay{mm}
{\begin{matrix}#1\\#2\end{matrix}}
\begin{document}
\begin{istgame}
\setistDecisionNodeStyle{4pt}
\xtdistance{20mm}{15mm}
\istroot[east](0)
\istb{R}[a]{\vpay31}[r]
\endist
\xtdistance{15mm}{30mm}
\istroot(0){1}
\istb{L}[al]
\istb{M}[ar]
\endist
\xtdistance{15mm}{15mm}
\istroot(1)(0-1)<135>{2}
\istb{\ell}[l]{\vpay41}
\istb{r}[r]{\vpay21}
\endist
\istroot(2)(0-2)<45>{2}
\istb{\ell}[l]{\vpay10}
\istb{r}[r]{\vpay04}
\endist
\end{istgame}
\end{document}
答案2
使用该forest
包:
\documentclass[border=3mm]{standalone}
\usepackage{forest}
\begin{document}
\tikzset{ELS/.style={% Edge Label Style
font=\footnotesize, inner sep=2pt,
anchor=south #1, % label position: "ELS=west" or "ELS=east"
pos=0.6},
every label/.style = {font=\small\linespread{0.84}\selectfont,align=center,inner sep=2pt},
}
%---------------------------------------------------------------%
\begin{forest}
for tree={
l sep=13 mm,
s sep= 7 mm,
where level=2{inner sep=0pt}{circle, fill, outer sep=0pt}
},
EL/.style = {% shortenes for edge label, defined as style
before typesetting nodes={% edge labels positioning
where n=1{%
edge label/.wrap value={node[ELS=east]{#1}}% above left
}{%
edge label/.wrap value={node[ELS=west]{#1}}% above right
}
}
}
[,label=1, name=n1
[,label=above left:2, EL=L
[, label=below:4\\ 1,EL=l]
[, label=below:2\\ 1,EL=r]
]
[,label=above right:2, EL=M
[, label=below:1\\ 0,EL=l]
[, label=below:0\\ 4,EL=r]
]
]
\draw (n1) -- node[font=\small\sffamily, above] {R} ++ (1.4,0) node[label=east:3\\4] {};
\end{forest}
\end{document}
编辑:增加了节点之间的距离:
答案3
另一种forest
选择,但不需要手动添加太多东西。它只是一棵树,边缘标签是根据边缘的实际坡度放置的。
\documentclass[border=3mm]{standalone}
\usepackage[edges]{forest}
\tikzset{lbl/.style={circle,fill,inner sep=1.5pt,label=above:#1},
/forest/el/.style={edge label={
let \p1=($(.child anchor)-(!u.parent anchor)$),\n1={atan2(\y1,\x1)},
\n2={ifthenelse(cos(\n1)<0,\n1+90,\n1-90)} in
node[pos=1/2,anchor=\n2,font=\sffamily]{#1}}}}
\begin{document}
\begin{forest}
for tree={grow=south,
l sep+=2em,
s sep+=1em,
edge+={thick},
font=\sffamily
}
[,label=above:3,label=below:1,grow'=west
[,lbl=1,el=R
[,lbl=2,el=L
[4,label=below:1,el=l]
[2,label=below:0,el=r]
]
[,lbl=2,el=M
[1,label=below:0,el=l]
[0,label=below:4,el=r]
]
]
]
\end{forest}
\end{document}