我是 TikZ 新手,在决策树方面遇到了问题。在下面的示例中,我希望将文本放在边缘的水平线上方。例如:
Check
A
|
reject | accept
---------------
| |
Check decision 1
B
当我尝试使用类似
edge from parent node[above] {accept}
这棵树完全乱了。(下面代码中注释掉了示例)
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees,positioning,arrows,shapes,backgrounds}
\begin{document}
\begin{tikzpicture}[
% Node style
test/.style={diamond, aspect=2.5,very thick,draw=black,fill=gray!20,text width=1.1cm,
align= center, anchor=north},
dec/.style={rectangle,very thick,draw=black,fill=gray!50,text width=2cm,
text centered, anchor=north},
% Children and edges style
edge from parent/.style={very thick,draw=black},
edge from parent fork down,
level 1/.style={sibling distance=4cm,level distance=2cm}
]
\node (t1) [test] {Check\\A}
child{node (t2) [test] {Check\\B} % edge from parent node[above] {reject}
child{node (t3) [test] {Check\\C}
child{node (t4) [test] {Check\\D}}
child{node (d3) [dec] {decision 3}}
}
child{node (d2) [dec] {decision 2}}
}
child{node (d1) [dec] {decision 1} % edge from parent node[above] {accept}
}
;
\end{tikzpicture}
\end{document}
然后我还想知道是否可以强制边缘的水平线位于父节点和子节点的中间。节点是否可以使其中心具有相同的高度?
答案1
由于我不确定我是否完全理解的原因,似乎edge from parent node
必须在小组child
结束前结束}
编辑:手册(2.10 版第 18.6 节“来自父节点的边缘”)指出
此路径操作只能在子路径内使用,并且应在末尾给出,可能后面跟着节点规范
并给出了一些进一步的例子。
无论如何,下面的内容似乎可以满足您的要求。希望该示例能够比说明更好地说明如何使用它。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{trees,shapes}
\begin{document}
\begin{tikzpicture}[
test/.style={
% Node style
diamond,
aspect=2.5,
very thick,
draw=black,
fill=gray!20,
text width=1.1cm,
align=center,
anchor=north},
dec/.style={
rectangle,
very thick,
draw=black,
fill=gray!50,
text width=2cm,
text centered,
anchor=north
},
% Children and edges style
edge from parent/.style={
very thick,
draw=black},
edge from parent fork down,
level 1/.style={
sibling distance=4cm,
level distance=2cm}
]
\node (t1) [test] {Check\\A}
child { node (t2) [test] {Check\\B}
child { node (t3) [test] {Check\\C}
child { node (t4) [test] {Check\\D} edge from parent node [above] {reject C} }
child { node (d3) [dec] {decision 3} edge from parent node [above] {accept C} }
edge from parent node[above] {reject B}
}
child { node (d2) [dec] {decision 2} edge from parent node[above] {accept B} }
edge from parent node[above] {reject A}
}
child{ node (d1) [dec] {decision 1} edge from parent node[above] {accept A} } %
;
\end{tikzpicture}
\end{document}