% Copyright 2019 by Till Tantau and Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.


\section{Circuit Libraries}
\label{section-library-circuits}

\emph{Written and documented by Till Tantau, and Mark Wibrow. Inspired
by the work of Massimo Redaelli.}


\subsection{Introduction}

The circuit libraries can be used to draw different kinds of electrical or
logical circuits. There is not a single library for this, but a whole hierarchy
of libraries that work in concert. The main design goal was to create a balance
between ease-of-use and ease-of-extending, while creating high-quality
graphical representations of circuits.
%
\begin{codeexample}[setup code,hidden]
\tikzset{
  % from `shape` library
  shape example/.style= {color      = black!30,
                         draw,
                         fill       = yellow!30,
                         line width =  .5cm,
                         inner xsep = 2.5cm,
                         inner ysep = 0.5cm}
}
\end{codeexample}


\subsubsection{A First Example}

\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[circuit ee IEC,x=3cm,y=2cm,semithick,
                    every info/.style={font=\footnotesize},
                    small circuit symbols,
                    set resistor graphic=var resistor IEC graphic,
                    set diode graphic=var diode IEC graphic,
                    set make contact graphic= var make contact IEC graphic]
  % Let us start with some contacts:
  \foreach \contact/\y in {1/1,2/2,3/3.5,4/4.5,5/5.5}
  {
    \node [contact] (left contact \contact) at (0,\y) {};
    \node [contact] (right contact \contact) at (1,\y) {};
  }
  \draw (right contact 1) -- (right contact 2) -- (right contact 3)
     -- (right contact 4) -- (right contact 5);

  \draw (left contact 1) to [diode] ++(down:1)
                         to [voltage source={near start,
                                             direction info={volt=3}},
                             resistor={near end,ohm=3}] ++(right:1)
                         to (right contact 1);
  \draw (left contact 1) to [resistor={ohm=4}] (right contact 1);
  \draw (left contact 1) to [resistor={ohm=3}] (left contact 2);
  \draw (left contact 2) to [voltage source={near start,
                                             direction info={<-,volt=8}},
                             resistor={ohm=2,near end}] (right contact 2);
  \draw (left contact 2) to [resistor={near start,ohm=1},
                             make contact={near end,info'={[red]$S_1$}}]
                         (left contact 3);
  \draw (left contact 3) to [current direction'={near start,info=$\iota$},
                             resistor={near end,info={$R=4\Omega$}}]
                         (right contact 3);
  \draw (left contact 4) to [voltage source={near start,
                                             direction info={<-,volt=8}},
                             resistor={ohm=2,near end}] (right contact 4);
  \draw (left contact 3) to [resistor={ohm=1}] (left contact 4);
  \draw (left contact 4) to [resistor={ohm=3}] (left contact 5);
  \draw (left contact 5) to [resistor={ohm=4}] (right contact 5);
  \draw (left contact 5) to [diode] ++(up:1)
                         to [voltage source={near start,
                                             direction info={volt=3}},
                             resistor={near end,ohm=3}] ++(right:1)
                         to (right contact 5);
\end{tikzpicture}
\end{codeexample}

An important feature of the |circuits| library is that the appearance of a
circuit can be configured in general ways and that the labels are placed
automatically by default. Here is the graphic once more, generated from
\emph{exactly the same source code}, with only the options of the
|{tikzpicture}| environment replaced by
|[rotate=-90,circuit ee IEC,x=3.25cm,y=2.25cm]|:
%
\begin{tikzpicture}[rotate=-90,circuit ee IEC,x=3cm,y=2.25cm]
  % Let us start with some contacts:
  \foreach \contact/\y in {1/1,2/2,3/3.5,4/4.5,5/5.5}
  {
    \node [contact] (left contact \contact) at (0,\y) {};
    \node [contact] (right contact \contact) at (1,\y) {};
  }
  \draw (right contact 1) -- (right contact 2) -- (right contact 3)
     -- (right contact 4) -- (right contact 5);

  \draw (left contact 1) to [diode] ++(down:1)
                      to [voltage source={near start,direction info={volt=3}},
                          resistor={near end,ohm=3}] ++(right:1)
                      to (right contact 1);
  \draw (left contact 1) to [resistor={ohm=4}] (right contact 1);
  \draw (left contact 1) to [resistor={ohm=3}] (left contact 2);
  \draw (left contact 2) to [voltage source={near start,
                                          direction info={<-,volt=8}},
                          resistor={ohm=2,near end}] (right contact 2);
  \draw (left contact 2) to [resistor={near start,ohm=1},
                          make contact={near end,info'={[red]$S_1$}}] (left contact 3);
  \draw (left contact 3) to [current direction'={near start,info=$\iota$},
                          resistor={near end,info={$R=4\Omega$}}]
                            (right contact 3);
  \draw (left contact 4) to [voltage source={near start,
                                          direction info={<-,volt=8}},
                          resistor={ohm=2,near end}] (right contact 4);
  \draw (left contact 3) to [resistor={ohm=1}] (left contact 4);
  \draw (left contact 4) to [resistor={ohm=3}] (left contact 5);
  \draw (left contact 5) to [resistor={ohm=4}] (right contact 5);
  \draw (left contact 5) to [diode] ++(up:1)
                      to [voltage source={near start,direction info={volt=3}},
                          resistor={near end,ohm=3}] ++(right:1)
                      to (right contact 5);
\end{tikzpicture}


\subsubsection{Symbols}

A circuit typically consists of numerous electronic elements like logical gates
or resistors or diodes that are connected by wires. In \pgfname/\tikzname, we
use nodes for the electronic elements and normal lines for the wires.
\tikzname\ offers a large number of different ways of positioning and
connecting nodes in general, all of which can be used here. Additionally, the
|circuits| library defines an additional useful |to|-path that is particularly
useful for elements like a resistor on a line.

There are many different names that are used to refer to electrical
``elements'', so a bit of terminology standardization is useful: We will call
such elements \emph{symbols}. A \emph{symbol shape} is a \pgfname\ shape
declared using the |\pgfdeclareshape| command. A \emph{symbol node} is a node
whose shape is a symbol shape.


\subsubsection{Symbol Graphics}

Symbols can be created by |\node[shape=some symbol shape]|. However, in order
to represent some symbols correctly, just using standard \pgfname\ shapes is
not sufficient. For instance, most symbols have a visually appealing ``default
size'', but the size of a symbol shape depends only on the current values of
parameters like |minimum height| or |inner xsep|.

For these reasons, the circuit libraries introduce the concept of a
\emph{symbol graphic}. This is a style that causes a |\node| to not only have
the correct shape, but also the correct size and the correct path usage. More
generally, this style may set up things in any way so that the ``symbol looks
correct''. When you write, for instance, |\node[diode]|, then the style called
|diode graphic| is used, which in turn is set to something like
|shape=diode IEC,draw,minimum height=...|.

Here is an overview of the different kinds of circuit libraries:
%
\begin{itemize}
    \item The \tikzname-library |circuits| defines general keys for creating
        circuits. Mostly, these keys are useful for defining more specialized
        libraries.

        You normally do not use this library directly since it does not define
        any symbol graphics.
    \item The \tikzname-library |circuits.logic| defines keys for creating
        logical gates like and-gates or xor-gates. However, this library also
        does not actually define any symbol graphics; this is done by two
        sublibraries:
        %
        \begin{itemize}
            \item The library |circuits.logic.US| defines symbol graphics that
                cause the logical gates to be rendered in the ``US-style''. It
                includes all of the above libraries and you can use this
                library directly.
            \item The library |circuits.logic.IEC| also defines symbol graphics
                for logical gates, but it uses rectangular gates rather that
                the round US-gates. This library can coexist peacefully with
                the above library, you can change which symbol graphics are
                used ``on the fly''.
        \end{itemize}
    \item The \tikzname-library |circuits.ee| defines keys for symbols from
        electrical engineering like resistors or capacitors. Again,
        sublibraries define the actual symbol graphics.
        %
        \begin{itemize}
            \item The library |circuits.ee.IEC| defines symbol shapes that
                follow the IEC norm.
        \end{itemize}
    \item The \pgfname-libraries |shapes.gates.*| define (circuit) symbol
        shapes. However, you normally do not use these shapes directly, rather
        you use a style that uses an appropriate symbol graphic, which in turn
        uses one of these shapes.
\end{itemize}

Let us have a look at a simple example. Suppose we wish to create a logical
circuit. Then we first have to decide which symbol graphics we would like to
use. Suppose we wish to use the US-style, then we would include the library
|circuits.logic.US|. If you wish to use IEC-style symbols, use
|circuits.logic.IEC|. If you cannot decide, include both:
%
\begin{codeexample}[code only]
\usetikzlibrary{circuits.logic.US,circuits.logic.IEC}
\end{codeexample}
%
To create a picture that contains a US-style circuit you can now use the option
|circuit logic US|. This will set up keys like |and gate| to create use an
appropriate symbol graphic for rendering an |and gate|. Using the
|circuit logic IEC| instead will set up |and gate| to use another symbol
graphic.
%
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.US}}]
\begin{tikzpicture}[circuit logic US]
  \matrix[column sep=7mm]
  {
    \node (i0) {0}; &                            & \\
                    & \node [and gate] (a1) {};  & \\
    \node (i1) {0}; &                            & \node [or gate] (o) {};\\
                    & \node [nand gate] (a2) {}; & \\
    \node (i2) {1}; &                            & \\
  };
  \draw (i0.east) -- ++(right:3mm) |- (a1.input 1);
  \draw (i1.east) -- ++(right:3mm) |- (a1.input 2);
  \draw (i1.east) -- ++(right:3mm) |- (a2.input 1);
  \draw (i2.east) -- ++(right:3mm) |- (a2.input 2);
  \draw (a1.output) -- ++(right:3mm) |- (o.input 1);
  \draw (a2.output) -- ++(right:3mm) |- (o.input 2);
  \draw (o.output) -- ++(right:3mm);
\end{tikzpicture}
\end{codeexample}

\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.IEC}}]
\begin{tikzpicture}[circuit logic IEC]
  \matrix[column sep=7mm]
  {
    \node (i0) {0}; &                            & \\
                    & \node [and gate] (a1) {};  & \\
    \node (i1) {0}; &                            & \node [or gate] (o) {};\\
                    & \node [nand gate] (a2) {}; & \\
    \node (i2) {1}; &                            & \\
  };
  \draw (i0.east) -- ++(right:3mm) |- (a1.input 1);
  \draw (i1.east) -- ++(right:3mm) |- (a1.input 2);
  \draw (i1.east) -- ++(right:3mm) |- (a2.input 1);
  \draw (i2.east) -- ++(right:3mm) |- (a2.input 2);
  \draw (a1.output) -- ++(right:3mm) |- (o.input 1);
  \draw (a2.output) -- ++(right:3mm) |- (o.input 2);
  \draw (o.output) -- ++(right:3mm);
