3

I don't have much experience with creating new symbols using the \pgfdeclareshape macro, and I believe there are a few things I'm overlooking.

My specific question is: Why the line associated with the tikz \draw operation is not obscured by the symbol when placed on a path? For instance, in many examples of the circuits library documentation, resistors are clearly not drawn with a line extending through the center of the symbol.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\usetikzlibrary{circuits}
\begin{document}
\makeatletter
\pgfdeclareshape{symbol shape}
{
\anchor{center}{\pgfpointorigin}
\backgroundpath{%
    \pgfpathmoveto{\pgfpoint{-2cm}{-0.5cm}}
    \pgfpathlineto{\pgfpoint{-2cm}{0.5cm}}
    \pgfpathlineto{\pgfpoint{2cm}{0.5cm}}
    \pgfpathlineto{\pgfpoint{2cm}{-0.5cm}}
    \pgfpathlineto{\pgfpoint{-2cm}{-0.5cm}}

    \pgfpathmoveto{\pgfpoint{-2cm}{-0.5cm}}
    \pgfpathlineto{\pgfpoint{2cm}{0.5cm}}

    \pgfpathmoveto{\pgfpoint{-2cm}{0.5cm}}
    \pgfpathlineto{\pgfpoint{2cm}{-0.5cm}}
    \pgfusepath{stroke}
    }
}
\makeatother


\begin{tikzpicture}[
circuit,
circuit declare symbol=symbol,
set symbol graphic={shape=symbol shape, draw, minimum size=1cm,transform shape}
]
\draw (0,0) to [symbol] (8,0);
\end{tikzpicture}
\end{document}

Can anyone suggest an improvement to the code shown above in order to prevent the \draw operation line from extending through the symbol?

0
2

TikZ doesn’t know where the lines should start and end. You haven’t defined a border or any anchors besides the center one.

When we base our shape on rectangle ee we get all the properties of the rectangle shape plus the input and output anchors that are additionally defined for rectangle ee.

The size of the rectangle (2cm x .5cm) is based on \tikzcircuitssizeunit (initially 7pt) via the circuit symbol size style. Now the rectangle scales properly with all other circuit shape options. The resistor’s default setting is width 4 height 1. For the sizes 2cm x .5cm the value is width 8.12936 height 2.03233.

Of course, if the shape should always have the same size you can use minimum width=2cm, minimum height=.5cm.

Code

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{circuits,circuits.ee.IEC}
\makeatletter
\pgfdeclareshape{symbol shape}{%
  \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]
  \behindbackgroundpath{%
    \pgf@process{\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}%
    \pgf@xa\pgf@x\pgf@ya\pgf@y
    \pgf@process{\pgfpointadd{\northeast}{\pgfpointscale{-1}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}}}%
    \pgf@xb\pgf@x\pgf@yb\pgf@y
    \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}}%
    \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}}%
    \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@ya}}%
    \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}%
  }
}
\makeatother
\tikzset{
  circuit declare symbol=symbol,
  set symbol graphic={
    shape=symbol shape,
    draw,
    transform shape,
    circuit symbol size=width 8.12936 height 2.03233,
%    circuit symbol size=width 4 height 1,% standard value for an resistor
  }
}
\begin{document}
\begin{tikzpicture}[circuit ee IEC]
\draw (0,0) to [symbol] (3,0) to [resistor] (5,0);
\end{tikzpicture}
\end{document}

Output

enter image description here

1
  • Thanks very much, this is what I wanted to achieve. Your pointing out that a border is needed makes sense.
    – John Chris
    Jul 2 '13 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.