我想排版图像左侧的树。不幸的是,TikZ 对我来说似乎太复杂了……我尝试使用此代码,但没有成功
\documentclass{beamer}
\usepackage{tikz} % Game theory packages
\usetikzlibrary{calc}
% Node styles
\tikzset{
% Two node styles for game trees: solid and hollow
solid node/.style={circle,draw,inner sep=1.5,fill=black},
hollow node/.style={circle,draw,inner sep=1.5}
}
\begin{document}
\begin{frame}
\begin{tikzpicture}[scale=0.7,font=\footnotesize]
% Specify spacing for each level of the tree
\tikzstyle{level 1}=[level distance=18mm,sibling distance=27mm]
\tikzstyle{level 2}=[level distance=18mm,sibling distance=27mm]
% The Tree
\node(0)[hollow node,label=above:{$1$}]{}
child{node(1)[solid node, label=above:{$2$}]{}
child{node(0-1-1)[solid node, label=above:{$1$}]{} edge from parent node[left]{$E$}}
child{node(0-1-2)[label=below:{$(3,1)$}]{} edge from parent node[right]{$F$}}
child{node(0-1-1-1)[label=below:{$(1,2)$}]{} edge from parent node[right]{$G$}} % The error is here
edge from parent node[left,xshift=-3]{$C$}
}
child{node(2)[label=below:{$(0,3)$}]{} edge from parent node[right, xshift=3]{$D$}
};
\end{tikzpicture}
\end{frame}
\end{document}
我还想问您是否对于博弈论而言存在比 Tikz 更简单的树。
答案1
请注意,有一个包istgame
,专门用于游戏树。对于一般的树,forest
是一个非常强大的工具。它允许您非常轻松地绘制树。
\documentclass{article}
\usepackage{forest}
\forestset{el/.style={edge label={node [pos=0.7,above,outer sep=3pt] {$#1$} }}}
\begin{document}
\begin{forest}
for tree={circle,fill,inner sep=1pt,calign=fixed edge angles}
[,label=above:1
[,label=above left:2,el=A
[,label=above left:1,el=C
[,label=below:{0,2},el=E ]
[,label=below:{2,0},el=F ]
]
[,label=below:{1,1},el=D]
]
[,label=below:{3,3},el=B]
]
\end{forest}
\end{document}
答案2
我的普通 TikZ 代码有点长但易于阅读。
\documentclass{article}
%\documentclass{beamer}
\usepackage{tikz}
\begin{document}
%\begin{frame}
\begin{center}
\begin{tikzpicture}[scale=1,
h/.style={circle,draw=magenta,thick,inner sep=1.5},
s/.style={h,fill=magenta}]
\path
(0,0) node[h] (1) {} node[above] {1}
+(1,-1) node[s] (2b) {} node[below] {3,3}
++(-1,-1) node[s] (2a) {} node[left] {2}
+(1,-1) node[s] (3b) {} node[below] {1,1}
++(-1,-1) node[s] (3a) {} node[left] {1}
+(1,-1) node[s] (4b) {} node[below] {0,2}
+(-1,-1) node[s] (4a) {} node[below] {2,0};
\draw (1)
--(2a) node[pos=.4,left]{A}
--(3a) node[pos=.4,left]{C}
--(4a) node[pos=.4,left]{E}
(1)--(2b) node[pos=.4,right]{B}
(2a)--(3b) node[pos=.4,right]{D}
(3a)--(4b) node[pos=.4,right]{F};
\newcommand{\putcolumn}[6]{%
\path #1 node{#2}
++(-90:1) node{#3} ++(-90:1) node{#4}
++(-90:1) node{#5} ++(-90:1) node{#6};
}%
\begin{scope}[shift={(4,0)}]
\draw (0,0) grid (2,-4);
\putcolumn{(.5,.5)}{C}{2,0}{0,2}{3,3}{3,3}
\putcolumn{(1.5,.5)}{D}{1,1}{1,1}{3,3}{3,3}
\putcolumn{(-.5,.5)}{}{AE}{AF}{BE}{BF}
\end{scope}
\end{tikzpicture}
\end{center}
%\end{frame}
\end{document}
答案3
我建议使用这个简短的代码pstricks
– 更具体地说pst-tree
:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{pst-tree}
\usepackage{auto-pst-pdf}
\begin{document}
\begin{pspicture}
\psset{tnsep=1pt, labelsep=3pt}
\pstree[nodesep=0pt, levelsep=1.5cm, treesep=3cm]{\Tc{2pt}~[ tnpos=a, tnsep=-1pt]{1}} {
\pstree{\Tdot\nbput{$ A $} \uput[ul](\pssucc){$ 2 $}} {%
\pstree{\Tdot \nbput{$ C $} \uput[ul](\pssucc){{1}}}{%
\pstree{\Tdot \nbput{$ E $} \uput[ul](\pssucc){{2}}}{}%
\Tdot\naput{$F$}\uput[d](\pssucc){$0,2$}}%
\Tdot_{$D$} \uput[d](\pssucc){$1,1$}}%
\Tdot_{$B$} \uput[d](\pssucc){$3,3$}}
\end{pspicture}%
\end{document}
答案4
我尝试了tikz-qtree
,但我没有找到如何修复角度:
\usepackage{tikz-qtree}
\tikzset{grow'=down}
\begin{document}
\begin{standalone}
\begin{tikzpicture}[level distance=30pt,
sibling distance=10pt,
every tree node/.style={draw,circle,inner sep=0pt,minimum size=3pt},
edge from parent path={(\tikzparentnode) -- (\tikzchildnode)},
]
\Tree [. \node[label=above:1] {};
\edge node[auto=left,pos=.6] {$B$};
[. \node[fill=black,label=below:{3,3}] {}; ]
\edge node[auto=right,pos=.6] {$A$};
[.\node[fill=black,label=above left:2] {};
\edge node[auto=left,pos=.6] {$D$};
[.\node[fill=black,label=below:{1,1}] {}; ]
\edge node[auto=right,pos=.6] {$C$};
[.\node[fill=black,label=above left:1] {};
\edge node[auto=left,pos=.6] {$F$};
[.\node[fill=black,label=below:{2,2}] {}; ]
\edge node[auto=right,pos=.6] {$E$};
[.\node[fill=black,label=above left:2] {}; ]
]
]
]
\end{tikzpicture}
\end{standalone}
\end{document}