\end{tikzpicture}
\end{codeexample}


\subsubsection{Annotations}

An \emph{annotation} is a little extra drawing that can be added to a symbol.
For instance, when you add two little parallel arrows pointing away from some
electrical element, this usually means that the element is light emitting.

Instead of having one symbol for ``diode'' and another for ``light emitting
diode'', there is just one |diode| symbol, but you can add the |light emitting|
annotation to it. This is done by passing the annotation as a parameter to the
symbol as in the following example:
%
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC]
  \draw (0,0) to [diode={light emitting}] (3,0)
              to [resistor={adjustable}]  (3,2);
\end{codeexample}


\subsection{The Base Circuit Library}

\begin{tikzlibrary}{circuits}
    This library is a base library that is included by other circuit libraries.
    You do not include it directly, but you will typically use some of the
    general keys, described below.
\end{tikzlibrary}

\begin{key}{/tikz/circuits}
  This key should be passed as an option to a picture or a scope that contains
  a circuit. It will do some internal setups. This key is normally called by
  more specialized keys like |circuit ee IEC|.
\end{key}


\subsubsection{Symbol Size}

\begin{key}{/tikz/circuit symbol unit=\meta{dimension} (initially 7pt)}
    This dimension is a ``unit'' for the size of symbols. The libraries
    generally define the sizes of symbols relative to this dimension. For
    instance, the longer side of an inductor is, by default, in the IEC library
    equal to five times this \meta{dimension}. When you change this
    \meta{dimension}, the size of all symbols will automatically change
    accordingly.

    Note, that it is still possible to overwrite the size of any particular
    symbol. These settings apply only to the default sizes.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[circuit ee IEC]
  \draw (0,1) to [resistor] (3.5,1);
  \draw[circuit symbol unit=14pt]
        (0,0) to [resistor] (3.5,0);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{stylekey}{/tikz/huge circuit symbols}
    This style sets the default circuit symbol unit to |10pt|.
\end{stylekey}
%
\begin{stylekey}{/tikz/large circuit symbols}
    This style sets the default circuit symbol unit to |8pt|.
\end{stylekey}
%
\begin{stylekey}{/tikz/medium circuit symbols}
    This style sets the default circuit symbol unit to |7pt|.
\end{stylekey}
%
\begin{stylekey}{/tikz/small circuit symbols}
    This style sets the default circuit symbol unit to |6pt|.
\end{stylekey}
%
\begin{stylekey}{/tikz/tiny circuit symbols}
    This style sets the default circuit symbol unit to |5pt|.
\end{stylekey}

\begin{key}{/tikz/circuit symbol size=|width| \meta{width} |height| \meta{height}}
    This key sets |minimum height| to \meta{height} times the current value of
    the circuit symbol unit and the |minimum width| to \meta{width} times this
    value. Thus, this option can be used with a node command to set the size of
    the node as a multiple of the circuit symbol unit.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[circuit ee IEC]
  \draw (0,1) to [resistor] (2,1) to[inductor] (4,1);

  \begin{scope}
    [every resistor/.style={circuit symbol size=width 3 height 1}]
    \draw (0,0) to [resistor] (2,0) to[inductor] (4,0);
  \end{scope}
\end{tikzpicture}
\end{codeexample}
    %
\end{key}


\subsubsection{Declaring New Symbols}

\begin{key}{/tikz/circuit declare symbol=\meta{name}}
    This key is used to declare a symbol. It does not cause this symbol to be
    shown nor does it set a graphic to be used for the symbol, it simply
    ``prepares'' several keys that can later be used to draw a symbol and to
    configure it.

    In detail, the first key that is defined is just called \meta{name}. This
    key should be given as an option to a |node| or on a |to| path, as
    explained below. The key will take options, which can be used to influence
    the way the symbol graphic is rendered.

    Let us have a look at an example. Suppose we want to define a symbol called
    |foo|, which just looks like a simple rectangle. We could then say
    %
\begin{codeexample}[code only]
\tikzset{circuit declare symbol=foo}
\end{codeexample}
    %
    The symbol could now be used like this:
    %
\begin{codeexample}[code only]
  \node [foo]       at (1,1) {};
  \node [foo={red}] at (2,1) {};
\end{codeexample}

    However, in the above example we would not actually see anything since we
    have not yet set up the graphic to be used by |foo|. For this, we must use
    a key called |set foo graphic| or, generally, |set| \meta{name} |graphic|.
    This key gets graphic options as parameter that will be set when a symbol
    |foo| should be shown:
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits}}]
\begin{tikzpicture}
  [circuit declare symbol=foo,
   set foo graphic={draw,shape=rectangle,minimum size=5mm}]

  \node [foo]       at (1,1) {};
  \node [foo={red}] at (2,1) {};
\end{tikzpicture}
\end{codeexample}

    In detail, when you use the key \meta{name}=\meta{options} with a node, the
    following happens:
    %
    \begin{enumerate}
        \item The |inner sep| is set to |0.5pt|.
        \item The following style is executed:
            %
            \begin{stylekey}{/tikz/every circuit symbol}
                Use this style to set up things in general.
            \end{stylekey}
        \item The graphic options that have been set using |set| \meta{name}
            |graphic| are set.
        \item The style |every |\meta{name} is executed. You can use it to
            configure the symbol further.
        \item The \meta{options} are executed.
  \end{enumerate}

    The key \meta{name} will have a different effect when it is used on a |to|
    path command inside a |circuit| environment (the |circuit| environment sets
    up |to| paths in such a way that the use of a key declared using
    |circuit declare symbol| is automatically detected). When \meta{name} is
    used on a |to| path, the above actions also happen (setting the inner
    separation, using the symbol graphic, and so on), but they are passed to
    the key |circuit handle symbol|, which is explained next.
\end{key}

\begin{key}{/tikz/circuit handle symbol=\meta{options}}
    This key is mostly used internally. Its purpose is to render a symbol. The
    effect of this key differs, depending on whether it is used as the optional
    argument of a |to| path command or elsewhere.

    If the key is not used as an argument of a |to| path command, the
    \meta{options} are simply executed.

    The more interesting case happens when the key is given on a |to| path
    command. In this case, several things happen:
    %
    \begin{enumerate}
        \item The |to| path is locally changed and set to an internal path
            (which you should not try to change) that consists mostly of a
            single straight line.
        \item The \meta{options} are tentatively executed with filtering
            switched on. Everything is filtered out, except for the key |pos|
            and also the styles |at start|, |very near start|, |near start|,
            |midway|, |near end|, |very near end|, and |at end|. If none of
            them is found, |midway| is used.
        \item The filtered option is used to determine a position for the
            symbol on the path. At the given position (with |pos=0|
            representing the start and |pos=1| representing the end), a node
            will be added to the path (in a manner to be described presently).
        \item This node gets \meta{options} as its option list.
        \item The node is added by virtue of a special |markings| decoration.
            This means that a |mark| command is executed that causes the node
            to be placed as a mark on the path.
        \item The marking decoration will automatically subdivide the path and
            cause a line to be drawn from the start of the path to the node's
            border (at the position that lies on a line from the node's center
            to the start of the path) and then from the node's border (at a
            position on the other side of the node) to the end of the path.
        \item The marking decoration will also take care of the case that
            multiple marks are present on a path, in this case the lines from
            and to the borders of the nodes are only between consecutive nodes.
        \item The marking decoration will also rotate the coordinate system in
            such a way that the $x$-axis points along the path. Thus, if you
            use the |transform shape| option, the node will ``point along'' the
            path.
        \item In case a node is at |pos=0| or at |pos=1| some special code will
            suppress the superfluous lines to the start or end of the path.
    \end{enumerate}

    The net effect of all of the above is that a node will be placed ``on the
    path'' and the path will have a ``gap'' just large enough to encompass the
    node. Another effect is that you can use this key multiple times on a path
    to add several node to a path, provided they do not overlap.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits}}]
\begin{tikzpicture}[circuit]
  \draw (0,0) to [circuit handle symbol={draw,shape=rectangle,near start},
                  circuit handle symbol={draw,shape=circle,near end}] (3,2);
  \end{tikzpicture}
\end{codeexample}
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits}}]
\begin{tikzpicture}[transform shape,circuit]
  \draw (0,0) to [circuit handle symbol={draw,shape=rectangle,at start},
                  circuit handle symbol={draw,shape=circle,near end}] (3,2);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}


\subsubsection{Pointing Symbols in the Right Direction}

Unlike normal nodes, which generally should not be rotated since this will make
their text hard to read, symbols often need to be rotated. There are two ways
of achieving such rotations:
%
\begin{enumerate}
    \item When you place a symbol on a |to| path, the graphic symbol is
        automatically rotated such that it ``points along the path''. Here is
        an examples that shows how the inductor shape (which looks, unrotated,
        like this: \tikz[circuit ee IEC]\node[inductor]{};) is automatically
        rotated around:
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC]
  \draw (3,0) to[inductor] (1,0) to[inductor] (0,2);
\end{codeexample}
        %
    \item Many shapes cannot be placed ``on'' a path in this way, namely
        whenever there are more than two possible inputs. Also, you may wish to
        place the nodes first, possibly using a matrix, and connect them
        afterwards. In this case, you can simply add rotations like |rotate=90|
        to the shapes to rotate them. The following four keys make this
        slightly more convenient:
        %
        \begin{key}{/tikz/point up}
            This is the same as |rotate=90|.
            %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC] \node [diode,point up] {};
\end{codeexample}
        \end{key}
        %
        \begin{key}{/tikz/point down}
            This is the same as |rotate=-90|.
            %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC] \node [diode,point down] {};
\end{codeexample}
        \end{key}
        %
        \begin{key}{/tikz/point left}
            This is the same as |rotate=-180|.
            %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC] \node [diode,point left] {};
\end{codeexample}
        \end{key}
        %
        \begin{key}{/tikz/point right}
            This key has no effect.
            %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC] \node [diode,point right] {};
\end{codeexample}
        \end{key}
\end{enumerate}


