我在向上移动树的层级时遇到了问题。我想向上移动带有黄色框和或门的行,以节省一些空间。我尝试在层级 3/.style 中添加“层级距离”,但这没有帮助。
对于与门中的文本“k / n”,我如何对其进行级别转换?
代码:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees,calc,shadings,shapes.gates.logic.US,positioning,arrows, fit}
\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, scale=1.3},
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},
tri/.style={regular polygon,regular polygon sides=3,thick,draw=black,fill=black!60,rotate=180, anchor=south, scale=0.5},
% 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=3cm,level distance=1.5cm, growth parent anchor=south,nodes=event},
level 2/.style={sibling distance=2cm},
level 3/.style={sibling distance=1.5cm},
level 4/.style={sibling distance=2cm},
level 5/.style={},
level 6/.style={},
]
\node (g1) [event] {System Failure}
child {node [be, scale=0.2] (e1) {}}
child[child anchor=east] {node [and, text width=3pt, ] (e2) {k/n}
{[parent anchor=west,growth parent anchor=west,child anchor=north]
child {node [sq] (e2a) {}}
child {node [sq] (e2b) {}}
child[child anchor=east] {node (e2c) [or, text width=0pt] {}}
child {node [sq] (e2d) {}}
child[sibling distance=3cm] {node [sq] (e2e) {}}
}
}
child {node [be, scale=0.2] (e3) {}}
child {node [be, scale=0.2] (e4) {}}
;
% Remove what follows if no gates are required
\node [or] at (g1.south) [] {};
% \node [and] at (e2.south) [] {};
% text descriptions go here
\node [rotate=360] at ([yshift=-3mm]e1.south) {Midplane};
\node [rotate=360] at ([yshift=-3mm]e3.south) {Cooling};
\node [rotate=360] at ([yshift=-3mm]e4.south) {Power domain 1};
\node [rotate=360] at ([xshift=20mm]e2d.east) {$\dots$};
\end{tikzpicture}
\end{document}
答案1
由于从节点 (e2) 开始,父结构和子结构就不再是全局的,因此全局设置不起作用。节省空间的愿望需要从 (e2) 节点开始进行本地调整。因此,此解决方案在
edge from parent path={(\tikzparentnode\tikzparentanchor) -- ++(0,-0.5cm) -| (\tikzchildnode\tikzchildanchor)},
level distance=0.8cm % -0.5cm and 0.8cm can be adjusted to suit one's need.
至于标签的第二个问题k/n
,因为节点属性是由and
定义的门OP更改的。为了进行属性对齐,此解决方案label={center:{k/n}}
在选项中使用写入,而不是(e2){external label}
。请注意,可以根据需要center
将更改为。left, right, above, below
代码
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{trees,calc,shadings,shapes.gates.logic.US,positioning,arrows, fit}
\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, scale=1.3},
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},
tri/.style={regular polygon,regular polygon sides=3,thick,draw=black,fill=black!60,rotate=180, anchor=south, scale=0.5},
% 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=3cm,level distance=1.5cm, growth parent anchor=south,nodes=event},
level 2/.style={sibling distance=2cm},
level 3/.style={growth parent anchor=west,level distance=-0.5cm, sibling distance=1.5cm},
level 4/.style={sibling distance=2cm},
level 5/.style={},
level 6/.style={},
]
\node (g1) [event] {System Failure}
child {node [be, scale=0.2] (e1) {}}
child[child anchor=east] {node [and, text width=3pt,label={center:{k/n}}
] (e2) {}
{[parent anchor=west,growth parent anchor=west,child anchor=north,
edge from parent path={(\tikzparentnode\tikzparentanchor) -- ++(0,-0.5cm) -| (\tikzchildnode\tikzchildanchor)},
level distance=0.8cm]
child {node [sq] (e2a) {}}
child {node [sq] (e2b) {}}
child[child anchor=east] {node (e2c) [or, text width=0pt] {}}
child {node [sq] (e2d) {}}
child[sibling distance=3cm] {node [sq] (e2e) {}}
}
}
child {node [be, scale=0.2] (e3) {}}
child {node [be, scale=0.2] (e4) {}}
;
% Remove what follows if no gates are required
\node [or] at (g1.south) [] {};
% \node [and] at (e2.south) [] {};
% text descriptions go here
\node [rotate=360] at ([yshift=-3mm]e1.south) {Midplane};
\node [rotate=360] at ([yshift=-3mm]e3.south) {Cooling};
\node [rotate=360] at ([yshift=-3mm]e4.south) {Power domain 1};
\node [rotate=360] at ([xshift=20mm]e2d.east) {$\dots$};
\end{tikzpicture}
\end{document}