如何使用带有参数的节点样式在 TikZ 节点内部绘图?

如何使用带有参数的节点样式在 TikZ 节点内部绘图?

这个问题是对这个问题,并且还与这个

我想画一个Simulink 样式求和块它基本上是一个圆形节点,每个方向(北、西、南、东)都有一个端口。如果将端口用作添加(或减去)输入,则端口上会显示加号(或减号)。我希望我可以使用带有参数的自定义节点样式来指定端口配置:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
    positive/.pic = {\draw (-1mm,0)--(1mm,0) (0,-1mm)--(0,1mm);},
    negative/.pic = {\draw (-1mm,0)--(1mm,0);},
    sum block/.style = {draw, circle, inner sep=0pt, minimum size=9mm,
        north/.style = {append after command={pic at +(0,3mm) {#1}}},
        south/.style = {append after command={pic at +(0,-3mm) {#1}}},
        west/.style = {append after command={pic at +(-3mm,0) {#1}}},
        east/.style = {append after command={pic at +(3mm,0) {#1}}},
    },
]
% I wish to create a node like this:
\path (1cm, 0) node [sum block] {} pic at +(0,3mm) {positive} pic at +(-3mm, 0) {negative};
% using a more elegant syntax like this:
\node at (2cm, 0) [sum block, north=positive, west=negative] {};
\end{tikzpicture}
\end{document}

结果如下;所需的花哨语法没有产生任何端口符号。

测试输出

有没有简单的方法可以实现目标?仅限 TikZ 的解决方案这个是优选的。

答案1

你想要这样的东西吗?

\documentclass[tikz, border=5pt]{standalone}
\begin{document}
  \tikzset{
    charge node/.style={inner sep=0pt},
    pics/sum block/.style n args={4}{
      code={
        \path node (n) [draw, circle, inner sep=0pt, minimum size=9mm] {}
          (n.north) +(0,-1.5mm) node [charge node] {$#1$}
          (n.south) +(0,1.5mm) node [charge node] {$#2$}
          (n.west) +(1.5mm,0) node [charge node] {$#3$}
          (n.east) +(-1.5mm,0) node [charge node] {$#4$}
          ;
      }
    }
  }
  \begin{tikzpicture}
    \path pic at (10mm,0) {sum block={+}{-}{+}{-}}
    pic at (20mm,0) {sum block={}{+}{-}{}}
    ;
  \end{tikzpicture}
\end{document}

充电图片

相关内容