\subsubsection{Info Labels}

Info labels are used to add text to a circuit symbol. Unlike normal nodes like
a rectangle, circuit symbols typically do not have text ``on'' them, but the
text is placed next to them (like the text ``$3\,\Omega$'' next to a resistor).

\tikzname\ already provides the |label| option for this purpose. The |info|
option is built on top of this option, but it comes in some predefined variants
that are especially useful in conjunction with circuits.

\begin{key}{/tikz/info=\opt{|[|\meta{options}|]|\meta{angle}|:|}\meta{text}}
    This key has nearly the same effect as the |label| key, only the following
    style is used additionally automatically:
    %
    \begin{stylekey}{/tikz/every info}
        Set this style to configure the styling of info labels. Since this
        key is \emph{not} used with normal labels, it provides an easy way
        of changing the way info labels look without changing other
        labels.
    \end{stylekey}
    %
    The \meta{options} and \meta{angle} are passed directly to the |label|
    command.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[circuit ee IEC,every info/.style=red]
  \node [resistor,info=$3\Omega$] {};
\end{tikzpicture}
\end{codeexample}

    You will find a detailed discussion of the |label| option on
    page~\pageref{label-option}.

    Hint: To place some text \emph{on} the main node, use |center| as the
    \meta{angle}:
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[circuit ee IEC,every info/.style=red]
  \node [resistor,info=center:$3\Omega$] {};
  \node [resistor,point up,info=center:$R_1$] at (2,0) {};
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/info'=\opt{|[|\meta{options}|]|\meta{angle}|:|}\meta{text}}
    This key works exactly like the |info| key, only in case the \meta{angle}
    is missing, it defaults to |below| instead of the current value of
    |label position|, which is usually |above|. This means that when you use
    |info|, you get a label above the node, while when you use the |info'| key
    you get a label below the node. In case the node has been rotated, the
    positions of the info nodes are rotated accordingly.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[circuit ee IEC,every info/.style=red]
  \draw (0,0) to[resistor={info={$3\Omega$},info'={$R_1$}}] (3,0)
              to[resistor={info={$4\Omega$},info'={$R_2$}}] (3,2);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/info sloped=\opt{|[|\meta{options}|]|\meta{angle}|:|}\meta{text}}
    This key works like |info|, only the |transform shape| option is set when
    the label is drawn, causing it to follow the sloping of the main node.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[circuit ee IEC,every info/.style=red]
  \draw (0,0) to[resistor={info sloped={$3\Omega$}}] (3,0)
              to[resistor={info sloped={$4\Omega$}}] (3,2);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/info' sloped=}
    This is a combination of |info'| and |info sloped|.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[circuit ee IEC,every info/.style=red]
  \draw (0,0) to[resistor={info' sloped={$3\Omega$}}] (3,0)
              to[resistor={info' sloped={$4\Omega$}}] (3,2);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}

\begin{key}{/tikz/circuit declare unit=\marg{name}\marg{unit}}
    This key is used to declare keys that make it easy to attach physical units
    to nodes. The idea is that instead of |info=$3\Omega$| you can write
    |ohm=3| or instead of |info'=$5\mathrm{S}$| you can write |siemens'=5|.

    In detail, four keys are defined, namely |/tikz/|\meta{name},
    |/tikz/|\meta{name}|'|, |/tikz/|\meta{name} |sloped|, and
    |/tikz/|\meta{name}|'| |sloped|. The arguments of all of these keys are of
    the form \opt{|[|\meta{options}|]|\meta{angle}|:|}\meta{value} and it is
    passed (slightly modified) to the corresponding key |info|, |info'|, |info|
    |sloped|, or |info'| |sloped|. The ``slight modification'' is the
    following: The text that is passed to the, say, |info| key is not
    \meta{value}, but rather |$\mathrm{|\meta{value}\meta{unit}|}$|

    This means that after you said |circuit declare unit={ohm}{\Omega}|, then
    |ohm=5k| will have the same effect as
    |info={[every ohm]$\mathrm{5k\Omega}$}|. Here, |every ohm| is a style that
    allows you to configure the appearance of this unit. Since the |info| key
    is used internally, by changing the |every info| style, you can change the
    appearance of all units infos.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[circuit ee IEC,circuit declare unit={my ohm}{O}]
  \draw (0,0) to[resistor={my ohm' sloped=3}] (3,2);
\end{tikzpicture}
\end{codeexample}
    %
\end{key}


\subsubsection{Declaring and Using Annotations}

Annotations are quite similar to info labels. The main difference is that they
generally cause something to be drawn by default rather than some text to be
added (although an annotation might also add some text).

Annotations can be declared using the following key:

\begin{key}{/tikz/circuit declare annotation=\marg{name}\marg{distance}\marg{path}}
    This key is used to declare an annotation named \meta{name}. Once declared,
    it can be used as an argument of a symbol and will add the drawing in
    \meta{path} to the symbol. In detail, the following happens:


    \textbf{The Main Keys.}
    Two keys called \meta{name} and \meta{name}|'| are defined. The second
    causes the annotation to be ``mirrored and placed on the other side'' of
    the symbol. Both of these keys may also take further keys as parameter like
    |info| keys. Whenever the \meta{name} key is used, a local scope is opened
    and in this scope the following things are done:
    %
    \begin{enumerate}
        \item The style |every| \meta{name} is executed.
        \item The following style is executed and then |arrows=->|:
            %
            \begin{stylekey}{/tikz/annotation arrow}
                This style should set the |>| key to some desirable arrow tip.
            \end{stylekey}
        \item The coordinate system is shifted such that the origin is at the
            north anchor of the symbol. (For the \meta{name}|'| key the
            coordinate system is flipped and shifted such that the origin is at
            the south anchor of the symbol.)
        \item The |label distance| is locally set to \meta{distance}.
        \item The parameter options given to the \meta{name} key are executed.
        \item The \meta{path} is executed.
    \end{enumerate}


    \textbf{Usage.}
    What all of the above amounts to is best explained by an example. Suppose
    we wish to create an annotation that looks like a little circular arrow
    (like \tikz \draw [->] (0,0) arc (-270:80:1ex);). We could then say:
    %
\begin{codeexample}[code only]
\tikzset{circuit declare annotation=
  {circular annotation}
  {9pt}
  {(0pt,8pt) arc (-270:80:3.5pt)}
}
\end{codeexample}
    %
    We can then use it like this:
    %
\tikzset{circuit declare annotation=
  {circular annotation}
  {8pt}
  {(0pt,8pt) arc (-270:80:3.5pt)}
}
\begin{codeexample}[
    preamble={\usetikzlibrary{circuits.ee.IEC}}
    pre={\tikzset{circuit declare annotation=
  {circular annotation}
  {8pt}
  {(0pt,8pt) arc (-270:80:3.5pt)}
}}]
\tikz[circuit ee IEC]
  \draw (0,0) to [resistor={circular annotation}]   (3,0);
\end{codeexample}
    %
    Well, not very impressive since we do not see anything. This is due to the
    fact that the \meta{path} becomes part of a path that contains the symbol
    node an nothing else. This path is not drawn or filled, so we do not see
    anything. What we must do is to use an |edge| path operation:
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikzset{circuit declare annotation={circular annotation}{9pt}
  {(0pt,8pt) edge[to path={arc(-270:80:3.5pt)}] ()}
}
\tikz[circuit ee IEC]
  \draw (0,0) to [resistor={circular annotation}]   (3,0)
              to [capacitor={circular annotation'}] (3,2);
\end{codeexample}
    %
    The \meta{distance} is important for the correct placement of additional
    |info| labels. When an annotation is present, the info labels may need to
    be moved further away from the symbol, but not always. For this reason, an
    annotation defines an additional \meta{distance} that is applied to all
    info labels given as parameters to the annotation. Here is an example, that
    shows the difference:
    %
\tikzset{circuit declare annotation={circular annotation}{9pt}
  {(0pt,8pt) edge[to path={arc (-270:80:3.5pt)}] ()}
}
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}
    pre={\tikzset{circuit declare annotation=
  {circular annotation}
  {8pt}
  {(0pt,8pt) arc (-270:80:3.5pt)}
}}]
\tikz[circuit ee IEC]
  \draw (0,0) to [resistor={circular annotation,ohm=5}]   (2,0)
              to [resistor={circular annotation={ohm=5}}] (4,0);
\end{codeexample}
    %
\end{key}


\subsubsection{Theming Symbols}
\label{section-theming-symbols}

For each symbol, a certain graphical representation is chosen to actually show
the symbol. You can modify this graphical representation in several ways:
%
\begin{itemize}
    \item You can select a different library and use a different |circuit ...|
        key. This will change all graphics used for the symbols.
    \item You can generally change the size of graphic symbols by setting
        |circuit size unit| to a different value or using a key like
        |small circuit symbols|.
    \item You can add options to the graphics used by symbols either globally
        by setting the |every circuit| |symbol| style or locally by setting the
        |every| \meta{name} style, where \meta{name} is the name of a symbol.
        For instance, in the following picture the symbols are ridiculously
        thick and resistors are red.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}
  [circuit ee IEC,
   every circuit symbol/.style={ultra thick},
   every resistor/.style={red}]

  \draw (0,0) to [inductor] ++(right:3) to [resistor] ++(up:2);
\end{tikzpicture}
\end{codeexample}
        %
    \item You can selectively change the graphic used for a symbol by saying
        |set resistor graphic=|.
    \item You can change one or more of the following styles:
        %
        \begin{stylekey}{/tikz/circuit symbol open (initially draw)}
            This style is used with symbols that consist of lines that surround
            some area. For instance, the IEC version of a resistor is an open
            symbol.
            %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC,
       circuit symbol open/.style={thick,draw,fill=yellow}]
  \draw (0,0) to [inductor] ++(right:3) to [resistor] ++(up:2);
\end{codeexample}
        \end{stylekey}
        %
        \begin{stylekey}{/tikz/circuit symbol filled (initially {draw,fill=black})}
            This style is used with symbols that are completely filled. For
            instance, the variant IEC version of an inductor is a filled, black
            rectangle.
        \end{stylekey}
        %
        \begin{stylekey}{/tikz/circuit symbol lines (initially draw)}
            This style is used with symbols that consist only of lines that do
            not surround anything. Examples are a capacitor.
            %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC,
       circuit symbol lines/.style={thick,draw=red}]
  \draw (0,0) to [capacitor] ++(right:3) to [resistor] ++(up:2);
\end{codeexample}
        \end{stylekey}
        %
        \begin{stylekey}{/tikz/circuit symbol wires (initially draw)}
            This style is used for symbols that consist only of ``wires''. The
            difference to the previous style is that a symbol consisting of
            wires will look strange when the lines are thicker than the lines
            of normal wires, while for symbols consisting of lines (but not
            wires) it may look nice to make them thicker. An example is the
            |make contact| symbol.

            Compare
            %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC,circuit symbol lines/.style={draw,very thick}]
  \draw (0,0) to [capacitor={near start},
                  make contact={near end}] (3,0);
\end{codeexample}
            %
            to
            %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC,circuit symbol wires/.style={draw,very thick}]
  \draw (0,0) to [capacitor={near start},
                  make contact={near end}] (3,0);
\end{codeexample}
        \end{stylekey}
\end{itemize}

All circuit environments like |circuit logic IEC| mainly use options like
|set and gate graphic=...| to set up the graphics used for a certain symbol. It
turns out that graphic hidden in the ``|...|'' part is also always available as
a separate style, whose name contains the library's initials. For instance, the
|circuit logic IEC| option actually contains the following command:
%
\begin{codeexample}[code only]
  set and gate graphic = and gate IEC graphic,
\end{codeexample}
%
The |and gate IEC graphic| style, in turn, is defined as follows:
%
\begin{codeexample}[code only]
\tikzset{and gate IEC graphic/.style=
  {
    circuit symbol open,
    circuit symbol size=width 2.5 height 4,
    shape=and gate IEC,
    inner sep=.5ex
  }
}
\end{codeexample}

Normally, you do not need to worry about this, since you will not need to
access a style like |and gate IEC graphic| directly; you will only use the
|and gate| key. However, sometimes libraries define \emph{variants} of a
graphic; for instance, there are two variants for the resistor graphic in the
IEC library. In this case you can set the graphic for the resistor to this
variant (or back to the original) by saying |set resistor graphic| yourself:
%
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[circuit ee IEC]
  % Standard resistor
  \draw (0,2) to [resistor] (3,2);

  % Var resistor
  \begin{scope}[set resistor graphic=var resistor IEC graphic]
    \draw (0,1) to [resistor] (3,1);

    % Back to original
    \draw [set resistor graphic=resistor IEC graphic]
      (0,0) to [resistor] (3,0);
  \end{scope}
\end{tikzpicture}
\end{codeexample}


\subsection{Logical Circuits}

\subsubsection{Overview}

A \emph{logical circuit} is a circuit that contains what we call \emph{logical
gates} like an |and gate| or an |xor gate|. The logical libraries are intended
to make it easy to draw such circuits.

In the following, we first have a look at the different libraries that can be
used in principle and how the symbols look like. Then we have a more detailed
look at how the symbols are used. Finally, we discuss the implementation
details.

There are different ways of depicting logical gates, which is why there are
different (sub-)libraries for drawing them. They provide the necessary
graphical representations of the symbols declared in the following library:

\begin{tikzlibrary}{circuits.logic}
    This library declares the logical gate symbols, but does not provide the
    symbol graphics. The library also defines the following key which, however,
    is also only used indirectly, namely by other libraries:
    %
    \begin{key}{/tikz/circuit logic}
        This style calls the keys |circuit| (which internally calls
        |every circuit|, then it defines the |inputs| key and it calls the
        |every circuit logic| key.
        %
        \begin{key}{/tikz/inputs=\meta{inputs}}
            This key is defined only inside the scope of a |circuit logic|.
            There, it has the same effect as |logic gate inputs|, described on
            page~\pageref{logic-gate-inputs}.
        \end{key}
        %
        \begin{stylekey}{/tikz/every circuit logic}
            Use this key to configure the appearance of logical circuits.
        \end{stylekey}
    \end{key}
\end{tikzlibrary}

Since the |circuits.logic| library does not define any actual graphics, you need
to use one of the following libraries, instead:

\begin{tikzlibrary}{circuits.logic.IEC}
    This library provides graphics based on gates recommended by the
    International Electrotechnical Commission.  When you include this library,
    you can use the following key to set up a scope that contains a logical
    circuit where the gates are shown in this style.

    \begin{key}{/tikz/circuit logic IEC}
        This key calls |circuit logic| and installs the IEC-like graphics for
        the logical symbols like |and gate|.

        As explained in Section~\ref{section-theming-symbols}, for each graphic
        symbol of the library there is also a style that stores this particular
        appearance. These keys are called |and gate IEC graphic|,
        |or gate IEC graphic|, and so on.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.IEC}}]
\begin{tikzpicture}[circuit logic IEC,
                    every circuit symbol/.style={
                      logic gate IEC symbol color=black,
                      fill=blue!20,draw=blue,very thick}]
  \matrix[column sep=7mm]
  {
    \node (i0) {0}; &                            & \\
                    & \node [and gate] (a1) {};  & \\
    \node (i1) {0}; &                            & \node [or gate] (o) {};\\
                    & \node [nand gate] (a2) {}; & \\
    \node (i2) {1}; &                            & \\
  };
  \draw (i0.east) -- ++(right:3mm) |- (a1.input 1);
  \draw (i1.east) -- ++(right:3mm) |- (a1.input 2);
  \draw (i1.east) -- ++(right:3mm) |- (a2.input 1);
  \draw (i2.east) -- ++(right:3mm) |- (a2.input 2);
  \draw (a1.output) -- ++(right:3mm) |- (o.input 1);
  \draw (a2.output) -- ++(right:3mm) |- (o.input 2);
  \draw (o.output) -- ++(right:3mm);
\end{tikzpicture}
\end{codeexample}
    \end{key}
\end{tikzlibrary}

\begin{tikzlibrary}{circuits.logic.US}
    This library provides graphics showing ``American'' logic gates. It defines
    the following key:

    \begin{key}{/tikz/circuit logic US}
        This style calls |circuit logic| and installs US-like graphics for the
        logical symbols like |and gate|. For instance, it says
        %
\begin{codeexample}[code only]
set and gate graphic = and gate US graphic
\end{codeexample}

        Here is an example:
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.CDH}}]
\begin{tikzpicture}[circuit logic CDH,
                    tiny circuit symbols,
                    every circuit symbol/.style={
                      fill=white,draw}]
  \matrix[column sep=7mm]
  {
    \node (i0) {0}; &                            & \\
                    & \node [and gate] (a1) {};  & \\
    \node (i1) {0}; &                            & \node [or gate] (o) {};\\
                    & \node [nand gate] (a2) {}; & \\
    \node (i2) {1}; &                            & \\
  };
  \draw (i0.east) -- ++(right:3mm) |- (a1.input 1);
  \draw (i1.east) -- ++(right:3mm) |- (a1.input 2);
  \draw (i1.east) -- ++(right:3mm) |- (a2.input 1);
  \draw (i2.east) -- ++(right:3mm) |- (a2.input 2);
  \draw (a1.output) -- ++(right:3mm) |- (o.input 1);
  \draw (a2.output) -- ++(right:3mm) |- (o.input 2);
  \draw (o.output) -- ++(right:3mm);
