我尝试以某种方式排列树中的子项,以使树能够适合页面且不会重叠。
我已经根据这个制作了自己的故障树例子,没什么特别的。代码的重要部分如下:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[edges]{forest}
\usepackage{tikz}
\usetikzlibrary{shapes.gates.logic.US,trees,positioning,arrows}
\begin{document}
\begin{tikzpicture}[
event/.style={rectangle,thick,draw,fill=yellow!20,text width=2cm,
text centered,font=\sffamily,anchor=north},
edge from parent/.style={very thick,draw=black!70},
edge from parent path={(\tikzparentnode.south) -- ++(0,-1.05cm)
-| (\tikzchildnode.north)},
level 1/.style={sibling distance=5cm,level distance=1.4cm,
growth parent anchor=south,nodes=event},
level 2/.style={sibling distance=2.5cm},
level 3/.style={sibling distance=2.5cm},
]
\node (g1) [event] {Het klimaatsysteem levert geen goede luchtkwaliteit}
child {node (g2) {Geen lucht}
child {node (g4){Falen ventilator}}
child {node (g5){Lek luchtkanalen}}
child {node (g6){Lek kleppen}}
}
child[level distance=3cm] {node (g3) {Slechte kwaliteit}
child[level distance=1.5cm, sibling distance=4cm] {node (g7){Vuile lucht}
child {node (g10){Vuil filter}}
child {node (g11){Verstopte buitenroosters}}
}
child[level distance=1.5cm] {node (g8){Verkeerde luchtvochtigheid}
child {node (g12){Erosie druppelvanger}}
child {node (g13){Te weinig bevochtiging}}
}
child[level distance=1.5cm, sibling distance=4cm] {node (g9){Verkeerde temperatuur}
child {node (g14){Lekkende verwarming}}
child {node (g15){Lekkende koelbatterij}}
}
};
\end{tikzpicture}
\end{document}
现在,我想要右侧写着“Verkeerde temperatuur”的块直接位于上一级别的块下方,就像写着“Verkeerde temperatuur”的级别左对齐一样。
我希望我说清楚了,任何人都可以找到解决方案。提前致谢。
PS. 请不要介意图片中的重叠和其他小错误
答案1
如果您将选项放在grow=down
此子节点上,它将被放置在上方节点的正下方。然后您需要将其他两个子节点向左移动,例如xshift=5cm
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.gates.logic.US,trees,positioning,arrows}
\begin{document}
\begin{tikzpicture}[
and/.style={and gate US,thick,draw,fill=red!60,rotate=90,
anchor=east,xshift=-1mm},
or/.style={or gate US,thick,draw,fill=blue!60,rotate=90,
anchor=east,xshift=-1mm},
be/.style={circle,thick,draw,fill=green!60,anchor=north,
minimum width=0.7cm},
tr/.style={buffer gate US,thick,draw,fill=purple!60,rotate=90,
anchor=east,minimum width=0.8cm},
label distance=3mm,
every label/.style={blue},
event/.style={rectangle,thick,draw,fill=yellow!20,text width=2cm,
text centered,font=\sffamily,anchor=north},
edge from parent/.style={very thick,draw=black!70},
edge from parent path={(\tikzparentnode.south) -- ++(0,-1.05cm)
-| (\tikzchildnode.north)},
level 1/.style={sibling distance=5cm,level distance=1.4cm,
growth parent anchor=south,nodes=event},
level 2/.style={sibling distance=2.5cm},
level 3/.style={sibling distance=2.5cm},
]
\node (g1) [event] {Het klimaatsysteem levert geen goede luchtkwaliteit}
child {node (g2) {Geen lucht}
child {node (g4){Falen ventilator}}
child {node (g5){Lek luchtkanalen}}
child {node (g6){Lek kleppen}}
}
child[level distance=3cm] {node (g3) {Slechte kwaliteit}
child[level distance=1.5cm, sibling distance=4cm, xshift=-6cm] {node (g7){Vuile lucht}
child {node (g10){Vuil filter}}
child {node (g11){Verstopte buitenroosters}}
}
child[level distance=1.5cm, xshift=-5cm] {node[text width=2.5cm] (g8){Verkeerde luchtvochtigheid}
child {node (g12){Erosie druppelvanger}}
child {node (g13){Te weinig bevochtiging}}
}
child[level distance=1.5cm, sibling distance=4cm,grow=down] {node (g9){Verkeerde temperatuur}
child {node (g14){Lekkende verwarming}}
child {node (g15){Lekkende koelbatterij}}
}
};
\end{tikzpicture}
\end{document}