我在 TikZ 中绘制了以下树。我不喜欢的是
- 我必须手动调整
sibling distance
每个级别;并且 - 语法太繁琐。
@percusse 和 @AlanMunn 建议我使用tikz-qtree
and/orforest
可能是一种更有效的方式来产生相同的结果。但我没有使用这两个包的经验。
如果有人可以使用其中任何一个包重现我的例子那就太好了,这样我就可以快速地从例子中学习,而不必阅读 150 页的手册来寻找答案。
\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\tikzset{
% Two node styles for game trees: solid and hollow
solid node/.style={circle,draw,inner sep=1.2,fill=black},
hollow node/.style={circle,draw,inner sep=1.2},
}
% macro for entering payoffs
\newcommand\payoff[1]{
$\begin{pmatrix} #1 \end{pmatrix}$
}
\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[hollow node,label=above:{Nature}]{}
child{node[solid node,label=left:{P1}]{}
child{node(l1)[solid node]{}
child{node[solid node,label=below:{\payoff{1\\-1}}]{}edge from parent node[left]{L}}
child{node[solid node,label=below:{\payoff{0\\0}}]{}edge from parent node[right]{R}}
edge from parent node[left]{U}
}
child{node(l2)[solid node]{}
child{node[solid node,label=below:{\payoff{0\\0}}]{}edge from parent node[left]{L}}
child{node[solid node,label=below:{\payoff{2\\-2}}]{}edge from parent node[right]{R}}
edge from parent node[right]{D}
}
edge from parent node[left,xshift=-10]{A}
}
child{node[solid node,label=right:{P1}]{}
child{node(r1)[solid node]{}
child{node[solid node,label=below:{\payoff{-2\\2}}]{}edge from parent node[left]{L}}
child{node[solid node,label=below:{\payoff{0\\0}}]{}edge from parent node[right]{R}}
edge from parent node[left]{U}
}
child{node(r2)[solid node]{}
child{node[solid node,label=below:{\payoff{0\\0}}]{}edge from parent node[left]{L}}
child{node[solid node,label=below:{\payoff{-1\\1}}]{}edge from parent node[right]{R}}
edge from parent node[right]{D}
}
edge from parent node[right,xshift=10]{B}
}
;
\draw[dashed](l1)--(l2)--node[midway,above]{P2}(r1)--(r2);
\end{tikzpicture}
\end{document}
答案1
由于这个答案涉及不同的包,我单独添加了它。这是使用该forest
包的解决方案。原理相似,但我认为一旦你习惯了语法,使用这个包绘制树可能会更容易一些。
\documentclass{article}
\usepackage{forest}
\usepackage{amsmath}
\tikzset{
% Two node styles for game trees: solid and hollow
solid node/.style={circle,draw,inner sep=1.2,fill=black},
hollow node/.style={circle,draw,inner sep=1.2},
% styles for long branch labels
left label/.style={above left,midway},
right label/.style={above right,midway}
}
% macro for entering payoffs
\newcommand\payoff[1]{
$\begin{pmatrix} #1 \end{pmatrix}$
}
\begin{document}
\begin{forest}
[ ,for tree={s sep=.5in},label={Nature},hollow node
[ ,for tree={solid node},edge label={node[left label]{A}}
[ ,name=P2left,edge label={node[left label] {U}}
[ ,label={below:\payoff{1\\-1}},edge label={node[left,midway] {L}} ]
[ ,label={below:\payoff{0\\0}},edge label={node[right,midway] {R}} ] ]
[ ,edge label={node[right label] {D}}
[ ,label={below:\payoff{0\\0}},edge label={node[left,midway] {L}} ]
[ ,label={below:\payoff{2\\-2}},edge label={node[right,midway] {R}} ] ]
]
[ ,for tree={solid node},edge label={node[right label]{B}}
[ ,edge label={node[left label] {U}}
[ ,label={below:\payoff{-2\\2}},,edge label={node[left,midway] {L}} ]
[ ,label={below:\payoff{0\\0}},,edge label={node[right,midway] {R}} ]
]
[ ,name=P2right,edge label={node[right label] {D}}
[ ,label={below:\payoff{0\\0}}, edge label={node[left,midway] {L}} ]
[ ,label={below:\payoff{-1\\1}},,edge label={node[right,midway] {R}} ] ]
]
]
\draw[dashed] (P2left) -- node[above] {P2} (P2right);
\end{forest}
\end{document}
答案2
这不是您想要的答案,但是......
我(滥用)使用自动机包编写游戏树,因为它的结构和语法比所有可用的树包更直观。
这就是我实现示例的方式。(注:稍后您可以使用选项对其进行调整,以标记信息集而不是节点位置的节点。您还可以使用在和出去选项边缘。
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{external,automata,trees,positioning,shadows,arrows,shapes.geometric}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,semithick]
\tikzstyle{every state}=[fill,draw=none,green,text=white,circular drop shadow]
\tikzstyle{accepting}=[circle split, draw,text=black,inner sep=0.05cm]
\tikzstyle{initial}=[red,text=white]
\node[state,initial] (0) {Nature};
\node[state] (A) [below left of=0] {P1};
\node[state] (B) [below right of=0] {P1};
\node[state] (AU) [left of=A] {P2};
\node[state] (AD) [below of=A] {P2};
\node[state] (BU) [below of=B] {P2};
\node[state] (BD) [right of=B] {P2};
\node[accepting] (AUL) [above left of=AU] {$1$\nodepart{lower}$-1$};
\node[accepting] (AUR) [below left of=AU] {$0$\nodepart{lower}$0$};
\node[accepting] (ADL) [left of=AD] {2\nodepart{lower}-2};
\node[accepting] (ADR) [below of=AD] {0\nodepart{lower}0};
\node[accepting] (BUL) [below of=BU] {2\nodepart{lower}-2};
\node[accepting] (BUR) [right of=BU] {0\nodepart{lower}0};
\node[accepting] (BDL) [below right of=BD] {0\nodepart{lower}0};
\node[accepting] (BDR) [above right of=BD] {$1$\nodepart{lower}$-1$};
\path(0) edge node[above]{A}(A) edge node[above]{B}(B);
\path(A) edge node[above]{U}(AU) edge node[left]{D}(AD);
\path(B) edge node[right]{U}(BU) edge node[above]{D}(BD);
\path(AU) edge node[above]{L}(AUL) edge node[left]{R}(AUR);
\path(AD) edge node[above left]{L}(ADL) edge node[right]{R}(ADR);
\path(BU) edge node[left]{L}(BUL) edge node[above]{R}(BUR);
\path(BD) edge node[below left]{L}(BDL) edge node[above left]{R}(BDR);
\path(AU) edge [-,dotted,thick,bend right] (AD);
\path(AD) edge [-,dotted,thick] (BU);
\path(BU) edge [-,dotted,thick,bend right] (BD);
\end{tikzpicture}
答案3
这种树不太容易画,tikz-qtree
因为它是为绘制语言树而设计的,而语言树的分支几乎从未被标记过。要用它标记一条边,tikz-qtree
你必须分别绘制每条边。所以很难说下面的例子是否tikz-qtree
对你有帮助,但至少它应该向你展示如何可以用包来做这样的树。
\documentclass{article}
\usepackage{tikz-qtree}
\usepackage{amsmath}
\tikzset{
% Two node styles for game trees: solid and hollow
solid node/.style={circle,draw,inner sep=1.2,fill=black},
hollow node/.style={circle,draw,inner sep=1.2},
}
% macro for entering payoffs
\newcommand\payoff[1]{
$\begin{pmatrix} #1 \end{pmatrix}$
}
\begin{document}
\begin{tikzpicture}[every level 0 node/.style={draw,hollow node},
every level 1 node/.style={draw,solid node},
every level 2 node/.style={draw,solid node},
every level 3 node/.style={draw,solid node},
level distance=.65in,
edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}]
\Tree [.\node[label={Nature}]{};
\edge node [auto=right ] {A}; [.\node[label=left:P1]{};
\edge node [auto=right] {U}; [.\node (P2left) {};
\edge node[auto=right] {L}; [.\node [label=below:{\payoff{1\\-1}}] {}; ]
\edge node[auto=left] {R}; [.\node [label=below:{\payoff{0\\0}}] {}; ]]
\edge node [auto=left] {D}; [.{}
\edge node[auto=right] {L}; [.\node [label=below:{\payoff{0\\0}}] {}; ]
\edge node[auto=left] {R}; [.\node [label=below:{\payoff{2\\-2}}] {}; ]]]
\edge node [auto=left] {B}; [.\node[label=right:P2]{};
\edge node [auto=right] {U}; [.{}
\edge node[auto=right] {L}; [.\node [label=below:{\payoff{-2\\2}}] {}; ]
\edge node[auto=left] {R}; [.\node [label=below:{\payoff{0\\0}}] {}; ]]
\edge node [auto=left] {D}; [.\node (P2right) {};
\edge node[auto=right] {L}; [.\node [label=below:{\payoff{0\\0}}] {}; ]
\edge node[auto=left] {R}; [.\node [label=below:{\payoff{-1\\1}}] {}; ]]] ]
]
\draw[dashed] (P2left) -- node[above] {P2} (P2right);
\end{tikzpicture}
\end{document}
答案4
这是使用istgame
包裹,它基于 TikZ。由于istgame
环境与环境几乎相同,因此您可以使用环境tikzpicture
中可用的任何命令和选项。tikzpicture
istgame
\documentclass{standalone}
\usepackage{amsmath}
\usepackage{istgame}
\begin{document}
\begin{istgame}
\xtShowEndPoints
\xtdistance{15mm}{50mm}
\istroot(0)[initial node]{Nature}
\istb{A}[al]
\istb{B}[ar]
\endist
\xtdistance{15mm}{24mm}
\istroot(l)(0-1)<135>{P1}
\istb{U}[l]
\istb{D}[r]
\endist
\istroot(r)(0-2)<45>{P1}
\istb{U}[l]
\istb{D}[r]
\endist
\xtdistance{15mm}{12mm}
\istroot(l1)(l-1)
\istb{L}[l]{\binom{1}{-1}}
\istb{R}[r]{\binom{0}{0}}
\endist
\istroot(l2)(l-2)
\istb{L}[l]{\binom{0}{0}}
\istb{R}[r]{\binom{2}{-2}}
\endist
\istroot(r1)(r-1)
\istb{L}[l]{\binom{-2}{2}}
\istb{R}[r]{\binom{0}{0}}
\endist
\istroot(r2)(r-2)
\istb{L}[l]{\binom{0}{0}}
\istb{R}[r]{\binom{-1}{1}}
\endist
\xtInfoset[dashed](l1)(r2){P2}
\end{istgame}
\end{document}