我想绘制一个由多个队列组成的网络。要构建一个节点,我想用这个宏 (DrawStation) 创建一个节点并为其命名。
\usepackage{tikz}
\usetikzlibrary{positioning} % for positioning below,right,...
\usetikzlibrary{shapes.geometric}
% TikZ styles for drawing
\tikzstyle{connector} = [->,thick]
\newcommand{\DrawStation}[1]{
\begin{tikzpicture}
\draw (0,0) -- ++(2cm,0) -- ++(0,-1.5cm) -- ++(-2cm,0);
\foreach \i in {1,...,4}
\draw (2cm-\i*10pt,0) -- +(0,-1.5cm);
% the circle
\draw (2.75,-0.75cm) circle [radius=0.75cm];
\node at (2.75,-0.75cm) {$\mu$};
\node[align=center] at (2.1cm,-2.2cm) {#1};
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}
\DrawStation{CPU}
\end{tikzpicture}
\end{document}
一旦我得到这个,我想连接 \DrawStation 的多个节点。因此我想使用这个连接:
\path[connector]
(Name of node 1) edge (Name of node 2);
有没有办法实现我想象中的这幅图?谢谢
答案1
最好的解决方案可能是使用pic
与 类似\newcommand
但对于 的tikzpictures
。它避免了tikzpictures
不推荐的嵌套。
有关更多信息,请pics
参见:TikZ 中的形状可以由“子形状”组成吗?或者TikZ/pgf 3.0 有哪些新功能?
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning} % for positioning below,right,...
\usetikzlibrary{shapes.geometric}
% TikZ styles for drawing
\tikzstyle{connector} = [->,thick]
\tikzset{%
Station/.pic={
\draw (0,0) -- ++(2cm,0) -- ++(0,-1.5cm) -- ++(-2cm,0);
\foreach \i in {1,...,4}
\draw (2cm-\i*10pt,0) -- +(0,-1.5cm);
% the circle
\draw (2.75,-0.75cm) circle [radius=0.75cm];
\node (-mu) at (2.75,-0.75cm) {$\mu$};
\node[align=center] at (2.1cm,-2.2cm) {#1};
}}
\begin{document}
\begin{tikzpicture}
\draw pic (a) {Station=CPU};
\draw[red] pic (b) at (0,3) {Station=CPU2};
\draw (a-mu) -- (b-mu);
\end{tikzpicture}
\end{document}
答案2
如果我明白您想做什么,这里有一个使用新命令的替代解决方案。
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.geometric, arrows.meta}
\tikzset{
connect/.style={-{Latex}},
}
\newcommand\DrawStation[4][black]{%
\begin{scope}[shift={(#3)}, #1]
\draw (0,0) -- ++(2cm,0) -- ++(0,-1.5cm) -- ++(-2cm,0);
\foreach \i in {1,...,4}
\draw (2cm-\i*10pt,0) -- +(0,-1.5cm);
% the circle
\draw (2.75,-0.75cm) circle [radius=0.75cm];
\node (#2) at (2.75,-0.75cm) {$\mu$};
\node[align=center] at (2.1cm,-2.2cm) {#4};
\end{scope}
}
\begin{document}
\begin{tikzpicture}
\DrawStation{a}{0,0}{CPU}
\DrawStation[red]{b}{0,3}{CPU2}
\draw (a) -- (b);
\end{tikzpicture}
\end{document}