![如何使用带有参数的节点样式在 TikZ 节点内部绘图?](https://linux22.com/image/307884/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E5%B8%A6%E6%9C%89%E5%8F%82%E6%95%B0%E7%9A%84%E8%8A%82%E7%82%B9%E6%A0%B7%E5%BC%8F%E5%9C%A8%20TikZ%20%E8%8A%82%E7%82%B9%E5%86%85%E9%83%A8%E7%BB%98%E5%9B%BE%EF%BC%9F.png)
我想画一个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}