我想通过对多个元素进行分组来创建一个多switch
部分形状,同时保留锚点(sw)
、、和,以便我可以使用命令将其插入并通过引用其子锚点将其连接到其他节点。(sw1)
(sw2)
(sw3)
\node
简而言之,我想要做的就是像在 Microsoft Visio 中一样进行分组和锚定。
\documentclass[tikz,border=6pt]{standalone}
\usetikzlibrary{graphs,positioning,calc,arrows.meta}
\tikzset{
neuron/.style = {circle, very thick, minimum size=7.5mm, inner sep=0, outer sep=0, draw},
cost/.style={rectangle, minimum size=7.5mm, inner sep=0, outer sep=0},
switch/.style={cost, draw=gray, opacity=.1, densely dashed, node contents={}},
contact/.style={neuron, node contents={}, minimum size=2mm},
location/.style={circle, inner sep=0, outer sep=0, node contents={}, minimum size=3pt, fill},
node distance=7.5mm,
}
\begin{document}
\begin{tikzpicture}
% Place nodes
\node (n1) [neuron] {\(n_1\)};
\node (n2) [neuron, right=of n1] {\(n_2\)};
% Switch elements ------------------------------------------------
\node at ($ (n1)!.5!(n2) + (0,1)$) (sw) [switch];
\node at (sw.west) (sw1) [contact, anchor=west];
\node at (sw.east) (sw2) [contact, anchor=east];
\draw [very thick, line cap=round]
($(sw1.south)!.5!(sw2.south)$) -- ++(0,2mm)
node (sw3) [location];
\def\r{2.3mm}\def\delta{90}
\draw [{Latex[length=2pt]}-{Latex[length=2pt]}, gray]
($(-90-\delta/2:\r)+(sw3)$)
arc [radius=\r, start angle=-90-\delta/2, delta angle=\delta];
% ----------------------------------------------------------------
\node at ($(sw) + (0,1)$) (n3) [neuron] {\(n_3\)};
% Connect nodes
\graph[use existing nodes] {
n1 ->[to path={|- (\tikztotarget)}] sw1;
n2 ->[to path={|- (\tikztotarget)}] sw2;
sw3 -> n3;
};
\end{tikzpicture}
\end{document}
答案1
您可以使用,但这种方法与使用连接事物的方法\pic
不直接兼容:\graph
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc, arrows.meta}
\tikzset{
neuron/.style={
circle, very thick, minimum size=7.5mm, inner sep=0, outer sep=0, draw
},
cost/.style={
rectangle, minimum size=7.5mm, inner sep=0, outer sep=0
},
switch/.style={
cost, draw=gray, opacity=.1, densely dashed, node contents={},
},
switch pic/radius/.initial=2.3mm,
switch pic/delta angle/.initial=90,
pics/switch pic/.style={
code={
\tikzset{switch pic/.cd, #1}
\node (-center) [switch];
\node at (-center.west) (-west) [contact, anchor=west];
\node at (-center.east) (-east) [contact, anchor=east];
\draw[very thick, line cap=round]
($(-west.south)!.5!(-east.south)$) -- ++(0,2mm)
node (-location) [location];
\draw[{Latex[length=2pt]}-{Latex[length=2pt]}, gray]
($(-90-\pgfkeysvalueof{/tikz/switch pic/delta angle}/2:
\pgfkeysvalueof{/tikz/switch pic/radius})+(-location)$)
arc[radius=\pgfkeysvalueof{/tikz/switch pic/radius},
start angle=-90-\pgfkeysvalueof{/tikz/switch pic/delta angle}/2,
delta angle=\pgfkeysvalueof{/tikz/switch pic/delta angle}];
}
},
contact/.style={
neuron, node contents={}, minimum size=2mm
},
location/.style={
circle, inner sep=0, outer sep=0, node contents={}, minimum size=3pt, fill
},
node distance=7.5mm,
}
\begin{document}
\begin{tikzpicture}
% Place nodes
\node (n1) [neuron] {\(n_1\)};
\node (n2) [neuron, right=of n1] {\(n_2\)};
% Switch elements ------------------------------------------------
\pic at ($(n1)!.5!(n2) + (0,1)$) (sw) {switch pic={radius=2.3mm, delta angle=90}};
% ----------------------------------------------------------------
\node at ($(sw-center) + (0,1)$) (n3) [neuron] {\(n_3\)};
% Connect nodes
\draw[->] (n1) |- (sw-west);
\draw[->] (n2) |- (sw-east);
\draw[->] (sw-location) -- (n3);
\end{tikzpicture}
\end{document}
您还可以使用path picture
,这将允许您使用\graph
,但需要进行一些其他调整:
- 您需要稍微放大分配的节点
path picture
,因为否则内容将被剪掉。这里需要考虑非常粗的圆圈的线宽。 - 我建议您为
switch
样式添加一个参数,以便以后多次使用此样式时可以识别正确的节点。
完整 MWE:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs, positioning, calc, arrows.meta}
\tikzset{
neuron/.style={
circle, very thick, minimum size=7.5mm, inner sep=0, outer sep=0, draw
},
cost/.style={
rectangle, minimum size=7.5mm, inner sep=0, outer sep=0
},
switch radius/.initial=2.3mm,
switch delta angle/.initial=90,
switch/.style={
minimum size={7.5mm+1.2pt},
path picture={
\node at (path picture bounding box.center)
(switch center #1) [cost, draw=gray, opacity=.1, densely dashed] {};
\node at (switch center #1.west)
(switch west #1) [contact, anchor=west];
\node at (switch center #1.east)
(switch east #1) [contact, anchor=east];
\draw[very thick, line cap=round]
($(switch west #1.south)!.5!(switch east #1.south)$) -- ++(0,2mm)
node (switch location #1) [location];
\draw[{Latex[length=2pt]}-{Latex[length=2pt]}, gray]
($(-90-\pgfkeysvalueof{/tikz/switch delta angle}/2:
\pgfkeysvalueof{/tikz/switch radius})+(switch location #1)$)
arc[radius=\pgfkeysvalueof{/tikz/switch radius},
start angle=-90-\pgfkeysvalueof{/tikz/switch delta angle}/2,
delta angle=\pgfkeysvalueof{/tikz/switch delta angle}];
},
node contents={}
},
contact/.style={
neuron, node contents={}, minimum size=2mm
},
location/.style={
circle, inner sep=0, outer sep=0, node contents={}, minimum size=3pt, fill
},
node distance=7.5mm,
}
\begin{document}
\begin{tikzpicture}
% Place nodes
\node (n1) [neuron] {\(n_1\)};
\node (n2) [neuron, right=of n1] {\(n_2\)};
% Switch elements ------------------------------------------------
\node at ($(n1)!.5!(n2) + (0,1)$) (sw) [switch={1}];
% ----------------------------------------------------------------
\node at ($(sw) + (0,1)$) (n3) [neuron] {\(n_3\)};
% Connect nodes
\graph[use existing nodes] {
n1 -> [to path={|- (\tikztotarget)}] switch west 1;
n2 -> [to path={|- (\tikztotarget)}] switch east 1;
switch location 1 -> n3;
};
\end{tikzpicture}
\end{document}