如何在 TikZ 節點上繪製可重複使用的符號

如何在 TikZ 節點上繪製可重複使用的符號

我試著在 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}

在此輸入影像描述

相關內容