我有以下代码。如果我删除命令edge
,一切都很好。我想问题是我的节点具有相同的内容,但我显然无法避免这种情况。
错误代码:
\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{positioning,shapes,arrows}
\begin{document}
\tikzset{
treenode/.style = {align=center, inner sep=0pt, text centered,
font=\sffamily},
}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}]
\node (n) { 0 }
child { node (n0) { 0 }
edge from parent node[left] { 0 }
child { node (n00) { 0 }
edge from parent node[left] { 0 }
}
child { node (n01) { 1 }
edge from parent node[left] { 1 }
}
}
child { node (n1) { 1 }
edge from parent node[left] { 1 }
child { node (n10) { 0 }
edge from parent node[left] { 0 }
}
child { node (n11) { 1 }
edge from parent node[left] { 1 }
}
}
;
\end{tikzpicture}
\end{document}
\end{document}
好的代码(但没有标签):
\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{positioning,shapes,arrows}
\begin{document}
\tikzset{
treenode/.style = {align=center, inner sep=0pt, text centered,
font=\sffamily},
}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}]
\node (n) { 0 }
child { node (n0) { 0 }
child { node (n00) { 0 }
}
child { node (n01) { 1 }
}
}
child { node (n1) { 1 }
child { node (n10) { 0 }
}
child { node (n11) { 1 }
}
}
;
\end{tikzpicture}
\end{document}
\end{document}
答案1
看看下面的代码。你必须将边放在正确的位置,它才会起作用。你必须将边粘贴在所有子节点之后。如果你在它创建(我假设如此)之前插入它,它将被视为所有后续子节点的根节点。
如果您尝试在代码中向最低节点添加额外的子节点,您将会看到这个问题。
\documentclass[tikz,margin=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[->, %
>=stealth, %
level distance=1.5cm, %
level 1/.style={sibling distance=2.5cm}, %
level 2/.style={sibling distance=1.5cm}]
\node (n) {0}
child { node (n0) {0} {
child { node (n00) {0} edge from parent node [left] {0} }
child { node (n01) {1} edge from parent node [left] {1} }
} edge from parent node [left] {0}
}
child { node (n1) {1} {
child { node (n10) {0} edge from parent node [left] {0} }
child { node (n11) {1} edge from parent node [left] {1} }
} edge from parent node [left] {1}
}
;
\end{tikzpicture}
\end{document}
渲染图像:
答案2
来自 pgfmanual:
\path . . . edge from parent[<Options>] . . . ;
此路径操作只能在内部使用<child paths>
,并且应放在末尾,可能后跟<node specifications>
likenode {a}
。如果 a<child path>
不包含此操作,则会自动将其添加到末尾<child path>
。\documentclass[tikz,margin=10pt]{standalone} \usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[
->,
>=stealth',
level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}
]
\node (n) { 0 }
child { node (n0) { 0 }
child { node (n00) { 0 }
edge from parent node[left] {0}
}
child { node (n01) { 1 }
edge from parent node[right] {1}
}
edge from parent node[above left, inner sep=1pt,red] {0}% <-
}
child { node (n0) { 1 }
child { node (n00) { 0 }
edge from parent node[left] {0}
}
child { node (n01) { 1 }
edge from parent node[right] {1}
}
edge from parent node[above right, inner sep=1pt,red] {1}% <-
}
;
\end{tikzpicture}
\end{document}