Using \pgfpointanchor in the background path of a circuit symbol shape

I have tried to create a circuit symbol for general sources as used in physics textbooks for high schools:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{circuits.ee.IEC,shapes.gates.ee}
\makeatletter
\pgfdeclareshape{mybox}{%
\inheritsavedanchors[from=rectangle ee]
\inheritanchor[from=rectangle ee]{center}
\inheritanchor[from=rectangle ee]{north}
\inheritanchor[from=rectangle ee]{south}
\inheritanchor[from=rectangle ee]{east}
\inheritanchor[from=rectangle ee]{west}
\inheritanchor[from=rectangle ee]{north east}
\inheritanchor[from=rectangle ee]{north west}
\inheritanchor[from=rectangle ee]{south east}
\inheritanchor[from=rectangle ee]{south west}
\inheritanchor[from=rectangle ee]{input}
\inheritanchor[from=rectangle ee]{output}
\inheritanchorborder[from=rectangle ee]
\inheritbackgroundpath[from=rectangle ee]
\anchor{west circ center}{
\pgfpointlineattime{0.5}{\southwest}{\northeast}
\advance\pgf@x by-0.5\tikzcircuitssizeunit%
}
\anchor{east circ center}{
\pgfpointlineattime{0.5}{\southwest}{\northeast}
\advance\pgf@x by0.5\tikzcircuitssizeunit%
}
\backgroundpath{%
\pgfpathmoveto{\pgfpointanchor{\tikz@fig@name}{west}}
\pgfpathlineto{\pgfpointanchor{\tikz@fig@name}{west circ center}}
\pgfpathmoveto{\pgfpointanchor{\tikz@fig@name}{east}}
\pgfpathlineto{\pgfpointanchor{\tikz@fig@name}{east circ center}}
\pgfusepath{stroke}
\pgfpathcircle{\pgfpointanchor{\tikz@fig@name}{east circ center}}{0.25\tikzcircuitssizeunit}
\pgfpathcircle{\pgfpointanchor{\tikz@fig@name}{west circ center}}{0.25\tikzcircuitssizeunit}
\pgfusepath{stroke,fill}
}
}
\makeatother
\tikzset{
circuit declare symbol=general source,
set general source graphic={
draw,fill=white,shape=mybox, circuit symbol size=width 3 height 1,
transform shape,
}
}
\begin{document}
\begin{tikzpicture}[circuit ee IEC]
\draw(0,0)to[general source={info={foo}}](2,0)to[resistor={info=bar}](4,0);
\draw(0,0)to[resistor](0,-3)to[make contact](4,-3)to[bulb](4,0);
\end{tikzpicture}

\end{document}


The \backgroundpath is drawn using commands like \pgfpathlineto and \pgfpointanchor, which works at my machine. tikz itself defines its own derived shapes using background paths with hard calculations based solely on \southwest and \northeas which looks cumbersome to me, once the other anchors are established.

Is there any harm or risk in my way and should I switch to tikz's way, or can I leave it like this?

• The reason tikz code looks so odd is that it is plain tex compatible. – John Kormylo Jan 22 '17 at 22:49

1 Answer

In short, you are not wrong. It is up to you whether it is worth it.

The PGF manual makes it clear that \anchor, in contrast to \savedanchor, is a create-on-demand object. That is, an anchor's coordinate is calculated only if the user explicitly write (A.east).

This saves a lot of time because 90% of anchors are not used at all and we should not waste any time calculating them.

When TikZ is drawing the background path, these \anchor's coordinates are not calculated yet. If TikZ calculates these coordinates just for the drawing then it is pointless to use \anchor at the first place... simply make everything \savedanchor.

So it is not about TeX-compatibility. TikZ prefer pure TeX-code because they are faster. The author decides to spend two more hours to save millions of people two seconds. In my opinion, we should say that TikZ is designed for TeX, and is compatible with LaTeX.

In your case, it is all up to you. Do you want to write simple codes and wait for couple more milliseconds? Or you rather playing with \pgf@x? I personally prefer the former.