Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I change the appearence of a voltage source in TikZ.

The picture shows the actual and the desired result (faked manually)

actual and desired result

Code

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{circuits.ee.IEC}

\begin{document}
\begin{tikzpicture}[circuit ee IEC]
  \node at (0,0) [anchor=south west] {desired};
  \node at (0,2) [anchor=south west] {actual};
% actual:
  \draw (0,2) to[voltage source={info={$U_\sim$}}] ++(5,0);
% desired (faked):
  \draw [radius=1pt,fill=white] (0,0) -- ++(2.25,0) circle ++(0.5,0) circle --++(2.25,0);
  \fill [radius=0.8pt,fill=white] (0,0) -- ++(2.25,0) circle ++(0.5,0) circle --++(2.25,0);
  \path (0,0) -- (5,0) node [midway,above] {$U_\sim$};
\end{tikzpicture}
\end{document}
share|improve this question
    
I think you can adapt the answer of this question for your taste. –  percusse Oct 30 '11 at 22:46
add comment

1 Answer

up vote 7 down vote accepted

Here's a new shape and the accompanying style to use the shape in a circuit. Defining a new circuit symbol can be a bit confusing at first: To define a completely new symbol (one that isn't based on a circle or a diode), you first need to define a PGF shape, then declare a new circuit symbol in TikZ, and then set the graphics for this symbol to use the newly defined shape.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{circuits.ee.IEC}

\begin{document}

\makeatletter
\pgfdeclareshape{alt voltage source}    % Declare the PGF shape for the voltage source
{
  \inheritsavedanchors[from=rectangle ee]   % Start with the anchors of a rectangle
  \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]

  \backgroundpath{  % Draw the actual bits
    \pgf@process{\pgfpointadd{\southwest}{\pgfpoint{\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}} % Get lower left corner
    \pgf@xa=\pgf@x % Store left x-coordinate in macro
    \pgf@process{\pgfpointadd{\northeast}{\pgfpoint{-\pgfkeysvalueof{/pgf/outer xsep}}{\pgfkeysvalueof{/pgf/outer ysep}}}} % Get upper right corner
    \pgf@xb=\pgf@x % Store right x-coordinate in macro
    \pgfmathsetlength\pgfutil@tempdima{(\pgf@xb-\pgf@xa)/12} % calculate radius of point: 1/12th of width (this is what 'var make contact IEC' uses)
    {\pgfpathcircle{\pgfpoint{\pgf@xa+\pgfutil@tempdima}{0pt}}{\pgfutil@tempdima}} % Draw circles in own groups, to protect the macros
    {\pgfpathcircle{\pgfpoint{\pgf@xb-\pgfutil@tempdima}{0pt}}{\pgfutil@tempdima}}
  }
}
\makeatother

\tikzset{
    circuit declare symbol=alt voltage source, % Declare the circuit symbol
    set alt voltage source graphic={ % Define how to display the circuit symbol
        circuit symbol lines, % Draw with the `lines` style, not the `wires` style
        circuit symbol size=width 2 height 0.15, % The size of the bounding box
        transform shape, % The symbol rotates and scales
        shape=alt voltage source % Use the shape we defined earlier
    }
}
\begin{tikzpicture}[
    circuit ee IEC,
    set voltage source graphic=alt voltage source % redefine the normal voltage source
]
  \draw (0,0) to [alt voltage source={info={$U_\sim$}}] ++(2,0);
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thank you! But i wonder if it’s possible to overwrite the definition of voltage source in a similar way. I tried to delete all var s but that didn’t work … –  Tobi Oct 31 '11 at 17:31
    
The voltage source graphic is defined when you set circuit ee IEC in the options to the tikzpicture. If you want to use the newly defined symbol for your voltage source, you can say set voltage source graphic=alt voltage source after setting circuit ee IEC. –  Jake Oct 31 '11 at 22:19
    
Works fine :-D, thanks! –  Tobi Oct 31 '11 at 22:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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