\end{tikzpicture}
\end{codeexample}
    \end{key}
\end{tikzlibrary}

\begin{tikzlibrary}{circuits.logic.CDH}
    This library provides graphics based on the logic symbols used in A. Croft,
    R. Davidson, and M. Hargreaves (1992),  \emph{Engineering Mathematics},
    Addison-Wesley, 82--95. They are identical to the US-style symbols, except
    for the and- and nand-gates.

    \begin{key}{/tikz/circuit logic CDH}
        This key calls |circuit logic US| and installs the two special and- and
        nand-gates, that is, it uses |set and gate graphic| with
        |and gate CDH graphic| and likewise for nand-gates.
    \end{key}
\end{tikzlibrary}

Inside |circuit logic XYZ| scopes, you can now use the keys shown in
Section~\ref{section-logic-symbols}. We have a more detailed look at one of
them, all the other work the same way:

\begin{key}{/tikz/and gate}
    This key should be passed to a |node| command. It will cause the node to
    ``look like'' an |and gate|, where the exact appearance of the gate is
    dictated by the which circuit environment is used. To further configure the
    appearance of the |and gate|, see Section~\ref{section-theming-symbols}.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.IEC}}]
\tikz [circuit logic IEC] \node [and gate] {$A$};
\end{codeexample}
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.US}}]
\tikz [circuit logic US]
{
  \node [and gate,point down] {$A$};
  \node [and gate,point down,info=center:$A$] at (1,0) {};
}
\end{codeexample}

    \medskip\textbf{Inputs.}
    Multiple inputs can be specified for a logic gate (provided they support
    multiple inputs: a not gate -- also known as an inverter -- does not).
    However, there is an upper limit for the number of inputs which has been
    set to 1024, which should be \emph{way} more than would ever be needed.

    The following key is used to configure the inputs. It is available only
    inside a |circuit logic| environment.

    \begin{key}{/tikz/inputs=\meta{input list} (initially \char`\{normal,normal\char`\})}
        If a gate has $n$ inputs, the \meta{input list} should consists of $n$
        letters, each being |i| for ``inverted'' or |n| for ``normal''.
        Inverted gates will be indicated by a little circle. In any case the
        anchors for the inputs will be set up appropriately, numbered from top
        to bottom |input 1|, |input 2|, \ldots and so on. If the gate only
        supports one input the anchor is simply called |input| with no
        numerical index.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.IEC}}]
\begin{tikzpicture}[circuit logic IEC]
  \node[and gate,inputs={inini}] (A) {};
  \foreach \a in {1,...,5}
    \draw (A.input \a -| -1,0) -- (A.input \a);
  \draw (A.output) -- ++(right:5mm);
\end{tikzpicture}
\end{codeexample}
    \end{key}

    (This key is just a shorthand for |logic gate inputs|, described in detail
    on page~\pageref{logic-gate-inputs}. There you will also find descriptions
    of how to configure the size of the inverted circles and the way the symbol
    size increases when there are too many inputs.)

    \textbf{Output.}
    Every logic gate has one anchor called |output|.
\end{key}


\subsubsection{Symbols: The Gates}
\label{section-logic-symbols}

The following table shows which symbols are declared by the main
|circuits.logic| library and their appearance in the different sublibraries.
\medskip

