Tikz 解决图片嵌套问题

Tikz 解决图片嵌套问题

关注我的帖子Tikz 理解节点和绘制之间的空白我知道我不应该嵌套 tikz 图片,按照建议我发现了我的真正问题。

我想创建一些组件并能够相当轻松地连接它们(例如在 Simulink 中所做的那样),然后我想到了

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

有什么更好的方法可以进行?

答案1

您已经有了这些漂亮的图片。您可以使用选项为它们指定一个类似节点的名称local bounding box

\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

当然,您也可以使用\saveboxes。对于无限总线,您甚至不需要\savebox,您只需使用带有 的普通节点即可circle,draw,在第二种情况下,您可以使用\savebox。此版本没有硬编码的水平线,并且您之前的问题中的间隙也不是问题,因为您绘制了边框。

\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

另一个选项是使用路径图片。在这种情况下,这可能是有利的,因为节点随后会从圆形而不是矩形继承锚点,如果您使用 ,它就会这样做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

相关内容