我正在尝试在 TikZ 中绘制一个“科学”符号,用风格化的原子表示,但不知道该怎么做。有没有办法以某种方式定义神奇的 TikZ 样式,以便我可以执行类似操作\node[science](C1) at (0,0){};
?
我尝试过使用\def
宏,它可以绘制我想要的符号,但不知何故,节点名称无法以我以后可以引用的方式起作用。
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\title{science symbol}
\begin{tikzpicture}[>=latex,
font=\sffamily
]
\def\science#1#2 {
\node[circle, draw, minimum size=10mm] (#2) at #1 {};
\foreach \ang in {0,120,240}
\draw[rotate around={\ang:#1}] #1 ellipse (4.5mm and 1.5mm);
\fill #1 circle (0.5mm);
}
\science{(0,0)}{C1};
\science{(2cm, 0)}{C2};
\draw[->] (C1) -- (C2);
\end{tikzpicture}
\end{document}
这绘制了我想要的节点
但我的陈述有误\draw[->] (C1) -- (C2);
:
Package pgf Error: No shape named C1 is known.
答案1
我摸索着使用append after command
,并让它工作:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, calc}
\begin{document}
\title{science symbol}
\begin{tikzpicture}[>=latex,
font=\sffamily,
atom/.style = {circle, minimum size=#1,
append after command={%
\pgfextra{
\foreach \ang in {0,120,240}
\draw[rotate around={\ang:(0,0)}] (\tikzlastnode.center) ellipse (0.45*#1 and 0.15*#1);
\fill (\tikzlastnode.center) circle (0.05*#1);
}
}
}
]
\node[draw, atom=10mm] (C1) at (0,0){};
\node[draw, atom=5mm] (C2) at (2cm,0){};
\draw[->] (C1) -- (C2);
\end{tikzpicture}
\end{document}