\def\gateexamples#1{%
  \texttt{#1}
  \indexkey{#1} &
  \tikz[baseline,circuit logic IEC] \node[#1,label=] {}; &
  \tikz[baseline,circuit logic US]  \node[#1] {}; &
  \tikz[baseline,circuit logic CDH] \node[#1] {};
}
\begin{tabular}{lccc}
  \emph{Key} & \emph{Appearance inside} & \emph{Appearance inside} & \emph{Appearance inside} \\
      & |circuit logic IEC| & |circuit logic US| & |circuit logic CDH| \\
  \gateexamples{/tikz/and gate}\\
  \gateexamples{/tikz/nand gate}\\
  \gateexamples{/tikz/or gate}\\
  \gateexamples{/tikz/nor gate}\\
  \gateexamples{/tikz/xor gate}\\
  \gateexamples{/tikz/xnor gate}\\
  \gateexamples{/tikz/not gate}\\
  \gateexamples{/tikz/buffer gate}
\end{tabular}


\subsubsection{Implementation: The Logic Gates Shape Library}

The previous sections described the \tikzname\ interface for creating logical
circuits. In this section we take a closer look at the underlying \pgfname\
libraries.

Just as there are several \tikzname\ circuit libraries, there are two
underlying \pgfname\ shape libraries, one for creating US-style gates and one
for IEC-style gates. These libraries define \emph{shapes} only. It is the job
of the circuit libraries to ``theme'' them so that they ``look nice''. However,
in principle, you can also use these shapes directly.

Let us begin with the base library that defines the handling of inputs.

\begin{pgflibrary}{shapes.gates.logic}
    This library defines common keys used by all logical gate shapes.

    \begin{key}{/pgf/logic gate inputs=\meta{input list} (initially \char`\{normal,normal\char`\})}
    \label{logic-gate-inputs}%
        Specify the inputs for the logic gate. The keyword |inverted| indicates
        an inverted input which will mean \pgfname{} will draw a circle
        attached to the main shape of the logic gate. Any keyword that is not
        |inverted| will be treated as a ``normal'' or ``non-inverted'' input
        (however, for readability, you may wish to use |normal| or
        |non-inverted|), and \pgfname{} will not draw the circle. In both cases
        the anchors for the inputs will be set up appropriately, numbered from
        top to bottom |input 1|, |input 2|, \ldots and so on. If the gate only
        supports one input the anchor is simply called |input| with no
        numerical index.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.IEC}}]
\begin{tikzpicture}[minimum height=0.75cm]
  \node[and gate IEC, draw, logic gate inputs={inverted, normal, inverted}]
    (A) {};
  \foreach \a in {1,...,3}
    \draw (A.input \a -| -1,0) -- (A.input \a);
  \draw (A.output) -- ([xshift=0.5cm]A.output);
\end{tikzpicture}
\end{codeexample}

        For multiple inputs it may be somewhat unwieldy to specify a long list,
        thus, the following ``shorthand'' is permitted (this is an extension of
        ideas due to Jürgen Werber and Christoph Bartoschek): Using |i| for
        inverted and |n| for normal inputs, \meta{input list} can be specified
        \emph{without the commas}. So, for example, |ini| is equivalent to
        |inverted, normal, inverted|.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.US}}]
\begin{tikzpicture}[minimum height=0.75cm]
  \node[or gate US, draw,logic gate inputs=inini] (A) {};
  \foreach \a in {1,...,5}
    \draw (A.input \a -| -1,0) -- (A.input \a);
  \draw (A.output) -- ([xshift=0.5cm]A.output);
\end{tikzpicture}
\end{codeexample}
    \end{key}

    The height of the gate may be increased to accommodate the number of
    inputs. In fact, it depends on three variables: $n$, the number of inputs,
    $r$, the radius of the circle used to indicate an inverted input and $s$,
    the distance between the centers of the inputs. The default height is then
    calculated according to the expression $(n+1)\times\max(2r,s)$. This then
    may be increased to accommodate the node contents or any minimum size
    specifications.

    The radius of the inverted input circle and the distance between the
    centers of the inputs can be customized using the following keys:

    \begin{key}{/pgf/logic gate inverted radius=\meta{length} (initially 2pt)}
        Set the radius of the circle that is used to indicate inverted inputs.
        This is also the radius of the circle used for the inverted output of
        the |nand|, |nor|, |xnor| and |not| gates.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.CDH}}]
\begin{tikzpicture}[minimum height=0.75cm]
  \tikzset{every node/.style={shape=nand gate CDH, draw, logic gate inputs=ii}}
  \node[logic gate inverted radius=2pt] {A};
  \node[logic gate inverted radius=4pt] at (0,-1) {B};
\end{tikzpicture}
\end{codeexample}
        %
    \end{key}

    \begin{key}{/pgf/logic gate input sep=\meta{length} (initially .125cm)}
        Set the distance between the \emph{centers} of the inputs to the logic
        gate.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.IEC}}]
\begin{tikzpicture}[minimum size=0.75cm]
  \draw [help lines] grid (3,2);
  \tikzset{every node/.style={shape=and gate IEC, draw, logic gate inputs=ini}}
  \node[logic gate input sep=0.33333cm] at (1,1)(A) {A};
  \node[logic gate input sep=0.5cm]     at (3,1) (B) {B};
  \foreach \a in {1,...,3}
    \draw (A.input \a -| 0,0) -- (A.input \a)
          (B.input \a -| 2,0) -- (B.input \a);
\end{tikzpicture}
\end{codeexample}
        %
    \end{key}

    \pgfname{} will increase the size of the logic gate to accommodate the
    number of inputs, and the size of the inverted radius and the separation
    between the inputs. However with all shapes in this library, any increase
    in size (including any minimum size requirements) will be applied so that
    the default aspect ratio is unaltered. This means that changing the height
    will change the width and vice versa.
\end{pgflibrary}


\subsubsection{Implementation: The US-Style Logic Gates Shape Library}

\begin{pgflibrary}{shapes.gates.logic.US}
    This library provides ``American'' logic gate shapes whose names are
    suffixed with the identifier |US|. Additionally, alternative |and| and
    |nand| gates are provided which are based on the logic symbols used in A.
    Croft, R. Davidson, and M. Hargreaves (1992), \emph{Engineering
    Mathematics}, Addison-Wesley, 82--95. These two shapes are suffixed with
    |CDH|.

    The ``compass point'' anchors apply to the main part of the shape and do
    not include any inverted inputs or outputs. This library provides an
    additional feature to facilitate the relative positioning of logic gates:

    \begin{key}{/pgf/logic gate anchors use bounding box=\meta{boolean} (initially false)}
        When set to |true| this key will ensure that the compass point anchors
        use the bounding rectangle of the main shape, which, ignore any
        inverted inputs or outputs, but includes any |outer sep|. This
        \emph{only} affects the compass point anchors and is not set on a shape
        by shape basis: whether the bounding box is used is determined by value
        of this key when the anchor is accessed.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.US}}]
\begin{tikzpicture}[minimum height=1.5cm]
  \node[xnor gate US, draw, gray!50,line width=2pt] (A) {};
  \foreach \x/\y/\z in {false/blue/1pt, true/red/2pt}
    \foreach \a in {north, south, east, west, north east,
      south east, north west, south west}
      \draw[logic gate anchors use bounding box=\x, color=\y]
        (A.\a) circle(\z);
\end{tikzpicture}
\end{codeexample}
        %
    \end{key}

    The library defines a number of shapes. For each shape the allowed number
    of inputs is also shown:
    %
    \begin{itemize}
        \item |and gate US|, two or more inputs
        \item |and gate CDH|, two or more inputs
        \item |nand gate US|, two or more inputs
        \item |nand gate CDH|, two or more inputs
        \item |or gate US|, two or more inputs
        \item |nor gate US|, two or more Inputs
        \item |xor gate US|, two inputs
        \item |xnor gate US|, two inputs
        \item |not gate US|, one input
        \item |buffer gate US|, one input
    \end{itemize}

    In the following, we only have a detailed look at the anchors defined by
    one of them. We choose the |nand gate US| because it shows all the
    ``interesting'' anchors.

    \begin{shape}{nand gate US}
        This shape is a nand gate, which supports two or more inputs. If less
        than two inputs are specified an error will result. The anchors for
        this gate with two non-inverted inputs (using the normal compass point
        anchors) are shown below. Anchor |30| is an example of a border anchor.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.US}}]
\Huge
\begin{tikzpicture}
  \node[name=s,shape=nand gate US,shape example, inner sep=0cm,
  logic gate inputs={in},
  logic gate inverted radius=.5cm] {Nand Gate\vrule width1pt height2cm};
  \foreach \anchor/\placement in
    {center/above, text/above, 30/above right,
     mid/right, mid east/left, mid west/above,
     base/below, base east/below, base west/left,
     north/above, south/below, east/above, west/above,
     north east/above, south east/below, south west/below, north west/above,
     output/right, input 1/above, input 2/below}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{codeexample}

        (For the definition of the |shape example| style, see
        Section~\ref{section-libs-shapes}.)
    \end{shape}
\end{pgflibrary}


\subsubsection{Implementation: The IEC-Style Logic Gates Shape Library}

