我有以下程式碼。如果我刪除這些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
來自 pgf 手冊:
\path . . . edge from parent[<Options>] . . . ;
此路徑操作只能在內部使用<child paths>
,並且應該在末尾給出,後面可能跟有<node specifications>
likenode {a}
。如果a<child path>
不包含該操作,則會自動新增至a的末端<child path>
。 \documentclass[tikz,margin=10pt]{獨立} \usetikzlibrary{箭頭}
\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}