我不知道应该使用哪些包来使该代码运行,请有人帮帮我。
\begin{tikzpicture}[scale=1.5,font=\footnotesize]
\tikzset{
solid node/.style={circle,draw,inner sep=1.5,fill=black},
hollow node/.style={circle,draw,inner sep=1.5}
}
% Specify spacing for each level of the tree
\tikzstyle{level 1}=[level distance=10mm,sibling distance=37mm]
\tikzstyle{level 2}=[level distance=10mm,sibling distance=20mm]
\tikzstyle{level 3}=[level distance=15mm,sibling distance=10mm]
\tikzstyle arrowstyle=[scale=1]
\tikzstyle directed=[postaction={decorate,decoration={markings,
mark=at position .5 with {\arrow[arrowstyle]{stealth}}}}]
\node(0)[solid node,label=above:{$1$}]{}
child{node(1)[solid node, label=left:{$2$}]{}
child{node(3)[solid node]{}
child{node[hollow node,label=below:{$(6,3)$}]{}edge from parent node[left]{$G$} }
child{node[hollow node,label=below:{$(1,4)$}]{} edge from parent node[right]{$H$}}
edge from parent node[left]{$C$}}
child{node(4)[solid node]{}
child{node[hollow node,label=below:{$(1,2)$}]{}edge from parent node[left]{$G$} }
child{node[hollow node,label=below:{$(3,2)$}]{} edge from parent node[right]{$H$}}
edge from parent node[right]{$D$}
}
edge from parent node[left,xshift=-3]{$A$}
}
child{node(2)[solid node,label=right:{$2$}]{}
child{node(6)[solid node,label=right:{$1$}]{}
child{node[hollow node,label=below:{$(2,3)$}]{}edge from parent node[left]{$I$} }
child{node[hollow node,label=below:{$(1,4)$}]{} edge from parent node[right]{$J$}}
edge from parent node[left]{$E$}}
child{node(7)[solid node,label=right:{$1$}]{}
child{node[hollow node,label=below:{$(2,3)$}]{}edge from parent node[left]{$K$} }
child{node[hollow node,label=below:{$(3,2)$}]{} edge from parent node[right]{$L$}}
edge from parent node[right]{$F$}
}
edge from parent node[right,xshift=3]{$B$}
};
\draw[dashed,rounded corners=10]($(3) + (-.2,.25)$)rectangle($(4) +(.2,-.25)$);
\node at ($(3)!.5!(4)$) {$1$};
\end{tikzpicture}
答案1
您的 MWE 中缺少花括号和一个子节点。请参阅@Schrödinger 的猫评论。更正后格式更好的代码是:
\documentclass[tikz,margin=3mm]{standalone}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[scale=1.5,font=\footnotesize]
\tikzset{
solid node/.style = {circle,draw,inner sep=1.5,fill=black},
hollow node/.style = {circle,draw,inner sep=1.5},
% Specify spacing for each level of the tree
level 1/.style = {level distance=10mm,sibling distance=37mm},
level 2/.style = {level distance=10mm,sibling distance=20mm},
level 3/.style = {level distance=15mm,sibling distance=10mm},
directed/.style = {postaction={decorate,decoration={markings,
mark=at position .5 with {\arrow{stealth}}}}
}% where you like to use this style?
}% end of tikzset
% tree
\node(0)[solid node,label=above:{$1$}]{}
% left branch
child{node(1)[solid node, label=left:{$2$}]{}
child{node(3)[solid node]{}
child{node[hollow node,label=below:{$(6,3)$}]{} edge from parent node[left]{$G$} }
child{node[hollow node,label=below:{$(1,4)$}]{} edge from parent node[right]{$H$}}
edge from parent node[left]{$C$}
}
child{node(4)[solid node]{}
child{node[hollow node,label=below:{$(1,2)$}]{} edge from parent node[left]{$G$} }
child{node[hollow node,label=below:{$(3,2)$}]{} edge from parent node[right]{$H$}}
edge from parent node[right]{$D$}
}
edge from parent node[left,xshift=-3]{$A$}
}
% right branch
child{node(2)[solid node,label=right:{$2$}]{}
child{node(6)[solid node,label=right:{$1$}]{}
child{node[hollow node,label=below:{$(2,3)$}]{} edge from parent node[left]{$I$} }
child{node[hollow node,label=below:{$(1,4)$}]{} edge from parent node[right]{$J$}}
edge from parent node[left]{$E$}}
child{node(7)[solid node,label=right:{$1$}]{}
child{node[hollow node,label=below:{$(2,3)$}]{}edge from parent node[left]{$K$} }
child{node[hollow node,label=below:{$(3,2)$}]{} edge from parent node[right]{$L$}}
edge from parent node[right]{$F$}
}
edge from parent node[right,xshift=3]{$B$}
};
%
\node[draw,dashed,rounded corners=10,inner sep=8,
fit=(3) (4),
label=center:$1$] {};
\end{tikzpicture}
\end{document}
答案2
错误是由于缺少括号造成的。不过,我还是忍不住要提出一个替代方案:forest
。借用这个很好的答案你可以简化代码
\documentclass{article}
\usepackage[edges]{forest}
\forestset{declare toks={elo}{}} % https://tex.stackexchange.com/a/278184
\begin{document}
\begin{forest}
for tree={s sep+=2em,circle,draw,inner sep=1.5pt,
where n children=0{fill=white}{fill=black},
decision edge label/.style n args=3{
edge label/.expanded={node[midway,auto=#1,anchor=#2,\forestoption{elo}]{\strut$#3$}}
},
decision/.style={if n=1
{decision edge label={left}{east}{#1}}
{decision edge label={right}{west}{#1}}
},
}
[,label=above:1
[,label=left:2,decision=A
[,decision=C
[,label=below:{$(6,3)$},decision=H]
[,label=below:{$(1,4)$},decision=I]
]
[,decision=D
[,label=below:{$(1,2)$},decision=J]
[,label=below:{$(3,2)$},decision=K]
]
]
[,label=right:2,decision=B
[,decision=E
[,label=below:{$(2,3)$},decision=L]
[,label=below:{$(1,4)$},decision=M]
]
[,decision=F
[,label=below:{$(2,3)$},decision=N]
[,label=below:{$(3,2)$},decision=O]
]
]
]
\end{forest}
\end{document}