\begin{pgflibrary}{shapes.gates.logic.IEC}
    This library provides rectangular logic gate shapes. These shapes are
    suffixed with |IEC| as they are based on gates recommended by the
    International Electrotechnical Commission.

    By default each gate is drawn with a symbol, $\char`\&$ for |and| and
    |nand| gates, $\geq1$ for |or| and |nor| gates, $1$ for |not| and |buffer|
    gates, and $=1$ for |xor| and |xnor| gates. These symbols are drawn
    automatically (internally they are drawn using the ``foreground'' path),
    and are not strictly speaking part of the node contents. However, the gate
    is enlarged to make sure the symbols are within the border of the node. It
    is possible to change the symbols and their position within the node using
    the following keys:

    \begin{key}{/pgf/and gate IEC symbol=\meta{text} (initially \char`\\char\char`\`\char`\\\char`\&)}
        Set the symbol for the |and gate|. Note that if the node is filled,
        this color will be used for the symbol, making it invisible, so it will
        be necessary set \meta{text} to something like |\color{black}\char`\&|.
        Alternatively, the |logic gate IEC symbol color| key can be used to set
        the color of all symbols simultaneously.

        In \tikzname, when the |use IEC style logic gates| key has been used,
        this key can be replaced by |and gate symbol|.
    \end{key}

    \begin{key}{/pgf/nand gate IEC symbol=\meta{text} (initially \char`\\char\char`\`\char`\\\char`\&)}
        Set the symbol for the |nand gate|. In \tikzname, when the
        |use IEC style logic gates| key has been used, this key can be replaced
        by |nand gate symbol|.
    \end{key}

    \begin{key}{/pgf/or gate IEC symbol=\meta{text} (initially \char`\$\char`\\geq1\char`\$)}
        Set the symbol for the |or gate|. In \tikzname, when the
        |use IEC style logic gates| key has been used, this key can be replaced
        by |or gate symbol|.
    \end{key}

    \begin{key}{/pgf/nor gate IEC symbol=\meta{text} (initially \char`\$\char`\\geq1\char`\$)}
        Set the symbol for the |nor gate|. In \tikzname, when the
        |use IEC style logic gates| key has been used, this key can be replaced
        by |nor gate symbol|.
    \end{key}

    \begin{key}{/pgf/xor gate IEC symbol=\meta{text} (initially \char`\{\char`\$=1\char`\$\char`\})}
        Set the symbol for the |xor gate|. Note the necessity for braces, as
        the symbol contains |=|. In \tikzname, when the
        |use IEC style logic gates| key has been used, this key can be replaced
        by |xor gate symbol|.
    \end{key}

    \begin{key}{/pgf/xnor gate IEC symbol=\meta{text} (initially  \char`\{\char`\$=1\char`\$\char`\})}
        Set the symbol for the |xnor gate|. In \tikzname, when the
        |use IEC style logic gates| key has been used, this key can be replaced
        by |xnor gate symbol|.
    \end{key}

    \begin{key}{/pgf/not gate IEC symbol=\meta{text} (initially 1)}
        Set the symbol for the |not gate|. In \tikzname, when the
        |use IEC style logic gates| key has been used, this key can be replaced
        by |not gate symbol|.
    \end{key}

    \begin{key}{/pgf/buffer gate IEC symbol=\meta{text} (initially 1)}
        Set the symbol for the |buffer gate|. In \tikzname, when the
        |use IEC style logic gates| key has been used, this key can be replaced
        by |buffer gate symbol|.
    \end{key}

    \begin{key}{/pgf/logic gate IEC symbol align=\meta{align} (initially top)}
        Set the alignment of the logic gate symbol (in \tikzname, when the
        |use IEC style logic gates| key has been used, |IEC| can be omitted).
        The specification in \meta{align} is a comma separated list from |top|,
        |bottom|, |left| or |right|. The distance between the border of the
        node and the outer edge of the symbol is determined by the values of
        the |inner xsep| and |inner ysep|.
        %
\begin{codeexample}[preamble={\usetikzlibrary{shapes.gates.logic.IEC}}]
\begin{tikzpicture}[minimum size=1cm, use IEC style logic gates]
    \tikzset{every node/.style={nor gate, draw}}
  \node (A) at (0,1.5) {};
  \node [logic gate symbol align={bottom, right}] (B) at (0,0) {};
  \foreach \g in {A, B}{
    \foreach \i in {1,2}
      \draw ([xshift=-0.5cm]\g.input \i) -- (\g.input \i);
    \draw (\g.output) -- ([xshift=0.5cm]\g.output);
  }
\end{tikzpicture}
\end{codeexample}
        %
    \end{key}

    \begin{key}{/pgf/logic gate IEC symbol color=\meta{color}}
        This key sets the color for all symbols simultaneously. This color can
        be overridden on a case by case basis by specifying a color when
        setting the symbol text.
    \end{key}

    The library defines the following shapes:
    %
    \begin{itemize}
        \item |and gate IEC|, two or more inputs
        \item |nand gate IEC|, two or more inputs
        \item |or gate IEC|, two or more inputs
        \item |nor gate IEC|, two or more inputs
        \item |xor gate IEC|, two inputs
        \item |xnor gate IEC|, two inputs
        \item |not gate IEC|, one input
        \item |buffer gate IEC|, one input
    \end{itemize}

    Again, we only have a look at the nand-gate in more detail:

    \begin{shape}{nand gate IEC}
        This shape is a nand gate. It supports two or more inputs. If less than
        two inputs are specified an error will result. The anchors for this
        gate with two inverted inputs are shown below. Anchor |30| is an
        example of a border anchor.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.logic.IEC}}]
\Huge
\begin{tikzpicture}
  \node[name=s,shape=nand gate IEC ,shape example, inner xsep=1cm, inner ysep=1cm,
    minimum height=6cm, nand gate IEC symbol=\color{black!30}\char`\&,
    logic gate inputs={in},
    logic gate inverted radius=0.65cm]
  {Nand Gate\vrule width1pt height2cm};
  \foreach \anchor/\placement in
    {center/above, text/above, 30/above right,
     mid/right, mid east/left, mid west/above,
     base/below, base east/below, base west/left,
     north/above, south/below, east/above, west/above,
     north east/above, south east/below, south west/below, north west/above,
     output/right, input 1/above, input 2/below}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{codeexample}
    \end{shape}
\end{pgflibrary}


\subsection{Electrical Engineering Circuits}

\subsubsection{Overview}

An \emph{electrical engineering circuit} contains symbols like resistors or
capacitors or voltage sources and annotations like the two arrows pointing
toward an element whose behaviour is light dependent. The electrical
engineering libraries, abbreviated ee-libraries, provide such symbols and
annotations.

Just as for logical gates, there are different ways of drawing ee-symbols.
Currently, there is one main library for drawing circuits, which uses the
graphics from the International Electrotechnical Commission, but you can add
your own libs. This is why, just as for logical gates, there is a base library
and more specific libraries.

\begin{tikzlibrary}{circuits.ee}
    This library declares the ee symbols, but (mostly) does not provide the
    symbol graphics, which is left to the sublibraries. Just like the logical
    gates library, a key is defined that is normally only used internally:
    %
    \begin{key}{/tikz/circuit ee}
        This style calls the keys |circuit| (which internally calls
        |every circuit| and the following style:
        %
        \begin{stylekey}{/tikz/every circuit ee}
            Use this key to configure the appearance of logical circuits.
        \end{stylekey}
    \end{key}

    The library also declares some standard annotations and units.
\end{tikzlibrary}

As for logical circuits, to draw a circuit the first step is to include a
library containing the symbols graphics. Currently, you have to include
|circuits.ee.IEC|.

\begin{tikzlibrary}{circuits.ee.IEC}
    When this library is loaded, you can use the following style:
    %
    \begin{key}{/tikz/circuit ee IEC}
        This style calls |circuit ee| and installs the IEC-like graphics for
        the logical symbols like |resistor|.
    \end{key}
\end{tikzlibrary}

Inside the |circuit ee IEC| scope, you can now use the keys for symbols, units,
and annotations listed in the later sections. We have a more detailed look at
one of each of them, all the others work the same way.

Let us start with an example of a symbol: the resistor symbol. The other
predefined symbols are listed in Section~\ref{section-circuits-ee-symbols} and
later sections.

\begin{key}{/tikz/resistor=\opt{\meta{options}}}
    This key should be used with a |node| path command or with the |to| path
    command.

    \medskip\textbf{Using the Key with Normal Nodes.}
    When used with a node, it will cause this node to ``look like'' a resistor
    (by default, in the IEC library, this is just a simple rectangle).
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC]
  \node [resistor] {};
\end{codeexample}

    Unlike normal nodes, a resistor node generally should not take any text (as
    in |node [resistor] {foo}|). Instead, the labeling of resistors should be
    done using the |label|, |info| and |ohm| options.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC]
  \node [resistor,ohm=5] {};
\end{codeexample}

    The \meta{options} make no real sense when the |resistor| option is used
    with a normal node, you can just as well given them to the |node| itself.
    Thus, the following has the same effect as the above example:
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC]
  \node [resistor={ohm=5}] {};
\end{codeexample}

    In a circuit, you will often wish to rotate elements. For this, the options
    |point up|, |point down|, |point left| or |point right| may be especially
    useful. They are just shorthands for appropriate rotations like
    |rotate=90|.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC] {
  \node (R1) [resistor,point up,ohm=5] at (3,1) {};
  \node (R2) [resistor,ohm=10k]        at (0,0) {};
  \draw (R2) -| (R1);
}
\end{codeexample}

    \medskip\textbf{Using the Key on a To Path.}
    When the |resistor| key is used on a |to| path inside a |circuit ee IEC|,
    the |circuit handle symbol| key is called internally. This has a whole
    bunch of effects:
    %
    \begin{enumerate}
        \item The path currently being constructed is cut up to make place for
            a node.
        \item This node will be a |resistor node| that is rotated so that it
            points ``along'' the path (unless an option like |shift only| or an
            extra rotation is used to change this).
        \item The \meta{options} passed to the |resistor| key are passed on to
            the node.
        \item The \meta{options} are pre-parsed to identify a |pos| key or a
            key like |at start| or |midway|. These keys are used to determine
            where on the |to| path the node will lie.
    \end{enumerate}

    Since the \meta{options} of the |resistor| key are passed on to the
    resistor node on the path, you can use it to add labels to the node. Here
    is a simple example:
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC]
  \draw (0,0) to [resistor=red]        (3,0)
              to [resistor={ohm=2\mu}] (3,2);
\end{codeexample}

    You can add multiple labels to a resistor and you can have multiple
    resistors (or other elements) on a single path.

    \medskip\textbf{Inputs, Outputs, and Anchors.}
    Like the logical gates, all ee-symbols have an |input| and an |output|
    anchor. Special-purpose-nodes may have even more anchors of this type.
    Furthermore, the ee-symbols-nodes also have four standard compass direction
    anchors.

    \medskip\textbf{Changing the Appearance.}
    To configure the appearance of all |resistor|s, see
    Section~\ref{section-theming-symbols}. You can use the \meta{options} to
    locally change the appearance of a single resistor.
\end{key}

Let us now have a look at an example of a unit: the Ohm unit. The other
predefined units are listed in Section~\ref{section-circuits-units}.

\begin{key}{/tikz/ohm=\meta{value}}
    This key is used to add an |info| label to a node with a special text:
    |$\mathrm{|\meta{value}|\Omega}$|. In other words, the |ohm| key can only
    be used with the options of a node and, when used, it will cause the
    \meta{value} to be placed next to the node, followed by $\Omega$. Since the
    \meta{value} is typeset inside a |\mathrm| command, when you write |ohm=5k|
    you get $\mathrm{5k\Omega}$, |ohm=5p| yields $\mathrm{5p\Omega}$, and
    |ohm=5.6\cdot 10^{2}\mu| yields $\mathrm{5.6\cdot 10^{2}\mu\Omega}$.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC] \draw (0,0) to [resistor={ohm=5M}] (0,2);
