我正在尝试修改故障树,例如来自 Tikz 的故障树,其中节点是门之类的对象而不是文本框,并且如果可能的话,甚至会从一些路径中消除文本框。这是我到目前为止的代码。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees,calc,shadings,shapes.gates.logic.US,positioning,arrows}
\usetikzlibrary{shapes,snakes}
\begin{document}
\begin{tikzpicture}
[
% Gates and symbols style
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},
sq/.style={regular polygon,regular polygon sides=4,thick,draw,fill=orange!60,anchor=north, scale=0.2},
% Label style
label distance=3mm, every label/.style={blue},
% Event style
event/.style={rectangle,thick,draw,fill=yellow!20,text width=2cm, text centered,font=\sffamily,anchor=north},
event2/.style={rectangle,draw=white, fill=white,},
and2/.style={and gate US,draw,fill=red!60,rotate=90, scale=0.5 },
% Children and edges style
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=4cm,level distance=1.5cm, growth parent anchor=south,nodes=event},
level 2/.style={sibling distance=3cm, level distance=2cm},
level 3/.style={sibling distance=3cm},
level 4/.style={sibling distance=3cm}
]
%% Draw events and edges
\node (g1) [event] {System Failure}
child {node (e1) {Midplane}}
child {node [event] (e2) {k/n}
child {node [sq] (e2a) {1}}
child {node [sq] (e2b) {2}}
child[level distance=20mm] {node (e2c) [or, scale=0.4] {}
child {node [sq] (e2c1) {x}}
child {node [sq] (e2c2) {y}}
}
child {node [sq] (e2d) {4}}
}
child {node (e3) {Cooling}}
child {node (e4) {Power domain 1}}
;
% Remove what follows if no gates are required
\node [or] at (g1.south) [] {};
\node [be] at (e1.south) [] {};
\node [and] at (e2.south) [] {};
\node [be] at (e3.south) {};
\node [be] at (e4.south) {};
\end{tikzpicture}
\end{document}
如您所见,我显然在与中间的蓝色“或”门作斗争。我哪里做错了?
答案1
只是为了解释一下你的代码出了什么问题:
问题在于你对edge from parent path
edge from parent path={(\tikzparentnode.south) -- ++(0,-1.05cm)-| (\tikzchildnode.north)}
但由于旋转,节点的锚点or/.style
在它们的左侧,而锚点在它们的右侧。(见north
or
south
如何管理旋转矩形的定位) 因此您必须更改树中的子项的child anchor
、parent anchor
以及的。growth parent anchor
or
如果你使用
edge from parent path={(\tikzparentnode\tikzparentanchor) --
++(0,-1.05cm)-| (\tikzchildnode\tikzchildanchor)},
parent anchor=south,
child anchor=north,
您可以更改树内的parent anchor
和。child anchor
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees,calc,shadings,shapes.gates.logic.US,positioning,arrows}
\usetikzlibrary{shapes}
\begin{document}
\begin{tikzpicture}
[
% Gates and symbols style
and/.style={and gate US,thick,draw=black,fill=red!60,rotate=90,
anchor=east,xshift=-1mm},
or/.style={or gate US,thick,draw=black,fill=blue!60,rotate=90,
anchor=east,xshift=-1mm},
be/.style={circle,thick,draw=black,fill=green!60,anchor=north,
minimum width=0.7cm},
tr/.style={buffer gate US,thick,draw=black,fill=purple!60,rotate=90,
anchor=east,minimum width=0.8cm},
sq/.style={regular polygon,regular polygon sides=4,thick,draw=black,fill=orange!60,anchor=north, scale=0.2},
% Label style
label distance=3mm, every label/.style={blue},
% Event style
event/.style={rectangle,thick,draw=black,fill=yellow!20,text width=2cm, text centered,font=\sffamily,anchor=north},
event2/.style={rectangle,draw=white, fill=white,},
and2/.style={and gate US,draw,fill=red!60,rotate=90, scale=0.5 },
% Children and edges style
edge from parent/.style={very thick,draw=black!70},
edge from parent path={(\tikzparentnode\tikzparentanchor) -- ++(0,-1.05cm) -| (\tikzchildnode\tikzchildanchor)},
parent anchor=south,
child anchor=north,
level 1/.style={sibling distance=4cm,level distance=1.5cm, growth parent anchor=south,nodes=event},
level 2/.style={sibling distance=3cm, level distance=2cm},
level 3/.style={sibling distance=3cm},
level 4/.style={sibling distance=3cm}
]
\node (g1) [event] {System Failure}
child {node (e1) {Midplane}}
child {node [event] (e2) {k/n}
child {node [sq] (e2a) {1}}
child {node [sq] (e2b) {2}}
child[child anchor=east] {node (e2c) [or, text width=0pt] {}
{[parent anchor=west,growth parent anchor=west,child anchor=north]
child {node [sq] (e2c1) {x}}
child {node [sq] (e2c2) {y}}}
}
child {node [sq] (e2d) {4}}
}
child {node (e3) {Cooling}}
child {node (e4) {Power domain 1}}
;
% Remove what follows if no gates are required
\node [or] at (g1.south) [] {};
\node [be] at (e1.south) [] {};
\node [and] at (e2.south) [] {};
\node [be] at (e3.south) {};
\node [be] at (e4.south) {};
\end{tikzpicture}
\end{document}
答案2
根据显示的代码,所有门都通过\node
代码末尾列出的命令放置在它们所在的位置。因此,(e2c) 节点是与其邻居一样的矩形节点。因此,您不能简单地OR
在该节点的选项中放置一个,这会导致 OP 所显示的结果。一种解决方法是将 (e2c) 节点更改为坐标,使其没有形状,然后OR
通过额外的节点命令放置一个,如下所示,yshift
用于调整OR
门的位置。
代码
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees,calc,shadings,shapes.gates.logic.US,positioning,arrows}
\usetikzlibrary{shapes,snakes}
\begin{document}
\begin{tikzpicture}
[
% Gates and symbols style
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},
sq/.style={regular polygon,regular polygon sides=4,thick,draw,fill=orange!60,anchor=north, scale=0.2},
% Label style
label distance=3mm, every label/.style={blue},
% Event style
event/.style={rectangle,thick,draw,fill=yellow!20,text width=2cm, text centered,font=\sffamily,anchor=north},
event2/.style={rectangle,draw=white, fill=white,},
and2/.style={and gate US,draw,fill=red!60,rotate=90, scale=0.5 },
% Children and edges style
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=4cm,level distance=1.5cm, growth parent anchor=south,nodes=event},
level 2/.style={sibling distance=3cm, level distance=2cm},
level 3/.style={sibling distance=3cm},
level 4/.style={sibling distance=3cm}
]
%% Draw events and edges
\node (g1) [event] {System Failure}
child {node (e1) {Midplane}}
child {node [event] (e2) {k/n}
child {node [sq] (e2a) {1}}
child {node [sq] (e2b) {2}}
child[level distance=20mm] {node (e2c) [coordinate,scale=0.4] {} % change the node to coordinate property, the default is a rectangle.
child {node [sq] (e2c1) {x}}
child {node [sq] (e2c2) {y}}
}
child {node [sq] (e2d) {4}}
}
child {node (e3) {Cooling}}
child {node (e4) {Power domain 1}}
;
% Remove what follows if no gates are required
\node [or] at ([yshift=0.5cm]e2c.south) [] {}; % added one line here
\node [or] at (g1.south) [] {};
\node [be] at (e1.south) [] {};
\node [and] at (e2.south) [] {};
\node [be] at (e3.south) {};
\node [be] at (e4.south) {};
\end{tikzpicture}
\end{document}