3

i would like to draw a network of multiple queues. To build one node i would like to make a node out of this macro (DrawStation) and give it a name.

\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}

Once i got this i want to connect multiple nodes of \DrawStation.Therefor i would like to use this connection:

\path[connector]
(Name of node 1) edge (Name of node 2);

Is there way to realize this picture the way i imagined? Thank you

2 Answers2

6

The best solution could be to use a pic which is something similar to \newcommand but for tikzpictures. It avoids nesting tikzpictures which is not recommended.

Some more information abouts pics in: Can a shape be composed out of “subshapes” in TikZ? or What are the new features in 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}

enter image description here

Ignasi
  • 136,588
3

Here's an alternative solution with a newcommand, if I understood what you want to do.

Output

enter image description here

Code

\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}
Alenanno
  • 37,338