\end{codeexample}

    Instead of |ohm| you can also use |ohm'|, which places the label on the
    other side.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC] \draw (0,0) to [resistor={ohm'=5M}] (0,2);
\end{codeexample}

    Finally, there are also keys |ohm sloped| and |ohm' sloped| for having the
    info label rotate together with the main node.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC]
  \draw (0,0) to [resistor={ohm sloped=5M}] (0,2)
        (2,0) to [resistor={ohm' sloped=6f}]  (2,2);
\end{codeexample}

    You can configure the appearance of an Ohm info label using the key
    |every ohm|.
\end{key}

Finally, let us have a look at an annotation: the |light emitting| annotation.
The other predefined units are listed in
Section~\ref{section-circuits-annotations}.

\begin{key}{/tikz/light emitting=\opt{\meta{options}}}
    Like a unit, an annotation should be given as an additional option to a
    node. It causes some drawings (in this case, two parallel lines) to be
    placed next to the node.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC] \draw (0,0) to [diode=light emitting] (2,0);
\end{codeexample}

    The \meta{options} can be used for three different things:
    %
    \begin{enumerate}
        \item You can use keys like |red| to change the appearance of this
            annotation, locally.
        \item You can use keys like |<-|  or |-latex| to change the direction
            and kinds of arrows used in the annotation.
        \item You can use info labels like |ohm=5| or |info=foo| inside the
            \meta{options}. These info labels will be added to the main node
            (not to the annotation itself), but the label distance will have
            been changed to accommodate for the space taken up by the
            annotation.
            %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz [circuit ee IEC]
{
  \draw (0,2) to [diode={light emitting,info=not good}] (2,2);
  \draw (0,0) to [diode={light emitting={info=better},
                         info'=also good}]  (2,0);
}
\end{codeexample}
    \end{enumerate}

    In addition to |light emitting| there is also a key called
    |light emitting'|, which simply places the annotation on the other side of
    the node.

    You can configure the appearance of annotations in three ways:
    %
    \begin{itemize}
        \item You can set the |every circuit annotation| style.
        \item You can set the |every light emitting| style.
        \item You can set the following key:
            %
            \begin{stylekey}{/tikz/annotation arrow}
                This style should set the default |>| arrow to some nice value.
            \end{stylekey}
    \end{itemize}
\end{key}

\def\eelineexample#1#2{%
  \texttt{#1}\indexkey{#1}
   &
  \tikz[baseline=-.5ex,circuit ee IEC] \draw (0,0) to [#1] (3,0);
  &
  \relax\def\temp{#2}
  \ifx\temp\empty\else
  {\tikz[baseline=-.5ex,circuit ee IEC,set #2 graphic=var #2 IEC graphic]
    \draw (0,0) to [#2] (3,0);}
  \fi \\[.2em]
}
\def\eeendexample#1#2{%
  \texttt{#1}\indexkey{#1}
   &
  \tikz[baseline=-.5ex,circuit ee IEC] \draw (0,0) to [#1={at end}] (1.5,0)(3,0);
  &
  \relax\def\temp{#2}
  \ifx\temp\empty\else
  {\tikz[baseline=-.5ex,circuit ee IEC,set #2 graphic=var #2 IEC graphic]
    \draw (0,0) to [#2={at end}] (1.5,0)(3,0);}
  \fi \\[.2em]
}
\def\unitexample#1{%
  \texttt{#1}\indexkey{#1}
  &
  \tikz [baseline,inner sep=0pt] \node[#1=1] {};\\
}
\def\annotationexample#1{%
  \texttt{#1}\indexkey{#1}
  &
  \tikz[baseline=-.5ex,circuit ee IEC]
    \draw (0,0) to [resistor={#1}] (2,0)
                to [diode   ={#1'}] (4,0);\\
}
\def\empty{}


\subsubsection{Symbols: Indicating Current Directions}
\label{section-ee-symbols}
\label{section-circuits-ee-symbols}

There \todosp{why two labels? The first doesn't seem to be used.} are two
symbols for indicating current directions. These symbols are defined directly
inside |circuit ee|.
\medskip

\noindent
\begin{tabular}{p{5cm}ll}
    \emph{Key} & \emph{Appearance}\\[.25em]
    \eelineexample{/tikz/current direction}{}
    \eelineexample{/tikz/current direction'}{}
\end{tabular}

\medskip
The examples have been produced by (in essence)
|\draw (0,0) to[|\meta{symbol name}|] (3,0);|.


\subsubsection{Symbols: Basic Elements}

The following table show basic symbols as they are depicted inside the
|circuit ee IEC| environment. To install one of alternate graphics, you have to
say |set| \meta{symbol name} |graphic=var| \meta{symbol name} |IEC graphic|.
\medskip

\noindent
\begin{tabular}{p{5cm}ll}
    \emph{Key} & \emph{Appearance}  & \emph{Alternate appearance} \\[.25em]
    \eelineexample{/tikz/resistor}{resistor}
    \eelineexample{/tikz/inductor}{inductor}
    \eelineexample{/tikz/capacitor}{}
    \eelineexample{/tikz/battery}{}
    \eelineexample{/tikz/bulb}{}
    \eelineexample{/tikz/current source}{}
    \eelineexample{/tikz/voltage source}{}
    \eelineexample{/tikz/ac source}{}
    \eelineexample{/tikz/dc source}{}
    \eeendexample{/tikz/ground}{}
\end{tabular}


\subsubsection{Symbols: Diodes}

The following table shows diodes as they are depicted inside the
|circuit ee IEC| environment.
\medskip

\noindent
\begin{tabular}{p{5cm}ll}
  \emph{Key} & \emph{Appearance}  & \emph{Alternate appearance} \\[.25em]
  \eelineexample{/tikz/diode}{diode}
  \eelineexample{/tikz/Zener diode}{Zener diode}
  \eelineexample{/tikz/Schottky diode}{Schottky diode}
  \eelineexample{/tikz/tunnel diode}{tunnel diode}
  \eelineexample{/tikz/backward diode}{backward diode}
  \eelineexample{/tikz/breakdown diode}{breakdown diode}
\end{tabular}


\subsubsection{Symbols: Contacts}

The following table shows contacts as they are depicted inside the
|circuit ee IEC| environment.
\medskip

\noindent
\begin{tabular}{p{5cm}ll}
    \emph{Key} & \emph{Appearance}  & \emph{Alternate appearance} \\[.25em]
    \eelineexample{/tikz/contact}{}
    \eelineexample{/tikz/make contact}{make contact}
    \eelineexample{/tikz/break contact}{}
\end{tabular}


\subsubsection{Symbols: Measurement devices}

The following table shows measurement devices as they are depicted inside the
|circuit ee IEC| environment.
\medskip

\noindent
\begin{tabular}{p{5cm}ll}
    \emph{Key} & \emph{Appearance} \\[.25em]
    \eelineexample{/tikz/amperemeter}{}
    \eelineexample{/tikz/voltmeter}{}
    \eelineexample{/tikz/ohmmeter}{}
\end{tabular}


\subsubsection{Units}
\label{section-circuits-units}

The |circuits.ee| library predefines the following unit keys:
\medskip

\noindent
\begin{tabular}{p{5cm}c}
    \emph{Key} & \emph{Appearance of $1$ unit} \\[.25em]
    \unitexample{/tikz/ampere}
    \unitexample{/tikz/volt}
    \unitexample{/tikz/ohm}
    \unitexample{/tikz/siemens}
    \unitexample{/tikz/henry}
    \unitexample{/tikz/farad}
    \unitexample{/tikz/coulomb}
    \unitexample{/tikz/voltampere}
    \unitexample{/tikz/watt}
    \unitexample{/tikz/hertz}
\end{tabular}


\subsubsection{Annotations}
\label{section-circuits-annotations}

The |circuits.ee.IEC| library defines the following annotations:
\medskip

\noindent
\begin{tabular}{p{5cm}ll}
    \emph{Key} & \emph{Appearance} \\[.25em]
    \annotationexample{/tikz/light emitting}
    \annotationexample{/tikz/light dependent}
    \annotationexample{/tikz/direction info}
    \annotationexample{/tikz/adjustable}
\end{tabular}
\medskip

The lines have been produced using, in essence,
%
\begin{codeexample}[code only]
\draw (0,0)  to [resistor=light emitting] (2,0)  to [diode=light emitting'] (4,0);
\end{codeexample}
%
and similarly for the other annotations.


\subsubsection{Implementation: The EE-Symbols Shape Library}

The \tikzname\ libraries depend on two shape libraries, which are included
automatically. Usually, you will not need to use these shapes directly.

\begin{pgflibrary}{shapes.gates.ee}
    This library defines basic shapes that can be used by all ee-circuit
    libraries. Currently, it defines the following shapes:
    %
    \begin{itemize}
        \item |rectangle ee|
        \item |circle ee|
        \item |direction ee|
    \end{itemize}
    %
    Additionally, the library defines the following arrow tip: The
    |direction ee| arrow tip is basically the same as a |triangle 45| arrow tip
    with rounded joins.

    \begin{tabular}{ll}
        \symarrow{direction ee}
    \end{tabular}

    However, unlike normal arrow tips, its size does \emph{not} depend on the
    current line width. Rather, it depends on the value of its arrow options,
    which should be set to the desired size. Thus, you should say something
    like |\pgfsetarrowoptions{direction ee}{5pt}| to set the size of the arrow.
\end{pgflibrary}

\begin{shape}{rectangle ee}
    This shape is completely identical to a normal |rectangle|, only there are
    two additional anchors: The |input| anchor is an alias for the |west|
    anchor, while the |output| anchor is an alias for the |east| anchor.
\end{shape}

\begin{shape}{circle ee}
    Like the |rectangle ee| shape, only for circles.
\end{shape}

\begin{shape}{direction ee}
    This shape is rather special. It is intended to be used to ``turn an arrow
    tip into a shape''. First, you should set the following key to the name of
    an arrow tip:
    %
    \begin{key}{/pgf/direction ee arrow=\meta{right arrow tip name}}
        The value of this key will be used for the arrow tip depicted in an
        |direction ee| shape.
    \end{key}
    %
    When a node of shape |direction ee| is created, several things happen:
    %
    \begin{enumerate}
        \item The size of the shape is computed according to the following
            rules: The width of the shape is set up so that the left border of
            the shape is at the left end of the arrow tip and the right border
            is at the right end of the arrow tip. These left and right ``ends''
            of the arrow are the tip end and the back end specified by the
            arrow itself (see Section~\ref{section-arrow-terminology} for
            details). You usually need not worry about this width setting.

            By comparison, the height of the arrow is given by the current
            setting of |minimum height|. Thus, this key must have been set up
            correctly to reflect the ``real'' height of the arrow tip. The
            reason is that the height of an arrow is not specified when arrows
            are declared and is, thus, not available, here.

            Possibly, the height computation will change in the future to
            reflect the real height of the arrow, so you should generally set
            up the |minimum height| to be the same as the real height.
        \item A straight line from left to right inside the shape's boundaries
            is added to the background path.
        \item The arrow tip, pointing right, is drawn before the background
            path.
    \end{enumerate}
    %
    The anchors of this shape are just the compass anchors, which lie on a
    rectangle whose width and height are the above-computed height and width.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}
  \pgfsetarrowoptions{direction ee}{6cm}
  \node[name=s,shape=direction ee,shape example,minimum height=0.7654*6cm] {};
  \foreach \anchor/\placement in
    {center/above, 30/above right,
     north/above, south/below, east/left, west/right,
     north east/above, south east/below, south west/below, north west/above,
     input/left,output/right}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{codeexample}

\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}[direction ee arrow=angle 45]
  \node[name=s,shape=direction ee,shape example,minimum height=1.75cm] {};
  \foreach \anchor/\placement in {north/above, south/below,
                                  output/right, input/left}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{codeexample}
    %
\end{shape}


\subsubsection{Implementation: The IEC-Style EE-Symbols Shape Library}

\begin{pgflibrary}{shapes.gates.ee.IEC}
    This library defines shapes for depicting ee symbols according to the IEC
    recommendations. These shapes will typically be used in conjunction with
    the graphic mechanism detailed earlier, but you can also used them
    directly.
\end{pgflibrary}

\begin{shape}{generic circle IEC}
    This shape inherits from |circle ee|, which in turn is just a normal
    |circle| with additional |input| and |output| anchors at the left and right
    ends. However, additionally, this shape allows you to specify a path that
    should be added before the background path using the following key:
    %
    \begin{key}{/pgf/generic circle IEC/before background=\meta{code}}
        When a node of shape |generic circle IEC| is created, the current
        setting of this key is used as the ``before background path''. This
        means that after the circle's background has been
        drawn/filled/whatever, the \meta{code} is executed.

        When the \meta{code} is executed, the coordinate system will have been
        transformed in such a way that the point $(1\mathrm{pt},0\mathrm{pt})$
        lies at the right end of the circle and $(0\mathrm{pt},1\mathrm{pt})$
        lies at the top of the circle. (More precisely, these points will lie
        exactly on the middle of the radial line.)
    \end{key}
    %
    Here is an examples of how to use this shape:
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz \node [generic circle IEC,
             /pgf/generic circle IEC/before background={
               \pgfpathmoveto{\pgfpointorigin}
               \pgfpathlineto{\pgfpoint{1pt}{0pt}}
               \pgfpathlineto{\pgfpoint{0pt}{1pt}}
               \pgfpathlineto{\pgfpoint{-0.5pt}{-0.5pt}}
               \pgfusepathqstroke
             },
             draw] {Hello world};
\end{codeexample}
    %
\end{shape}

\begin{shape}{generic diode IEC}
    This shape is used to depict diodes. The main shape is taken up by a
    ``right pointing'' triangle. The anchors are positioned on the border of a
    rectangle around the diode, see the below example. The diode's size is
    based on the current settings of |minimum width| and |minimum height|.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}
  \node[name=s,shape=generic diode IEC,shape example,minimum size=6cm] {};
  \foreach \anchor/\placement in
    {center/above, 30/above right,
     north/above, south/below, east/left, west/right,
     north east/above, south east/below, south west/below, north west/above,
     input/left,output/right}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{codeexample}

    This shape, like the |generic circle IEC| shape, is generic in the sense
    that there is a special key that is used for the before background
    drawings:
    %
    \begin{key}{/pgf/generic diode IEC/before background=\meta{code}}
        Similarly to the |generic circle IEC| shape, when a node of shape
        |generic diode IEC| is created, the current setting of this key is used
        as the ``before background path''. When the \meta{code} is executed,
        the coordinate system will have been transformed in such a way that the
        origin is at the ``tip'' of the diode's triangle, the point
        $(0\mathrm{pt},1\mathrm{pt})$ is exactly half the diode's height above
        this origin, and the point $(1\mathrm{pt},0\mathrm{pt})$ is half the
        diode's height to the right of the origin.

        The idea is that you use this key to draw different kinds of diode
        endings.
        %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz \node [minimum size=1cm,generic diode IEC,
             /pgf/generic diode IEC/before background={
               \pgfpathmoveto{\pgfqpoint{-.5pt}{-1pt}}
               \pgfpathlineto{\pgfqpoint{.5pt}{-1pt}}
               \pgfpathmoveto{\pgfqpoint{0pt}{-1pt}}
               \pgfpathlineto{\pgfqpoint{0pt}{1pt}}
               \pgfpathmoveto{\pgfqpoint{-.5pt}{1pt}}
               \pgfpathlineto{\pgfqpoint{.5pt}{1pt}}
               \pgfusepathqstroke
             },
             draw] {};
\end{codeexample}
    \end{key}
\end{shape}

\begin{shape}{breakdown diode IEC}
    This shape is used to depict a bidirectional breakdown diode. The diode's
    size is based on the current settings of |minimum width| and
    |minimum height|.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}
  \node[name=s,shape=breakdown diode IEC,shape example,minimum width=6cm,minimum height=4cm] {};
  \foreach \anchor/\placement in
    {center/above, 30/above right,
     north/above, south/below, east/left, west/right,
     north east/above, south east/below, south west/below, north west/above,
     input/left,output/right}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{codeexample}
    %
\end{shape}

\begin{shape}{var resistor IEC}
    This shape is used to depict a variant version of a resistor. Its size is
    computed as for a rectangle (thus, its size depends things like the
    |minimum height|). Then, inside this rectangle, a background path is set up
    according to the following rule: Starting from the left end, zigzag
    segments are added to the path. Each segment consists of a line at a 45
    degree angle going up to the top of the rectangle, then going down to the
    bottom, then going up to mid height of the node. As many segments as
    possible are put inside as possible. The last segment is then connected to
    the output anchor via a straight line.

    All of this means that, in general, the shape should be much wider than
    high.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}
  \node[name=s,shape=var resistor IEC,shape example,minimum width=7cm,minimum height=1cm] {};
  \foreach \anchor/\placement in
    {center/above, 30/above right,
     north/above, south/below, east/left, west/right,
     north east/above, south east/below, south west/below, north west/above,
     input/left,output/right}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{codeexample}
    %
\end{shape}

\begin{shape}{inductor IEC}
    This shape is used to depict an inductor, using a bumpy line. Its size is
    computed as follows: Any text and |inner sep| are ignored (and should
    normally not be given). The |minimum height| plus (twice) the |outer ysep|
    specify the distance between the |north| and |south| anchors, similarly for
    the |minimum width| plus the |outer xsep| for the |east| and |west|. The
    bumpy line is drawn starting from the lower left corner to the lower right
    corner with bumps being half-circles whose height is exactly the
    |minimum height|. The |center| of the shape is just above the |south|
    anchor, at a distance of the |outer ysep|.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}
  \node[name=s,shape=inductor IEC,shape example,minimum width=7cm,minimum height=1cm] {};
  \foreach \anchor/\placement in
    {center/above, 30/above right,
     north/above, south/below, east/left, west/right,
     north east/above, south east/below, south west/below, north west/above,
     input/left,output/right}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{codeexample}
    %
    Just as for a |var resistor IEC|, as many bumps as possible are added and
    the last bump is connected to the output anchor via a straight line.
\end{shape}

\begin{shape}{capacitor IEC}
    This shape is based on a |rectangle ee|. However, instead of a rectangle as
    the background path, only the ``left and right lines'' that make up the
    rectangle are drawn.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}
  \node[name=s,shape=capacitor IEC,shape example,
        minimum width=2cm,minimum height=3cm,inner sep=0pt] {};
  \foreach \anchor/\placement in
    {center/above, 30/above right,
     north/above, south/below, east/left, west/right,
     north east/above, south east/below, south west/below, north west/above,
     input/left,output/right}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{codeexample}
    %
\end{shape}

\begin{shape}{battery IEC}
    This shape is similar to a |capacitor IEC|, however, the right line is only
    half the height of the left line.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz \node[shape=battery IEC,shape example,minimum size=2cm,
            inner sep=0pt] {};
\end{codeexample}
    %
\end{shape}

\begin{shape}{ground IEC}
    This shape is similar to a |batter IEC|, only three lines of different
    heights are drawn.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz \node[shape=ground IEC,shape example,minimum size=2cm,
            inner sep=0pt] {};
\end{codeexample}
    %
\end{shape}

\begin{shape}{make contact IEC}
    This shape consists of a line going from the lower left corner to the upper
    right corner. The size and anchors of this shape are computed in the same
    way as for an |inductor IEC|.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\begin{tikzpicture}
  \node[name=s,shape=make contact IEC,shape example,minimum width=3cm,minimum height=1cm] {};
  \foreach \anchor/\placement in
    {center/above, 30/above right,
     north/above, south/below, east/left, west/right,
     north east/above, south east/below, south west/below, north west/above,
     input/left,output/right}
     \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
       node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}
\end{codeexample}
    %
\end{shape}

\begin{shape}{var make contact IEC}
    This shape works like |make contact IEC|, only a little circle is added to
    the path at the lower left corner. The radius of this circle is one twelfth
    of the width of the node.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz \node[shape=var make contact IEC,shape example,
            minimum height=1cm,minimum width=3cm,inner sep=0pt] {};
\end{codeexample}
    %
\end{shape}

\begin{shape}{break contact IEC}
    This shape depicts a contact that can be broken. It works like
    |make contact IEC|.
    %
\begin{codeexample}[preamble={\usetikzlibrary{circuits.ee.IEC}}]
\tikz \node[shape=break contact IEC,shape example,
            minimum height=1cm,minimum width=3cm,inner sep=0pt] {};
\end{codeexample}
    %
\end{shape}
