4

Following my post Tikz Understand white space between node and draw I understand that I should not nest tikz pictures and following the advice I expose my real problem.

I want to create some components and be able to connect them rather easily (like one would do in Simulink for example) and I came up with

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\tikzset{generator/.pic={
    code={
        \draw (0,0) circle (2);
        \draw (0,0) arc (0:180:0.5);
        \draw (0,0) arc (180:360:0.5);
        \draw (-2,0) --++ (-2,0);
   }
 }
}

\tikzset{infinite bus/.pic={
    code={
    \draw (0,0) circle (2) node[inner sep=0, outer sep = 0] {{$\infty$}};
    \draw (2,0) --++ (2,0);
  }
 }
}

\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0}]
% Infinite bus 
\node at (0,0) (bus) {\tikz\path (0,0) pic[scale=0.2] {infinite bus};};
% Generator
\node at (5,0) (gen) {\tikz\path (0,0) pic[scale=0.2] {generator};};
% Line
\draw (bus.east) -- (gen.west);
\end{tikzpicture}
\end{document}

What would be a better way to proceed ?

3
  • What about circuitikz?
    – NBur
    Aug 24 '18 at 13:26
  • You could just use the pics themselves, why do you want to wrap a node around them?
    – user121799
    Aug 24 '18 at 13:28
  • 1
    With the pic itself I didn't didn't find a way to refer to east, west etc to be able to draw lines easily between components. As for cirtcuitz I do power system and not electronic systems so the conventions are not quite the same sometimes and would need to add some components of my own. Aug 24 '18 at 13:34
8

You already have these nice pics. You could give them a node-like name using the local bounding box option.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\tikzset{generator/.pic={
    code={
        \draw (0,0) circle (2);
        \draw (0,0) arc (0:180:0.5);
        \draw (0,0) arc (180:360:0.5);
        \draw (-2,0) --++ (-2,0);
   }
 }
}

\tikzset{infinite bus/.pic={
    code={
    \draw (0,0) circle (2) node[inner sep=0, outer sep = 0] {{$\infty$}};
    \draw (2,0) --++ (2,0);
  }
 }
}

\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0}]
% Infinite bus 
%\node at (0,0) (bus) {\tikz\path (0,0) pic[scale=0.2] {infinite bus};};
\path (0,0)  pic[scale=0.2,local bounding box=bus] {infinite bus};
% Generator
\path (5,0) pic[scale=0.2,local bounding box=gen] {generator};
% Line
\draw (bus.east) -- (gen.west);
\end{tikzpicture}
\end{document}

enter image description here

Of course, you could also work with \saveboxes. For the infinite bus you do not even need a \savebox, you can just use a plain node with circle,draw, and in the second case you could use a \savebox. This version does not have the hard coded horizontal lines, and the gap of your previous question isn't an issue either since you draw the border.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\newsavebox\Generator
\sbox\Generator{\tikz{\draw (0,0) arc (0:180:0.5);
        \draw (0,0) arc (180:360:0.5);}}
\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0}]
% Infinite bus 
\node[circle,draw,minimum size=1cm] at (0,0) (bus) {$\infty$};
% Generator
\node[circle,draw,minimum size=1cm] at (5,0) (gen) {\scalebox{0.2}{\usebox\Generator}};
% Line
\draw (bus.east) -- (gen.west);
\end{tikzpicture}
\end{document}

enter image description here

Yet another option is to work with path pictures. In this case this might be advantageous as the node then inherits the anchors from the circle shape rather than an rectangle, which it does if you use local bounding box.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\tikzset{generator/.style={circle,draw,minimum size=1cm,path picture={
\draw (-0.25,0) to[out=90,in=90,tension=1.2]  (0,0) to[out=-90,in=-90,tension=1.2] (0.25,0);
}},
infinite bus/.style={circle,draw,minimum size=1cm}}
\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0}]
% Infinite bus 
\path  (0,0) node[infinite bus]  (bus) {$\infty$} (5,0) node[generator] (gen){};
\draw (bus) -- (gen);
\end{tikzpicture}
\end{document}

enter image description here

1
  • That is exactly the type of thing I was looking for Aug 24 '18 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.