% Copyright 2018-2024 by Romano Giannetti
% Copyright 2015-2024 by Stefan Lindner
% Copyright 2013-2024 by Stefan Erhardt
% Copyright 2007-2024 by Massimo Redaelli
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the files gpl-3.0_license.txt and lppl-1-3c_license.txt for more details.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Tripoles
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Logic Ports
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Settings for Logic ports%<<<1
%% beware that the third option is in IEEE ports in pgfcircmultipoles.tex

% switches for logic gates
%
\pgfkeys{/tikz/number inputs/.initial=0}
\pgfkeys{/tikz/number inputs/.default=0}
% by default, use the default font (and color, etc.)
\ctikzset{european ports font/.initial={}}
\newif\ifpgf@circuit@europeanlogicport
\ctikzset{logic ports/.is choice}
\ctikzset{logic ports/european/.code= {\pgf@circuit@europeanlogicporttrue
    \tikzset{and port/.style={shape=european and port}}%
    \tikzset{or port/.style={shape=european or port}}%
    \tikzset{xor port/.style={shape=european xor port}}%
    \tikzset{buffer port/.style={shape=european buffer port}}%
    \tikzset{not port/.style={shape=european not port}}%
    \tikzset{nand port/.style={shape=european nand port}}%
    \tikzset{nor port/.style={shape=european nor port}}%
    \tikzset{xnor port/.style={shape=european xnor port}}%
    \tikzset{blank port/.style={shape=european blank port}}%
    \tikzset{blank not port/.style={shape=european blank not port}}%
    % there is no Schmitt ports in european style (yet)
    \tikzset{schmitt port/.style={shape=schmitt}}%
    \tikzset{invschmitt port/.style={shape=invschmitt}}%
}}
\ctikzset{logic ports/american/.code= {\pgf@circuit@europeanlogicportfalse
    \tikzset{and port/.style={shape=american and port}}%
    \tikzset{or port/.style={shape=american or port}}%
    \tikzset{xor port/.style={shape=american xor port}}%
    \tikzset{buffer port/.style={shape=american buffer port}}%
    \tikzset{not port/.style={shape=american not port}}%
    \tikzset{nand port/.style={shape=american nand port}}%
    \tikzset{nor port/.style={shape=american nor port}}%
    \tikzset{xnor port/.style={shape=american xnor port}}%
    \tikzset{schmitt port/.style={shape=schmitt}}%
    \tikzset{invschmitt port/.style={shape=invschmitt}}%
    %%% there are no blank ports for american (no sense to have them)
    \tikzset{blank port/.style={shape=european blank port}}%
    \tikzset{blank not port/.style={shape=european blank not port}}%
}}

\ctikzset{logic ports origin/.is choice}
\ctikzset{logic ports origin/legacy/.code={
    \ctikzset{tripoles/american and port/origin/.initial=0.8}%
    \ctikzset{tripoles/american nand port/origin/.initial=0.8}%
    \ctikzset{tripoles/american nor port/origin/.initial=0.8}%
    \ctikzset{tripoles/american or port/origin/.initial=0.8}%
    \ctikzset{tripoles/american xor port/origin/.initial=0.8}%
    \ctikzset{tripoles/american xnor port/origin/.initial=0.8}%
    \ctikzset{tripoles/european and port/origin/.initial=0.8}%
    \ctikzset{tripoles/european nand port/origin/.initial=0.8}%
    \ctikzset{tripoles/european or port/origin/.initial=0.8}%
    \ctikzset{tripoles/european nor port/origin/.initial=0.8}%
    \ctikzset{tripoles/european xor port/origin/.initial=0.8}%
    \ctikzset{tripoles/european xnor port/origin/.initial=0.8}%
    \ctikzset{tripoles/european buffer port/origin/.initial=0.8}%
    \ctikzset{tripoles/european not port/origin/.initial=0.8}%
    \ctikzset{tripoles/european blank port/origin/.initial=0.8}%
    \ctikzset{tripoles/european blank not port/origin/.initial=0.8}%
    }%
}
\ctikzset{logic ports origin/center/.code={%
    \ctikzset{tripoles/american and port/origin/.initial=0}%
    \ctikzset{tripoles/american nand port/origin/.initial=0}%
    \ctikzset{tripoles/american nor port/origin/.initial=0}%
    \ctikzset{tripoles/american or port/origin/.initial=0}%
    \ctikzset{tripoles/american xor port/origin/.initial=0}%
    \ctikzset{tripoles/american xnor port/origin/.initial=0}%
    \ctikzset{tripoles/european and port/origin/.initial=0}%
    \ctikzset{tripoles/european nand port/origin/.initial=0}%
    \ctikzset{tripoles/european or port/origin/.initial=0}%
    \ctikzset{tripoles/european nor port/origin/.initial=0}%
    \ctikzset{tripoles/european xor port/origin/.initial=0}%
    \ctikzset{tripoles/european xnor port/origin/.initial=0}%
    \ctikzset{tripoles/european buffer port/origin/.initial=0}%
    \ctikzset{tripoles/european not port/origin/.initial=0}%
    \ctikzset{tripoles/european blank port/origin/.initial=0}%
    \ctikzset{tripoles/european blank not port/origin/.initial=0}%
    }%
}

\newif\ifpgfcirc@roundy@or@shapes\pgfcirc@roundy@or@shapesfalse
\ctikzset{american or shape/.is choice}
\ctikzset{american or shape/roundy/.code={\pgfcirc@roundy@or@shapestrue}}
\ctikzset{american or shape/pointy/.code={\pgfcirc@roundy@or@shapesfalse}}

\newif\ifpgfcirc@draw@input@leads\pgfcirc@draw@input@leadstrue
\ctikzset{logic ports draw input leads/.is choice}
\ctikzset{logic ports draw input leads/true/.code={\pgfcirc@draw@input@leadstrue}}
\ctikzset{logic ports draw input leads/false/.code={\pgfcirc@draw@input@leadsfalse}}
\tikzset{input leads/.code={\pgfcirc@draw@input@leadstrue}}
\tikzset{no input leads/.code={\pgfcirc@draw@input@leadsfalse}}

\newif\ifpgfcirc@draw@output@leads\pgfcirc@draw@output@leadstrue
\ctikzset{logic ports draw output leads/.is choice}
\ctikzset{logic ports draw output leads/true/.code={\pgfcirc@draw@output@leadstrue}}
\ctikzset{logic ports draw output leads/false/.code={\pgfcirc@draw@output@leadsfalse}}
\tikzset{output leads/.code={\pgfcirc@draw@output@leadstrue}}
\tikzset{no output leads/.code={\pgfcirc@draw@output@leadsfalse}}

\ctikzset{logic ports draw leads/.is choice}
\ctikzset{logic ports draw leads/true/.code={\pgfcirc@draw@output@leadstrue\pgfcirc@draw@input@leadstrue}}
\ctikzset{logic ports draw leads/false/.code={\pgfcirc@draw@output@leadsfalse\pgfcirc@draw@input@leadsfalse}}
\tikzset{all leads/.code={\pgfcirc@draw@output@leadstrue\pgfcirc@draw@input@leadstrue}}
\tikzset{no leads/.code={\pgfcirc@draw@output@leadsfalse\pgfcirc@draw@input@leadsfalse}}

% adding a different style of xnor port
% see https://github.com/circuitikz/circuitikz/issues/467
\ctikzset{european xnor style/.is choice}
\ctikzset{european xnor style/default/.code={%
    \pgfcircdeclareeurologicport{xnor}{$=1$}{\pgf@circ@res@count}{not}}%
}
\ctikzset{european xnor style/direct/.code={%
    \pgfcircdeclareeurologicport{xnor}{$=$}{\pgf@circ@res@count}{}}%
}


% old, legacy keys that should be killed over
\ctikzset{bipoles/buffer/height/.initial=1}
\ctikzset{bipoles/buffer/width/.initial=1}
\ctikzset{bipoles/not port/width/.initial=1}
\ctikzset{bipoles/not port/height/.initial=.8}
\ctikzset{bipoles/not port/circle width/.initial=.15}

\ctikzset{tripoles/american and port/width/.initial=1.1}
\ctikzset{tripoles/american and port/height/.initial=.8}
\ctikzset{tripoles/american and port/port width/.initial=.7}
\ctikzset{tripoles/american and port/input height/.initial=.5}
\ctikzset{tripoles/american nand port/width/.initial=1.1}
\ctikzset{tripoles/american nand port/height/.initial=.8}
\ctikzset{tripoles/american nand port/port width/.initial=.7}
\ctikzset{tripoles/american nand port/circle width/.initial=.15}
\ctikzset{tripoles/american nand port/input height/.initial=.5}
\ctikzset{tripoles/american or port/width/.initial=1.1}
\ctikzset{tripoles/american or port/height/.initial=.8}
\ctikzset{tripoles/american or port/port width/.initial=.7}
\ctikzset{tripoles/american or port/input height/.initial=.5}
\ctikzset{tripoles/american or port/input skip/.initial=.25}
\ctikzset{tripoles/american or port/aaa/.initial=.6}
\ctikzset{tripoles/american or port/bbb/.initial=.4}
\ctikzset{tripoles/american or port/ccc/.initial=.5}
\ctikzset{tripoles/american or port/ddd/.initial=.0}
\ctikzset{tripoles/american nor port/width/.initial=1.1}
\ctikzset{tripoles/american nor port/height/.initial=.8}
\ctikzset{tripoles/american nor port/port width/.initial=.7}
\ctikzset{tripoles/american nor port/input height/.initial=.5}
\ctikzset{tripoles/american nor port/input skip/.initial=.25}
\ctikzset{tripoles/american nor port/circle width/.initial=.15}
\ctikzset{tripoles/american nor port/aaa/.initial=.6}
\ctikzset{tripoles/american nor port/bbb/.initial=.4}
\ctikzset{tripoles/american nor port/ccc/.initial=.5}
\ctikzset{tripoles/american nor port/ddd/.initial=.0}
\ctikzset{tripoles/american xor port/width/.initial=1.1}
\ctikzset{tripoles/american xor port/height/.initial=.8}
\ctikzset{tripoles/american xor port/port width/.initial=.7}
\ctikzset{tripoles/american xor port/input height/.initial=.5}
\ctikzset{tripoles/american xor port/input skip/.initial=.15}
\ctikzset{tripoles/american xor port/distance/.initial=.1}
\ctikzset{tripoles/american xnor port/width/.initial=1.1}
\ctikzset{tripoles/american xnor port/height/.initial=.8}
\ctikzset{tripoles/american xnor port/port width/.initial=.7}
\ctikzset{tripoles/american xnor port/input height/.initial=.5}
\ctikzset{tripoles/american xnor port/input skip/.initial=.15}
\ctikzset{tripoles/american xnor port/distance/.initial=.1}
\ctikzset{tripoles/american xnor port/circle width/.initial=.15}
\ctikzset{tripoles/american and port/origin/.initial=0.8}
\ctikzset{tripoles/american and port/inputs/.initial=2}
% variable number of inputs
\ctikzset{tripoles/american nand port/origin/.initial=0.8}
\ctikzset{tripoles/american nand port/inputs/.initial=2}
\ctikzset{tripoles/american nor port/origin/.initial=0.8}
\ctikzset{tripoles/american nor port/inputs/.initial=2}
\ctikzset{tripoles/american nor port/angle/.initial=70}
\ctikzset{tripoles/american nor port/inner/.initial=0.3}
\ctikzset{tripoles/american or port/origin/.initial=0.8}
\ctikzset{tripoles/american or port/inputs/.initial=2}
\ctikzset{tripoles/american or port/angle/.initial=70}
\ctikzset{tripoles/american or port/inner/.initial=0.3}
\ctikzset{tripoles/american xor port/origin/.initial=0.8}
\ctikzset{tripoles/american xor port/inputs/.initial=2}
\ctikzset{tripoles/american xor port/angle/.initial=70}
\ctikzset{tripoles/american xor port/inner/.initial=0.3}
\ctikzset{tripoles/american xnor port/origin/.initial=0.8}
\ctikzset{tripoles/american xnor port/inputs/.initial=2}
\ctikzset{tripoles/american xnor port/angle/.initial=70}
\ctikzset{tripoles/american xnor port/inner/.initial=0.3}
%
\ctikzset{tripoles/european and port/width/.initial=1.4}
\ctikzset{tripoles/european and port/height/.initial=.65}
\ctikzset{tripoles/european and port/reserved/.initial=.6}
\ctikzset{tripoles/european and port/input height/.initial=.6}
\ctikzset{tripoles/european or port/width/.initial=1.4}
\ctikzset{tripoles/european or port/height/.initial=.65}
\ctikzset{tripoles/european or port/reserved/.initial=.6}
\ctikzset{tripoles/european or port/input height/.initial=.6}
\ctikzset{tripoles/european xor port/width/.initial=1.4}
\ctikzset{tripoles/european xor port/height/.initial=.65}
\ctikzset{tripoles/european xor port/reserved/.initial=.6}
\ctikzset{tripoles/european xor port/input height/.initial=.6}
\ctikzset{tripoles/european nand port/width/.initial=1.4}
\ctikzset{tripoles/european nand port/not height/.initial=.3}
\ctikzset{tripoles/european nand port/not width/.initial=.9}
\ctikzset{tripoles/european nand port/height/.initial=.65}
\ctikzset{tripoles/european nand port/reserved/.initial=.6}
\ctikzset{tripoles/european nand port/input height/.initial=.6}
\ctikzset{tripoles/european buffer port/width/.initial=1.4}
\ctikzset{tripoles/european buffer port/not height/.initial=.3}
\ctikzset{tripoles/european buffer port/not width/.initial=.9}
\ctikzset{tripoles/european buffer port/height/.initial=.65}
\ctikzset{tripoles/european buffer port/reserved/.initial=.6}
\ctikzset{tripoles/european buffer port/input height/.initial=0}
\ctikzset{tripoles/european not port/width/.initial=1.4}
\ctikzset{tripoles/european not port/not height/.initial=.3}
\ctikzset{tripoles/european not port/not width/.initial=.9}
\ctikzset{tripoles/european not port/height/.initial=.65}
\ctikzset{tripoles/european not port/reserved/.initial=.6}
\ctikzset{tripoles/european not port/input height/.initial=0}
\ctikzset{tripoles/european xnor port/width/.initial=1.4}
\ctikzset{tripoles/european xnor port/not height/.initial=.3}
\ctikzset{tripoles/european xnor port/not width/.initial=.9}
\ctikzset{tripoles/european xnor port/height/.initial=.65}
\ctikzset{tripoles/european xnor port/reserved/.initial=.6}
\ctikzset{tripoles/european xnor port/input height/.initial=.6}
\ctikzset{tripoles/european nor port/width/.initial=1.4}
\ctikzset{tripoles/european nor port/not height/.initial=.3}
\ctikzset{tripoles/european nor port/not width/.initial=.9}
\ctikzset{tripoles/european nor port/height/.initial=.65}
\ctikzset{tripoles/european nor port/reserved/.initial=.6}
\ctikzset{tripoles/european nor port/input height/.initial=.6}
% variable number of inputs
\ctikzset{tripoles/european and port/origin/.initial=0.8}
\ctikzset{tripoles/european and port/inputs/.initial=2}
\ctikzset{tripoles/european nand port/origin/.initial=0.8}
\ctikzset{tripoles/european nand port/inputs/.initial=2}
\ctikzset{tripoles/european or port/origin/.initial=0.8}
\ctikzset{tripoles/european or port/inputs/.initial=2}
\ctikzset{tripoles/european nor port/origin/.initial=0.8}
\ctikzset{tripoles/european nor port/inputs/.initial=2}
\ctikzset{tripoles/european xor port/origin/.initial=0.8}
\ctikzset{tripoles/european xor port/inputs/.initial=2}
\ctikzset{tripoles/european xnor port/origin/.initial=0.8}
\ctikzset{tripoles/european xnor port/inputs/.initial=2}
\ctikzset{tripoles/european buffer port/origin/.initial=0.8}
\ctikzset{tripoles/european buffer port/inputs/.initial=1}%
\ctikzset{tripoles/european not port/origin/.initial=0.8}
\ctikzset{tripoles/european not port/inputs/.initial=1}%
%% "blank" ports
\ctikzset{tripoles/european blank port/width/.initial=1.4}
\ctikzset{tripoles/european blank port/height/.initial=.65}
\ctikzset{tripoles/european blank port/reserved/.initial=.6}
\ctikzset{tripoles/european blank port/input height/.initial=.6}
\ctikzset{tripoles/european blank not port/width/.initial=1.4}
\ctikzset{tripoles/european blank not port/not height/.initial=.3}
\ctikzset{tripoles/european blank not port/not width/.initial=.9}
\ctikzset{tripoles/european blank not port/height/.initial=.65}
\ctikzset{tripoles/european blank not port/reserved/.initial=.6}
\ctikzset{tripoles/european blank not port/input height/.initial=.6}
\ctikzset{tripoles/european blank port/origin/.initial=0.8}
\ctikzset{tripoles/european blank port/inputs/.initial=2}
\ctikzset{tripoles/european blank not port/origin/.initial=0.8}
\ctikzset{tripoles/european blank not port/inputs/.initial=2}
%%% parameters that are not used anymore after multi-input
%%% gates --- left for compatibility of source code.
\ctikzset{tripoles/american xor port/aaa/.initial=.6}
\ctikzset{tripoles/american xor port/bbb/.initial=.4}
\ctikzset{tripoles/american xor port/ccc/.initial=.5}
\ctikzset{tripoles/american xor port/ddd/.initial=.0}
\ctikzset{tripoles/american xnor port/aaa/.initial=.6}
\ctikzset{tripoles/american xnor port/bbb/.initial=.4}
\ctikzset{tripoles/american xnor port/ccc/.initial=.5}
\ctikzset{tripoles/american xnor port/ddd/.initial=.0}
%%>>>

%% Node shapes for Logic ports%<<<
%%
%% Code from John Kormylo at tex.stackexchange.com
%% See https://tex.stackexchange.com/questions/372993/is-it-possible-to-implement-multiple-input-logic-ports-with-circuitikz
%% Integration and fixes from Romano Giannetti and TheTeXnician <38565529+TheTeXnician@users.noreply.github.com>
%%

\newcount\pgf@circ@res@count% reserve global register

\def\pgf@circ@logicport@input#1% #1 = \pgfmathcounter
{%
    \pgfextracty{\pgf@circ@res@up}{\northeast}%
    \step
    \pgf@circ@res@step=\dimexpr 2\pgf@y -2\pgf@circ@res@up\relax
    \advance\pgf@y by -#1\pgf@circ@res@step\relax
}%

% #1 = \pgfmathcounter #2=type #3 specificic port
% type is 1 for and,nand; 2 for or,nor; 3 for xor,xnor, 4 for european.
\def\pgf@circ@logicport@baseinput#1#2#3%
{%
    % and and nand
    \ifnum #2=1\relax
        \pgfextracty{\pgf@circ@res@up}{\northeast}%
        \pgfextractx{\pgf@circ@res@left}{\southwest}%
        \step
        \pgf@circ@res@step=\dimexpr 2\pgf@y -2\pgf@circ@res@up\relax
        \advance\pgf@y by -#1\pgf@circ@res@step\relax
        \pgf@x=\ctikzvalof{tripoles/american #3 port/port width}\pgf@circ@res@left
    \fi
    % or and nor
    \ifnum #2=2\relax
        \pgfextracty{\pgf@circ@res@up}{\northeast}%
        \pgfextractx{\pgf@circ@res@left}{\southwest}%
        \pgfextractx{\pgf@circ@res@right}{\northeast}%
        \step
        \pgf@circ@res@step=\dimexpr 2\pgf@y -2\pgf@circ@res@up\relax
        \advance\pgf@y by -#1\pgf@circ@res@step\relax
        \edef\pgf@circ@math@angle{\ctikzvalof{tripoles/american #3 port/angle}}%
        \pgf@circ@res@other=\ctikzvalof{tripoles/american #3 port/inner}\pgf@circ@res@right
        \pgfmathsetlengthmacro{\pgf@circ@math@xradius}{\pgf@circ@res@other /(1 - cos(\pgf@circ@math@angle)}%
        \pgfmathsetlengthmacro{\pgf@circ@math@yradius}{\pgf@circ@res@up /sin(\pgf@circ@math@angle)}%
        \pgf@circ@res@other=\ctikzvalof{tripoles/american #3 port/port width}\pgf@circ@res@right
        \pgfmathsetlengthmacro{\pgf@circ@math@xorigin}{\pgf@circ@res@other + \pgf@circ@math@xradius*cos(\pgf@circ@math@angle)}%
        \pgf@circ@res@temp=\pgf@y
        \pgfmathsetmacro{\pgf@circ@math@angle}{asin(\pgf@circ@res@temp/\pgf@circ@math@yradius)}%
        \pgfmathsetlength{\pgf@circ@res@other}{\pgf@circ@math@xradius*cos(\pgf@circ@math@angle)-\pgf@circ@math@xorigin}%
        \pgf@x=\pgf@circ@res@other
    \fi
    % xor and xnor
    \ifnum #2=3\relax
        \pgfextracty{\pgf@circ@res@up}{\northeast}%
        \pgfextractx{\pgf@circ@res@left}{\southwest}%
        \pgfextractx{\pgf@circ@res@right}{\northeast}%
        \pgfkeysgetvalue{/tikz/circuitikz/tripoles/american #3 port/angle}{\pgf@circ@math@angle}%
        \pgf@circ@res@other=\ctikzvalof{tripoles/american #3 port/inner}\pgf@circ@res@right
        \pgfmathsetlengthmacro{\pgf@circ@math@xradius}{\pgf@circ@res@other /(1 - cos(\pgf@circ@math@angle)}%
        \pgfmathsetlengthmacro{\pgf@circ@math@yradius}{\pgf@circ@res@up / sin(\pgf@circ@math@angle))}%
        \pgf@circ@res@other=\ctikzvalof{tripoles/american #3 port/port width}\pgf@circ@res@right
        \pgfmathsetlengthmacro{\pgf@circ@math@xorigin}{\pgf@circ@res@other + \pgf@circ@math@xradius*cos(\pgf@circ@math@angle)}%
        \pgf@circ@res@temp=\ctikzvalof{tripoles/american #3 port/distance}\pgf@circ@res@right
        \pgfmathsetlengthmacro{\pgf@circ@math@distance}{\pgf@circ@res@temp}
        % this compensates for the effect of the line width on the gap between the arcs
        \pgfmathsetlengthmacro{\pgf@circ@math@yradiusA}{\pgf@circ@math@yradius -2\pgflinewidth}%
        \pgfmathsetlengthmacro{\pgf@circ@math@xradiusA}{\pgf@circ@math@xradius -2\pgflinewidth}%

        \step
        \pgf@circ@res@step=\dimexpr 2\pgf@y -2\pgf@circ@res@up\relax
        \advance\pgf@y by -#1\pgf@circ@res@step\relax
         \pgf@circ@res@temp=\pgf@y
            \pgfmathsetmacro{\pgf@circ@math@angle}{asin(\pgf@circ@res@temp  / \pgf@circ@math@yradiusA)}%
        \pgfmathsetlength{\pgf@circ@res@other}{\pgf@circ@math@xradiusA*cos(\pgf@circ@math@angle)-\pgf@circ@math@xorigin}%
        \advance\pgf@circ@res@other by -\pgf@circ@math@distance
        \pgf@x=\pgf@circ@res@other
    \fi
    % european
    \ifnum #2=4\relax
        \pgfextracty{\pgf@circ@res@up}{\northeast}%
        \pgfextractx{\pgf@circ@res@other}{\left}%
        \step
        \pgf@circ@res@step=\dimexpr 2\pgf@y -2\pgf@circ@res@up\relax
        \advance\pgf@y by -#1\pgf@circ@res@step\relax
        \pgf@x=\pgf@circ@res@other
    \fi
}%

%%% american
\long\def\pgfcircdeclarelogicport#1#2#3{%
    \pgfdeclareshape{american #1 port}%
    {%
        \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
        \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
        \savedmacro\resize{% automatic
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgf@circ@res@up = \ctikzvalof{tripoles/american #1 port/height}\pgf@circ@scaled@Rlen
            \pgf@circ@res@up = .5\pgf@circ@res@up
            \pgf@circ@res@down = -\pgf@circ@res@up
            \pgf@circ@res@right = \ctikzvalof{tripoles/american #1 port/width}\pgf@circ@scaled@Rlen
            \pgf@circ@res@right = .5\pgf@circ@res@right
            \pgf@circ@res@left = -\pgf@circ@res@right
    }%
    \savedmacro\inputs{% get number of inputs
        \pgf@circ@res@count=\pgfkeysvalueof{/tikz/number inputs}\relax%
        \ifnum\pgf@circ@res@count=0
            \pgf@circ@res@count=\ctikzvalof{tripoles/american #1 port/inputs}\relax%
        \fi
        \ifnum\pgf@circ@res@count<2 \pgf@circ@res@count=2\fi
        \ifnum\pgf@circ@res@count>16 \pgf@circ@res@count=16\fi
        \def\inputs{\the\pgf@circ@res@count}%
    }%
    \savedanchor\step{% 1/2 gap at edges
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@circ@res@step = \ctikzvalof{tripoles/american #1 port/height}\pgf@circ@scaled@Rlen
        \divide\pgf@circ@res@step by \pgf@circ@res@count
        \pgfpoint{\pgf@circ@res@left}{\dimexpr\pgf@circ@res@up+0.5\pgf@circ@res@step}%
    }%
    \savedanchor\northeast{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
    \savedanchor\southwest{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}
    \savedanchor\left{\pgfpoint{\ctikzvalof{tripoles/american #1 port/port width}\pgf@circ@res@left}{0pt}}
    \savedanchor\right{\pgfpoint{\ctikzvalof{tripoles/american #1 port/port width}\pgf@circ@res@right}{0pt}}
    \savedanchor\origin{\pgfpoint{\ctikzvalof{tripoles/american #1 port/origin}\pgf@circ@res@right}{0pt}}

    \anchor{center}{\origin}% for backwards compatibility
    \anchor{text}{\pgfpoint{-.5\wd\pgfnodeparttextbox}{\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox}}

    % create input anchors
    \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@american #1 port\endcsname{%
        \pgfmathloop%
        \ifnum\pgfmathcounter>\pgf@circ@res@count%
    \else%
        %\pgfutil@ifundefined{pgf@anchor@american #1 port@in \pgfmathcounter}{%
        \expandafter\xdef\csname pgf@anchor@american #1 port@in \pgfmathcounter\endcsname{%
            \noexpand\pgf@circ@logicport@input{\pgfmathcounter}% defined above
        }%
        \expandafter\xdef\csname pgf@anchor@american #1 port@bin \pgfmathcounter\endcsname{%
            \noexpand\pgf@circ@logicport@baseinput{\pgfmathcounter}{#2}{#1}% defined above
        }%
        %}{}%
        \repeatpgfmathloop%
    }

    \anchor{out}{\northeast\pgf@y=0pt}
    \anchor{bout}{\right\pgf@y=0pt}


    \anchor{left}{\left}% edges of component minus leads
    \anchor{right}{\right}

    \anchor{north east}{\northeast}% see \Compass macro
    \anchor{south west}{\southwest}
    \anchor{north}{\pgfextracty{\pgf@circ@res@up}{\northeast}%
    \pgfpoint{0cm}{\pgf@circ@res@up}}
    \anchor{north west}{\pgfextracty{\pgf@circ@res@up}{\northeast}%
        \pgfextractx{\pgf@circ@res@left}{\southwest}%
    \pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
    \anchor{west}{\pgfextractx{\pgf@circ@res@left}{\southwest}%
    \pgfpoint{\pgf@circ@res@left}{0cm}}
    \anchor{south}{\pgfextracty{\pgf@circ@res@down}{\southwest}%
    \pgfpoint{0cm}{\pgf@circ@res@down}}
    \anchor{south east}{\pgfextracty{\pgf@circ@res@down}{\southwest}%
        \pgfextractx{\pgf@circ@res@right}{\northeast}%
    \pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down}}
    \anchor{east}{\pgfextractx{\pgf@circ@res@right}{\northeast}%
    \pgfpoint{\pgf@circ@res@right}{0cm}}

    \pgf@circ@draw@component{
        \pgf@circ@setcolor
        % \pgf@circ@debug@colors
        #3%
    }
}
}
%%% american and %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfcircdeclarelogicport{and}{1}{
    \pgfextracty{\pgf@circ@res@temp}{\step}%
    \pgf@circ@res@step = \dimexpr 2\pgf@circ@res@temp -2\pgf@circ@res@up\relax
    \pgf@circ@res@count = \inputs\relax
    \ifpgfcirc@draw@input@leads
        %input leads
        \loop\ifnum\pgf@circ@res@count>0
            \advance\pgf@circ@res@temp by -\pgf@circ@res@step
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@temp}}%
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/american and port/port width}\pgf@circ@res@left}
            {\pgf@circ@res@temp}}
            \advance\pgf@circ@res@count by -1
        \repeat
    \fi
    % output lead
    \ifpgfcirc@draw@output@leads
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/american and port/port width}\pgf@circ@res@right} {0pt}}
        \pgfusepath{draw}
    \fi


    \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
    \pgf@circ@res@other=\ctikzvalof{tripoles/american and port/port width}\pgf@circ@res@left

    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@down}}
    \pgfpatharc{-90}{90}{-2\pgf@circ@res@other and \pgf@circ@res@up}
    \pgfpathclose
    \pgf@circ@draworfill
    }
%%% american nand %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \pgfcircdeclarelogicport{nand}{1}{
    \pgfextracty{\pgf@circ@res@temp}{\step}%
    \pgf@circ@res@step = \dimexpr 2\pgf@circ@res@temp -2\pgf@circ@res@up\relax
    \pgf@circ@res@count = \inputs\relax
    \ifpgfcirc@draw@input@leads
        %input leads
        \loop\ifnum\pgf@circ@res@count>0
            \advance\pgf@circ@res@temp by -\pgf@circ@res@step
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@temp}}%
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/american nand port/port width}\pgf@circ@res@left}
            {\pgf@circ@res@temp}}
            \advance\pgf@circ@res@count by -1
        \repeat
    \fi

    \ifpgfcirc@draw@output@leads
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/american nand port/port width}\pgf@circ@res@right} {0pt}}
        \pgfusepath{draw}
    \fi
    \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
    \pgf@circ@res@step = \ctikzvalof{tripoles/american nand port/circle width}\pgf@circ@res@right
    \pgf@circ@res@other = \ctikzvalof{tripoles/american nand port/port width}\pgf@circ@res@right
    \pgf@circ@res@temp = \dimexpr 2\pgf@circ@res@other - \pgf@circ@res@step\relax

    \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@other}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{-\pgf@circ@res@other}{\pgf@circ@res@down}}
    \pgfpatharc{-90}{90}{\pgf@circ@res@temp and \pgf@circ@res@up}
    \pgfpathclose

    \pgfpathellipse
    {\pgfpoint{\pgf@circ@res@other-.5\pgf@circ@res@step}{0pt}}
    {\pgfpoint{.5\pgf@circ@res@step}{0pt}}
    {\pgfpoint{0pt}{.5\pgf@circ@res@step}}

    \pgf@circ@draworfill
}
%%% american nor %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfcircdeclarelogicport{nor}{2}{
    \edef\pgf@circ@math@angle{\ctikzvalof{tripoles/american nor port/angle}}%
    \pgf@circ@res@other=\ctikzvalof{tripoles/american nor port/inner}\pgf@circ@res@right
    \pgfmathsetlengthmacro{\pgf@circ@math@xradius}{\pgf@circ@res@other /(1 - cos(\pgf@circ@math@angle)}%
    \pgfmathsetlengthmacro{\pgf@circ@math@yradius}{\pgf@circ@res@up /sin(\pgf@circ@math@angle)}%
    \pgf@circ@res@other=\ctikzvalof{tripoles/american nor port/port width}\pgf@circ@res@right
    \pgfmathsetlengthmacro{\pgf@circ@math@xorigin}{\pgf@circ@res@other + \pgf@circ@math@xradius*cos(\pgf@circ@math@angle)}%

    \pgfextracty{\pgf@circ@res@temp}{\step}%
    \pgf@circ@res@step = \dimexpr 2\pgf@circ@res@temp -2\pgf@circ@res@up\relax
    \pgf@circ@res@count = \inputs\relax
    \ifpgfcirc@draw@input@leads
        %input leads
        \loop\ifnum\pgf@circ@res@count>0
            \advance\pgf@circ@res@temp by -\pgf@circ@res@step
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@temp}}%
            \pgfmathsetmacro{\pgf@circ@math@angle}{asin(\pgf@circ@res@temp/\pgf@circ@math@yradius)}%
            \pgfmathsetlength{\pgf@circ@res@other}{\pgf@circ@math@xradius*cos(\pgf@circ@math@angle)-\pgf@circ@math@xorigin}%
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@temp}}%
            \advance\pgf@circ@res@count by -1
        \repeat
    \fi

    \pgf@circ@res@other=\ctikzvalof{tripoles/american nor port/port width}\pgf@circ@res@right
    \ifpgfcirc@draw@output@leads
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{0pt}}
        \pgfusepath{draw}
    \fi

    \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}

    \edef\pgf@circ@math@angle{\ctikzvalof{tripoles/american nor port/angle}}%
    \pgf@circ@res@step = \ctikzvalof{tripoles/american nor port/circle width}\pgf@circ@res@right
    \pgf@circ@res@temp = \dimexpr 2\pgf@circ@res@other - \pgf@circ@res@step\relax
    \advance\pgf@circ@res@other by -\pgf@circ@res@step

    % main shape
    \ifpgfcirc@roundy@or@shapes
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other}{0pt}}% for symmetry
        \pgfpatharc{0}{90}{\pgf@circ@res@temp and \pgf@circ@res@up}%
        \pgfpatharc{\pgf@circ@math@angle}{-\pgf@circ@math@angle}{\pgf@circ@math@xradius and \pgf@circ@math@yradius}%
        \pgfpatharc{-90}{0}{\pgf@circ@res@temp and \pgf@circ@res@up}%
        \pgfpathclose
    \else
        \pgfmathsetlength{\pgf@circ@res@temp}{\pgf@circ@math@xradius*cos(\pgf@circ@math@angle)-\pgf@circ@math@xorigin}%
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@up}}
        \pgfpatharc{\pgf@circ@math@angle}{-\pgf@circ@math@angle}{\pgf@circ@math@xradius and \pgf@circ@math@yradius}%
        \pgfpathcurveto{\pgfpoint{0.3\pgf@circ@res@right}{\pgf@circ@res@down}}{\pgfpoint{0.3\pgf@circ@res@right}{0.5\pgf@circ@res@down}}{\pgfpoint{\pgf@circ@res@other}{0pt}}
        \pgfpathcurveto{\pgfpoint{0.3\pgf@circ@res@right}{0.5\pgf@circ@res@up}}{\pgfpoint{0.3\pgf@circ@res@right}{\pgf@circ@res@up}}{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@up}}
        \pgfpathclose
    \fi

    % not dot
    \pgfpathellipse
    {\pgfpoint{\pgf@circ@res@other+.5\pgf@circ@res@step}{0pt}}
    {\pgfpoint{.5\pgf@circ@res@step}{0pt}}
    {\pgfpoint{0pt}{.5\pgf@circ@res@step}}

    \pgf@circ@draworfill
}
%%% american or %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfcircdeclarelogicport{or}{2}{
    \edef\pgf@circ@math@angle{\ctikzvalof{tripoles/american or port/angle}}%
    \pgf@circ@res@other=\ctikzvalof{tripoles/american or port/inner}\pgf@circ@res@right
    \pgfmathsetlengthmacro{\pgf@circ@math@xradius}{\pgf@circ@res@other /(1 - cos(\pgf@circ@math@angle)}%
    \pgfmathsetlengthmacro{\pgf@circ@math@yradius}{\pgf@circ@res@up /sin(\pgf@circ@math@angle)}%
    \pgf@circ@res@other=\ctikzvalof{tripoles/american or port/port width}\pgf@circ@res@right
    \pgfmathsetlengthmacro{\pgf@circ@math@xorigin}{\pgf@circ@res@other + \pgf@circ@math@xradius*cos(\pgf@circ@math@angle)}%

    \pgfextracty{\pgf@circ@res@temp}{\step}%
    \pgf@circ@res@step = \dimexpr 2\pgf@circ@res@temp -2\pgf@circ@res@up\relax
    \pgf@circ@res@count = \inputs\relax
    \ifpgfcirc@draw@input@leads
        %input leads
        \loop\ifnum\pgf@circ@res@count>0
            \advance\pgf@circ@res@temp by -\pgf@circ@res@step
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@temp}}%
            \pgfmathsetmacro{\pgf@circ@math@angle}{asin(\pgf@circ@res@temp/\pgf@circ@math@yradius)}%
            \pgfmathsetlength{\pgf@circ@res@other}{\pgf@circ@math@xradius*cos(\pgf@circ@math@angle)-\pgf@circ@math@xorigin}%
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@temp}}%
            \advance\pgf@circ@res@count by -1
        \repeat
    \fi

    \pgf@circ@res@other=\ctikzvalof{tripoles/american or port/port width}\pgf@circ@res@right
    \ifpgfcirc@draw@output@leads
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{0pt}}
        \pgfusepath{draw}
    \fi

    \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}

    \edef\pgf@circ@math@angle{\ctikzvalof{tripoles/american or port/angle}}%

    % main shape
    \ifpgfcirc@roundy@or@shapes
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other}{0pt}}% for symmetry
        \pgfpatharc{0}{90}{2\pgf@circ@res@other and \pgf@circ@res@up}%
        \pgfpatharc{\pgf@circ@math@angle}{-\pgf@circ@math@angle}{\pgf@circ@math@xradius and \pgf@circ@math@yradius}%
        \pgfpatharc{-90}{0}{2\pgf@circ@res@other and \pgf@circ@res@up}%
        \pgfpathclose
    \else
        \pgfmathsetlength{\pgf@circ@res@temp}{\pgf@circ@math@xradius*cos(\pgf@circ@math@angle)-\pgf@circ@math@xorigin}%
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@up}}
        \pgfpatharc{\pgf@circ@math@angle}{-\pgf@circ@math@angle}{\pgf@circ@math@xradius and \pgf@circ@math@yradius}%
        \pgfpathcurveto{\pgfpoint{0.3\pgf@circ@res@right}{\pgf@circ@res@down}}{\pgfpoint{0.3\pgf@circ@res@right}{0.5\pgf@circ@res@down}}{\pgfpoint{\pgf@circ@res@other}{0pt}}
        \pgfpathcurveto{\pgfpoint{0.3\pgf@circ@res@right}{0.5\pgf@circ@res@up}}{\pgfpoint{0.3\pgf@circ@res@right}{\pgf@circ@res@up}}{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@up}}
        \pgfpathclose
    \fi

    \pgf@circ@draworfill
}
%%% american xor %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfcircdeclarelogicport{xor}{3}{
    \pgfkeysgetvalue{/tikz/circuitikz/tripoles/american xor port/angle}{\pgf@circ@math@angle}%
    \pgf@circ@res@other=\ctikzvalof{tripoles/american xor port/inner}\pgf@circ@res@right
    \pgfmathsetlengthmacro{\pgf@circ@math@xradius}{\pgf@circ@res@other /(1 - cos(\pgf@circ@math@angle)}%
    \pgfmathsetlengthmacro{\pgf@circ@math@yradius}{\pgf@circ@res@up / sin(\pgf@circ@math@angle))}%
    \pgf@circ@res@other=\ctikzvalof{tripoles/american xor port/port width}\pgf@circ@res@right
    \pgfmathsetlengthmacro{\pgf@circ@math@xorigin}{\pgf@circ@res@other + \pgf@circ@math@xradius*cos(\pgf@circ@math@angle)}%
    \pgf@circ@res@temp=\ctikzvalof{tripoles/american xor port/distance}\pgf@circ@res@right
    \pgfmathsetlengthmacro{\pgf@circ@math@distance}{\pgf@circ@res@temp}
    % this compensates for the effect of the line width on the gap between the arcs
    \pgfmathsetlengthmacro{\pgf@circ@math@yradiusA}{\pgf@circ@math@yradius -2\pgflinewidth}%
    \pgfmathsetlengthmacro{\pgf@circ@math@xradiusA}{\pgf@circ@math@xradius -2\pgflinewidth}%

    \pgfextracty{\pgf@circ@res@temp}{\step}%
    \pgf@circ@res@step = \dimexpr 2\pgf@circ@res@temp -2\pgf@circ@res@up\relax
    \pgf@circ@res@count = \inputs\relax
    \ifpgfcirc@draw@input@leads
        %input leads
        \loop\ifnum\pgf@circ@res@count>0
            \advance\pgf@circ@res@temp by -\pgf@circ@res@step
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@temp}}%
            \pgfmathsetmacro{\pgf@circ@math@angle}{asin(\pgf@circ@res@temp  / \pgf@circ@math@yradiusA)}%
            \pgfmathsetlength{\pgf@circ@res@other}{\pgf@circ@math@xradiusA*cos(\pgf@circ@math@angle)-\pgf@circ@math@xorigin}%
            \advance\pgf@circ@res@other by -\pgf@circ@math@distance
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@temp}}%
            \advance\pgf@circ@res@count by -1
        \repeat
    \fi

    \pgf@circ@res@other=\ctikzvalof{tripoles/american xor port/port width}\pgf@circ@res@right
    \ifpgfcirc@draw@output@leads
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{0pt}}
        \pgfusepath{draw}
    \fi

    \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}

    \edef\pgf@circ@math@angle{\ctikzvalof{tripoles/american xor port/angle}}%

    % main shape
    \ifpgfcirc@roundy@or@shapes
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other}{0pt}}% for symmetry
        \pgfpatharc{0}{90}{2\pgf@circ@res@other and \pgf@circ@res@up}%
        \pgfpatharc{\pgf@circ@math@angle}{-\pgf@circ@math@angle}{\pgf@circ@math@xradius and \pgf@circ@math@yradius}%
        \pgfpatharc{-90}{0}{2\pgf@circ@res@other and \pgf@circ@res@up}%
        \pgfpathclose
    \else
        \pgfmathsetlength{\pgf@circ@res@temp}{\pgf@circ@math@xradius*cos(\pgf@circ@math@angle)-\pgf@circ@math@xorigin}%
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@up}}
        \pgfpatharc{\pgf@circ@math@angle}{-\pgf@circ@math@angle}{\pgf@circ@math@xradius and \pgf@circ@math@yradius}%
        \pgfpathcurveto{\pgfpoint{0.3\pgf@circ@res@right}{\pgf@circ@res@down}}{\pgfpoint{0.3\pgf@circ@res@right}{0.5\pgf@circ@res@down}}{\pgfpoint{\pgf@circ@res@other}{0pt}}
        \pgfpathcurveto{\pgfpoint{0.3\pgf@circ@res@right}{0.5\pgf@circ@res@up}}{\pgfpoint{0.3\pgf@circ@res@right}{\pgf@circ@res@up}}{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@up}}
        \pgfpathclose
    \fi
    \pgf@circ@draworfill

    \pgfmathsetlength{\pgf@circ@res@temp}{(\pgf@circ@math@yradiusA)*sin(\pgf@circ@math@angle)}%

    \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@other-\pgf@circ@math@distance}{\pgf@circ@res@temp}}% first arc
    \pgfpatharc{\pgf@circ@math@angle}{-\pgf@circ@math@angle}{\pgf@circ@math@xradiusA and \pgf@circ@math@yradiusA}%

    \pgfusepath{draw}
}
%%% american xnor %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pgfcircdeclarelogicport{xnor}{3}{
    \pgfkeysgetvalue{/tikz/circuitikz/tripoles/american xnor port/angle}{\pgf@circ@math@angle}%
    \pgf@circ@res@other=\ctikzvalof{tripoles/american xnor port/inner}\pgf@circ@res@right
    \pgfmathsetlengthmacro{\pgf@circ@math@xradius}{\pgf@circ@res@other /(1 - cos(\pgf@circ@math@angle)}%
    \pgfmathsetlengthmacro{\pgf@circ@math@yradius}{\pgf@circ@res@up / sin(\pgf@circ@math@angle))}%
    \pgf@circ@res@other=\ctikzvalof{tripoles/american xnor port/port width}\pgf@circ@res@right
    \pgfmathsetlengthmacro{\pgf@circ@math@xorigin}{\pgf@circ@res@other + \pgf@circ@math@xradius*cos(\pgf@circ@math@angle)}%
    \pgf@circ@res@temp=\ctikzvalof{tripoles/american xor port/distance}\pgf@circ@res@right
    \pgfmathsetlengthmacro{\pgf@circ@math@distance}{\pgf@circ@res@temp}
    % this compensates for the effect of the line width on the gap between the arcs
    \pgfmathsetlengthmacro{\pgf@circ@math@yradiusA}{\pgf@circ@math@yradius -2\pgflinewidth}%
    \pgfmathsetlengthmacro{\pgf@circ@math@xradiusA}{\pgf@circ@math@xradius -2\pgflinewidth}%

    \pgfextracty{\pgf@circ@res@temp}{\step}%
    \pgf@circ@res@step = \dimexpr 2\pgf@circ@res@temp -2\pgf@circ@res@up\relax
    \pgf@circ@res@count = \inputs\relax
    \ifpgfcirc@draw@input@leads
        %input leads
        \loop\ifnum\pgf@circ@res@count>0
            \advance\pgf@circ@res@temp by -\pgf@circ@res@step
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@temp}}%
            \pgfmathsetmacro{\pgf@circ@math@angle}{asin(\pgf@circ@res@temp  / \pgf@circ@math@yradiusA)}%
            \pgfmathsetlength{\pgf@circ@res@other}{\pgf@circ@math@xradiusA*cos(\pgf@circ@math@angle)-\pgf@circ@math@xorigin}%
            \advance\pgf@circ@res@other by -\pgf@circ@math@distance
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@temp}}%
            \advance\pgf@circ@res@count by -1
        \repeat
    \fi

    \pgf@circ@res@other=\ctikzvalof{tripoles/american xnor port/port width}\pgf@circ@res@right
    \ifpgfcirc@draw@output@leads
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{0pt}}
        \pgfusepath{draw}
    \fi

    \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}

    \edef\pgf@circ@math@angle{\ctikzvalof{tripoles/american xnor port/angle}}%
    \pgf@circ@res@step = \ctikzvalof{tripoles/american xnor port/circle width}\pgf@circ@res@right
    \pgf@circ@res@temp = \dimexpr 2\pgf@circ@res@other - \pgf@circ@res@step\relax
    \advance\pgf@circ@res@other by -\pgf@circ@res@step

    % main shape
    \ifpgfcirc@roundy@or@shapes
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other}{0pt}}% for symmetry
        \pgfpatharc{0}{90}{\pgf@circ@res@temp and \pgf@circ@res@up}%
        \pgfpatharc{\pgf@circ@math@angle}{-\pgf@circ@math@angle}{\pgf@circ@math@xradius and \pgf@circ@math@yradius}%
        \pgfpatharc{-90}{0}{\pgf@circ@res@temp and \pgf@circ@res@up}%
        \pgfpathclose
    \else
        \pgfmathsetlength{\pgf@circ@res@temp}{\pgf@circ@math@xradius*cos(\pgf@circ@math@angle)-\pgf@circ@math@xorigin}%
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@up}}
        \pgfpatharc{\pgf@circ@math@angle}{-\pgf@circ@math@angle}{\pgf@circ@math@xradius and \pgf@circ@math@yradius}%
        \pgfpathcurveto{\pgfpoint{0.3\pgf@circ@res@right}{\pgf@circ@res@down}}{\pgfpoint{0.3\pgf@circ@res@right}{0.5\pgf@circ@res@down}}{\pgfpoint{\pgf@circ@res@other}{0pt}}
        \pgfpathcurveto{\pgfpoint{0.3\pgf@circ@res@right}{0.5\pgf@circ@res@up}}{\pgfpoint{0.3\pgf@circ@res@right}{\pgf@circ@res@up}}{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@up}}
        \pgfpathclose
    \fi

    \pgfpathellipse
    {\pgfpoint{\pgf@circ@res@other+.5\pgf@circ@res@step}{0pt}}
    {\pgfpoint{.5\pgf@circ@res@step}{0pt}}
    {\pgfpoint{0pt}{.5\pgf@circ@res@step}}
    \pgf@circ@draworfill

    \pgf@circ@res@other=\ctikzvalof{tripoles/american xnor port/port width}\pgf@circ@res@left
    \pgfmathsetlength{\pgf@circ@res@temp}{(\pgf@circ@math@yradiusA)*sin(\pgf@circ@math@angle)}%

    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other-\pgf@circ@math@distance}{\pgf@circ@res@temp}}% first arc
    \pgfpatharc{\pgf@circ@math@angle}{-\pgf@circ@math@angle}{\pgf@circ@math@xradiusA and \pgf@circ@math@yradiusA}%

    \pgfusepath{draw}
}

%%% Original one-input ports

\pgfdeclareshape{american not port}{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{bipoles/not port/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{bipoles/not port/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{in}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{in 1}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{left}{
        \northwest
        \pgf@x=0.7\pgf@x
        \pgf@y=0pt
    }
    \anchor{bin}{
        \northwest
        \pgf@x=0.7\pgf@x
        \pgf@y=0pt
    }
    \anchor{bin 1}{
        \northwest
        \pgf@y=0pt
        \pgf@x=0.7\pgf@x
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{right}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-0.7\pgf@x
    }
    \anchor{bout}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-0.7\pgf@x
    }
    \anchor{center}{
        \pgfpointorigin
    }
    \anchor{east}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{west}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{south}{
        \northwest
        \pgf@x=0pt
        \pgf@y=-\pgf@y
    }
    \anchor{north}{
        \northwest
        \pgf@x=0pt
    }
    \anchor{south west}{
        \northwest
        \pgf@y=-\pgf@y
    }
    \anchor{north east}{
        \northwest
        \pgf@x=-\pgf@x
    }
    \anchor{north west}{
        \northwest
    }
    \anchor{south east}{
        \northwest
        \pgf@x=-\pgf@x
        \pgf@y=-\pgf@y
    }
    \anchorborder{% this is used when the node is used as a path element
        \pgf@xa=\pgf@x
        \pgf@ya=\pgf@y
        \pgfextracty{\pgf@circ@res@up}{\northwest}
        \pgfextractx{\pgf@circ@res@left}{\northwest}
        \pgfpointborderrectangle{\pgfpoint{\pgf@xa}{\pgf@ya}}
            {\pgfpoint{-\pgf@circ@res@left}{\pgf@circ@res@up}}
    }
    \anchor{text}{%
        % centered and a bit to the left (it's a triangle)!
        \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }
    \pgf@circ@draw@component{
        \pgf@circ@setcolor

        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x

        \pgf@circ@res@other = \ctikzvalof{bipoles/not port/circle width}\pgf@circ@res@right

        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{.7\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=.7\pgf@circ@res@step

            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step-\pgf@circ@res@other}{0pt}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@step-\pgf@circ@res@other}{0pt}}
            \pgf@circ@draworfill
            \pgfpathellipse
            {\pgfpoint{\pgf@circ@res@step-.5\pgf@circ@res@other}{0pt}}
            {\pgfpoint{.5\pgf@circ@res@other}{0pt}}
            {\pgfpoint{0pt}{.5\pgf@circ@res@other}}
            \pgf@circ@draworfill
        \endpgfscope

        \ifpgfcirc@draw@input@leads
            %input leads
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
            \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@left}{0pt}}
        \fi

        \ifpgfcirc@draw@output@leads
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
            \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@right}{0pt}}
        \fi

        \pgfusepath{draw}
    }
}

\pgfdeclareshape{american buffer port}{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{bipoles/not port/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{bipoles/not port/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{in}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{in 1}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{left}{
        \northwest
        \pgf@x=0.7\pgf@x
        \pgf@y=0pt
    }
    \anchor{bin}{
        \northwest
        \pgf@x=0.7\pgf@x
        \pgf@y=0pt
    }
    \anchor{bin 1}{
        \northwest
        \pgf@y=0pt
        \pgf@x=0.7\pgf@x
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{right}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-0.7\pgf@x
    }
    \anchor{bout}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-0.7\pgf@x
    }
    \anchor{center}{
        \pgfpointorigin
    }
    \anchor{east}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{west}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{south}{
        \northwest
        \pgf@x=0pt
        \pgf@y=-\pgf@y
    }
    \anchor{north}{
        \northwest
        \pgf@x=0pt
    }
    \anchor{south west}{
        \northwest
        \pgf@y=-\pgf@y
    }
    \anchor{north east}{
        \northwest
        \pgf@x=-\pgf@x
    }
    \anchor{north west}{
        \northwest
    }
    \anchor{south east}{
        \northwest
        \pgf@x=-\pgf@x
        \pgf@y=-\pgf@y
    }
    \anchorborder{% this is used when the node is used as a path element
        \pgf@xa=\pgf@x
        \pgf@ya=\pgf@y
        \pgfextracty{\pgf@circ@res@up}{\northwest}
        \pgfextractx{\pgf@circ@res@left}{\northwest}
        \pgfpointborderrectangle{\pgfpoint{\pgf@xa}{\pgf@ya}}
            {\pgfpoint{-\pgf@circ@res@left}{\pgf@circ@res@up}}
    }
    \anchor{text}{%
        % centered and a bit to the left (it's a triangle)!
        \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }
    \pgf@circ@draw@component{
        \pgf@circ@setcolor

        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x

        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{.7\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=.7\pgf@circ@res@step

            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgf@circ@draworfill
        \endpgfscope

        \ifpgfcirc@draw@input@leads
            %input leads
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
            \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@left}{0pt}}
        \fi

        \ifpgfcirc@draw@output@leads
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
            \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@right}{0pt}}
        \fi

        \pgfusepath{draw}
    }
}
\pgfdeclareshape{invschmitt}{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{bipoles/not port/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{bipoles/not port/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{in}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{in 1}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{left}{
        \northwest
        \pgf@y=0pt
        \pgf@x=0.7\pgf@x
    }
    \anchor{bin}{
        \northwest
        \pgf@y=0pt
        \pgf@x=0.7\pgf@x
    }
    \anchor{bin 1}{
        \northwest
        \pgf@y=0pt
        \pgf@x=0.7\pgf@x
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{right}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-0.7\pgf@x
    }
    \anchor{bout}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-0.7\pgf@x
    }
    \anchor{center}{
        \pgfpointorigin
    }
    \anchor{east}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{west}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{south}{
        \northwest
        \pgf@x=0pt
        \pgf@y=-\pgf@y
    }
    \anchor{north}{
        \northwest
        \pgf@x=0pt
    }
    \anchor{south west}{
        \northwest
        \pgf@y=-\pgf@y
    }
    \anchor{north east}{
        \northwest
        \pgf@x=-\pgf@x
    }
    \anchor{north west}{
        \northwest
    }
    \anchor{south east}{
        \northwest
        \pgf@x=-\pgf@x
        \pgf@y=-\pgf@y
    }
    \anchorborder{% this is used when the node is used as a path element
        \pgf@xa=\pgf@x
        \pgf@ya=\pgf@y
        \pgfextracty{\pgf@circ@res@up}{\northwest}
        \pgfextractx{\pgf@circ@res@left}{\northwest}
        \pgfpointborderrectangle{\pgfpoint{\pgf@xa}{\pgf@ya}}
            {\pgfpoint{-\pgf@circ@res@left}{\pgf@circ@res@up}}
    }
    \anchor{text}{%
        % centered and a bit to the left (it's a triangle)!
        \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }
    \pgf@circ@draw@component{
        \pgf@circ@setcolor

        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x

        \pgf@circ@res@other = \ctikzvalof{bipoles/not port/circle width}\pgf@circ@res@right

        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{.7\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=.7\pgf@circ@res@step

            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step-\pgf@circ@res@other}{0pt}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathclose
            \pgf@circ@draworfill
            \pgfpathellipse
            {\pgfpoint{\pgf@circ@res@step-.5\pgf@circ@res@other}{0pt}}
            {\pgfpoint{.5\pgf@circ@res@other}{0pt}}
            {\pgfpoint{0pt}{.5\pgf@circ@res@other}}
            \pgf@circ@draworfill
        \endpgfscope

        \ifpgfcirc@draw@input@leads
            %input leads
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
            \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@left}{0pt}}
        \fi

        \ifpgfcirc@draw@output@leads
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
            \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@right}{0pt}}
        \fi
        \pgfusepath{draw}
        %draw inner shape

        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}

        \pgfpathmoveto{\pgfpoint{.6\pgf@circ@res@left}{.3\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{.4\pgf@circ@res@left}{.3\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{.4\pgf@circ@res@left}{.3\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{.05\pgf@circ@res@left}{.3\pgf@circ@res@up}}
        \pgfpathmoveto{\pgfpoint{.4\pgf@circ@res@left}{.3\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{.25\pgf@circ@res@left}{.3\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{.25\pgf@circ@res@left}{.3\pgf@circ@res@up}}
        \pgfusepath{draw}

    }
}

\pgfdeclareshape{schmitt}{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{bipoles/not port/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{bipoles/not port/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{in}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{in 1}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{left}{
        \northwest
        \pgf@y=0pt
        \pgf@x=0.7\pgf@x
    }
    \anchor{bin}{
        \northwest
        \pgf@y=0pt
        \pgf@x=0.7\pgf@x
    }
    \anchor{bin 1}{
        \northwest
        \pgf@y=0pt
        \pgf@x=0.7\pgf@x
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{right}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-.7\pgf@x
    }
    \anchor{bout}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-.7\pgf@x
    }
    \anchor{center}{
        \pgfpointorigin
    }
    \anchor{east}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{west}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{south}{
        \northwest
        \pgf@x=0pt
        \pgf@y=-\pgf@y
    }
    \anchor{north}{
        \northwest
        \pgf@x=0pt
    }
    \anchor{south west}{
        \northwest
        \pgf@y=-\pgf@y
    }
    \anchor{north east}{
        \northwest
        \pgf@x=-\pgf@x
    }
    \anchor{north west}{
        \northwest
    }
    \anchor{south east}{
        \northwest
        \pgf@x=-\pgf@x
        \pgf@y=-\pgf@y
    }
    \anchorborder{% this is used when the node is used as a path element
        \pgf@xa=\pgf@x
        \pgf@ya=\pgf@y
        \pgfextracty{\pgf@circ@res@up}{\northwest}
        \pgfextractx{\pgf@circ@res@left}{\northwest}
        \pgfpointborderrectangle{\pgfpoint{\pgf@xa}{\pgf@ya}}
            {\pgfpoint{-\pgf@circ@res@left}{\pgf@circ@res@up}}
    }
    \anchor{text}{%
        % centered and a bit to the left (it's a triangle)!
        \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }
    \pgf@circ@draw@component{
        \pgf@circ@setcolor


        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x

        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{.7\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=.7\pgf@circ@res@step

            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope

        \ifpgfcirc@draw@input@leads
            %input leads
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
            \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@left}{0pt}}
        \fi

        \ifpgfcirc@draw@output@leads
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
            \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@right-\pgf@circ@res@other}{0pt}}
        \fi
        \pgfusepath{draw}
        %draw inner shape

        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}

        \pgfpathmoveto{\pgfpoint{.6\pgf@circ@res@left}{.3\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{.4\pgf@circ@res@left}{.3\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{.4\pgf@circ@res@left}{.3\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{.05\pgf@circ@res@left}{.3\pgf@circ@res@up}}
        \pgfpathmoveto{\pgfpoint{.4\pgf@circ@res@left}{.3\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{.25\pgf@circ@res@left}{.3\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{.25\pgf@circ@res@left}{.3\pgf@circ@res@up}}
        \pgfusepath{draw}

    }
}


%%% start european logic ports, from John Kormylo
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%

% #1 - name
% #2 - text inside
% #3 - number of inputs
% #4 = is it a not?
%
% option to add a circle for not-output, see
% https://github.com/circuitikz/circuitikz/issues/385
%
\newif\ifpgf@circ@european@port@circle\pgf@circ@european@port@circlefalse
\newif\ifpgf@circ@european@port@circle@ieee\pgf@circ@european@port@circle@ieeefalse
\ctikzset{tripoles/european not shape/.initial=ocirc}
\ctikzset{tripoles/european not symbol/.is choice}
\ctikzset{tripoles/european not symbol/triangle/.code={\pgf@circ@european@port@circlefalse}}
\ctikzset{tripoles/european not symbol/circle/.code={%
\pgf@circ@european@port@circletrue\pgf@circ@european@port@circle@ieeefalse\ctikzset{tripoles/european not shape=ocirc}}}
\ctikzset{tripoles/european not symbol/ieee circle/.code={%
\pgf@circ@european@port@circletrue\pgf@circ@european@port@circle@ieeetrue\ctikzset{tripoles/european not shape=notcirc}}}

\long\def\pgfcircdeclareeurologicport#1#2#3#4{
    \pgfdeclareshape{european #1 port}
    {
        \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
        \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
        \saveddimen{\boutshift}{%
            \edef\pgf@temp{not}
            \edef\pgf@circ@temp{#4}
            \ifx\pgf@temp\pgf@circ@temp % is a not
                \ifpgf@circ@european@port@circle
                    \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
                    \edef\pgf@temp{ocirc}
                    \edef\pgf@circ@temp{\ctikzvalof{tripoles/european not shape}}
                    \ifx\pgf@temp\pgf@circ@temp % it's ocirc
                        \pgfmathsetlength{\pgf@x}{2*\ctikzvalof{nodes width}*\pgf@circ@Rlen}
                    \else % it's ieee not circ
                        \pgf@circ@notradius % defined together with ieeestd ports
                        \pgf@x=2\pgf@circ@res@temp
                    \fi
                \else
                    \pgf@x=0pt
                \fi
            \else
                \pgf@x=0pt
            \fi
        }
        \savedmacro\resize{% automatic
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgf@circ@res@up = \ctikzvalof{tripoles/european #1 port/height}\pgf@circ@scaled@Rlen
            \pgf@circ@res@up = .5\pgf@circ@res@up
            \pgf@circ@res@down = -\pgf@circ@res@up
            \pgf@circ@res@right = \ctikzvalof{tripoles/european #1 port/width}\pgf@circ@scaled@Rlen
            \pgf@circ@res@right = .5\pgf@circ@res@right
            \pgf@circ@res@left = -\pgf@circ@res@right
        }%
        \savedmacro\inputs{% get number of inputs
            \pgf@circ@res@count=\pgfkeysvalueof{/tikz/number inputs}\relax%
            \ifnum\pgf@circ@res@count=0
                \pgf@circ@res@count=\ctikzvalof{tripoles/european #1 port/inputs}\relax%
            \fi
        \ifnum\pgf@circ@res@count<2 \pgf@circ@res@count=2\fi
    \ifnum\pgf@circ@res@count>16 \pgf@circ@res@count=16\fi
        \def\inputs{\the\pgf@circ@res@count}%
    }%
    \savedanchor\step{% 1/2 gap at edges
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@circ@res@step = \ctikzvalof{tripoles/european #1 port/height}\pgf@circ@scaled@Rlen
        \divide\pgf@circ@res@step by #3
        \pgfpoint{\pgf@circ@res@left}{\dimexpr\pgf@circ@res@up+0.5\pgf@circ@res@step}%
    }%
    \savedanchor\northeast{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}%
    \savedanchor\southwest{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}%
    \savedanchor\left{\pgfpoint{\ctikzvalof{tripoles/european #1 port/reserved}\pgf@circ@res@left}{0pt}}%
    \savedanchor\right{\pgfpoint{\ctikzvalof{tripoles/european #1 port/reserved}\pgf@circ@res@right}{0pt}}%
    \savedanchor\origin{\pgfpoint{\ctikzvalof{tripoles/european #1 port/origin}\pgf@circ@res@right}{0pt}}%

    \anchor{center}{\origin}% for backwards compatibility
    % the text anchor overlaps the logic symbol
    \anchor{text}{\pgfpoint{-.5\wd\pgfnodeparttextbox}{\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox}}%
    % create input anchors
    \ifnum#3=1\relax
        \anchor{in}{\southwest\pgfpoint{\pgf@x}{0pt}}% or \step
        \anchor{in 1}{\southwest\pgfpoint{\pgf@x}{0pt}}% or \step
        \anchor{bin}{\left\pgfpoint{\pgf@x}{0pt}}% or \step
        \anchor{bin 1}{\left\pgfpoint{\pgf@x}{0pt}}% or \step
    \else
        \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@european #1 port\endcsname{%
            \pgfmathloop%
            \ifnum\pgfmathcounter>#3
        \else%
            %\pgfutil@ifundefined{pgf@anchor@european #1 port@in \pgfmathcounter}{% redundant
            \expandafter\xdef\csname pgf@anchor@european #1 port@in \pgfmathcounter\endcsname{%
                \noexpand\pgf@circ@logicport@input{\pgfmathcounter}% defined above
            }%
            \expandafter\xdef\csname pgf@anchor@european #1 port@bin \pgfmathcounter\endcsname{%
                \noexpand\pgf@circ@logicport@baseinput{\pgfmathcounter}{4}{#1}% defined above
            }%
            %}{}%
            \repeatpgfmathloop%
        }
    \fi
    \anchor{out}{\northeast\pgf@y=0pt}
    \anchor{bout}{\right\advance\pgf@x by \boutshift\pgf@y=0pt}

    \anchor{left}{\left}% edges of component minus leads
    \anchor{right}{\right\advance\pgf@x by \boutshift\pgf@y=0pt}

    \anchor{north east}{\northeast}% see \Compass macro
    \anchor{south west}{\southwest}
    \anchor{north}{\pgfextracty{\pgf@circ@res@up}{\northeast}%
    \pgfpoint{0cm}{\pgf@circ@res@up}}
    \anchor{north west}{\pgfextracty{\pgf@circ@res@up}{\northeast}%
        \pgfextractx{\pgf@circ@res@left}{\southwest}%
    \pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
    \anchor{west}{\pgfextractx{\pgf@circ@res@left}{\southwest}%
    \pgfpoint{\pgf@circ@res@left}{0cm}}
    \anchor{south}{\pgfextracty{\pgf@circ@res@down}{\southwest}%
    \pgfpoint{0cm}{\pgf@circ@res@down}}
    \anchor{south east}{\pgfextracty{\pgf@circ@res@down}{\southwest}%
        \pgfextractx{\pgf@circ@res@right}{\northeast}%
    \pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down}}
    \anchor{east}{\pgfextractx{\pgf@circ@res@right}{\northeast}%
    \pgfpoint{\pgf@circ@res@right}{0cm}}

    \anchorborder{% this is used when the node is used as a path element
        \pgf@xa=\pgf@x
        \pgf@ya=\pgf@y
        \pgfextracty{\pgf@circ@res@up}{\northeast}
        \pgfextractx{\pgf@circ@res@right}{\northeast}
        \pgfpointborderrectangle{\pgfpoint{\pgf@xa}{\pgf@ya}}
            {\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
    }
    \pgf@circ@draw@component{
        \pgf@circ@setcolor
        \pgfstartlinewidth=\pgflinewidth
        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgfpathrectanglecorners
            {\pgfpoint{\ctikzvalof{tripoles/european #1 port/reserved}\pgf@circ@res@left}{\pgf@circ@res@up}}
            {\pgfpoint{\ctikzvalof{tripoles/european #1 port/reserved}\pgf@circ@res@right}{\pgf@circ@res@down}}
            \pgf@circ@draworfill
        \endpgfscope
        \ifpgfcirc@draw@input@leads
            %input leads
            \ifnum#3=1\relax
                \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}%
                \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/european #1 port/reserved}\pgf@circ@res@left}{0pt}}%
            \else
                \pgfextracty{\pgf@circ@res@temp}{\step}%
                \pgf@circ@res@step = \dimexpr 2\pgf@circ@res@temp -2\pgf@circ@res@up\relax
                %\pgf@circ@res@count = #3\relax% redundant
                \loop\ifnum\pgf@circ@res@count>0
                    \advance\pgf@circ@res@temp by -\pgf@circ@res@step
                    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@temp}}%
                    \pgfpathlineto{\pgfpoint
                        {\ctikzvalof{tripoles/european #1 port/reserved}\pgf@circ@res@left}
                    {\pgf@circ@res@temp}}
                    \advance\pgf@circ@res@count by -1
                \repeat
            \fi
            \pgfusepath{draw}
        \fi
        %
        \ifpgfcirc@draw@output@leads
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
            \pgfpathlineto{ \pgfpoint{\ctikzvalof{tripoles/european #1 port/reserved}\pgf@circ@res@right}{0pt}}
            \pgfusepath{draw}
        \fi
        %
        \edef\pgf@temp{not}
        \edef\pgf@circ@temp{#4}
        \ifx\pgf@temp\pgf@circ@temp % is a not
            \ifpgf@circ@european@port@circle
                \pgfscope
                    \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
                    \pgftransformxshift{\ctikzvalof{tripoles/european #1 port/reserved}\pgf@circ@res@right}
                    % reset  linewidth for IEEE ports, otherwise they will multiply...
                    \ifpgf@circ@european@port@circle@ieee\pgfsetlinewidth{\pgfstartlinewidth}\fi
                    \pgfnode{\ctikzvalof{tripoles/european not shape}}{west}{}{NOT}{\pgfusepath{stroke}}
                \endpgfscope
            \else
                \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/european #1 port/not width}\pgf@circ@res@right}{0pt}}
                \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/european #1 port/reserved}\pgf@circ@res@right}%
                {\ctikzvalof{tripoles/european #1 port/not height}\pgf@circ@res@up}}
                \pgfusepath{draw}
            \fi
        \fi
        \pgf@circ@text@strokecolor
        \pgfpathmoveto{\pgfpointorigin}
        \pgfscope
            % text is always in standard direction
            \pgftransformresetnontranslations
            \pgftext{\ctikzvalof{european ports font}#2}%
        \endpgfscope
        }
    }
}
\pgfcircdeclareeurologicport{and}{\&}{\pgf@circ@res@count}{}
\pgfcircdeclareeurologicport{or}{$\ge 1$}{\pgf@circ@res@count}{}
\pgfcircdeclareeurologicport{xor}{$=1$}{\pgf@circ@res@count}{}
\pgfcircdeclareeurologicport{not}{$1$}{1}{not}
\pgfcircdeclareeurologicport{buffer}{$1$}{1}{}
\pgfcircdeclareeurologicport{nand}{\&}{\pgf@circ@res@count}{not}
\pgfcircdeclareeurologicport{nor}{$\ge 1$}{\pgf@circ@res@count}{not}
\pgfcircdeclareeurologicport{xnor}{$=1$}{\pgf@circ@res@count}{not}
\pgfcircdeclareeurologicport{blank}{}{\pgf@circ@res@count}{}
\pgfcircdeclareeurologicport{blank not}{}{\pgf@circ@res@count}{not}
%% end european logic ports
% %>>>

%% IEEE standard logic ports module%<<<

%%
%% Original multi-input code from John Kormylo at tex.stackexchange.com
%% Help by TheTeXnician <38565529+TheTeXnician@users.noreply.github.com>
%% Suggested idea and example code by Jason Sachs <jmsachs@gmail.com>
%% Please see https://github.com/circuitikz/circuitikz/issues/383 for a lot of details
%% Most of the code, all errors and bugs by Romano Giannetti <romano.giannetti@gmail.com>
%% Everything is in the same place here --- more or less; first step to move towards
%% a module interface for circutikz
%%

% base settings for ieeestd ports.

\ctikzset{ieeestd ports/.is family}
% baselen is relative to pgfcirc@Rlen as ever; scaled if the class says so.
% the value of 0.4 is the standard pin distance for a port with height=num pins
% and matches the chip distance
\ctikzset{ieeestd ports/baselen/.initial=0.4}
% these are in term of baselen; width depends on height (fixed proportions)
\ctikzset{ieeestd ports/height/.initial=2}
\ctikzset{ieeestd ports/pin length/.initial=0.7}
% the standard "not" circle should be 1/6.5 of height (diameter);
% so radius/baselen=1/3.25/2    --- using 0.1 and no scaling is as a pole
\ctikzset{ieeestd ports/not radius/.initial=0.154}
\ctikzset{ieeestd ports/not radius fill/.initial=1}% change ony if you know why
% the suggested xnor distance is is 1.24, so 1.25/3.25/2
% xor/xnor leads go full in in IEEE; let this be optional
\ctikzset{ieeestd ports/xor bar distance/.initial=0.192}%
\ctikzset{ieeestd ports/xor leads in/.initial=1}%
%
% base size of a small external schmitt symbol
%
\ctikzset{ieeestd ports/schmitt symbol size/.initial=0.3}%
%
% input management
% we are using the same /tikz/number inputs than the legacy ports
%
\tikzset{/tikz/inner inputs/.initial=0} % using 0 means that all inputs are inner
%
% integrate with the other logic ports
%
%
\newif\ifpgf@circuit@ieeelogicport\pgf@circuit@ieeelogicportfalse
\ctikzset{logic ports/ieee/.code= {%
    \pgf@circuit@ieeelogicporttrue
    \pgf@circuit@europeanlogicportfalse
    \tikzset{and port/.style={shape=ieeestd and port}}%
    \tikzset{or port/.style={shape=ieeestd or port}}%
    \tikzset{xor port/.style={shape=ieeestd xor port}}%
    \tikzset{buffer port/.style={shape=ieeestd buffer port}}%
    \tikzset{not port/.style={shape=ieeestd not port}}%
    \tikzset{nand port/.style={shape=ieeestd nand port}}%
    \tikzset{nor port/.style={shape=ieeestd nor port}}%
    \tikzset{xnor port/.style={shape=ieeestd xnor port}}%
    \tikzset{schmitt port/.style={shape=ieeestd schmitt port}}%
    \tikzset{invschmitt port/.style={shape=ieeestd invschmitt port}}%
}}
% add code to be compatible with the other ports
\ctikzset{logic ports/european/.add code={\pgf@circuit@ieeelogicportfalse}}
\ctikzset{logic ports/american/.add code={\pgf@circuit@ieeelogicportfalse}}
\tikzset{ieee ports/.style = {\circuitikzbasekey/logic ports = ieee}}
%
% the base angle for the or port. See the drawings. This will not change with height
%
\pgfmathsetmacro{\pgf@circ@orangle}{atan(3.25/6.5)}
% \typeout{ANGLE-IS\space\pgf@circ@orangle}
\def\pgf@circ@ieeeport@input#1% #1 = \pgfmathcounter
{%
    \ifnum#1>\inputs
        \PackageError{circuitikz}{%
            You requested input pin #1 for logic port shape \thisshape\space \MessageBreak
            which has been defined with \inputs\space pins%
        }{Please check the manual about logic ports; if you press return I'll try to continue}
    \fi
    \pgfmathsetlength{\pgf@circ@res@up}{(\inputs/2)*\pind+0.5*\pind}% pin "0", above the rack/port
    \pgfextractx{\pgf@circ@res@left}{\bodyleft}
    \pgf@circ@res@step=\pind
    \pgf@y=\pgf@circ@res@up\advance\pgf@y by -#1\pgf@circ@res@step\relax
    \pgf@x=\pgf@circ@res@left\advance\pgf@x by -\pinlen
}%

% #1 = \pgfmathcounter #2=type
% type is 1 for and,nand; 2 for or,nor; 3 for xor,xnor
\def\pgf@circ@ieeeport@baseinput#1#2%
{%
    \ifnum#1>\inputs
        \PackageError{circuitikz}{%
            You requested border input pin #1 for logic port shape \thisshape\space \MessageBreak
            which has been defined with \inputs\space pins%
        }{Please check the manual about logic ports; if you press return I'll try to continue}
    \fi
    % Find the vertical position (this is the same for any port)
    \pgfmathsetlength{\pgf@circ@res@up}{(\inputs/2)*\pind+0.5*\pind}% pin "0", above the rack/port
    \pgf@circ@res@step=\pind\advance\pgf@circ@res@up by -#1\pgf@circ@res@step\relax
    % rack (extended) pins; they are the same for all the ports
    % call K = (inputs-inner)/2, rounded up; pins on the rack are:
    %      above: 1..K (included)
    %      below: inputs-K..inputs
    % Find the pins on the rack; they are 1...
    \pgf@circ@count@a=\numexpr (\inputs - \inners)/2\relax       % =K; numexpr rounds up!
    \pgf@circ@count@b=\numexpr \inputs - \pgf@circ@count@a +1 \relax % =inputs - K +1
    % border anchors for rack should be ok
    \pgfextractx{\pgf@circ@res@left}{\topleft}
    \pgfextractx{\pgf@circ@res@right}{\bodyleft}
    \pgf@y=\pgf@circ@res@up\pgf@x=\pgf@circ@res@left
    % we have finished if we are in the rack
    \ifnum #1 > \pgf@circ@count@a \ifnum #1 < \pgf@circ@count@b
        % we are on the inner ports; we have to do the hard work here
        % and and nand
        \ifnum #2=1
            \relax % It's an and/nand, all border ports are on the rack line
        \fi
        % or and nor
        \ifnum #2=2
            \pgfmathsetlength{\pgf@x}{\pgf@circ@res@right-2*\stdH*(1-cos(atan(\pgf@circ@res@up/(2*\stdH))))}
        \fi
        % xor and xnor
        \ifnum #2=3\relax
            \pgfmathsetlength{\pgf@x}{\pgf@circ@res@right-\xorbar-2*\stdH*(1-cos(atan(\pgf@circ@res@up/(2*\stdH))))}
        \fi
    \fi\fi
}%
% inner base ports for xor types port
% #1 = \pgfmathcounter #2=type
% type is 1 for and,nand; 2 for or,nor; 3 for xor,xnor
\def\pgf@circ@ieeeport@innerbaseinput#1%
{%
    \ifnum#1>\inputs
        \PackageError{circuitikz}{%
            You requested border input pin #1 for logic port shape \thisshape\space \MessageBreak
            which has been defined with \inputs\space pins%
        }{Please check the manual about logic ports; if you press return I'll try to continue}
    \fi
    % Find the vertical position (this is the same for any port)
    \pgfmathsetlength{\pgf@circ@res@up}{(\inputs/2)*\pind+0.5*\pind}% pin "0", above the rack/port
    \pgf@circ@res@step=\pind\advance\pgf@circ@res@up by -#1\pgf@circ@res@step\relax
    % rack (extended) pins; they are the same for all the ports
    % call K = (inputs-inner)/2, rounded up; pins on the rack are:
    %      above: 1..K (included)
    %      below: inputs-K..inputs
    % Find the pins on the rack; they are 1...
    \pgf@circ@count@a=\numexpr (\inputs - \inners)/2\relax       % =K; numexpr rounds up!
    \pgf@circ@count@b=\numexpr \inputs - \pgf@circ@count@a +1 \relax % =inputs - K +1
    % border anchors for rack should be ok
    \pgfextractx{\pgf@circ@res@left}{\topleft}
    \pgfextractx{\pgf@circ@res@right}{\bodyleft}
    \pgf@y=\pgf@circ@res@up\pgf@x=\pgf@circ@res@left
    % we have finished if we are in the rack
    \ifnum #1 > \pgf@circ@count@a \ifnum #1 < \pgf@circ@count@b
        % we are on the inner ports; we have to do the hard work here
        \pgfmathsetlength{\pgf@x}{\pgf@circ@res@right-2*\stdH*(1-cos(atan(\pgf@circ@res@up/(2*\stdH))))}
    \fi\fi
}%

%%% macro to find basic lenghts --- they leave it in \pgf@circ@res@temp
\def\pgf@circ@ieeestd@baselen{%
    \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
    \pgfmathsetlength{\pgf@circ@res@temp}{\ctikzvalof{ieeestd ports/baselen}*\pgf@circ@scaled@Rlen}
}
\def\pgf@circ@ieeestd@stdH{%
    \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
    \pgfmathsetlength{\pgf@circ@res@temp}{0.5*\ctikzvalof{ieeestd ports/baselen}*
        \ctikzvalof{ieeestd ports/height}*\pgf@circ@scaled@Rlen}
}
\def\pgf@circ@ieeestd@pinlen{%
    \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
    \pgfmathsetlength{\pgf@circ@res@temp}{\ctikzvalof{ieeestd ports/baselen}*
        \ctikzvalof{ieeestd ports/pin length}*\pgf@circ@scaled@Rlen}
}
\def\pgf@circ@ieeestd@xorbar{%
    \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
    \pgfmathsetlength{\pgf@circ@res@temp}{\ctikzvalof{ieeestd ports/baselen}*
        \ctikzvalof{ieeestd ports/xor bar distance}*\pgf@circ@scaled@Rlen}
}
%% Not circle
\def\pgf@circ@notradius{
    \pgf@circ@ieeestd@stdH % got the standard length. Notice that his is 3.25H for IEEE
    \pgfmathsetlength{\pgf@circ@res@temp}{\ctikzvalof{ieeestd ports/not radius}*\pgf@circ@res@temp}
    % \typeout{NOTRADIUS\space\the\pgf@circ@res@temp}
}
%% Find (in ...@other) the height of the rack
\def\pgf@circ@findrackH{%
    \pgf@circ@count@a=\pgfkeysvalueof{/tikz/number inputs}\relax%
    \pgf@circ@count@b=\pgfkeysvalueof{/tikz/inner inputs}\relax%
    \ifnum\pgf@circ@count@a=0 \pgf@circ@count@a=2\fi  % default pins
    \ifnum\pgf@circ@count@a<2 \pgf@circ@count@a=2\fi %
    \ifnum\pgf@circ@count@b=0 \pgf@circ@count@b=\pgf@circ@count@a\fi%
    \pgf@circ@ieeestd@stdH
    \multiply\pgf@circ@res@temp by 2\relax% full height
    \divide\pgf@circ@res@temp by \pgf@circ@count@b % the pin spacing
    \pgfmathsetlength{\pgf@circ@res@other}{(\pgf@circ@count@a/2)*\pgf@circ@res@temp} %top of the rack/port
    % \typeout{RACK-\thisshape\space\the\pgf@circ@res@other}
}
%%
\def\pgf@circ@find@ieeeport@up{% leave it in up
    % Normal port limits
    \pgf@circ@ieeestd@stdH
    \pgfmathsetlength{\pgf@circ@res@up}{\pgf@circ@res@temp}
    % rack top size
    \pgf@circ@findrackH
    \ifdim\pgf@circ@res@other > \pgf@circ@res@up
        \pgf@circ@res@up=\pgf@circ@res@other
    \else
    \fi
}
\def\pgf@circ@find@ieeeport@left#1{% leave it in left; #1 is type
    % Normal port limits
    \pgf@circ@ieeestd@stdH
    \pgfmathsetlength{\pgf@circ@res@left}{8*\pgf@circ@res@temp/6.5}
    \pgf@circ@res@right=\pgf@circ@res@left % save the border value
    \pgf@circ@res@step=\pgf@circ@res@temp % save the stdH value
    \pgf@circ@ieeestd@pinlen\advance\pgf@circ@res@left by \pgf@circ@res@temp
    % \typeout{LEFT1-\thisshape\space L\space\the\pgf@circ@res@left\space R\space\the\pgf@circ@res@right}
    % this is the normal left border
    % For the or or xor port, the limit can be the pointy thing (in case the
    % pinlen is zero or too small)
    % add to the body margin the or/nor peak:
    \ifnum #1 > 1\relax% "or", "nor", "xor", "xnor" gates.
        \pgfmathsetlength{\pgf@circ@res@other}{2*\pgf@circ@res@step*(1-cos(\pgf@circ@orangle))}
        \advance\pgf@circ@res@right by \pgf@circ@res@other
        % \typeout{LEFT2-\thisshape\space L\space\the\pgf@circ@res@left\space R\space\the\pgf@circ@res@right}
    \fi
    % add to the body margin the xor/xnor distance
    \ifnum #1 = 3\relax% "xor" or "xnor" gates.
        \pgf@circ@ieeestd@xorbar
        \advance\pgf@circ@res@right by \pgf@circ@res@temp
        % \typeout{LEFT3-\thisshape\space L\space\the\pgf@circ@res@left\space R\space\the\pgf@circ@res@right}
    \fi
    % and if this exceeds the normal margin, this is it
    % \typeout{LEFT4-\thisshape\space L\space\the\pgf@circ@res@left\space R\space\the\pgf@circ@res@right}
    \ifdim \pgf@circ@res@right > \pgf@circ@res@left
        \pgf@circ@res@left=\pgf@circ@res@right
    \fi
    % \typeout{LEFT5-\thisshape\space L\space\the\pgf@circ@res@left\space R\space\the\pgf@circ@res@right}
    \pgf@circ@res@left=-\pgf@circ@res@left
}
\def\pgf@circ@find@ieeeport@right#1{% leave it in right; #1 is plain or negated
    % Normal port limits
    \pgf@circ@ieeestd@stdH
    \pgfmathsetlength{\pgf@circ@res@right}{8*\pgf@circ@res@temp/6.5}
    \pgf@circ@notradius
    \pgfmathsetlength{\pgf@circ@res@other}{\pgf@circ@res@right+2*#1*\pgf@circ@res@temp}
    \pgf@circ@ieeestd@pinlen\advance\pgf@circ@res@right by \pgf@circ@res@temp
    \ifdim\pgf@circ@res@other > \pgf@circ@res@right
        \pgf@circ@res@right=\pgf@circ@res@other
    \fi
}
\def\pgf@circ@find@ieeeport@not@right#1{% leave it in right; #1 is plain or negated
    % Normal port limits
    \pgf@circ@ieeestd@stdH
    % notice 0.8660254 is cos(30)
    \pgfmathsetlength{\pgf@circ@res@right}{0.8660254*\pgf@circ@res@temp}
    \pgf@circ@notradius
    \pgfmathsetlength{\pgf@circ@res@other}{\pgf@circ@res@right+2*#1*\pgf@circ@res@temp}
    \pgf@circ@ieeestd@pinlen\advance\pgf@circ@res@right by \pgf@circ@res@temp
    \ifdim\pgf@circ@res@other > \pgf@circ@res@right
        \pgf@circ@res@right=\pgf@circ@res@other
    \fi
}


%%% ieeestd multi-input ports
%%% #1: name
%%% #2: type: 1 for and,nand; 2 for or,nor; 3 for xor,xnor
%%% #3: polarity: 0 for direct, 1 for inverted (not at the output)
%%% #4: drawing for the port
\long\def\pgfcircdeclareieeeport#1#2#3#4{%
    \pgfdeclareshape{ieeestd #1 port}%
    {%
        \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
        \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
        \savedmacro{\thisshape}{\def\thisshape{\tikz@fig@name}}
        \saveddimen{\baselen}{%
            \pgf@circ@ieeestd@baselen\pgf@x=\pgf@circ@res@temp
        }
        \saveddimen{\stdH}{% This is HALF the height of the inner port
            \pgf@circ@ieeestd@stdH\pgf@x=\pgf@circ@res@temp
        }
        \saveddimen{\notdiameter}{
            \pgf@circ@notradius\pgf@x=2\pgf@circ@res@temp
        }
        \saveddimen{\pind}{% pin distance;
            \pgf@circ@count@a=\pgfkeysvalueof{/tikz/number inputs}\relax%
            \pgf@circ@count@b=\pgfkeysvalueof{/tikz/inner inputs}\relax%
            \ifnum\pgf@circ@count@a=0 \pgf@circ@count@a=2\fi  % default pins
            \ifnum\pgf@circ@count@a<2 \pgf@circ@count@a=2\fi %
            \ifnum\pgf@circ@count@b=0 \pgf@circ@count@b=\pgf@circ@count@a\fi%
            \pgf@circ@ieeestd@stdH\pgf@x=2\pgf@circ@res@temp % full height
            \divide\pgf@x by \pgf@circ@count@b
        }
        \saveddimen{\pinlen}{%
            \pgf@circ@ieeestd@pinlen\pgf@x=\pgf@circ@res@temp
        }
        \saveddimen{\xorbar}{%
            \pgf@circ@ieeestd@xorbar\pgf@x=\pgf@circ@res@temp
        }
        % anchors for the body (no pins included here)
        \savedanchor{\bodyleft}{% This DOES NOT take into account the pointy or/xor thing
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{-8*\pgf@circ@res@temp/6.5}
            \pgfmathsetlength{\pgf@y}{\pgf@circ@res@temp}
        }
        \savedanchor{\topleft}{%
            \pgf@circ@ieeestd@xorbar\pgf@circ@res@right=\pgf@circ@res@temp % save \xorbar
            \pgf@circ@ieeestd@stdH
            \pgf@circ@res@other=0pt\relax
            \ifnum #2 = 2\relax% "or" or "nor" gates.
                \pgfmathsetlength{\pgf@circ@res@other}{2*\pgf@circ@res@temp*(1-cos(\pgf@circ@orangle))}
            \fi
            \ifnum #2 = 3\relax% "xor" or "xnor" gates.
                \pgfmathsetlength{\pgf@circ@res@other}{2*\pgf@circ@res@temp*(1-cos(\pgf@circ@orangle))
                    +\pgf@circ@res@right}
            \fi
            \pgfmathsetlength{\pgf@x}{-8*\pgf@circ@res@temp/6.5-\pgf@circ@res@other}
            \pgfmathsetlength{\pgf@y}{\pgf@circ@res@temp}
        }
        \savedanchor{\bodyright}{% This DOES NOT take into account the "NOT" circle
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{8*\pgf@circ@res@temp/6.5}
            \pgfmathsetlength{\pgf@y}{-\pgf@circ@res@temp}
        }
        \savedanchor{\bottomright}{% This DOES take into account the "NOT" circle
            \pgf@circ@notradius\pgf@circ@res@other=\pgf@circ@res@temp
            \pgf@circ@ieeestd@stdH
            % #3 is =1 if the port is a negated output one
            \pgfmathsetlength{\pgf@x}{8*\pgf@circ@res@temp/6.5+#3*2*\pgf@circ@res@other}
            \pgfmathsetlength{\pgf@y}{-\pgf@circ@res@temp}
        }
        % geographical anchors --- must be rectangulars!
        \savedanchor{\northwest}{%
            \pgf@circ@find@ieeeport@up
            \pgf@circ@find@ieeeport@left{#2}
            % \typeout{ANCH5-\thisshape\space L\space\the\pgf@circ@res@left\space U\space\the\pgf@circ@res@up}
            \pgf@x=\pgf@circ@res@left
            \pgf@y=\pgf@circ@res@up
        }
        \savedanchor{\southwest}{%
            \pgf@circ@find@ieeeport@up
            \pgf@circ@find@ieeeport@left{#2}
            \pgf@x=\pgf@circ@res@left
            \pgf@y=-\pgf@circ@res@up
        }
        \savedanchor{\southeast}{%
            \pgf@circ@find@ieeeport@up
            \pgf@circ@find@ieeeport@right{#3}
            \pgf@x=\pgf@circ@res@right
            \pgf@y=-\pgf@circ@res@up
        }
        \savedanchor{\northeast}{%
            \pgf@circ@find@ieeeport@up
            \pgf@circ@find@ieeeport@right{#3}
            \pgf@x=\pgf@circ@res@right
            \pgf@y=\pgf@circ@res@up
        }
        \savedmacro\inputs{% get number of inputs
            \pgf@circ@count@a=\pgfkeysvalueof{/tikz/number inputs}\relax
            \ifnum\pgf@circ@count@a=0\pgf@circ@count@a=2\fi    % default
            \ifnum\pgf@circ@count@a<2 \pgf@circ@count@a=2\fi   % minimum pins
            % \ifnum\pgf@circ@count@a>16 \pgf@circ@count@a=16\fi
            \def\inputs{\the\pgf@circ@count@a}%
        }%
        \savedmacro\inners{% get number of "inner" inputs (for racks)
            \pgf@circ@count@a=\pgfkeysvalueof{/tikz/number inputs}\relax
            \pgf@circ@count@b=\pgfkeysvalueof{/tikz/inner inputs}\relax
            \ifnum\pgf@circ@count@a=0 \pgf@circ@count@a=2\fi  % default pins
            \ifnum\pgf@circ@count@a<2 \pgf@circ@count@a=2\fi  % minimum pins
            \ifnum\pgf@circ@count@b=0 \pgf@circ@count@b=\pgf@circ@count@a\fi
            % \typeout{INNER is \the\pgf@circ@count@b}%
            % \ifnum\pgf@circ@count@a>16 \pgf@circ@count@a=16\fi
            \def\inners{\the\pgf@circ@count@b}%
        }%
        \anchor{center}{\pgfpointorigin}
        \anchor{text}{
            \ifpgf@circ@center@text
                \pgfpoint{-.5\wd\pgfnodeparttextbox}{\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox}
            \else
                \pgf@circ@ieeestd@stdH
                \pgfmathsetlength{\pgf@circ@res@left}{-8*\pgf@circ@res@temp/6.5} % left border
                \pgfpoint{\pgf@circ@res@left + \ctikzvalof{left text distance}}{\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox}
            \fi
        }
        % create input anchors
        \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@ieeestd #1 port\endcsname{%
            \pgf@circ@count@a=\inputs
            \pgfmathloop%
            \ifnum\pgfmathcounter>\pgf@circ@count@a%
            \else%
            %\pgfutil@ifundefined{pgf@anchor@american #1 port@in \pgfmathcounter}{%
            \expandafter\xdef\csname pgf@anchor@ieeestd #1 port@in \pgfmathcounter\endcsname{%
                \noexpand\pgf@circ@ieeeport@input{\pgfmathcounter}% defined above
            }%
            \expandafter\xdef\csname pgf@anchor@ieeestd #1 port@bin \pgfmathcounter\endcsname{%
                \noexpand\pgf@circ@ieeeport@baseinput{\pgfmathcounter}{#2}% defined above
            }%
            \ifnum #2 = 3\relax % xor/xnor inner border pins
                \expandafter\xdef\csname pgf@anchor@ieeestd #1 port@ibin \pgfmathcounter\endcsname{%
                    \noexpand\pgf@circ@ieeeport@innerbaseinput{\pgfmathcounter}% defined above
                }%
            \fi
            %}{}%
            \repeatpgfmathloop%
        }
        % output anchor
        \anchor{out}{%
            \pgfextractx{\pgf@circ@res@other}{\bodyright}   %body border, without not ball
            \advance\pgf@circ@res@other by\pinlen
            \pgfextractx{\pgf@circ@res@temp}{\bottomright}   %body + ball border
            \ifdim \pgf@circ@res@temp > \pgf@circ@res@other
                \pgf@circ@res@other = \pgf@circ@res@temp % do not enter in the ball...
            \fi
            \pgf@x=\pgf@circ@res@other\pgf@y=0pt
        }
        \anchor{bout}{\bottomright\pgf@y=0pt}

        \anchor{body right}{\bodyright\pgf@y=0pt}
        \anchor{right}{\bottomright\pgf@y=0pt}
        \anchor{body left}{\bodyleft\pgf@y=0pt}% central edge of the body
        \anchor{left}{% central edge of the component
            \bodyleft\pgf@y=0pt
            \ifnum #2=3\relax
                \advance\pgf@x by -\xorbar
            \fi
        }
        \anchor{up}{%
            \bodyleft
            \ifnum #2 > 1 % pointy shapes
                \pgf@circ@ieeestd@stdH
                % horizontal coordinate where the right semicircle starts
                \pgfmathsetlength{\pgf@circ@res@other}{-8*\pgf@circ@res@temp/6.5+2*\pgf@circ@res@temp*cos(\pgf@circ@orangle)}
                % vertical drop of the circle at the above coordinate
                \pgfmathsetlength{\pgf@circ@res@step}{2*\pgf@circ@res@temp*(1-cos(atan(\pgf@circ@res@other/(2*\pgf@circ@res@temp))))}
                \advance \pgf@y by -\pgf@circ@res@step
            \fi
            \pgf@x=0pt
        }
        \anchor{down}{%
            \bodyleft
            \ifnum #2 > 1 % pointy shapes
                \pgf@circ@ieeestd@stdH
                % horizontal coordinate where the right semicircle starts
                \pgfmathsetlength{\pgf@circ@res@other}{-8*\pgf@circ@res@temp/6.5+2*\pgf@circ@res@temp*cos(\pgf@circ@orangle)}
                % vertical drop of the circle at the above coordinate
                \pgfmathsetlength{\pgf@circ@res@step}{2*\pgf@circ@res@temp*(1-cos(atan(\pgf@circ@res@other/(2*\pgf@circ@res@temp))))}
                \advance \pgf@y by -\pgf@circ@res@step
            \fi
            \pgf@y=-\pgf@y\pgf@x=0pt
        }

        % geographical anchors
        \anchor{nw}{\northwest}
        \anchor{ne}{\northeast}
        \anchor{se}{\southeast}
        \anchor{sw}{\southwest}
        \anchor{north west}{\northwest}
        \anchor{north east}{\northeast}
        \anchor{south east}{\southeast}
        \anchor{south west}{\southwest}
        % over 0,0 even if asymmetric
        % will break if the geocoords are not rectangular
        \anchor{n}{\northwest\pgf@x=0pt\relax}
        \anchor{e}{\northeast\pgf@y=0pt\relax}
        \anchor{s}{\southwest\pgf@x=0pt\relax}
        \anchor{w}{\northwest\pgf@y=0pt\relax}
        \anchor{north}{\northwest\pgf@x=0pt\relax}
        \anchor{east}{\northeast\pgf@y=0pt\relax}
        \anchor{south}{\southwest\pgf@x=0pt\relax}
        \anchor{west}{\northwest\pgf@y=0pt\relax}

        \pgf@circ@draw@component{
            \pgfscope
                \pgf@circ@setcolor
                #4%
            \endpgfscope
            % output lead:
            \pgfextractx{\pgf@circ@res@right}{\bottomright} %body+ball border
            \pgfextractx{\pgf@circ@res@other}{\bodyright}   %body border, without "not" ball
            \advance\pgf@circ@res@other by \pinlen\relax
            \ifdim \pgf@circ@res@other > \pgf@circ@res@right
                \ifpgfcirc@draw@output@leads
                    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
                    \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{0pt}}
                    \pgfusepath{draw}
                \fi
            \fi
            \ifnum #3=1\relax\pgfscope
                \pgftransformshift{\pgfpoint{\pgf@circ@res@right}{0pt}}
                \pgfnode{notcirc}{east}{}{\thisshape-not}{\pgfusepath{stroke}}
            \endpgfscope\fi
        }
    }
}

%%% #1 direct name #2 negated name #3 type #4 drawing (without output)
\long\def\pgfcircdeclareieeeportpair#1#2#3#4{%
    \pgfcircdeclareieeeport{#1}{#3}{0}{#4}% direct
    \pgfcircdeclareieeeport{#2}{#3}{1}{#4}% negated
}
%
% ieeestd "and" and "nand"
%
\pgfcircdeclareieeeportpair{and}{nand}{1}{%
    \pgf@circ@count@a = \inputs\relax
    \pgfmathsetlength{\pgf@circ@res@up}{(\inputs/2)*\pind} %top of the rack/port
    \pgfmathsetlength{\pgf@circ@res@temp}{\pgf@circ@res@up+0.5*\pind}
    \pgfextractx{\pgf@circ@res@left}{\bodyleft}
    \pgfextracty{\pgf@circ@res@down}{\bodyleft}
    \ifpgfcirc@draw@input@leads
        %input leads --- all the same for AND ports
        \loop\ifnum\pgf@circ@count@a>0
        \advance\pgf@circ@res@temp by -\pind
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@temp}}%
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pinlen}{\pgf@circ@res@temp}}%
        \advance\pgf@circ@count@a by -1
        \repeat
    \fi
    \pgfusepath{draw}
    %% Body. let's start from the top left
    \pgfscope
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfpathmoveto{\pgfpoint{-8*\stdH/6.5}{\stdH}}
        \pgfpathlineto{\pgfpoint{1.5*\stdH/6.5}{\stdH}}
        \pgfpatharc{90}{-90}{\stdH}
        \pgfpathlineto{\pgfpoint{-8*\stdH/6.5}{-\stdH}}
        \pgfpathclose
        \pgf@circ@draworfill
        % rack now; skip if not needed.
        % \typeout{WHAT\space \inputs\space \inners}
        \ifnum\inputs>\inners
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@up}}
            \pgfusepath{draw}
        \fi
    \endpgfscope
}
%
% or/nor and xor/xnor are practically the same. Let factor out everything
% the argument #1 is put just before the drawing of the inner pins
% the argument #2 is put after the drawing of the body
%
\long\def\pgf@circ@ieeeport@orxor#1#2{%
    \pgf@circ@count@a = \inputs\relax
    \pgfmathsetlength{\pgf@circ@res@up}{(\inputs/2)*\pind} %top of the rack/port
    \pgfmathsetlength{\pgf@circ@res@temp}{\pgf@circ@res@up+0.5*\pind}
    \pgfextractx{\pgf@circ@res@left}{\bodyleft}
    \pgfextracty{\pgf@circ@res@down}{\bodyleft}
    % rack (extended) pins; they are the same for all the ports
    % call K = (inputs-inner)/2, rounded up; pins on the rack are:
    %      above: 1..K (included)
    %      below: inputs-K..inputs
    % Find the pins on the rack; they are 1...
    \pgf@circ@count@b=\numexpr (\inputs - \inners)/2\relax       % =K; numexpr rounds up!
    \pgf@circ@count@c=\numexpr \inputs - \pgf@circ@count@b +1 \relax % =inputs - K +1
    \ifpgfcirc@draw@input@leads
        %input leads --- for or ports
        \loop\ifnum\pgf@circ@count@a>0
        \pgfextractx{\pgf@circ@res@right}{\topleft}
        \advance\pgf@circ@res@temp by -\pind
        % this is the height; let's find the "right" position
        \ifnum \pgf@circ@count@a > \pgf@circ@count@b \ifnum \pgf@circ@count@a < \pgf@circ@count@c
            % inner pins
            % \typeout{INNER\space\pgf@circ@count@a}
            \pgfmathsetlength{\pgf@circ@res@right}{\pgf@circ@res@left-2*\stdH*(1-cos(atan(\pgf@circ@res@temp/(2*\stdH))))}
            % hook for xor/xnor
            #1%
        \fi\fi
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@temp}}%
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pinlen}{\pgf@circ@res@temp}}%
        \pgfusepath{draw}
        \advance\pgf@circ@count@a by -1
        \repeat
    \fi
    %% Body. let's start from the top left
    \pgfscope
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        % it should start here, but with this trick the close comes out better.
        % \pgfpathmoveto{\pgfpoint{-8*\stdH/6.5-2*\stdH*(1-cos(\pgf@circ@orangle))}{\stdH}}
        \pgfpathmoveto{\pgfpoint{-8*\stdH/6.5}{\stdH}}
        \pgfpathlineto{\pgfpoint{8*\stdH/6.5-2*\stdH*cos(\pgf@circ@orangle)}{\stdH}} %
        \pgfpatharcto{2*\stdH}{2*\stdH}{0}{0}{0}{\pgfpoint{8*\stdH/6.5}{0pt}}
        \pgfpatharcto{2*\stdH}{2*\stdH}{0}{0}{0}{\pgfpoint{8*\stdH/6.5-2*\stdH*cos(\pgf@circ@orangle)}{-\stdH}}
        \pgfpathlineto{\pgfpoint{-8*\stdH/6.5-2*\stdH*(1-cos(\pgf@circ@orangle))}{-\stdH}}
        %% this should be 2 and 2; but the round part is not a perfect circle that way
        %% so the 2.15 is ajusted "by taste" to touch the anchors exactly.
        \pgfpatharcto{2*\stdH}{2.2*\stdH}{0}{0}{1}{\pgfpoint{-8*\stdH/6.5-2*\stdH*(1-cos(\pgf@circ@orangle))}{\stdH}}
        \pgfpathclose
        \pgf@circ@draworfill
        % hook for xor/xnor
        #2%
        % rack now; skip if not needed.
        % \typeout{WHAT\space \inputs\space \inners}
        \ifnum\inputs>\inners
            \pgfextractx{\pgf@circ@res@left}{\topleft}
            \pgfextracty{\pgf@circ@res@down}{\topleft}
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@up}}
            \pgfusepath{draw}
        \fi
    \endpgfscope
}
%
% ieeestd "or" and "nor"
%
\pgfcircdeclareieeeportpair{or}{nor}{2}{%
    \pgf@circ@ieeeport@orxor{}{}
}
\pgfcircdeclareieeeportpair{xor}{xnor}{3}{%
    \pgf@circ@ieeeport@orxor{
        \edef\@@tmp{\ctikzvalof{ieeestd ports/xor leads in}}
        \ifnum\@@tmp=0\relax
            % move pin start to the left to leave the xor gap free (not standard)
            \advance\pgf@circ@res@right by -\xorbar
        \fi
        }{%
        % add the xor/xnor bar
        \pgfpathmoveto{\pgfpoint{-\xorbar-8*\stdH/6.5-2*\stdH*(1-cos(\pgf@circ@orangle))}{-\stdH}}
        % see the comment on the main body about the 2.2
        \pgfpatharcto{2*\stdH}{2.2*\stdH}{0}{0}{1}{\pgfpoint{-\xorbar -8*\stdH/6.5-2*\stdH*(1-cos(\pgf@circ@orangle))}{\stdH}}
        \pgfusepath{draw}
    }
}
%
% Buffer and inverters
%
% #1: name
% #2: polarity
% #3: content
\long\def\pgfcircdeclareieeebufferport#1#2#3{%
    \pgfdeclareshape{ieeestd #1 port}%
    {%
        \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
        \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
        \savedmacro{\thisshape}{\def\thisshape{\tikz@fig@name}}
        \saveddimen{\baselen}{%
            \pgf@circ@ieeestd@baselen\pgf@x=\pgf@circ@res@temp
        }
        \saveddimen{\stdH}{% This is HALF the height of the inner port
            \pgf@circ@ieeestd@stdH\pgf@x=\pgf@circ@res@temp
        }
        \saveddimen{\notdiameter}{
            \pgf@circ@notradius\pgf@x=2\pgf@circ@res@temp
        }
        \saveddimen{\pinlen}{%
            \pgf@circ@ieeestd@pinlen\pgf@x=\pgf@circ@res@temp
        }
        % anchors for the body (no pins included here)
        % Notice that 0.8660254 is cos(30)
        \savedanchor{\bodyleft}{% This DOES NOT take into account the pointy or/xor thing
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{-0.8660254*\pgf@circ@res@temp}
            \pgfmathsetlength{\pgf@y}{\pgf@circ@res@temp}
        }
        \savedanchor{\topleft}{%
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{-0.8660254*\pgf@circ@res@temp}
            \pgfmathsetlength{\pgf@y}{\pgf@circ@res@temp}
        }
        \savedanchor{\bodyright}{% This DOES NOT take into account the "NOT" circle
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{0.8660254*\pgf@circ@res@temp}
            \pgfmathsetlength{\pgf@y}{\pgf@circ@res@temp}
        }
        \savedanchor{\bottomright}{% This DOES take into account the "NOT" circle
            \pgf@circ@notradius\pgf@circ@res@other=\pgf@circ@res@temp
            \pgf@circ@ieeestd@stdH
            % #2 is =1 if the port is a negated output one
            \pgfmathsetlength{\pgf@x}{0.8660254*\pgf@circ@res@temp+#2*2*\pgf@circ@res@other}
            \pgfmathsetlength{\pgf@y}{-\pgf@circ@res@temp}
        }
        % geographical anchors --- must be rectangulars!
        \savedanchor{\northwest}{%
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{-0.8660254*\pgf@circ@res@temp}
            \pgfmathsetlength{\pgf@y}{\pgf@circ@res@temp}
        }
        \savedanchor{\southwest}{%
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{-0.8660254*\pgf@circ@res@temp}
            \pgfmathsetlength{\pgf@y}{-\pgf@circ@res@temp}
        }
        \savedanchor{\southeast}{%
            \pgf@circ@ieeestd@stdH
            \pgf@circ@res@up=\pgf@circ@res@temp
            \pgf@circ@find@ieeeport@not@right{#2}
            \pgf@x=\pgf@circ@res@right
            \pgf@y=-\pgf@circ@res@up
        }
        \savedanchor{\northeast}{%
            \pgf@circ@ieeestd@stdH
            \pgf@circ@res@up=\pgf@circ@res@temp
            \pgf@circ@find@ieeeport@not@right{#2}
            \pgf@x=\pgf@circ@res@right
            \pgf@y=\pgf@circ@res@up
        }
        \anchor{center}{\pgfpointorigin}
        \anchor{text}{
            \ifpgf@circ@center@text
                \pgfpoint{-.5\wd\pgfnodeparttextbox}{\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox}
            \else
                \pgf@circ@ieeestd@stdH
                \pgfpoint{-0.8660254*\pgf@circ@res@temp + \ctikzvalof{left text distance}}{\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox}
            \fi
        }
        % input anchors
        \anchor{in}{\bodyleft\pgf@y=0pt\advance\pgf@x by-\pinlen}
        \anchor{in 1}{\bodyleft\pgf@y=0pt\advance\pgf@x by-\pinlen}
        \anchor{bin}{\bodyleft\pgf@y=0pt\relax}
        \anchor{bin 1}{\bodyleft\pgf@y=0pt\relax}
        % output anchors
        \anchor{out}{%
            \pgfextractx{\pgf@circ@res@other}{\bodyright}   %body border, without not ball
            \advance\pgf@circ@res@other by\pinlen
            \pgfextractx{\pgf@circ@res@temp}{\bottomright}   %body + ball border
            \ifdim \pgf@circ@res@temp > \pgf@circ@res@other
                \pgf@circ@res@other = \pgf@circ@res@temp % do not enter in the ball...
            \fi
            \pgf@x=\pgf@circ@res@other\pgf@y=0pt
        }
        \anchor{bout}{\bottomright\pgf@y=0pt}

        \anchor{body right}{\bodyright\pgf@y=0pt}
        \anchor{right}{\bottomright\pgf@y=0pt}
        \anchor{body left}{\bodyleft\pgf@y=0pt}% central edge of the body
        \anchor{left}{\bodyleft\pgf@y=0pt}
        \anchor{up}{%
            \bodyleft
            \pgf@y=+0.5\pgf@y\pgf@x=0pt
        }
        \anchor{down}{%
            \bodyleft
            \pgf@y=-0.5\pgf@y\pgf@x=0pt
        }
        % this is for when it's used as a bipole
        % we use the enclosing rectangle (see below)
        \anchorborder{%
            %% This (commented out) is the correct border anchor. But if we use the correct
            %% border anchor there is no horizontal space for the label ;-) because
            %% the triangle is too steep. So we will use a simple square border
            %%
            %% find the border anchor of a triangle (like a not port or an
            %% amplifier) ---
            %%
            %%      -----^ up
            %%     I --- |
            %%     I    -|--
            %%left I     |  ---    right
            %% ----I-----|-------I-----
            %%
            %%
            %\pgf@xa=\pgf@x
            %% it's simmetrical w/ vertical side, use only positive y
            %\pgfmathsetmacro{\@@switchy}{ifthenelse(\pgf@y>0,1,-1)}
            %\pgfmathsetlength{\pgf@ya}{abs(\pgf@y)}
            %\pgfextracty{\pgf@circ@res@up}{\bodyleft}
            %\pgfextractx{\pgf@circ@res@left}{\bodyleft}
            %\pgfextractx{\pgf@circ@res@right}{\bodyright}
            %% limit angle for the left (vertical) side
            %\pgfmathsetmacro{\@@phimax}{atan2(\pgf@circ@res@up,\pgf@circ@res@left)}
            %\pgfmathsetmacro{\@@phi}{atan2(\pgf@ya,\pgf@xa)}
            %\pgfmathsetmacro{\@@leftside}{ifthenelse(\@@phi>\@@phimax,1,0)}
            %% find the border
            %\ifnum\@@leftside>0
            %    % vertical side
            %    \pgfpointborderrectangle{\pgfpoint{\pgf@xa}{\pgf@ya}}
            %        {\pgfpoint{-\pgf@circ@res@left}{\pgf@circ@res@up}}
            %\else
            %    % triangle upper line
            %    \pgfpointintersectionoflines
            %        {\pgfpointorigin}{\pgfqpoint{\pgf@xa}{\pgf@ya}}
            %        {\pgfqpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}{\pgfqpoint{\pgf@circ@res@right}{0pt}}
            %\fi
            %% readjust y sign
            %\pgf@y=\@@switchy\pgf@y
            %
            % this is the square border to position the path label with a bit of horizontal space
            %
            \pgf@xa=\pgf@x
            \pgf@ya=\pgf@y
            \pgfextracty{\pgf@circ@res@up}{\bodyleft}
            \pgfextractx{\pgf@circ@res@left}{\bodyleft}
            \pgfpointborderrectangle{\pgfpoint{\pgf@xa}{\pgf@ya}}
                {\pgfpoint{-\pgf@circ@res@left}{\pgf@circ@res@up}}
        }
        % geographical anchors
        \anchor{nw}{\northwest}
        \anchor{ne}{\northeast}
        \anchor{se}{\southeast}
        \anchor{sw}{\southwest}
        \anchor{north west}{\northwest}
        \anchor{north east}{\northeast}
        \anchor{south east}{\southeast}
        \anchor{south west}{\southwest}
        % over 0,0 even if asymmetric
        % will break if the geocoords are not rectangular
        \anchor{n}{\northwest\pgf@x=0pt\relax}
        \anchor{e}{\northeast\pgf@y=0pt\relax}
        \anchor{s}{\southwest\pgf@x=0pt\relax}
        \anchor{w}{\northwest\pgf@y=0pt\relax}
        \anchor{north}{\northwest\pgf@x=0pt\relax}
        \anchor{east}{\northeast\pgf@y=0pt\relax}
        \anchor{south}{\southwest\pgf@x=0pt\relax}
        \anchor{west}{\northwest\pgf@y=0pt\relax}

        \pgf@circ@draw@component{
            \pgfscope
                \pgf@circ@setcolor
                #3%
            \endpgfscope
            % output lead:
            \pgfextractx{\pgf@circ@res@right}{\bottomright} %body+ball border
            \pgfextractx{\pgf@circ@res@other}{\bodyright}   %body border, without "not" ball
            \advance\pgf@circ@res@other by \pinlen\relax
            \ifdim \pgf@circ@res@other > \pgf@circ@res@right
                \ifpgfcirc@draw@output@leads
                    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
                    \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{0pt}}
                    \pgfusepath{draw}
                \fi
            \fi
            \ifnum #2=1\relax\pgfscope
                \pgftransformshift{\pgfpoint{\pgf@circ@res@right}{0pt}}
                \pgfnode{notcirc}{east}{}{\thisshape-not}{\pgfusepath{stroke}}
            \endpgfscope\fi
        }
    }
}

%%% #1 direct name #2 negated name #3 drawing (without output)
\long\def\pgfcircdeclareieeebufferportpair#1#2#3{%
    \pgfcircdeclareieeebufferport{#1}{0}{#3}% direct
    \pgfcircdeclareieeebufferport{#2}{1}{#3}% negated
}

\pgfcircdeclareieeebufferportpair{buffer}{not}{%
    \pgfextractx{\pgf@circ@res@left}{\bodyleft}
    \pgfextracty{\pgf@circ@res@up}{\bodyleft}
    \pgfextractx{\pgf@circ@res@right}{\bodyright}
    % \draw input pin
    \ifpgfcirc@draw@input@leads
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pinlen}{0pt}}
        \pgfusepath{draw}
    \fi
    \pgfscope
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@up}}
        \pgfpathclose
        \pgf@circ@draworfill
    \endpgfscope
}

\pgfcircdeclareieeebufferportpair{schmitt}{invschmitt}{%
    \pgfextractx{\pgf@circ@res@left}{\bodyleft}
    \pgfextracty{\pgf@circ@res@up}{\bodyleft}
    \pgfextractx{\pgf@circ@res@right}{\bodyright}
    % \draw input pin
    \ifpgfcirc@draw@input@leads
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pinlen}{0pt}}
        \pgfusepath{draw}
    \fi
    \pgfscope
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@up}}
        \pgfpathclose
        \pgf@circ@draworfill
    \endpgfscope
    % draw schmitt symbol in normal line thickness
    \pgfpathmoveto{\pgfpoint{0.75*\pgf@circ@res@left}{-0.25*\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{0.50*\pgf@circ@res@left}{-0.25*\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{0.50*\pgf@circ@res@left}{0.25*\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{0.00*\pgf@circ@res@left}{0.25*\pgf@circ@res@up}}
    \pgfpathmoveto{\pgfpoint{0.25*\pgf@circ@res@left}{0.25*\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{0.25*\pgf@circ@res@left}{-0.25*\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{0.5*\pgf@circ@res@left}{-0.25*\pgf@circ@res@up}}
    \pgfusepath{draw}
}


\pgfdeclareshape{schmitt symbol}{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgf@circ@ieeestd@stdH
        \pgf@y=\ctikzvalof{ieeestd ports/schmitt symbol size}\pgf@circ@res@temp
        \pgf@x=-1.5\pgf@y
    }
    \anchor{center}{\pgf@y=0pt \pgf@x=0pt}
    \anchor{east}{\northwest\pgf@y=0pt\pgf@x=-\pgf@x}
    \anchor{e}{\northwest\pgf@y=0pt \pgf@x=-\pgf@x}
    \anchor{west}{\northwest\pgf@y=0pt}
    \anchor{w}{\northwest \pgf@y=0pt}
    \anchor{south}{\northwest \pgf@x=0pt\pgf@y=-\pgf@y}
    \anchor{s}{\northwest\pgf@x=0pt\pgf@y=-\pgf@y}
    \anchor{north}{\northwest\pgf@x=0pt}
    \anchor{n}{\northwest\pgf@x=0pt}
    \anchor{south west}{\northwest\pgf@y=-\pgf@y}
    \anchor{north east}{\northwest\pgf@x=-\pgf@x}
    \anchor{north west}{\northwest}
    \anchor{south east}{\northwest\pgf@x=-\pgf@x\pgf@y=-\pgf@y}
    \pgf@circ@draw@component{
        \pgfscope
            \pgf@circ@setcolor
            \pgfextractx{\pgf@circ@res@left}{\northwest}
            \pgfextracty{\pgf@circ@res@up}{\northwest}
            \pgf@circ@res@left=0.7\pgf@circ@res@left
            \pgf@circ@res@up=0.7\pgf@circ@res@up
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left/3}{-\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left/3}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{-\pgf@circ@res@left}{\pgf@circ@res@up}}
            \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@left/3}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{-\pgf@circ@res@left/3}{-\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@up}}
            \pgfusepath{draw}
        \endpgfscope
    }
}

\pgfdeclareshape{notcirc}{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
    \savedanchor\northwest{%
        \pgf@circ@notradius
        \pgf@y=\pgf@circ@res@temp
        \pgf@x=-\pgf@y
    }
    \pgfcirc@northwest@symmetric@geoanchors
    \anchor{center}{\pgfpointorigin}
    \anchorborder{
        \pgf@circ@res@left=\pgf@x
        \pgf@circ@res@up=\pgf@y
        \northwest\pgf@circ@res@temp=\pgf@y
        \pgfpointborderellipse{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}%
        {\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@temp}}
    }
    \pgf@circ@draw@component{
        \pgfscope
            \northwest\pgf@circ@res@temp=\pgf@y
            \pgf@circ@setcolor
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgfpathcircle{\pgfpointorigin}{\pgf@circ@res@temp}
            \ifx\tikz@fillcolor\pgfutil@empty
                % set the default fill color to white
                \pgfsetfillcolor{white}
                % ...but override it if the class is defined!
                \pgf@circ@setifdefinedfill{draw, fill}{draw, fill}
            \else
                \pgfsetfillcolor{\tikz@fillcolor}
            \fi
            \pgfsetfillopacity{\ctikzvalof{ieeestd ports/not radius fill}}% normally 1.0
            \pgfusepath{draw,fill}
        \endpgfscope
    }
}

%%%% Transmission gates
% tgates are only ieee style for now
\tikzset{%
    tgate/.style ={shape=ieee tgate},
    double tgate/.style ={shape=ieee double tgate},
}
\ctikzset{tgate scale/.initial=0.7}
% Buffer and inverters
%
% #1: name
% #2: 1: one-not, 2:double-not
% #3: content
\long\def\pgfcircdeclareieeetgate#1#2#3{%
    \pgfdeclareshape{ieee #1}%
    {%
        \savedmacro{\ctikzclass}{\edef\ctikzclass{logic ports}}
        \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
        \savedmacro{\thisshape}{\def\thisshape{\tikz@fig@name}}
        \saveddimen{\baselen}{%
            \pgf@circ@ieeestd@baselen\pgf@x=\pgf@circ@res@temp
        }
        \saveddimen{\stdH}{% This is HALF the height of the inner port
            \pgf@circ@ieeestd@stdH\pgf@x=\pgf@circ@res@temp
        }
        \saveddimen{\notdiameter}{
            \pgf@circ@notradius\pgf@x=2\pgf@circ@res@temp
        }
        \saveddimen{\notradius}{
            \pgf@circ@notradius\pgf@x=\pgf@circ@res@temp
        }
        \saveddimen{\pinlen}{%
            \pgf@circ@ieeestd@pinlen\pgf@x=\pgf@circ@res@temp
        }
        % anchors for the body (no pins included here)
        % Notice that 0.8660254 is cos(30)
        \savedanchor{\bodyleft}{% This DOES NOT take into account the pointy or/xor thing
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{-2*0.8660254*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
            \pgfmathsetlength{\pgf@y}{#2*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
        }
        \savedanchor{\topleft}{%
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{-2*0.8660254*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
            \pgfmathsetlength{\pgf@y}{#2*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
        }
        \savedanchor{\bodyright}{%
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{2*0.8660254*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
            \pgfmathsetlength{\pgf@y}{#2*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
        }
        \savedanchor{\bottomright}{% Here it is the same as \bodyright
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{2*0.8660254*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
            \pgfmathsetlength{\pgf@y}{#2*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
        }
        % geographical anchors --- must be rectangulars!
        \savedanchor{\northwest}{%
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{-2*0.8660254*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
            \pgfmathsetlength{\pgf@y}{#2*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
        }
        \savedanchor{\southwest}{%
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{-2*0.8660254*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
            \pgfmathsetlength{\pgf@y}{-#2*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
        }
        \savedanchor{\southeast}{%
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{2*0.8660254*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
            \pgfmathsetlength{\pgf@y}{-#2*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
        }
        \savedanchor{\northeast}{%
            \pgf@circ@ieeestd@stdH
            \pgfmathsetlength{\pgf@x}{2*0.8660254*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
            \pgfmathsetlength{\pgf@y}{#2*\pgf@circ@res@temp*\ctikzvalof{tgate scale}}
        }
        \anchor{center}{\pgfpointorigin}
        \anchor{text}{
            \ifpgf@circ@center@text
                \pgfpoint{-.5\wd\pgfnodeparttextbox}{\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox}
            \else
                \pgf@circ@ieeestd@stdH
                \pgfpoint{-0.8660254*\pgf@circ@res@temp + \ctikzvalof{left text distance}}{\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox}
            \fi
        }
        % input anchors
        \anchor{in}{\bodyleft\pgf@y=0pt\advance\pgf@x by-\pinlen}
        \anchor{in 1}{\bodyleft\pgf@y=0pt\advance\pgf@x by-\pinlen}
        \anchor{bin}{\bodyleft\pgf@y=0pt\relax}
        \anchor{bin 1}{\bodyleft\pgf@y=0pt\relax}
        % gates
        \anchor{gate}{\bodyleft\pgf@circ@res@up=\pgf@y
            \pgf@x=0pt\pgfmathsetlength{\pgf@y}{-(#2-1)*0.5*\pgf@circ@res@up-\pinlen}}
        \anchor{bgate}{\bodyleft\pgf@circ@res@up=\pgf@y
            \pgf@x=0pt\pgfmathsetlength{\pgf@y}{-(#2-1)*0.5*\pgf@circ@res@up}}
        % gate (up) (2.1547 = 1+1/cos(30)
        \anchor{notgate}{\bodyleft\pgf@circ@res@up=\pgf@y
            \pgf@x=0pt\relax
            \pgfmathsetlength{\pgf@circ@res@temp}{2.1547*\notradius}
            \ifdim\pinlen>\pgf@circ@res@temp
                \pgfmathsetlength{\pgf@y}{(#2-1)*0.5*\pgf@circ@res@up+\pinlen}
            \else
                \pgf@y=\pgf@circ@res@temp
            \fi
        }
        \anchor{bnotgate}{\bodyleft\pgf@circ@res@up=\pgf@y
            \pgf@x=0pt\pgfmathsetlength{\pgf@y}{(#2-1)*0.5*\pgf@circ@res@up+2.1547*\notradius}}

        % output anchors
        \anchor{out}{%
            \pgfextractx{\pgf@circ@res@other}{\bodyright}   %body border, without not ball
            \advance\pgf@circ@res@other by\pinlen
            \pgfextractx{\pgf@circ@res@temp}{\bottomright}   %body + ball border
            \ifdim \pgf@circ@res@temp > \pgf@circ@res@other
                \pgf@circ@res@other = \pgf@circ@res@temp % do not enter in the ball...
            \fi
            \pgf@x=\pgf@circ@res@other\pgf@y=0pt
        }
        \anchor{bout}{\bottomright\pgf@y=0pt}

        \anchor{body right}{\bodyright\pgf@y=0pt}
        \anchor{right}{\bottomright\pgf@y=0pt}
        \anchor{body left}{\bodyleft\pgf@y=0pt}% central edge of the body
        \anchor{left}{\bodyleft\pgf@y=0pt}
        \anchor{up}{%
            \bodyleft
            \pgf@x=0pt
            \ifnum#2=1\relax
                \pgf@y=0pt
            \else
                \pgf@y=.5\pgf@y
            \fi
            \advance\pgf@y by \notdiameter
        }
        \anchor{down}{%
            \bodyleft
            \pgf@x=0pt
            \ifnum#2=1\relax
                \pgf@y=0pt
            \else
                \pgf@y=-.5\pgf@y
            \fi
        }
        % this is for when it's used as a bipole
        % we use the enclosing rectangle (see below)
        \anchorborder{%
            %
            % this is the square border to position the path label with a bit of horizontal space
            %
            \pgf@xa=\pgf@x
            \pgf@ya=\pgf@y
            \pgfextracty{\pgf@circ@res@up}{\bodyleft}
            \pgfextractx{\pgf@circ@res@left}{\bodyleft}
            \pgfpointborderrectangle{\pgfpoint{\pgf@xa}{\pgf@ya}}
                {\pgfpoint{-\pgf@circ@res@left}{\pgf@circ@res@up}}
        }
        % geographical anchors
        \anchor{nw}{\northwest}
        \anchor{ne}{\northeast}
        \anchor{se}{\southeast}
        \anchor{sw}{\southwest}
        \anchor{north west}{\northwest}
        \anchor{north east}{\northeast}
        \anchor{south east}{\southeast}
        \anchor{south west}{\southwest}
        % over 0,0 even if asymmetric
        % will break if the geocoords are not rectangular
        \anchor{n}{\northwest\pgf@x=0pt\relax}
        \anchor{e}{\northeast\pgf@y=0pt\relax}
        \anchor{s}{\southwest\pgf@x=0pt\relax}
        \anchor{w}{\northwest\pgf@y=0pt\relax}
        \anchor{north}{\northwest\pgf@x=0pt\relax}
        \anchor{east}{\northeast\pgf@y=0pt\relax}
        \anchor{south}{\southwest\pgf@x=0pt\relax}
        \anchor{west}{\northwest\pgf@y=0pt\relax}

        \pgf@circ@draw@component{
            \pgfscope
                \pgf@circ@setcolor
                \pgfextractx{\pgf@circ@res@left}{\bodyleft}
                \pgfextracty{\pgf@circ@res@up}{\bodyleft}
                \pgfextractx{\pgf@circ@res@right}{\bodyright}
                % \draw input pins
                \ifpgfcirc@draw@input@leads
                    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
                    \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pinlen}{0pt}}
                    % gate (down)
                    \pgfpathmoveto{\pgfpoint{0pt}{-(#2-1)*0.5*\pgf@circ@res@up}}
                    \pgfpathlineto{\pgfpoint{0pt}{-(#2-1)*0.5*\pgf@circ@res@up-\pinlen}}
                    % gate (up) (2.1547 = 1+1/cos(30)
                    \pgfmathsetlength{\pgf@circ@res@temp}{2.1547*\notradius}
                    \ifdim\pinlen>\pgf@circ@res@temp
                        \pgfpathmoveto{\pgfpoint{0pt}{(#2-1)*0.5*\pgf@circ@res@up+\pgf@circ@res@temp}}
                        \pgfpathlineto{\pgfpoint{0pt}{(#2-1)*0.5*\pgf@circ@res@up+\pinlen}}
                    \fi
                    \pgfusepath{draw}
                \fi
                #3%
            \endpgfscope
            % output lead:
            \pgfextractx{\pgf@circ@res@right}{\bottomright} %body+ball border
            \pgfextractx{\pgf@circ@res@other}{\bodyright}   %body border, without "not" ball
            \advance\pgf@circ@res@other by \pinlen\relax
            \ifdim \pgf@circ@res@other > \pgf@circ@res@right
                \ifpgfcirc@draw@output@leads
                    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
                    \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{0pt}}
                    \pgfusepath{draw}
                \fi
            \fi
        }
    }
}

\pgfcircdeclareieeetgate{tgate}{1}{%
    \pgfscope
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@up}}
        \pgfpathclose
        \pgf@circ@draworfill
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{-\pgf@circ@res@up}}
        \pgfpathclose
        \pgf@circ@draworfill
    \endpgfscope
    \pgfscope
        % 1.1547 is 1/cos(30)
        \pgftransformshift{\pgfpoint{0pt}{1.1547*\notradius}}
        \pgfnode{notcirc}{center}{}{\thisshape-not}{\pgfusepath{stroke}}
    \endpgfscope
}

\pgfcircdeclareieeetgate{double tgate}{2}{%
    \pgfscope
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{.5\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathclose
        \pgf@circ@draworfill
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{.5\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathclose
        \pgf@circ@draworfill
        % bottom triangles
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{-\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{-.5\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathclose
        \pgf@circ@draworfill
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{-\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{-.5\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathclose
        \pgf@circ@draworfill
    \endpgfscope
    \pgfscope
        % 1.1547 is 1/cos(30)
        \pgftransformshift{\pgfpoint{0pt}{.5*\pgf@circ@res@up+1.1547*\notradius}}
        \pgfnode{notcirc}{center}{}{\thisshape-not}{\pgfusepath{stroke}}
    \endpgfscope
}% %>>>

%% Path-style definitions for logical ports%<<<
%
% create path-style element for one input --- one output logical ports
%
\def\pgfcirc@port@node@to@path#1#2{%
    %
    % add a logic port path style component --- we need to suppress leads
    % and use the correct center
    %
    \pgfcirc@node@to@path{#1}{#2}{/tikz/no leads, \circuitikzbasekey/logic ports origin=center}%
}
\pgfcirc@port@node@to@path{not port}{inline not}
\pgfcirc@port@node@to@path{buffer port}{inline buffer}
\pgfcirc@port@node@to@path{schmitt port}{inline schmitt}
\pgfcirc@port@node@to@path{invschmitt port}{inline invschmitt}

\pgfcirc@port@node@to@path{tgate}{inline tgate}
\pgfcirc@port@node@to@path{double tgate}{inline double tgate}
% %>>>

%%%%%%%%%%%%%%%%%%%%%%%%
%% Transistors
%%%%%%%%%%%%%%%%%%%%%%%%

% Settings for Transistors %<<<1

\newif\ifpgf@circuit@trans@depletiontype
\pgf@circuit@trans@depletiontypefalse

\newif\ifpgf@circuit@mos@arrows
\ctikzset{tripoles/mos style/.is choice}
\ctikzset{tripoles/mos style/no arrows/.code={\pgf@circuit@mos@arrowsfalse}}
\ctikzset{tripoles/mos style/arrows/.code={\pgf@circuit@mos@arrowstrue}}
\pgfkeys{/tikz/arrowmos/.add code={}{\pgf@circuit@mos@arrowstrue}}
\pgfkeys{/tikz/noarrowmos/.add code={}{\pgf@circuit@mos@arrowsfalse}}

% Fixed label positions
\newif\ifpgf@circuit@transisors@fixlabels
\pgf@circuit@transisors@fixlabelstrue
\pgfkeys{/tikz/center transistors text/.add code={}{\pgf@circuit@transisors@fixlabelstrue}}
\ctikzset{fix transistors text/.add code={}{\pgf@circuit@transisors@fixlabelstrue}}
\pgfkeys{/tikz/legacy transistors text/.add code={}{\pgf@circuit@transisors@fixlabelsfalse}}
\ctikzset{legacy transistors text/.add code={}{\pgf@circuit@transisors@fixlabelsfalse}}

% Option solderdot for fet
\newif\ifpgf@circuit@fet@solderdot
\pgfkeys{/tikz/solderdot/.add code={}{\pgf@circuit@fet@solderdottrue}}
\ctikzset{solderdot/.add code={}{\pgf@circuit@fet@solderdottrue}}
\pgfkeys{/tikz/nosolderdot/.add code={}{\pgf@circuit@fet@solderdotfalse}}
\ctikzset{nosolderdot/.add code={}{\pgf@circuit@fet@solderdotfalse}}

%%%% activate doublegate
\newif\ifpgf@circuit@fet@doublegate
\pgfkeys{/tikz/doublegate/.add code={}{\pgf@circuit@fet@doublegatetrue}}
\ctikzset{doublegate/.add code={}{\pgf@circuit@fet@doublegatetrue}}
\pgfkeys{/tikz/nodoublegate/.add code={}{\pgf@circuit@fet@doublegatefalse}}
\ctikzset{nodoublegate/.add code={}{\pgf@circuit@fet@doublegatefalse}}

% Option bodydiode for fet
\newif\ifpgf@circuit@fet@bodydiode
\pgfkeys{/tikz/bodydiode/.add code={}{\pgf@circuit@fet@bodydiodetrue}}
\ctikzset{bodydiode/.add code={}{\pgf@circuit@fet@bodydiodetrue}}
\pgfkeys{/tikz/nobodydiode/.add code={}{\pgf@circuit@fet@bodydiodefalse}}
\ctikzset{nobodydiode/.add code={}{\pgf@circuit@fet@bodydiodefalse}}

% Option draw fet without gate connection
\newif\ifpgf@circuit@bpt@drawgate
\pgf@circuit@bpt@drawgatetrue
\pgfkeys{/tikz/nogate/.add code={}{\pgf@circuit@bpt@drawgatefalse}}
\ctikzset{nogate/.add code={}{\pgf@circuit@bpt@drawgatefalse}}
\pgfkeys{/tikz/nobase/.add code={}{\pgf@circuit@bpt@drawgatefalse}}
\ctikzset{nobase/.add code={}{\pgf@circuit@bpt@drawgatefalse}}

% Option draw bpt with schottky base
\newif\ifpgf@circuit@bpt@schottky
\pgf@circuit@bpt@schottkyfalse
\pgfkeys{/tikz/schottky base/.add code={}{\pgf@circuit@bpt@schottkytrue}}
\ctikzset{schottky base/.add code={}{\pgf@circuit@bpt@schottkytrue}}
\pgfkeys{/tikz/no schottky base/.add code={}{\pgf@circuit@bpt@schottkyfalse}}
\ctikzset{no schottky base/.add code={}{\pgf@circuit@bpt@schottkyfalse}}
\ctikzset{tripoles/schottky base size/.initial=0.05}

% Option to add ferroelectric symbol
\newif\ifpgf@circuit@tr@ferroel
\pgf@circuit@tr@ferroelfalse
\pgfkeys{/tikz/ferroel gate/.add code={}{\pgf@circuit@tr@ferroeltrue}}
\ctikzset{ferroel gate/.add code={}{\pgf@circuit@tr@ferroeltrue}}
\pgfkeys{/tikz/no ferroel gate/.add code={}{\pgf@circuit@tr@ferroelfalse}}
\ctikzset{no ferroel gate/.add code={}{\pgf@circuit@tr@ferroelfalse}}


% Option draw bpt without base connection
\newif\ifpgf@circuit@bpt@drawbase
\pgf@circuit@bpt@drawbasetrue
\pgfkeys{/tikz/nobase/.add code={}{\pgf@circuit@bpt@drawbasefalse}}
\ctikzset{nobase/.add code={}{\pgf@circuit@bpt@drawbasefalse}}
\pgfkeys{/tikz/nogate/.add code={}{\pgf@circuit@bpt@drawbasefalse}}
\ctikzset{nogate/.add code={}{\pgf@circuit@bpt@drawbasefalse}}

% Option draw bpt with optical input
\newif\ifpgf@circuit@bpt@drawphoto
\pgf@circuit@bpt@drawphotofalse
\pgfkeys{/tikz/photo/.add code={}{\pgf@circuit@bpt@drawphototrue}}
\ctikzset{photo/.add code={}{\pgf@circuit@bpt@drawphototrue}}
\pgfkeys{/tikz/photo/.add code={}{\pgf@circuit@bpt@drawphototrue}}
\ctikzset{photo/.add code={}{\pgf@circuit@bpt@drawphototrue}}

% Option draw fet without bulk connection -- Added by Burak Kelleci
\newif\ifpgf@circuit@bpt@drawbulk
\pgfkeys{/tikz/bulk/.add code={}{\pgf@circuit@bpt@drawbulktrue}}
\ctikzset{bulk/.add code={}{\pgf@circuit@bpt@drawbulktrue}}
\pgfkeys{/tikz/nobulk/.add code={}{\pgf@circuit@bpt@drawbulkfalse}}
\ctikzset{nobulk/.add code={}{\pgf@circuit@bpt@drawbulkfalse}}

% Option draw pmos with empty circle
\newif\ifpgf@circuit@pmos@emptycircle
\pgf@circuit@pmos@emptycirclefalse
\ctikzset{tripoles/pmos style/.is choice}
\pgfkeys{/tikz/emptycircle/.add code={}{\pgf@circuit@pmos@emptycircletrue}}
\ctikzset{tripoles/pmos style/emptycircle/.add code={}{\pgf@circuit@pmos@emptycircletrue}}
% Option draw pmos with no circle
\newif\ifpgf@circuit@pmos@nocircle
\pgf@circuit@pmos@nocirclefalse
\pgfkeys{/tikz/nocircle/.add code={}{\pgf@circuit@pmos@nocircletrue}}
\ctikzset{tripoles/pmos style/nocircle/.add code={}{\pgf@circuit@pmos@nocircletrue}}
% back to normal
\pgfkeys{/tikz/fullcircle/.add code={}{\pgf@circuit@pmos@emptycirclefalse\pgf@circuit@pmos@nocirclefalse}}
\ctikzset{tripoles/pmos style/fullcircle/.add code={}{\pgf@circuit@pmos@emptycirclefalse\pgf@circuit@pmos@nocirclefalse}}
% arrows at the end, the correct way
\newif\ifpgf@circuit@trans@arrowatend
\pgf@circuit@trans@arrowatendfalse
\ctikzset{transistors/arrow pos/.is choice}
\ctikzset{transistors/arrow pos/legacy/.code={\pgf@circuit@trans@arrowatendfalse}}
\ctikzset{transistors/arrow pos/end/.code={\pgf@circuit@trans@arrowatendtrue}}

\newif\ifpgf@circuit@trans@ntype
\pgf@circuit@trans@ntypetrue %default true
%\newif\ifpgf@circuit@trans@ptype
%\ctikzset{tripoles/nmos/.add code={\pgf@circuit@trans@ntypetrue}}
\ctikzset{tripoles/nmos/width/.initial=.7}
\ctikzset{tripoles/nmos/gate height/.initial=.35}
\ctikzset{tripoles/nmos/base height/.initial=.5}
\ctikzset{tripoles/nmos/conn height/.initial=0}
\ctikzset{tripoles/nmos/height/.initial=1.1}
\ctikzset{tripoles/nmos/base width/.initial=.5}
\ctikzset{tripoles/nmos/gate width/.initial=.62}
\ctikzset{tripoles/nmos/arrow pos/.initial=.6}
\ctikzset{tripoles/nmos/bodydiode scale/.initial=.3}
\ctikzset{tripoles/nmos/bodydiode distance/.initial=.3}
\ctikzset{tripoles/nmos/bodydiode conn/.initial=.6}
\ctikzset{tripoles/nmos/curr direction/.initial=1}

\ctikzset{tripoles/pmos/width/.initial=.7}
\ctikzset{tripoles/pmos/gate height/.initial=.35}
\ctikzset{tripoles/pmos/base height/.initial=.5}
\ctikzset{tripoles/pmos/conn height/.initial=0}
\ctikzset{tripoles/pmos/height/.initial=1.1}
\ctikzset{tripoles/pmos/base width/.initial=.5}
\ctikzset{tripoles/pmos/gate width/.initial=.62}
\ctikzset{tripoles/pmos/arrow pos/.initial=.4}
\ctikzset{tripoles/pmos/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pmos/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pmos/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pmos/curr direction/.initial=-1}

\ctikzset{tripoles/nmosd/width/.initial=.7}
\ctikzset{tripoles/nmosd/gate height/.initial=.35}
\ctikzset{tripoles/nmosd/base height/.initial=.5}
\ctikzset{tripoles/nmosd/conn height/.initial=0}
\ctikzset{tripoles/nmosd/height/.initial=1.1}
\ctikzset{tripoles/nmosd/base width/.initial=.5}
\ctikzset{tripoles/nmosd/gate width/.initial=.62}
\ctikzset{tripoles/nmosd/arrow pos/.initial=.6}
\ctikzset{tripoles/nmosd/bodydiode scale/.initial=.3}
\ctikzset{tripoles/nmosd/bodydiode distance/.initial=.3}
\ctikzset{tripoles/nmosd/bodydiode conn/.initial=.6}
\ctikzset{tripoles/nmosd/curr direction/.initial=1}
\ctikzset{tripoles/nmosd/depletion width/.initial=.1}
\ctikzset{tripoles/nmosd/depletion color/.initial=default}

\ctikzset{tripoles/pmosd/width/.initial=.7}
\ctikzset{tripoles/pmosd/gate height/.initial=.35}
\ctikzset{tripoles/pmosd/base height/.initial=.5}
\ctikzset{tripoles/pmosd/conn height/.initial=0}
\ctikzset{tripoles/pmosd/height/.initial=1.1}
\ctikzset{tripoles/pmosd/base width/.initial=.5}
\ctikzset{tripoles/pmosd/gate width/.initial=.62}
\ctikzset{tripoles/pmosd/arrow pos/.initial=.4}
\ctikzset{tripoles/pmosd/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pmosd/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pmosd/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pmosd/curr direction/.initial=-1}
\ctikzset{tripoles/pmosd/depletion width/.initial=.1}
\ctikzset{tripoles/pmosd/depletion color/.initial=default}
\ctikzset{tripoles/hemt/width/.initial=.7}
\ctikzset{tripoles/hemt/gate height/.initial=.35}
\ctikzset{tripoles/hemt/base height/.initial=.5}
\ctikzset{tripoles/hemt/conn height/.initial=0}
\ctikzset{tripoles/hemt/height/.initial=1.1}
\ctikzset{tripoles/hemt/base width/.initial=.5}
\ctikzset{tripoles/hemt/gate width/.initial=.5}% the horizontal position is the same
\ctikzset{tripoles/hemt/bodydiode scale/.initial=.3}
\ctikzset{tripoles/hemt/bodydiode distance/.initial=.3}
\ctikzset{tripoles/hemt/bodydiode conn/.initial=.6}
\ctikzset{tripoles/hemt/curr direction/.initial=1}
%% New parameters
\ctikzset{tripoles/hemt/curr direction/.initial=1}
\ctikzset{tripoles/hemt/gate asym/.initial=0}
\newif\ifpgf@circ@hemt@split
\ctikzset{tripoles/hemt/split gate/.is if=pgf@circ@hemt@split}
\ctikzset{tripoles/hemt/source arrow/.initial=0}

\ctikzset{tripoles/nfet/width/.initial=.7}
\ctikzset{tripoles/nfet/gate height/.initial=.35}
\ctikzset{tripoles/nfet/base height/.initial=.5}
\ctikzset{tripoles/nfet/conn height/.initial=0}
\ctikzset{tripoles/nfet/height/.initial=1.1}
\ctikzset{tripoles/nfet/base width/.initial=.5}
\ctikzset{tripoles/nfet/gate width/.initial=.62}
\ctikzset{tripoles/nfet/bodydiode scale/.initial=.3}
\ctikzset{tripoles/nfet/bodydiode distance/.initial=.3}
\ctikzset{tripoles/nfet/bodydiode conn/.initial=.6}
\ctikzset{tripoles/nfet/curr direction/.initial=1}

\ctikzset{tripoles/pfet/width/.initial=.7}
\ctikzset{tripoles/pfet/gate height/.initial=.35}
\ctikzset{tripoles/pfet/base height/.initial=.5}
\ctikzset{tripoles/pfet/conn height/.initial=0}
\ctikzset{tripoles/pfet/height/.initial=1.1}
\ctikzset{tripoles/pfet/base width/.initial=.5}
\ctikzset{tripoles/pfet/gate width/.initial=.62}
\ctikzset{tripoles/pfet/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pfet/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pfet/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pfet/curr direction/.initial=-1}

\ctikzset{tripoles/nfetd/width/.initial=.7}
\ctikzset{tripoles/nfetd/gate height/.initial=.35}
\ctikzset{tripoles/nfetd/base height/.initial=.5}
\ctikzset{tripoles/nfetd/conn height/.initial=0}
\ctikzset{tripoles/nfetd/height/.initial=1.1}
\ctikzset{tripoles/nfetd/base width/.initial=.5}
\ctikzset{tripoles/nfetd/gate width/.initial=.62}
\ctikzset{tripoles/nfetd/bodydiode scale/.initial=.3}
\ctikzset{tripoles/nfetd/bodydiode distance/.initial=.3}
\ctikzset{tripoles/nfetd/bodydiode conn/.initial=.6}
\ctikzset{tripoles/nfetd/curr direction/.initial=1}

\ctikzset{tripoles/pfetd/width/.initial=.7}
\ctikzset{tripoles/pfetd/gate height/.initial=.35}
\ctikzset{tripoles/pfetd/base height/.initial=.5}
\ctikzset{tripoles/pfetd/conn height/.initial=0}
\ctikzset{tripoles/pfetd/height/.initial=1.1}
\ctikzset{tripoles/pfetd/base width/.initial=.5}
\ctikzset{tripoles/pfetd/gate width/.initial=.62}
\ctikzset{tripoles/pfetd/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pfetd/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pfetd/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pfetd/curr direction/.initial=-1}

\ctikzset{tripoles/nigfete/width/.initial=.7}
\ctikzset{tripoles/nigfete/gate height/.initial=.35}
\ctikzset{tripoles/nigfete/base height/.initial=.5}
\ctikzset{tripoles/nigfete/conn height/.initial=.35}
\ctikzset{tripoles/nigfete/height/.initial=1.1}
\ctikzset{tripoles/nigfete/base width/.initial=.5}
\ctikzset{tripoles/nigfete/gate width/.initial=.62}
\ctikzset{tripoles/nigfete/bodydiode scale/.initial=.3}
\ctikzset{tripoles/nigfete/bodydiode distance/.initial=.3}
\ctikzset{tripoles/nigfete/bodydiode conn/.initial=.6}
\ctikzset{tripoles/nigfete/curr direction/.initial=1}

\ctikzset{tripoles/nigfetd/width/.initial=.7}
\ctikzset{tripoles/nigfetd/gate height/.initial=.35}
\ctikzset{tripoles/nigfetd/base height/.initial=.5}
\ctikzset{tripoles/nigfetd/conn height/.initial=.35}
\ctikzset{tripoles/nigfetd/height/.initial=1.1}
\ctikzset{tripoles/nigfetd/base width/.initial=.5}
\ctikzset{tripoles/nigfetd/gate width/.initial=.62}
\ctikzset{tripoles/nigfetd/bodydiode scale/.initial=.3}
\ctikzset{tripoles/nigfetd/bodydiode distance/.initial=.3}
\ctikzset{tripoles/nigfetd/bodydiode conn/.initial=.6}
\ctikzset{tripoles/nigfetd/curr direction/.initial=1}

\ctikzset{tripoles/nigfetebulk/width/.initial=.7}
\ctikzset{tripoles/nigfetebulk/gate height/.initial=.35}
\ctikzset{tripoles/nigfetebulk/base height/.initial=.5}
\ctikzset{tripoles/nigfetebulk/conn height/.initial=.35}
\ctikzset{tripoles/nigfetebulk/height/.initial=1.1}
\ctikzset{tripoles/nigfetebulk/base width/.initial=.5}
\ctikzset{tripoles/nigfetebulk/gate width/.initial=.62}
\ctikzset{tripoles/nigfetebulk/bodydiode scale/.initial=.3}
\ctikzset{tripoles/nigfetebulk/bodydiode distance/.initial=.3}
\ctikzset{tripoles/nigfetebulk/bodydiode conn/.initial=.6}
\ctikzset{tripoles/nigfetebulk/curr direction/.initial=1}

\ctikzset{tripoles/pigfete/width/.initial=.7}
\ctikzset{tripoles/pigfete/gate height/.initial=.35}
\ctikzset{tripoles/pigfete/base height/.initial=.5}
\ctikzset{tripoles/pigfete/conn height/.initial=.35}
\ctikzset{tripoles/pigfete/height/.initial=1.1}
\ctikzset{tripoles/pigfete/base width/.initial=.5}
\ctikzset{tripoles/pigfete/gate width/.initial=.62}
\ctikzset{tripoles/pigfete/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pigfete/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pigfete/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pigfete/curr direction/.initial=-1}

\ctikzset{tripoles/pigfetd/width/.initial=.7}
\ctikzset{tripoles/pigfetd/gate height/.initial=.35}
\ctikzset{tripoles/pigfetd/base height/.initial=.5}
\ctikzset{tripoles/pigfetd/conn height/.initial=.35}
\ctikzset{tripoles/pigfetd/height/.initial=1.1}
\ctikzset{tripoles/pigfetd/base width/.initial=.5}
\ctikzset{tripoles/pigfetd/gate width/.initial=.62}
\ctikzset{tripoles/pigfetd/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pigfetd/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pigfetd/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pigfetd/curr direction/.initial=-1}

\ctikzset{tripoles/pigfetebulk/width/.initial=.7}
\ctikzset{tripoles/pigfetebulk/gate height/.initial=.35}
\ctikzset{tripoles/pigfetebulk/conn height/.initial=.35}
\ctikzset{tripoles/pigfetebulk/base height/.initial=.5}
\ctikzset{tripoles/pigfetebulk/height/.initial=1.1}
\ctikzset{tripoles/pigfetebulk/base width/.initial=.5}
\ctikzset{tripoles/pigfetebulk/gate width/.initial=.62}
\ctikzset{tripoles/pigfetebulk/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pigfetebulk/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pigfetebulk/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pigfetebulk/curr direction/.initial=-1}

\ctikzset{tripoles/npn/width/.initial=.6}
\ctikzset{tripoles/npn/base height/.initial=.45}
\ctikzset{tripoles/npn/base height 2/.initial=.15}
\ctikzset{tripoles/npn/base height/.initial=.4}
\ctikzset{tripoles/npn/conn height/.initial=0}
\ctikzset{tripoles/npn/height/.initial=1.1}
\ctikzset{tripoles/npn/base width/.initial=.5}
\ctikzset{tripoles/npn/arrow pos/.initial=.5}
\ctikzset{tripoles/npn/bodydiode scale/.initial=.3}
\ctikzset{tripoles/npn/bodydiode distance/.initial=.3}
\ctikzset{tripoles/npn/bodydiode conn/.initial=.6}
\ctikzset{tripoles/npn/curr direction/.initial=1}

\ctikzset{tripoles/pnp/width/.initial=.6}
\ctikzset{tripoles/pnp/base height/.initial=.45}
\ctikzset{tripoles/pnp/base height 2/.initial=.15}
\ctikzset{tripoles/pnp/base height/.initial=.4}
\ctikzset{tripoles/pnp/conn height/.initial=0}
\ctikzset{tripoles/pnp/height/.initial=1.1}
\ctikzset{tripoles/pnp/base width/.initial=.5}
\ctikzset{tripoles/pnp/arrow pos/.initial=.5}
\ctikzset{tripoles/pnp/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pnp/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pnp/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pnp/curr direction/.initial=-1}

\ctikzset{tripoles/pigbt/width/.initial=.6}
\ctikzset{tripoles/pigbt/gate height/.initial=.45}
\ctikzset{tripoles/pigbt/gate height 2/.initial=.15}
\ctikzset{tripoles/pigbt/base height/.initial=.4}
\ctikzset{tripoles/pigbt/outer base height/.initial=.4}
\ctikzset{tripoles/pigbt/outer base thickness/.initial=1}
\ctikzset{tripoles/pigbt/conn height/.initial=0}
\ctikzset{tripoles/pigbt/height/.initial=1.1}
\ctikzset{tripoles/pigbt/gate width/.initial=.62}
\ctikzset{tripoles/pigbt/base width/.initial=.5}
\ctikzset{tripoles/pigbt/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pigbt/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pigbt/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pigbt/curr direction/.initial=-1}

\ctikzset{tripoles/nigbt/width/.initial=.6}
\ctikzset{tripoles/nigbt/gate height/.initial=.45}
\ctikzset{tripoles/nigbt/gate height 2/.initial=.15}
\ctikzset{tripoles/nigbt/base height/.initial=.4}
\ctikzset{tripoles/nigbt/outer base height/.initial=.4}
\ctikzset{tripoles/nigbt/outer base thickness/.initial=1}
\ctikzset{tripoles/nigbt/conn height/.initial=0}
\ctikzset{tripoles/nigbt/height/.initial=1.1}
\ctikzset{tripoles/nigbt/gate width/.initial=.62}
\ctikzset{tripoles/nigbt/base width/.initial=.5}
\ctikzset{tripoles/nigbt/bodydiode scale/.initial=.3}
\ctikzset{tripoles/nigbt/bodydiode distance/.initial=.3}
\ctikzset{tripoles/nigbt/bodydiode conn/.initial=.6}
\ctikzset{tripoles/nigbt/curr direction/.initial=1}

\ctikzset{tripoles/Lpigbt/width/.initial=.6}
\ctikzset{tripoles/Lpigbt/gate height/.initial=.45}
\ctikzset{tripoles/Lpigbt/gate height 2/.initial=.15}
\ctikzset{tripoles/Lpigbt/base height/.initial=.4}
\ctikzset{tripoles/Lpigbt/outer base height/.initial=.4}
\ctikzset{tripoles/Lpigbt/outer base thickness/.initial=1}
\ctikzset{tripoles/Lpigbt/conn height/.initial=.4}
\ctikzset{tripoles/Lpigbt/height/.initial=1.1}
\ctikzset{tripoles/Lpigbt/gate width/.initial=.62}
\ctikzset{tripoles/Lpigbt/base width/.initial=.5}
\ctikzset{tripoles/Lpigbt/bodydiode scale/.initial=.3}
\ctikzset{tripoles/Lpigbt/bodydiode distance/.initial=.3}
\ctikzset{tripoles/Lpigbt/bodydiode conn/.initial=.6}
\ctikzset{tripoles/Lpigbt/curr direction/.initial=-1}

\ctikzset{tripoles/Lnigbt/width/.initial=.6}
\ctikzset{tripoles/Lnigbt/gate height/.initial=.45}
\ctikzset{tripoles/Lnigbt/gate height 2/.initial=.15}
\ctikzset{tripoles/Lnigbt/base height/.initial=.4}
\ctikzset{tripoles/Lnigbt/outer base height/.initial=.4}
\ctikzset{tripoles/Lnigbt/outer base thickness/.initial=1}
\ctikzset{tripoles/Lnigbt/conn height/.initial=.4}
\ctikzset{tripoles/Lnigbt/height/.initial=1.1}
\ctikzset{tripoles/Lnigbt/gate width/.initial=.62}
\ctikzset{tripoles/Lnigbt/base width/.initial=.5}
\ctikzset{tripoles/Lnigbt/bodydiode scale/.initial=.3}
\ctikzset{tripoles/Lnigbt/bodydiode distance/.initial=.3}
\ctikzset{tripoles/Lnigbt/bodydiode conn/.initial=.6}
\ctikzset{tripoles/Lnigbt/curr direction/.initial=1}

\ctikzset{tripoles/igbt/outer base height/.code={
    \ctikzset{tripoles/nigbt/outer base height/.initial=#1}
    \ctikzset{tripoles/pigbt/outer base height/.initial=#1}
    \ctikzset{tripoles/Lnigbt/outer base height/.initial=#1}
    \ctikzset{tripoles/Lpigbt/outer base height/.initial=#1}
    \ctikzset{tripoles/Lnigbt/conn height/.initial=#1}
    \ctikzset{tripoles/Lpigbt/conn height/.initial=#1}
}}
\ctikzset{tripoles/igbt/outer base thickness/.code={
    \ctikzset{tripoles/nigbt/outer base thickness=#1}
    \ctikzset{tripoles/pigbt/outer base thickness=#1}
    \ctikzset{tripoles/Lnigbt/outer base thickness=#1}
    \ctikzset{tripoles/Lpigbt/outer base thickness=#1}
}}

\ctikzset{tripoles/njfet/width/.initial=.7}
\ctikzset{tripoles/njfet/gate height/.initial=.5}
\ctikzset{tripoles/njfet/gate height 2/.initial=.35}
\ctikzset{tripoles/njfet/gate width/.initial=.5}
\ctikzset{tripoles/njfet/base width/.initial=.5}
\ctikzset{tripoles/njfet/conn height/.initial=.35}% at the exterior
\ctikzset{tripoles/njfet/union height/.initial=.35}% at the gate
\ctikzset{tripoles/njfet/conn kink/.initial=1}% 1=no kink
\ctikzset{tripoles/njfet/height/.initial=1.1}
\ctikzset{tripoles/njfet/bodydiode scale/.initial=.3}
\ctikzset{tripoles/njfet/bodydiode distance/.initial=.3}
\ctikzset{tripoles/njfet/bodydiode conn/.initial=.6}
\ctikzset{tripoles/njfet/curr direction/.initial=1}

\ctikzset{tripoles/pjfet/width/.initial=.7}
\ctikzset{tripoles/pjfet/gate height/.initial=.5}
\ctikzset{tripoles/pjfet/gate height 2/.initial=.35}
\ctikzset{tripoles/pjfet/gate width/.initial=.5}
\ctikzset{tripoles/pjfet/base width/.initial=.5}
\ctikzset{tripoles/pjfet/conn height/.initial=.35}
\ctikzset{tripoles/pjfet/union height/.initial=.35}% at the gate
\ctikzset{tripoles/pjfet/conn kink/.initial=1}% 1=no kink
\ctikzset{tripoles/pjfet/height/.initial=1.1}
\ctikzset{tripoles/pjfet/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pjfet/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pjfet/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pjfet/curr direction/.initial=-1}

\ctikzset{tripoles/nujt/width/.initial=.7}
\ctikzset{tripoles/nujt/gate height/.initial=.5}
\ctikzset{tripoles/nujt/gate height 2/.initial=.35}
\ctikzset{tripoles/nujt/gate width/.initial=.5}
\ctikzset{tripoles/nujt/base width/.initial=.5}
\ctikzset{tripoles/nujt/conn height/.initial=.35}
\ctikzset{tripoles/nujt/union height/.initial=0}% at the gate
\ctikzset{tripoles/nujt/conn kink/.initial=.82}% to match the circle position
\ctikzset{tripoles/nujt/height/.initial=1.1}
\ctikzset{tripoles/nujt/bodydiode scale/.initial=.3}
\ctikzset{tripoles/nujt/bodydiode distance/.initial=.3}
\ctikzset{tripoles/nujt/bodydiode conn/.initial=.6}
\ctikzset{tripoles/nujt/curr direction/.initial=1}

\ctikzset{tripoles/pujt/width/.initial=.7}
\ctikzset{tripoles/pujt/gate height/.initial=.5}
\ctikzset{tripoles/pujt/gate height 2/.initial=.35}
\ctikzset{tripoles/pujt/gate width/.initial=.5}
\ctikzset{tripoles/pujt/base width/.initial=.5}
\ctikzset{tripoles/pujt/conn height/.initial=.35}
\ctikzset{tripoles/pujt/union height/.initial=0}% at the gate
\ctikzset{tripoles/pujt/conn kink/.initial=.82}% to match the circle position
\ctikzset{tripoles/pujt/height/.initial=1.1}
\ctikzset{tripoles/pujt/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pujt/bodydiode distance/.initial=.3}
\ctikzset{tripoles/pujt/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pujt/curr direction/.initial=-1}

\ctikzset{tripoles/isfet/width/.initial=1}
\ctikzset{tripoles/isfet/gate height/.initial=.35}
\ctikzset{tripoles/isfet/base height/.initial=.5}
\ctikzset{tripoles/isfet/height/.initial=1.1}
\ctikzset{tripoles/isfet/base width/.initial=.3}
\ctikzset{tripoles/isfet/gate width/.initial=.5}
\ctikzset{tripoles/isfet/conn height/.initial=0}
\ctikzset{tripoles/isfet/wave width/.initial=.16}
\ctikzset{tripoles/isfet/wave amp/.initial=.06}
\ctikzset{tripoles/isfet/waves y sep/.initial=.22}
\ctikzset{tripoles/isfet/waves x sep/.initial=.8}
\ctikzset{tripoles/isfet/bodydiode scale/.initial=.3}
\ctikzset{tripoles/isfet/bodydiode distance/.initial=.3}
\ctikzset{tripoles/isfet/bodydiode conn/.initial=.6}
\ctikzset{tripoles/isfet/curr direction/.initial=1}
%
% graphene FET
%
\ctikzset{tripoles/pgfet/width/.initial=.6}
\ctikzset{tripoles/pgfet/gate height/.initial=.5}
\ctikzset{tripoles/pgfet/gate height 2/.initial=0.25}  % must be "gate height"/2
\ctikzset{tripoles/pgfet/base height/.initial=.5}  % must be "gate height"
\ctikzset{tripoles/pgfet/outer base height/.initial=.25} % must be "gate height 2"
\ctikzset{tripoles/pgfet/outer base thickness/.initial=1}
\ctikzset{tripoles/pgfet/conn height/.initial=0}
\ctikzset{tripoles/pgfet/height/.initial=1.1}
\ctikzset{tripoles/pgfet/gate width/.initial=.5}
\ctikzset{tripoles/pgfet/base width/.initial=0.36084} % must be "gate height"*cos(60)/(2*width)
\ctikzset{tripoles/pgfet/bodydiode scale/.initial=.3}
\ctikzset{tripoles/pgfet/bodydiode distance/.initial=.6}
\ctikzset{tripoles/pgfet/bodydiode conn/.initial=.6}
\ctikzset{tripoles/pgfet/curr direction/.initial=-1}

\ctikzset{tripoles/ngfet/width/.initial=.6}
\ctikzset{tripoles/ngfet/gate height/.initial=.5}
\ctikzset{tripoles/ngfet/gate height 2/.initial=0.25}  % must be "gate height"/2
\ctikzset{tripoles/ngfet/base height/.initial=.5}  % must be "gate height"
\ctikzset{tripoles/ngfet/outer base height/.initial=.25} % must be "gate height 2"
\ctikzset{tripoles/ngfet/outer base thickness/.initial=1}
\ctikzset{tripoles/ngfet/conn height/.initial=0}
\ctikzset{tripoles/ngfet/height/.initial=1.1}
\ctikzset{tripoles/ngfet/gate width/.initial=.5}
\ctikzset{tripoles/ngfet/base width/.initial=0.36084} % must be "gate height"*cos(60)/(2*width)
\ctikzset{tripoles/ngfet/bodydiode scale/.initial=.3}
\ctikzset{tripoles/ngfet/bodydiode distance/.initial=.6}
\ctikzset{tripoles/ngfet/bodydiode conn/.initial=.6}
\ctikzset{tripoles/ngfet/curr direction/.initial=1}
%
% multi-emitter and multi-collector BJTs by Romano Giannetti
%
\ctikzset{tripoles/bjt/emitters/.initial=1}
\ctikzset{tripoles/bjt/collectors/.initial=1}
\pgfkeys{/tikz/emitters/.add code={}{\ctikzset{tripoles/bjt/emitters=#1}}}
\pgfkeys{/tikz/collectors/.add code={}{\ctikzset{tripoles/bjt/collectors=#1}}}
\ctikzset{tripoles/bjt/pins width/.initial=0.3}
\pgfkeys{/tikz/bjt pins width/.add code={}{\ctikzset{tripoles/bjt/pins width=#1}}}
\ctikzset{tripoles/bjt/multi height/.initial=.5}
\pgfkeys{/tikz/bjt multi height/.add code={}{\ctikzset{tripoles/bjt/multi height/.initial=#1}}}
%
\ctikzset{tripoles/bjt/width/.initial=.3}
\ctikzset{tripoles/bjt/base height 2/.initial=.4}
\ctikzset{tripoles/bjt/base height/.initial=1.1}
\ctikzset{tripoles/bjt/height/.initial=.4}
\ctikzset{tripoles/bjt/base width/.initial=1}
\ctikzset{tripoles/bjt/arrow pos/.initial=.5}
% do NOT touch these two!
\ctikzset{tripoles/bjt/npn/curr direction/.initial=1}
\ctikzset{tripoles/bjt/pnp/curr direction/.initial=-1}


%
% definitions for transistor circles
%
\ctikzset{transistor circle/.is family}
\ctikzset{transistor circle/relative thickness/.initial=1}
\ctikzset{transistor circle/color/.initial=default}
\ctikzset{transistor circle/dash/.initial=default}
\ctikzset{transistor circle/scale circle radius/.initial=1}
\ctikzset{transistor circle/default base in/.initial=0.9}
\ctikzset{transistor circle/njfet base in/.initial=1.05}
\ctikzset{transistor circle/pjfet base in/.initial=1.05}
\ctikzset{transistor circle/isfet base in/.initial=0.65}

\newif\ifpgf@circ@trcircle\pgf@circ@trcirclefalse
\ctikzset{tr circle/.is if=pgf@circ@trcircle}
\tikzset{tr circle/.is if=pgf@circ@trcircle}
% partial borders styles
% this can be "none" or 4 numbers saying the style for each part:
% 0 --- nothing, 1 --- solid, 2 --- dashed
\ctikzset{transistor circle/partial borders/.initial=none}       % Value none for normal borders
\ctikzset{transistor circle/partial border dash/.initial={{2pt}{2pt}}}
%
% body diode style
\ctikzset{transistor bodydiode/.is family}
\ctikzset{transistor bodydiode/relative thickness/.initial=1}
\ctikzset{transistor bodydiode/color/.initial=default}
\ctikzset{transistor bodydiode/dash/.initial=default}
\ctikzset{transistor bodydiode/dot scale/.initial=0.7}
\ctikzset{transistor solderdot scale/.initial=0.7}
% this is unfortunate, but needed for backward compatibility
\ctikzset{transistor bodydiode/scale/.code={%
    \ctikzset{tripoles/nmos/bodydiode scale=#1}%
    \ctikzset{tripoles/pmos/bodydiode scale=#1}%
    \ctikzset{tripoles/nmosd/bodydiode scale=#1}%
    \ctikzset{tripoles/pmosd/bodydiode scale=#1}%
    \ctikzset{tripoles/hemt/bodydiode scale=#1}%
    \ctikzset{tripoles/nfet/bodydiode scale=#1}%
    \ctikzset{tripoles/pfet/bodydiode scale=#1}%
    \ctikzset{tripoles/nfetd/bodydiode scale=#1}%
    \ctikzset{tripoles/pfetd/bodydiode scale=#1}%
    \ctikzset{tripoles/nigfete/bodydiode scale=#1}%
    \ctikzset{tripoles/nigfetd/bodydiode scale=#1}%
    \ctikzset{tripoles/nigfetebulk/bodydiode scale=#1}%
    \ctikzset{tripoles/pigfete/bodydiode scale=#1}%
    \ctikzset{tripoles/pigfetd/bodydiode scale=#1}%
    \ctikzset{tripoles/pigfetebulk/bodydiode scale=#1}%
    \ctikzset{tripoles/npn/bodydiode scale=#1}%
    \ctikzset{tripoles/pnp/bodydiode scale=#1}%
    \ctikzset{tripoles/pigbt/bodydiode scale=#1}%
    \ctikzset{tripoles/nigbt/bodydiode scale=#1}%
    \ctikzset{tripoles/Lpigbt/bodydiode scale=#1}%
    \ctikzset{tripoles/Lnigbt/bodydiode scale=#1}%
    \ctikzset{tripoles/njfet/bodydiode scale=#1}%
    \ctikzset{tripoles/pjfet/bodydiode scale=#1}%
    \ctikzset{tripoles/nujt/bodydiode scale=#1}%
    \ctikzset{tripoles/pujt/bodydiode scale=#1}%
    \ctikzset{tripoles/isfet/bodydiode scale=#1}%
    \ctikzset{tripoles/pgfet/bodydiode scale=#1}%
    \ctikzset{tripoles/ngfet/bodydiode scale=#1}%
}}
%%>>>


%% definitions of transistor shapes%<<<
%
% draw partial transistor outline
%
\def\pgf@circ@trcircle@split#1#2#3#4\relax{%split the six numbers
    \edef\@@a{#1}\edef\@@b{#2}\edef\@@c{#3}\edef\@@d{#4}%
}
\def\pgf@circ@trcircle@setdash{%
    \edef\@@dash{\ctikzvalof{transistor circle/partial border dash}}%
    % \typeout{DASH:\@@dash}%
    \expandafter\pgfsetdash\expandafter{\@@dash}{0pt}%
}
% this is the same as the tubes' one, but let's keep it separated
\def\pgf@circ@trcircle@draw@style#1{%
    \ifcase#1
        \pgfusepath{discard}% case 0, will discard the path
    \or
        \pgfsetdash{}{0pt}\pgfusepath{draw}% case 1, solid
    \or
        \pgf@circ@trcircle@setdash\pgfusepath{draw}% case 2, dashed
    \else
        \pgfutil@packagewarning{circuitikz}{Transistor circle draw style not known!}%
        \pgfusepath{draw}%
    \fi
}
%
% add a circle to the transistor:
\def\pgf@circ@trcircle@draw@partial#1{%
    \expandafter\pgf@circ@trcircle@split#1\relax
    % \typeout{PARTIAL: \@@a:\@@b:\@@c:\@@d}
    \pgfscope % right side upper arc
        \pgfpathmoveto{\pgfpoint{\circleleft+\circleradius}{\circleradius}}
        \pgfpatharc{90}{0}{\circleradius}
        \pgf@circ@trcircle@draw@style{\@@a}
    \endpgfscope
    \pgfscope % right side lower arc
        \pgfpathmoveto{\pgfpoint{\circleleft+2*\circleradius}{0pt}}
        \pgfpatharc{0}{-90}{\circleradius}
        \pgf@circ@trcircle@draw@style{\@@b}
    \endpgfscope
    \pgfscope % left side lower arc
        \pgfpathmoveto{\pgfpoint{\circleleft+\circleradius}{-\circleradius}}
        \pgfpatharc{-90}{-180}{\circleradius}
        \pgf@circ@trcircle@draw@style{\@@c}
    \endpgfscope
    \pgfscope % left side upper arc
        \pgfpathmoveto{\pgfpoint{\circleleft}{0pt}}
        \pgfpatharc{-180}{-270}{\circleradius}
        \pgf@circ@trcircle@draw@style{\@@d}
    \endpgfscope
}
%
\def\pgfcirc@transistorcircle{
    \ifpgf@circ@trcircle
    \pgfscope
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfsetlinewidth{\ctikzvalof{transistor circle/relative thickness}\pgflinewidth}
        \pgf@circ@subset@color@dash{transistor circle}
        % radius of the circle
        % \pgfmathsetlength{\pgf@circ@res@temp}{((#2+\extrabodydiodelen)-(#1)+(#3)*(#3)/((#2+\extrabodydiodelen)-(#1)))/2}
        % \pgfpathcircle{\pgfpoint{#1+\pgf@circ@res@temp}{0pt}}{\pgf@circ@res@temp}
        \pgfpathcircle{\pgfpoint{\circleleft+\circleradius}{0pt}}{\circleradius}
        \edef\@@partial{\ctikzvalof{transistor circle/partial borders}}
        \ifx\@@partial\pgf@nonetext
            % Circle fill and draw if it's not partial... for speed
            \pgf@circ@draworfill
        \else
            % otherwise, just fill and do the partial fill
            \pgf@circ@maybefill
            \pgf@circ@trcircle@draw@partial{\@@partial}
        \fi
    \endpgfscope
    \fi
}

% add a ferroelectric squish to transistors

\def\pgfcirc@ferroelectric#1#2{% #1 -> transistor type, #2 horizontal enlargment (prop to ...@left),
    \ifpgf@circuit@tr@ferroel\pgfscope
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgf@circ@set@relative@thickness{modifier thickness}
        % base-gate center and width of the sigmoid
        \pgfmathsetlength{\pgf@circ@res@other}
        {0.5*(\ctikzvalof{tripoles/#1/gate width}+\ctikzvalof{tripoles/#1/base width}-(#2))*\pgf@circ@res@left}
        \pgfmathsetlength{\pgf@circ@res@temp}
        {1.2*abs(\ctikzvalof{tripoles/#1/gate width}-\ctikzvalof{tripoles/#1/base width}+0.5*(#2))*\pgf@circ@res@left}
        \pgfsetcornersarced{\pgfpoint{-0.2\pgf@circ@res@temp}{-0.2\pgf@circ@res@temp}}
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@other+\pgf@circ@res@temp}
            {1.1*\ctikzvalof{tripoles/#1/base height}*\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\pgf@circ@res@other+\pgf@circ@res@temp}
            {0.5*\ctikzvalof{tripoles/#1/base height}*\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\pgf@circ@res@other-\pgf@circ@res@temp}
            {0.5*\ctikzvalof{tripoles/#1/base height}*\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint
            {\pgf@circ@res@other-\pgf@circ@res@temp}
            {1.1*\ctikzvalof{tripoles/#1/base height}*\pgf@circ@res@down}}
        \pgfusepath{draw}
    \endpgfscope\fi
}

\ctikzset{tr gap fill/.initial=none}
\tikzset{tr gap fill/.style={\circuitikzbasekey/tr gap fill={#1}}}
% fill the gap in relevant transistors
\def\pgfcirc@fillgategap#1{% #1 transistor type
    \edef\@@@none{none}\edef\@@@color{\ctikzvalof{tr gap fill}}% always exists
    \ifx\@@@none\@@@color\relax\else
        \begingroup % save the value of @res: other, step, temp
        \edef\@@@doit{1}% draw it, set to zero if we give up
        % ok, we have the color here. we have to get center position, with and height
        % normally is gate height; but in igbt is outer base height (grrrr)
        \pgfkeysifdefined{\circuitikzbasekey/tripoles/#1/outer base height}{
            % yes, it's an IGBT
            \pgfmathsetlength{\pgf@circ@res@other}{\ctikzvalof{tripoles/#1/outer base height}*\pgf@circ@res@up}
        }{
            %no. Let's try with gate height
             \pgfkeysifdefined{\circuitikzbasekey/tripoles/#1/gate height}{
                 % yes, go for it
                \pgfmathsetlength{\pgf@circ@res@other}{\ctikzvalof{tripoles/#1/gate height}*\pgf@circ@res@up}
            }{
                %no, give up, let value to 0
                \edef\@@@doit{0}
            }
        }
        % now we have to find x1 and x2: these are base width and gate width
        % if do not have one of that, bail out.
        \pgfkeysifdefined{\circuitikzbasekey/tripoles/#1/gate width}{
                \pgfmathsetlength{\pgf@circ@res@temp}{\ctikzvalof{tripoles/#1/gate width}*\pgf@circ@res@left}
            }{
                \edef\@@@doit{0}
            }
        \pgfkeysifdefined{\circuitikzbasekey/tripoles/#1/base width}{
                \pgfmathsetlength{\pgf@circ@res@step}{\ctikzvalof{tripoles/#1/base width}*\pgf@circ@res@left}
            }{
                \edef\@@@doit{0}
            }
        % if found, draw it.
        \ifnum\@@@doit>0\relax
            \pgfscope
                \pgfsetfillcolor{\@@@color}
                \pgfsetcolor{\@@@color}
                \pgfpathrectanglecorners{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@other}}{\pgfpoint{\pgf@circ@res@step}{-\pgf@circ@res@other}}
                \pgfusepath{fill, draw}
            \endpgfscope
        \fi
        \endgroup
    \fi
}

\long\def\pgfcircdeclaretransistor#1#2#3{
    \pgfdeclareshape{#1}
    {
        \savedmacro{\ctikzclass}{\edef\ctikzclass{transistors}}
        \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
        \savedmacro{\circlebase}{
            \pgfkeysifdefined{\circuitikzbasekey/transistor circle/#1 base in}%
            {% yes, we have it
                \edef\circlebase{\ctikzvalof{transistor circle/#1 base in}}%
            }{% no, use default
                \edef\circlebase{\ctikzvalof{transistor circle/default base in}}
            }}
        % \savedmacro{\thistypeoftr}{\edef\thistypeoftr{#1}}
        \saveddimen{\extrabodydiodelen}{
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \ifpgf@circuit@fet@bodydiode
                % try to put the text to the right of the flyback diode
                \pgfmathsetlength{\pgf@x}{(
                    \ctikzvalof{tripoles/#1/bodydiode distance}*
                    \ctikzvalof{tripoles/#1/width} +
                    \ctikzvalof{tripoles/#1/bodydiode scale}*
                    \ctikzvalof{bipoles/diode/height}/2
                )*\pgf@circ@scaled@Rlen}
            \else
                \pgf@x=0pt\relax
            \fi
        }
        \savedmacro{\scalecircleradius}{
            \edef\scalecircleradius{\ctikzvalof{transistor circle/scale circle radius}}
        }
        \saveddimen{\circleradius}{
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            % repeat the extrabodydiodelen (grrr)
            \ifpgf@circuit@fet@bodydiode
                % try to put the text to the right of the flyback diode
                \pgfmathsetlength{\pgf@circ@res@other}{(
                    \ctikzvalof{tripoles/#1/bodydiode distance}*
                    \ctikzvalof{tripoles/#1/width} +
                    \ctikzvalof{tripoles/#1/bodydiode scale}*
                    \ctikzvalof{bipoles/diode/height}/2
                )*\pgf@circ@scaled@Rlen}
            \else
                \pgf@circ@res@other=0pt\relax
            \fi
            % left
            \pgf@xa=-\ctikzvalof{tripoles/#1/width}\pgf@circ@scaled@Rlen
            \pgf@xa=\circlebase\pgf@xa % this is the base point of the circle
            % northeast
            \pgf@yb=\ctikzvalof{tripoles/#1/height}\pgf@circ@scaled@Rlen % y of the left point of circle
            \pgf@yb=.5\pgf@yb
            \pgf@xb=\pgf@circ@res@other % this is the x of the right points of the circle
            %
            \pgf@yb=\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@yb %this is #3 of the circle
            % find the radius of the circle
            \pgfmathsetlength{\pgf@x}{((\pgf@xb)-(\pgf@xa)+(\pgf@yb)*(\pgf@yb)/((\pgf@xb)-(\pgf@xa)))/2*\scalecircleradius}
        }
        \saveddimen{\circleleft}{
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgf@xa=-\ctikzvalof{tripoles/#1/width}\pgf@circ@scaled@Rlen
            \pgf@x=\circlebase\pgf@xa
        }
        \anchor{circle center}{
            \pgf@y=0pt\pgf@x=\circleleft\advance\pgf@x by\circleradius
        }
        \anchor{circle top}{
            \pgf@y=\circleradius\pgf@x=\circleleft\advance\pgf@x by\circleradius
        }
        \anchor{circle bottom}{
            \pgf@y=-\circleradius\pgf@x=\circleleft\advance\pgf@x by\circleradius
        }
        \anchor{circle left}{
            \pgf@y=0pt\pgf@x=\circleleft
        }
        \anchor{circle right}{
            \pgf@y=0pt\pgf@x=\circleleft
            \advance\pgf@x by \circleradius\advance\pgf@x by \circleradius
        }
        \savedanchor{\centergap}{% this is the center position between gate and base
            % get "left"
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgf@xa=-\ctikzvalof{tripoles/#1/width}\pgf@circ@scaled@Rlen
            \pgfkeysifdefined{\circuitikzbasekey/tripoles/#1/gate width}{
                % yes, we have a separated gate
                \pgfmathsetlength{\pgf@x}
                {0.5*(\ctikzvalof{tripoles/#1/gate width}+\ctikzvalof{tripoles/#1/base width})*\pgf@xa}
            }{
                % no, use just the base
                \pgfmathsetlength{\pgf@x}
                {\ctikzvalof{tripoles/#1/base width}*\pgf@xa}
            }
            \pgf@y=0pt\relax
        }
        \anchor{centergap}{\centergap}
        \anchor{center}{
            \pgfpointorigin
        }
        \savedanchor\northeast{% upper right
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgf@y=\ctikzvalof{tripoles/#1/height}\pgf@circ@scaled@Rlen
            \pgf@y=.5\pgf@y
            \pgf@x=0pt
        }
        \savedanchor\left{%center left
            \pgf@y=0pt
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgf@x=-\ctikzvalof{tripoles/#1/width}\pgf@circ@scaled@Rlen
        }
        \savedanchor\right{
            \pgf@x=0pt\pgf@y=0pt
            \ifpgf@circ@trcircle
            % repeat the \circleradius computation (GRRRR)
                \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
                % repeat the extrabodydiodelen (grrr)
                \ifpgf@circuit@fet@bodydiode
                    % try to put the text to the right of the flyback diode
                    \pgfmathsetlength{\pgf@circ@res@other}{(
                        \ctikzvalof{tripoles/#1/bodydiode distance}*
                        \ctikzvalof{tripoles/#1/width} +
                        \ctikzvalof{tripoles/#1/bodydiode scale}*
                        \ctikzvalof{bipoles/diode/height}/2
                    )*\pgf@circ@scaled@Rlen}
                \else
                    \pgf@circ@res@other=0pt\relax
                \fi
                % left
                \pgf@xa=-\ctikzvalof{tripoles/#1/width}\pgf@circ@scaled@Rlen
                \pgf@xa=\circlebase\pgf@xa % this is the base point of the circle
                % northeast
                \pgf@yb=\ctikzvalof{tripoles/#1/height}\pgf@circ@scaled@Rlen % y of the left point of circle
                \pgf@yb=.5\pgf@yb
                \pgf@xb=\pgf@circ@res@other % this is the x of the right points of the circle
                %
                \pgf@yb=\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@yb %this is #3 of the circle
                % find the radius of the circle
                \pgfmathsetlength{\pgf@circ@res@other}{((\pgf@xb)-(\pgf@xa)+(\pgf@yb)*(\pgf@yb)/((\pgf@xb)-(\pgf@xa)))/2*\scalecircleradius}
                % repeat the circleleft computation
                \pgf@xa=-\ctikzvalof{tripoles/#1/width}\pgf@circ@scaled@Rlen
                \pgf@xb=\circlebase\pgf@xa
                \pgfmathsetlength{\pgf@x}{\pgf@xb+2*\pgf@circ@res@other}
            \fi
        }
        \anchor{text}{
            \northeast
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \ifpgf@circuit@transisors@fixlabels
                \ifpgf@circuit@fet@bodydiode
                    \advance \pgf@x by \extrabodydiodelen
                \fi
                \ifpgf@circ@trcircle
                    \left\pgf@xa=\pgf@x
                    \pgfmathsetlength{\pgf@x}{\circleleft+2*\circleradius}
                    % \advance \pgf@x by \circleradius
                \fi
                % add a bit of space to avoid central (substrate) terminal if drawn
                \advance\pgf@x by 0.05\pgf@circ@scaled@Rlen\relax
                \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
            \else
                \pgf@y=.7\pgf@y
                \pgf@x= \pgf@circ@scaled@Rlen
                \pgf@x=0.1\pgf@x
            \fi
        }
        \anchor{pathstart}{ % south
            \northeast
            \pgf@y=-\pgf@y
        }
        \anchor{pathend}{
            \northeast
        }
        \anchor{north}{
            \northeast
            \pgf@circ@res@step=\pgf@y
            \left
            \pgf@y=\pgf@circ@res@step
            \pgf@x=.5\pgf@x
        }
        \anchor{west}{
            \left
        }
        \anchor{left}{
            \left
        }
        \anchor{east}{
            \northeast
            \pgf@y=0pt
        }
        \anchor{right}{
            \right
        }
        \anchor{south}{
            \northeast
            \pgf@circ@res@step=\pgf@y
            \left
            \pgf@y=-\pgf@circ@res@step
            \pgf@x=.5\pgf@x
        }
        \anchor{south west}{
            \northeast
            \pgf@circ@res@step=\pgf@y
            \left
            \pgf@y=-\pgf@circ@res@step
        }
        \anchor{north east}{
            \northeast
        }
        \anchor{north west}{
            \northeast
            \pgf@circ@res@step=\pgf@y
            \left
            \pgf@y=\pgf@circ@res@step
        }
        \anchor{south east}{
            \northeast
            \pgf@y=-\pgf@y
        }
        \anchor{B}{
            \northeast
            \pgf@circ@res@step=\ctikzvalof{tripoles/#1/conn height}\pgf@y
            \left
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@circ@res@step
        }
        \anchor{base}{
            \northeast
            \pgf@circ@res@step=\ctikzvalof{tripoles/#1/conn height}\pgf@y
            \left
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@circ@res@step
        }
        \anchor{bulk}{ %added by Burak Kelleci
            \northeast
            \pgf@circ@res@step=\ctikzvalof{tripoles/#1/conn height}\pgf@y
            \right
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@circ@res@step
        }
        \anchor{nobulk}{ %added by Burak Kelleci
            \left
            \pgf@x=\ctikzvalof{tripoles/#1/base width}\pgf@x
        }
        \anchor{G}{
            \northeast
            \pgf@circ@res@step=\ctikzvalof{tripoles/#1/conn height}\pgf@y
            \left
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@circ@res@step
        }
        \anchor{G1}{
            \northeast
            \pgf@circ@res@step=\ctikzvalof{tripoles/#1/conn height}\pgf@y
            \left
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@circ@res@step
        }
        \anchor{G2}{
            \northeast
            \pgf@circ@res@step=\ctikzvalof{tripoles/#1/conn height}\pgf@y
            \pgf@circ@res@step=-0.3333333\pgf@circ@res@step
            \left
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@circ@res@step
        }
        \anchor{gate}{
            \northeast
            \pgf@circ@res@step=\ctikzvalof{tripoles/#1/conn height}\pgf@y
            \left
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@circ@res@step
        }
        \anchor{gate1}{
            \northeast
            \pgf@circ@res@step=\ctikzvalof{tripoles/#1/conn height}\pgf@y
            \left
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@circ@res@step
        }
        \anchor{gate2}{
            \northeast
            \pgf@circ@res@step=\ctikzvalof{tripoles/#1/conn height}\pgf@y
            \pgf@circ@res@step=-0.3333333\pgf@circ@res@step
            \left
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@circ@res@step
        }
        \anchor{nobase}{
            \left
            \pgf@x=\ctikzvalof{tripoles/#1/base width}\pgf@x
        }
        \anchor{circle base}{
            \left
            \pgf@x=\circlebase\pgf@x
        }
        \anchor{nogate}{
            \left
            \pgf@x=\ctikzvalof{tripoles/#1/gate width}\pgf@x
        }
        \anchor{E}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@y
        }
        \anchor{emitter}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@y
        }
        \anchor{C}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/curr direction}\pgf@y
        }
        \anchor{collector}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/curr direction}\pgf@y
        }
        \anchor{S}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@y
        }
        \anchor{source}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@y
        }
        \anchor{D}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/curr direction}\pgf@y
        }
        \anchor{drain}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/curr direction}\pgf@y
        }
        \anchor{body C in}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/curr direction}\pgf@y
            \pgf@y=\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@y
        }
        \anchor{circle C}{
            \left
            \pgf@xa=\circlebase\pgf@x % this is #1 of the circle
            \northeast
            \pgf@xb=\pgf@x %this is #2 of the circle
            \pgf@yb=\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@y %this is #3 of the circle
            % the base of the triangle is x_2 - x_1 - r
            \pgfmathsetlength{\pgf@circ@res@other}{\pgf@xb-\pgf@xa-\circleradius}
            % so let's go with the height
            \pgfmathsetlength{\pgf@ya}{sqrt(abs(\circleradius*\circleradius-\pgf@circ@res@other*\pgf@circ@res@other))}
            % finally, direction
            \pgf@y=\ctikzvalof{tripoles/#1/curr direction}\pgf@ya
        }
        \anchor{circle E}{
            \left
            \pgf@xa=\circlebase\pgf@x % this is #1 of the circle
            \northeast
            \pgf@xb=\pgf@x %this is #2 of the circle
            \pgf@yb=\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@y %this is #3 of the circle
            % the base of the triangle is x_2 - x_1 - r
            \pgfmathsetlength{\pgf@circ@res@other}{\pgf@xb-\pgf@xa-\circleradius}
            % so let's go with the height
            \pgfmathsetlength{\pgf@ya}{sqrt(abs(\circleradius*\circleradius-\pgf@circ@res@other*\pgf@circ@res@other))}
            % finally, direction
            % finally, direction
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@ya
        }
        \anchor{body E in}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/#1/curr direction}\pgf@y
            \pgf@y=\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@y
        }
        \anchor{body C out}{
            \northeast
            \pgf@ya=\ctikzvalof{tripoles/#1/curr direction}\pgf@y
            \pgf@ya=\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@ya
            \pgf@y=\pgf@ya
            \left
            \pgf@x=-\ctikzvalof{tripoles/#1/bodydiode distance}\pgf@x
            \pgf@y=\pgf@ya
        }
        \anchor{body E out}{
            \northeast
            \pgf@ya=-\ctikzvalof{tripoles/#1/curr direction}\pgf@y
            \pgf@ya=\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@ya
            \pgf@y=\pgf@ya
            \left
            \pgf@x=-\ctikzvalof{tripoles/#1/bodydiode distance}\pgf@x
            \pgf@y=\pgf@ya
        }
        #2%
        \pgf@circ@draw@component{
            \pgftransformationadjustments
            \pgf@circ@setcolor
            %
            \ifnum \ctikzvalof{tripoles/#1/curr direction} > 0
                \pgf@circuit@trans@ntypetrue
            \else
                \pgf@circuit@trans@ntypefalse
            \fi
            \northeast
            \pgf@circ@res@up = \pgf@y
            \pgf@circ@res@down = -\pgf@y
            \pgf@circ@res@right = \pgf@x
            \left
            \pgf@circ@res@left = \pgf@x
            \pgf@circ@scaled@Rlen=\scaledRlen
            %
            #3%
            % BODY DIODE
            \ifpgf@circuit@fet@bodydiode
                \drawbodydiode{#1}
            \fi
            %
        }
    }
}

\def\drawdobydiodedot#1#2{
    % retrieve dot scale
    \edef\@@tmp{\ctikzvalof{transistor bodydiode/dot scale}}
    \ifdim\@@tmp pt>0pt
        \pgfscope
            \pgftransformshift{\pgfpoint{\pgf@circ@res@right}
                {\ctikzvalof{tripoles/#1/bodydiode conn}#2}}
            \pgftransformscale{\@@tmp}
            % I'm not sure why this is needed, but...
            \pgf@circ@maybe@color{transistor bodydiode/color}
            \pgfnode{circ}{center}{}{}{\pgfusepath{draw,fill}}
        \endpgfscope
    \fi
}

\long\def\drawbodydiode#1{
    \pgfsetlinewidth{\ctikzvalof{transistor bodydiode/relative thickness}\pgflinewidth}
    \pgf@circ@subset@color@dash{transistor bodydiode}
    \pgfscope
        \pgftransformshift{\pgfpoint{-\ctikzvalof{tripoles/#1/bodydiode distance}\pgf@circ@res@left}{\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgftransformrotate{90}
        % diode scale and bodydiode scale interacts. We want the size of the diode
        % proportional to the transistor, so we will:
        % 1) undo diode scale 2) apply transistor scale (using the current class) 3) apply bodydiode scale
        \pgfmathsetmacro{\@@BDscale}{\ctikzvalof{tripoles/#1/bodydiode scale}* \ctikzvalof{\ctikzclass/scale}/\ctikzvalof{diodes/scale}}
        \pgftransformscale{\@@BDscale}
        \ifpgf@circuit@fulldiode
            % I'm not sure why this is needed, but...
            \pgf@circ@maybe@color{transistor bodydiode/color}
            \pgfnode{fulldiodeshape}{center}{}{pgf@bodydiode}{\pgfusepath{fill}}
        \else
            \pgfnode{emptydiodeshape}{center}{}{pgf@bodydiode}{\pgfusepath{fill}}
        \fi
    \endpgfscope
    % Draw stroke line
    \ifpgf@circuit@strokediode
        \pgfpathmoveto{\pgfpointanchor{pgf@bodydiode}{west}}
        \pgfpathlineto{\pgfpointanchor{pgf@bodydiode}{east}}
        \pgfusepath{stroke}
    \fi
    %Draw upper connection to body diode
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{-\ctikzvalof{tripoles/#1/bodydiode distance}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpointanchor{pgf@bodydiode}{east}}
    \pgfusepath{draw}
    \drawdobydiodedot{#1}{\pgf@circ@res@up}
    %Draw lower connection to body diode
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{-\ctikzvalof{tripoles/#1/bodydiode distance}\pgf@circ@res@left}{\ctikzvalof{tripoles/#1/bodydiode conn}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpointanchor{pgf@bodydiode}{west}}
    \pgfusepath{draw}
    \drawdobydiodedot{#1}{\pgf@circ@res@down}
}

\long\def\declarebpt#1{
    \pgfcircdeclaretransistor{#1}{
        \anchor{inner up}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/base height}\pgf@y
        }
        \anchor{inner down}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/#1/base height}\pgf@y
        }
        \anchor{arrows}{
            \northeast
            \pgf@circ@res@up = \pgf@y
            \left
            \pgf@circ@res@left = \pgf@x
            \pgf@x=\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left
            \advance\pgf@x by 0.6\pgf@circ@res@left
            \pgf@y=0.2\pgf@circ@res@up
        }
        }{
        % add the circle if requested (before everything else, so we can fill it)
        \pgfcirc@transistorcircle

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
        {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/#1/base height 2}\pgf@circ@res@up}}
        \pgfusepath{draw}

        % drawing base
        \pgfscope
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down}}
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \ifpgf@circuit@bpt@schottky
                % upper
                \pgfpathmoveto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
                    {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up}}
                \pgfpathlineto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
                    {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up+
                    \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}}
                \pgfpathlineto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left+
                        \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}
                    {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up+
                        \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}}
                \pgfpathlineto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left+
                        \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}
                    {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up}}
                % % lower
                \pgfpathmoveto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
                    {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down}}
                \pgfpathlineto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
                    {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down-
                    \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}}
                \pgfpathlineto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left-
                        \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}
                    {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down-
                    \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}}
                \pgfpathlineto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left-
                        \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}
                    {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down}}
                \fi
            \pgfusepath{draw}
        \endpgfscope

        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/#1/base height 2}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
        {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
        \pgfusepath{draw}
        %draw arrow depending on type of transistor
        \pgfscope
            \pgfslopedattimetrue
            \pgfallowupsidedownattimetrue
            \pgfresetnontranslationattimefalse
            \edef\@@anchor{center}
            \ifpgf@circuit@trans@ntype
                \ifpgf@circuit@trans@arrowatend
                    \edef\@@anchor{btip}
                    \pgftransformlineattime{1.0}{%
                        \pgfpoint%
                        {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}%
                        {\ctikzvalof{tripoles/#1/base height 2}\pgf@circ@res@down}%
                        }{%
                        \pgfpoint{\pgf@circ@res@right}%
                        {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down}%
                    }
                \else
                    \pgftransformlineattime{\ctikzvalof{tripoles/#1/arrow pos}}{%
                        \pgfpoint%
                        {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}%
                        {\ctikzvalof{tripoles/#1/base height 2}\pgf@circ@res@down}%
                        }{%
                        \pgfpoint{\pgf@circ@res@right}%
                        {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down}%
                    }
                \fi
            \else % p-type
                \ifpgf@circuit@trans@arrowatend
                    \edef\@@anchor{tip}
                    \pgftransformlineattime{1.0}{%
                        \pgfpoint{\pgf@circ@res@right}%
                        {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up}%
                        }{%
                        \pgfpoint{\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}%
                        {\ctikzvalof{tripoles/#1/base height 2}\pgf@circ@res@up}%
                    }
                \else
                    \pgftransformlineattime{\ctikzvalof{tripoles/#1/arrow pos}}{%
                        \pgfpoint{\pgf@circ@res@right}%
                        {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up}%
                        }{%
                        \pgfpoint{\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}%
                        {\ctikzvalof{tripoles/#1/base height 2}\pgf@circ@res@up}%
                    }
                \fi
            \fi
            \pgfnode{trarrow}{\@@anchor}{}{}{\pgfusepath{stroke}}
        \endpgfscope

        \ifpgf@circuit@bpt@drawphoto
            \pgfscope
                \pgf@circ@fill@strokecolor
                \pgf@circ@set@optoarrow@style
                \pgfpathmoveto{\pgfpoint
                    {(0.5+\ctikzvalof{tripoles/#1/base width})*\pgf@circ@res@left}
                    {0.3\pgf@circ@res@up}}
                \pgfpathlineto{\pgfpoint
                    {(0.05+\ctikzvalof{tripoles/#1/base width})*\pgf@circ@res@left}
                    {0.1\pgf@circ@res@up}}
                \pgfusepath{draw}
                \pgfpathmoveto{\pgfpoint
                    {(0.5+\ctikzvalof{tripoles/#1/base width})*\pgf@circ@res@left}
                    {0.1\pgf@circ@res@up}}
                \pgfpathlineto{\pgfpoint
                    {(0.05+\ctikzvalof{tripoles/#1/base width})*\pgf@circ@res@left}
                    {-0.1\pgf@circ@res@up}}
                \pgfusepath{draw}
            \endpgfscope
            \else
            \ifpgf@circuit@bpt@drawbase
                \pgfpathmoveto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
                {\pgf@circ@res@up+\pgf@circ@res@down}}
                \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}{\pgf@circ@res@up+\pgf@circ@res@down}}
                \pgfusepath{draw}
            \fi
        \fi
    }
}

\declarebpt{npn}
\declarebpt{pnp}
%
% multi-emitter and multi-collector BJTs by Romano Giannetti
%
\def\pgf@circ@bjt@C@anchor#1{% #1: collector number
    \pgfextractx{\pgf@circ@res@temp}{\basedimension}
    \pgfextracty{\pgf@circ@res@other}{\basedimension}
    \ifnum\cdir>0 % NPN, above
        \pgfpoint{\pgf@circ@res@temp}{%
        \pgf@circ@res@other+\pgfverticaltransformationadjustment*.5*\pgflinewidth+(#1-1)*\multistep}
    \else % PNP, below
        \pgfpoint{\pgf@circ@res@temp}{%
        -\pgf@circ@res@other-\pgfverticaltransformationadjustment*.5*\pgflinewidth-(#1-1)*\multistep}
    \fi
}
\def\pgf@circ@bjt@E@anchor#1{% #1: collector number
    \pgfextractx{\pgf@circ@res@temp}{\basedimension}
    \pgfextracty{\pgf@circ@res@other}{\basedimension}
    \ifnum\cdir<0 % PNP, above
        \pgfpoint{\pgf@circ@res@temp}{%
        \pgf@circ@res@other+\pgfverticaltransformationadjustment*.5*\pgflinewidth+(#1-1)*\multistep}
    \else % PNP, below
        \pgfpoint{\pgf@circ@res@temp}{%
        -\pgf@circ@res@other-\pgfverticaltransformationadjustment*.5*\pgflinewidth-(#1-1)*\multistep}
    \fi
}

\long\def\declarebjt#1{
    \pgfdeclareshape{bjt#1}{
        \savedmacro{\ctikzclass}{\edef\ctikzclass{transistors}}
        \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
        % \cdir is 1 for npn, -1 for pnp
        \savedmacro{\cdir}{\edef\cdir{\ctikzvalof{tripoles/bjt/#1/curr direction}}}
        \savedmacro{\numE}{\edef\numE{\ctikzvalof{tripoles/bjt/emitters}}}
        \savedmacro{\numC}{\edef\numC{\ctikzvalof{tripoles/bjt/collectors}}}
        % step up or down for the additional C/Es
        \saveddimen{\multistep}{\pgfmathsetlength{\pgf@x}{%
            \ctikzvalof{tripoles/bjt/height}*\ctikzvalof{tripoles/bjt/multi height}*
            \ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}}
        \saveddimen{\external}{\pgfmathsetlength{\pgf@x}{%
            \ctikzvalof{tripoles/bjt/pins width}*\ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}}
        \savedanchor\basedimension{% these are the dimensions if nC=1 y nE=1
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgf@y=\ctikzvalof{tripoles/bjt/height}\pgf@circ@scaled@Rlen
            \pgf@y=.5\pgf@y
            \pgf@x=\ctikzvalof{tripoles/bjt/width}\pgf@circ@scaled@Rlen
            \pgf@x=.5\pgf@x
        }
        \savedanchor\northeast{% upper right
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgfmathsetlength{\pgf@circ@res@step}{%
                \ctikzvalof{tripoles/bjt/height}*\ctikzvalof{tripoles/bjt/multi height}*
                \ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
            \ifnum\cdir> 0
                \edef\numup{\numC}\edef\numdown{\numE}
            \else
                \edef\numup{\numE}\edef\numdown{\numC}
            \fi
            \pgfmathsetlength{\pgf@y}{0.5*\ctikzvalof{tripoles/bjt/height}\pgf@circ@scaled@Rlen
                + (\numup-1)*\pgf@circ@res@step+\pgfverticaltransformationadjustment*.5*\pgflinewidth
                + \ctikzvalof{tripoles/bjt/pins width}*\ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
            \pgf@x=\ctikzvalof{tripoles/bjt/width}\pgf@circ@scaled@Rlen
            \pgf@x=.5\pgf@x
        }
        \savedanchor\southeast{% lower right
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgfmathsetlength{\pgf@circ@res@step}{%
                \ctikzvalof{tripoles/bjt/height}*\ctikzvalof{tripoles/bjt/multi height}*
                \ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
            \ifnum\cdir> 0
                \edef\numup{\numC}\edef\numdown{\numE}
            \else
                \edef\numup{\numE}\edef\numdown{\numC}
            \fi
            \pgfmathsetlength{\pgf@y}{-0.5*\ctikzvalof{tripoles/bjt/height}\pgf@circ@scaled@Rlen
                - (\numdown-1)*\pgf@circ@res@step-\pgfverticaltransformationadjustment*.5*\pgflinewidth
                - \ctikzvalof{tripoles/bjt/pins width}*\ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
            \pgf@x=\ctikzvalof{tripoles/bjt/width}\pgf@circ@scaled@Rlen
            \pgf@x=.5\pgf@x
        }
        \savedanchor\southwest{% lower left
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgfmathsetlength{\pgf@circ@res@step}{%
                \ctikzvalof{tripoles/bjt/height}*\ctikzvalof{tripoles/bjt/multi height}*
                \ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
            \ifnum\cdir> 0
                \edef\numup{\numC}\edef\numdown{\numE}
            \else
                \edef\numup{\numE}\edef\numdown{\numC}
            \fi
            \pgfmathsetlength{\pgf@y}{-0.5*\ctikzvalof{tripoles/bjt/height}\pgf@circ@scaled@Rlen
                - (\numdown-1)*\pgf@circ@res@step-\pgfverticaltransformationadjustment*.5*\pgflinewidth
                - \ctikzvalof{tripoles/bjt/pins width}*\ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
            \pgfmathsetlength{\pgf@x}{-0.5*\ctikzvalof{tripoles/bjt/width}*\pgf@circ@scaled@Rlen
                - \ctikzvalof{tripoles/bjt/pins width}*\ctikzvalof{\ctikzclass/scale}*\pgf@circ@Rlen}
        }
        \anchor{center}{\pgfpointorigin}
        \anchor{north}{\northeast\pgf@x=0cm\relax}
        \anchor{east}{\northeast\pgf@y=0cm\relax}
        \anchor{south}{\southwest\pgf@x=0cm\relax}
        \anchor{west}{\southwest\pgf@y=0cm}
        \anchor{north east}{\northeast}
        \anchor{north west}{\northeast\pgf@ya=\pgf@y\southwest\pgf@y=\pgf@ya}
        \anchor{south west}{\southwest}
        \anchor{south east}{\southeast}

        \anchor{text}{\northeast\pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax}
        \anchor{B}{\northeast\pgf@y=0cm\pgf@x=-\pgf@x\advance\pgf@x by -\external}
        \anchor{base}{\basedimension\pgf@y=0cm\pgf@x=-\pgf@x\advance\pgf@x by -\external}
        \anchor{nobase}{\basedimension\pgf@y=0cm\pgf@x=-\pgf@x}
        % center of the base "bar"
        \anchor{cbase}{\basedimension\pgf@x=-\pgf@x
            \pgfmathsetlength{\pgf@y}{\cdir*(\numC-\numE)*\multistep/2}%
        }
        % geometrical centers
        \anchor{vcenter}{\pgf@x=0cm\relax
            \pgfmathsetlength{\pgf@y}{\cdir*(\numC-\numE)*\multistep/2}%
        }
        \anchor{gcenter}{%
            \northeast\pgf@xa=0.5\pgf@x
            \southwest\advance\pgf@xa by 0.5\pgf@x
            \pgf@x=\pgf@xa
            \pgfmathsetlength{\pgf@y}{\cdir*(\numC-\numE)*\multistep/2}%
        }
        % external connections
        \anchor{E}{
            \ifnum\cdir>0% npn, emitter down
            \southeast
            \else
            \northeast
            \fi
        }% first emitter
        \anchor{emitter}{\ifnum\cdir>0\southeast\else\northeast\fi}% first emitter
        \anchor{C}{\ifnum\cdir<0\southeast\else\northeast\fi}
        \anchor{collector}{\ifnum\cdir<0\southeast\else\northeast\fi}

        \pgf@circ@draw@component{
            \pgftransformationadjustments
            \pgf@circ@setcolor
            %
            % set the type and up and down number of connections
            %
            \ifnum\cdir> 0
                \pgf@circuit@trans@ntypetrue
                \edef\numup{\numC}
                \edef\numdown{\numE}
            \else
                \pgf@circuit@trans@ntypefalse
                \edef\numup{\numE}
                \edef\numdown{\numC}
            \fi
            \basedimension
            \pgf@circ@res@up = \pgf@y
            \pgf@circ@res@down = -\pgf@y
            \pgf@circ@res@right = \pgf@x
            \pgf@circ@res@left = -\pgf@x
            \pgf@circ@scaled@Rlen=\scaledRlen
            \pgf@circ@res@step=\multistep
            %
            % set arrow positions options
            %
            \edef\@@anchor{center}\edef\@@pos{\ctikzvalof{tripoles/#1/arrow pos}}
            \ifpgf@circuit@trans@arrowatend
                \edef\@@pos{1.0}
                \ifpgf@circuit@trans@ntype % arrow is toward outside, push it a bit
                    \edef\@@anchor{btip}
                \else
                    \edef\@@anchor{tip}
                \fi
            \fi
            %
            % Drawing upper connections
            %
            \pgfscope
            \pgf@circ@count@a=\numup\relax
            \pgfmathloop%
            \ifnum\pgf@circ@count@a>0
            \advance\pgf@circ@count@a-1\relax
            \pgfmathsetlength{\pgf@circ@res@other}{\the\pgf@circ@count@a*\multistep}%
            \ifnum\pgf@circ@count@a=\numexpr\numup-1\relax % draw the external pin connection
            \pgfpathmoveto{\pgfpoint
                {\pgf@circ@res@right}%
                {\pgf@circ@res@up+\external+\pgfverticaltransformationadjustment*.5*\pgflinewidth+\pgf@circ@res@other}}%
            \pgfpathlineto{\pgfpoint
                {\pgf@circ@res@right}%
                {\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth+\pgf@circ@res@other}}%
            \else
            \pgfpathmoveto{\pgfpoint
                {\pgf@circ@res@right}%
                {\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth+\pgf@circ@res@other}}%
            \fi
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}%
                {\ctikzvalof{tripoles/bjt/base height 2}\pgf@circ@res@up+\pgf@circ@res@other}}%
            \pgfsetroundcap % better when connecting to sloped lines
            \pgfusepath{draw}
            \ifpgf@circuit@trans@ntype\else % it's a PNP; draw arrow
            \pgfscope
                \pgfslopedattimetrue
                \pgfallowupsidedownattimetrue
                \pgfresetnontranslationattimefalse
                \pgftransformlineattime{\@@pos}{%
                    \pgfpoint
                        {\pgf@circ@res@right}%
                        {\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth+\pgf@circ@res@other}%
                    }{\pgfpoint
                        {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}%
                        {\ctikzvalof{tripoles/bjt/base height 2}\pgf@circ@res@up+\pgf@circ@res@other}%
                    }
                    \pgfnode{trarrow}{\@@anchor}{}{}{\pgfusepath{stroke}}
            \endpgfscope
            \fi
            \repeatpgfmathloop
            \endpgfscope
            %
            % Drawing base
            %
            \pgfscope
                \pgfpathmoveto{\pgfpoint
                    {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}
                    {\ctikzvalof{tripoles/bjt/base height}\pgf@circ@res@up+(\numup-1)*\multistep}}
                \pgfpathlineto{\pgfpoint
                    {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}
                    {\ctikzvalof{tripoles/bjt/base height}\pgf@circ@res@down-(\numdown-1)*\multistep}}
                \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
                \ifpgf@circuit@bpt@schottky
                    % upper
                    \pgfpathmoveto{\pgfpoint
                        {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}
                        {\ctikzvalof{tripoles/bjt/base height}\pgf@circ@res@up+(\numup-1)*\multistep}}
                    \pgfpathlineto{\pgfpoint
                        {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}
                        {\ctikzvalof{tripoles/bjt/base height}\pgf@circ@res@up+(\numup-1)*\multistep+
                        \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}}
                    \pgfpathlineto{\pgfpoint
                        {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left+
                            \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}
                        {\ctikzvalof{tripoles/bjt/base height}\pgf@circ@res@up+(\numup-1)*\multistep+
                            \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}}
                    \pgfpathlineto{\pgfpoint
                        {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left+
                            \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}
                        {\ctikzvalof{tripoles/bjt/base height}\pgf@circ@res@up+(\numup-1)*\multistep}}
                    % lower
                    \pgfpathmoveto{\pgfpoint
                        {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}
                        {\ctikzvalof{tripoles/bjt/base height}\pgf@circ@res@down-(\numdown-1)*\multistep}}
                    \pgfpathlineto{\pgfpoint
                        {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}
                        {\ctikzvalof{tripoles/bjt/base height}\pgf@circ@res@down-(\numdown-1)*\multistep-
                        \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}}
                    \pgfpathlineto{\pgfpoint
                        {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left-
                            \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}
                        {\ctikzvalof{tripoles/bjt/base height}\pgf@circ@res@down-(\numdown-1)*\multistep-
                        \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}}
                    \pgfpathlineto{\pgfpoint
                        {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left-
                            \ctikzvalof{tripoles/schottky base size}\pgf@circ@scaled@Rlen}
                        {\ctikzvalof{tripoles/bjt/base height}\pgf@circ@res@down-(\numdown-1)*\multistep}}
                \fi
                \pgfsetroundcap % I like it more...
                \pgfusepath{draw}
            \endpgfscope
            %
            % draw base external connection
            %
            \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}{0pt}}
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left-\external}{0pt}}
            \pgfusepath{draw}
            %
            % Drawing lower connections
            %
            \pgfscope
            \pgf@circ@count@a=\numdown\relax
            \pgfmathloop%
            \ifnum\pgf@circ@count@a>0
            \advance\pgf@circ@count@a-1\relax
            \pgfmathsetlength{\pgf@circ@res@other}{\the\pgf@circ@count@a*\multistep}%
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}
                {\ctikzvalof{tripoles/bjt/base height 2}\pgf@circ@res@down-\pgf@circ@res@other}}
            \pgfpathlineto{\pgfpoint
                {\pgf@circ@res@right}
                {\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth-\pgf@circ@res@other}}
            \ifnum\pgf@circ@count@a=\numexpr\numdown-1\relax % draw the external pin connection
            \pgfpathlineto{\pgfpoint
                {\pgf@circ@res@right}
                {\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth-\pgf@circ@res@other-\external}}
            \fi
            \pgfsetroundcap % better when connecting to sloped lines
            \pgfusepath{draw}
            \ifpgf@circuit@trans@ntype % it's a NPN; draw arrow
            \pgfscope
                \pgfslopedattimetrue
                \pgfallowupsidedownattimetrue
                \pgfresetnontranslationattimefalse
                \pgftransformlineattime{\@@pos}{%
                    \pgfpoint
                        {\ctikzvalof{tripoles/bjt/base width}\pgf@circ@res@left}%
                        {\ctikzvalof{tripoles/bjt/base height 2}\pgf@circ@res@down-\pgf@circ@res@other}%
                    }{\pgfpoint
                        {\pgf@circ@res@right}%
                        {\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth-\pgf@circ@res@other}%
                    }
                    \pgfnode{trarrow}{\@@anchor}{}{}{\pgfusepath{stroke}}
            \endpgfscope
            \fi
            \repeatpgfmathloop
            \endpgfscope
        }
        % \pgf@sh@s@<name of the shape here> contains all the code for the shape
        % and is executed just before a node is drawn.
        \expandafter\pgfutil@g@addto@macro\csname pgf@sh@s@bjt#1\endcsname{%
            % Start with the maximum collector number and go backwards.
            \pgf@circ@count@a=\numC\relax
            \pgfmathloop%
            \ifnum\pgf@circ@count@a>0
                % we will create two anchors per pin: the "normal one" like `pin 1` for the
                % electrical contact, and the "border one" like `bpin 1` for labels.
                % they will coincide if `external pins width` is set to 0.
                \expandafter\xdef\csname pgf@anchor@bjt#1@C\the\pgf@circ@count@a\endcsname{%
                    \noexpand\pgf@circ@bjt@C@anchor{\the\pgf@circ@count@a}%
                }
            \advance\pgf@circ@count@a by -1\relax%
            \repeatpgfmathloop%
            % and emitters
            \pgf@circ@count@a=\numE\relax
            \pgfmathloop%
            \ifnum\pgf@circ@count@a>0
                % we will create two anchors per pin: the "normal one" like `pin 1` for the
                % electrical contact, and the "border one" like `bpin 1` for labels.
                % they will coincide if `external pins width` is set to 0.
                \expandafter\xdef\csname pgf@anchor@bjt#1@E\the\pgf@circ@count@a\endcsname{%
                    \noexpand\pgf@circ@bjt@E@anchor{\the\pgf@circ@count@a}%
                }
            \advance\pgf@circ@count@a by -1\relax%
            \repeatpgfmathloop%
            }%
    }
}

\declarebjt{npn}
\declarebjt{pnp}

% end of multi-bjts

\long\def\declareigbt#1{
    \pgfcircdeclaretransistor{#1}{
        \anchor{inner up}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/gate height}\pgf@y
        }
        \anchor{inner down}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/#1/gate height}\pgf@y
        }
        \anchor{nobase}{
            \left
            \pgf@x=\ctikzvalof{tripoles/#1/gate width}\pgf@x
        }
    }
    {
        % add the circle if requested (before everything else, so we can fill it)
        \pgfcirc@transistorcircle
        % fill the gap color if requested
        \pgfcirc@fillgategap{#1}
        %draw upper connection
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
        {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@up}}
        \pgfusepath{draw}

        %draw thicker gate lines
        \pgfscope
            \pgfscope
                \pgfpathmoveto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
                {\ctikzvalof{tripoles/#1/outer base height}\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5\pgflinewidth}}
                \pgfpathlineto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
                {\ctikzvalof{tripoles/#1/outer base height}\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5\pgflinewidth}}
                % set the normal thickness
                \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
                \edef\@@extrat{\ctikzvalof{tripoles/#1/outer base thickness}}
                \pgfsetlinewidth{\@@extrat\pgflinewidth}
                \pgfusepath{draw}
            \endpgfscope
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5\pgflinewidth}}
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5\pgflinewidth}}
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgfusepath{draw}
        \endpgfscope
        %draw lower connection
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
        {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
        \pgfusepath{draw}
        %draw arrow depending on type of transistor
        \pgfscope
            \pgfslopedattimetrue
            \pgfallowupsidedownattimetrue
            \pgfresetnontranslationattimefalse
            \ifpgf@circuit@trans@arrowatend
                \ifpgf@circuit@trans@ntype
                    \edef\@@anchor{btip}\edef\@@pos{1.0}
                \else
                    \edef\@@anchor{tip}\edef\@@pos{1.0}
                \fi
            \else
                \edef\@@anchor{center}\edef\@@pos{0.5}
            \fi
            \ifpgf@circuit@trans@ntype
                \pgftransformlineattime{\@@pos}{%
                    \pgfpoint%
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@down}%
                    }{%
                    \pgfpoint{\pgf@circ@res@right}%
                    {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@down}%
                }
            \else
                \pgftransformlineattime{\@@pos}{%
                    \pgfpoint{\pgf@circ@res@right}%
                    {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@up}%
                    }{%
                    \pgfpoint{\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@up}%
                }
            \fi
            \pgfnode{trarrow}{\@@anchor}{}{}{\pgfusepath{stroke}}
        \endpgfscope
        %draw gate
        \ifpgf@circuit@bpt@drawgate
            \ifpgf@circuit@trans@ntype
                \pgfpathmoveto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
                {\ctikzvalof{tripoles/#1/conn height}\pgf@circ@res@down}}
                \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}%
                {\ctikzvalof{tripoles/#1/conn height}\pgf@circ@res@down}}
            \else
                \pgfpathmoveto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
                {\ctikzvalof{tripoles/#1/conn height}\pgf@circ@res@up}}
                \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}%
                {\ctikzvalof{tripoles/#1/conn height}\pgf@circ@res@up}}
            \fi
        \fi
        \pgfusepath{draw}
    }
}

\declareigbt{pigbt}
\declareigbt{nigbt}
\declareigbt{Lnigbt}
\declareigbt{Lpigbt}

% Graphene FET, See https://github.com/circuitikz/circuitikz/issues/496
\long\def\declaregfet#1{
    \pgfcircdeclaretransistor{#1}{
        \anchor{inner up}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/gate height}\pgf@y
        }
        \anchor{inner down}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/#1/gate height}\pgf@y
        }
        \anchor{outer hex up}{
            \left
            \pgf@xa=\ctikzvalof{tripoles/#1/base width}\pgf@x
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/gate height 2}\pgf@y
            \pgf@x=-\pgf@xa
        }
        \anchor{outer hex down}{
            \left
            \pgf@xa=\ctikzvalof{tripoles/#1/base width}\pgf@x
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/gate height 2}\pgf@y
            \pgf@y=-\pgf@y
            \pgf@x=-\pgf@xa
        }
        \anchor{inner hex up}{
            \left
            \pgf@xa=\ctikzvalof{tripoles/#1/base width}\pgf@x
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/gate height 2}\pgf@y
            \pgf@x=\pgf@xa
        }
        \anchor{inner hex down}{
            \left
            \pgf@xa=\ctikzvalof{tripoles/#1/base width}\pgf@x
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/gate height 2}\pgf@y
            \pgf@y=-\pgf@y
            \pgf@x=\pgf@xa
        }
        \anchor{right}{
            \northeast\pgf@y=0pt\relax
            \pgfmathsetlength{\pgf@xa}{\ctikzvalof{tripoles/#1/base width}*
                \ctikzvalof{tripoles/#1/width}*\pgf@circ@scaled@Rlen}
            \advance \pgf@x by \pgf@xa
        }
        \anchor{text}{% need a different text: no circle, shift to the right
            \northeast
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \ifpgf@circuit@transisors@fixlabels
                \ifpgf@circuit@fet@bodydiode
                    \advance \pgf@x by \extrabodydiodelen
                \else
                    \pgfmathsetlength{\pgf@xa}{\ctikzvalof{tripoles/#1/base width}*
                        \ctikzvalof{tripoles/#1/width}*\pgf@circ@scaled@Rlen}
                    \advance \pgf@x by \pgf@xa
                \fi
                % no circle for gfets
                % add a bit of space to avoid central (substrate) terminal if drawn
                \advance\pgf@x by 0.05\pgf@circ@scaled@Rlen\relax
                \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
            \else
                \pgf@y=.7\pgf@y
                \pgf@x= \pgf@circ@scaled@Rlen
                \pgf@x=0.1\pgf@x
            \fi
        }
    }
    {
        % no circle for gfets (clearly!)
        %draw upper connection to hexagon
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
            {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@up}}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}
            {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
        \pgfusepath{draw}
        % draw (fillable) hexagon
        \pgfscope
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
                {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
                {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint
                 {-\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
                 {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint
                 {-\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
                 {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
                 {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
                {-\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@up}}
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope
        %draw thicker gate lines
        \pgfscope
            \pgfscope
                \pgfpathmoveto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
                {\ctikzvalof{tripoles/#1/outer base height}\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5\pgflinewidth}}
                \pgfpathlineto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
                {\ctikzvalof{tripoles/#1/outer base height}\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5\pgflinewidth}}
                % set the normal thickness
                \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
                \edef\@@extrat{\ctikzvalof{tripoles/#1/outer base thickness}}
                \pgfsetlinewidth{\@@extrat\pgflinewidth}
                \pgfusepath{draw}
            \endpgfscope
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5\pgflinewidth}}
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5\pgflinewidth}}
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgfusepath{draw}
        \endpgfscope
        %draw arrow depending on type of transistor
        \pgfscope
            \pgfslopedattimetrue
            \pgfallowupsidedownattimetrue
            \pgfresetnontranslationattimefalse
            \ifpgf@circuit@trans@arrowatend
                \ifpgf@circuit@trans@ntype
                    \edef\@@anchor{btip}\edef\@@pos{1.0}
                \else
                    \edef\@@anchor{tip}\edef\@@pos{1.0}
                \fi
            \else
                \edef\@@anchor{center}\edef\@@pos{0.5}
            \fi
            \ifpgf@circuit@trans@ntype
                \pgftransformlineattime{\@@pos}{%
                    \pgfpoint%
                    {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@down}%
                    }{%
                    \pgfpoint{\pgf@circ@res@right}%
                    {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@down}%
                }
            \else
                \pgftransformlineattime{\@@pos}{%
                    \pgfpoint{\pgf@circ@res@right}%
                    {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@up}%
                    }{%
                    \pgfpoint{\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@up}%
                }
            \fi
            \pgfnode{trarrow}{\@@anchor}{}{}{\pgfusepath{stroke}}
        \endpgfscope
        %draw gate
        \ifpgf@circuit@trans@ntype
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/conn height}\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}%
            {\ctikzvalof{tripoles/#1/conn height}\pgf@circ@res@down}}
        \else
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/conn height}\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}%
            {\ctikzvalof{tripoles/#1/conn height}\pgf@circ@res@up}}
        \fi
        \pgfusepath{draw}
    }
}

\declaregfet{pgfet}
\declaregfet{ngfet}

% Plain MOSes

\pgfcircdeclaretransistor{nmos}{
        \anchor{inner up}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/nmos/gate height}\pgf@y
        }
        \anchor{inner down}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/nmos/gate height}\pgf@y
        }
    }{%
    % add the circle if requested (before everything else, so we can fill it)
    \pgfcirc@transistorcircle
    % fill the gap color if requested
    \pgfcirc@fillgategap{nmos}

    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/nmos/gate height}\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint
        {\ctikzvalof{tripoles/nmos/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/nmos/gate height}\pgf@circ@res@up}}

    \pgfpathmoveto{\pgfpoint
        {\ctikzvalof{tripoles/nmos/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/nmos/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/nmos/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfusepath{draw}

    \pgfscope
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/nmos/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/nmos/base height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/nmos/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/nmos/base height}\pgf@circ@res@down}}
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/nmos/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/nmos/gate height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/nmos/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/nmos/gate height}\pgf@circ@res@down}}
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfusepath{draw}
    \endpgfscope
    \ifpgf@circuit@mos@arrows
        \pgfscope
            \ifpgf@circuit@trans@arrowatend
                \pgftransformshift{\pgfpoint
                    {\pgf@circ@res@right}%
                    {\ctikzvalof{tripoles/pmos/gate height}\pgf@circ@res@down}%
                }
                \pgfnode{trarrow}{btip}{}{}{\pgfusepath{stroke}}
            \else
                \pgfslopedattimetrue
                \pgfallowupsidedownattimetrue
                \pgfresetnontranslationattimefalse
                \pgftransformlineattime{\ctikzvalof{tripoles/nmos/arrow pos}}{%
                    \pgfpoint%
                    {\ctikzvalof{tripoles/nmos/gate width}\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/nmos/gate height}\pgf@circ@res@down}%
                    }{%
                    \pgfpoint
                    {\pgf@circ@res@right}%
                    {\ctikzvalof{tripoles/nmos/gate height}\pgf@circ@res@down}%
                }
                \pgfnode{currarrow}{center}{}{}{\pgfusepath{stroke}}
            \fi
        \endpgfscope
    \fi

    \ifpgf@circuit@bpt@drawgate
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/nmos/gate width}\pgf@circ@res@left}
        {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}{\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfusepath{draw}
    \fi

    \ifpgf@circuit@bpt@drawbulk % added by Burak Kelleci
        \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/nmos/base width}\pgf@circ@res@left}
                {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right+\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}
                {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfusepath{draw}
    \fi

    \pgfcirc@ferroelectric{nmos}{0}
}

\pgfcircdeclaretransistor{pmos}{
        \anchor{inner up}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/pmos/gate height}\pgf@y
        }
        \anchor{inner down}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/pmos/gate height}\pgf@y
        }
    }{%
    % add the circle if requested (before everything else, so we can fill it)
    \pgfcirc@transistorcircle
    % fill the gap color if requested
    \pgfcirc@fillgategap{nmos}

    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/pmos/gate height}\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint
        {\ctikzvalof{tripoles/pmos/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/pmos/gate height}\pgf@circ@res@up}}
    \pgfusepath{draw}

    \ifpgf@circuit@mos@arrows
        \pgfscope
            \ifpgf@circuit@trans@arrowatend
                \pgftransformshift{\pgfpoint
                    {\ctikzvalof{tripoles/pmos/base width}\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/pmos/gate height}\pgf@circ@res@up}%
                }
                \pgftransformrotate{180}
                \pgfnode{trarrow}{tip}{}{}{\pgfusepath{stroke}}
            \else
                \pgfslopedattimetrue
                \pgfallowupsidedownattimetrue
                \pgfresetnontranslationattimefalse
                \pgftransformlineattime{\ctikzvalof{tripoles/pmos/arrow pos}}{%
                    \pgfpoint%
                    {\pgf@circ@res@right}%
                    {\ctikzvalof{tripoles/pmos/gate height}\pgf@circ@res@up}%
                    }{%
                    \pgfpoint
                    {\ctikzvalof{tripoles/pmos/gate width}\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/pmos/gate height}\pgf@circ@res@up}%
                }
                \pgfnode{currarrow}{center}{}{}{\pgfusepath{stroke}}
            \fi
        \endpgfscope
    \fi

    \pgfscope
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/pmos/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/pmos/base height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/pmos/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/pmos/base height}\pgf@circ@res@down}}
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/pmos/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/pmos/gate height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/pmos/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/pmos/gate height}\pgf@circ@res@down}}
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfusepath{draw}
    \endpgfscope

    \pgfpathmoveto{\pgfpoint
        {\ctikzvalof{tripoles/pmos/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/pmos/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/pmos/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}


    \pgfusepath{draw}
    \ifpgf@circuit@bpt@drawgate
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/pmos/gate width}\pgf@circ@res@left}
        {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}{\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfusepath{draw}
    \fi

    \ifpgf@circuit@bpt@drawbulk % added by Burak Kelleci
        \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/pmos/base width}\pgf@circ@res@left}
                {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right+\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}
                {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfusepath{draw}
    \fi

    \ifpgf@circuit@pmos@nocircle\else
        % we are not scaling the circle with the MOS --- I think it's better to have it
        % coherent with the poles/nodes of the rest of the circuit.
        \pgfpathcircle{\pgfpoint
            {\ctikzvalof{tripoles/pmos/gate width}\pgf@circ@res@left - \ctikzvalof{nodes width}*\pgf@circ@Rlen}
        {\pgf@circ@res@up+\pgf@circ@res@down}}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}
        \ifpgf@circuit@pmos@emptycircle
            \pgfsetfillcolor{white}
        \fi
        \pgfusepath{draw,fill}
    \fi

    \pgfcirc@ferroelectric{pmos}{0}
}

%%% depletion MOSFET (simplified symbols)

\long\def\pgfcirc@filldraw@depletion#1{%
    \ifx\tikz@fillcolor\pgfutil@empty
        % if there is no explicit fill check the specific key
        \edef\@@tmp{\ctikzvalof{tripoles/#1/depletion color}}\edef\@@none{none}%
        \ifx\@@tmp\@@none % if  it's none
            \pgfusepath{draw}%
        \else
            \edef\@@default{default}%
            \ifx\@@tmp\@@default % fill with the pen color
                \pgf@circ@fill@strokecolor
                \pgfusepath{draw, fill}%
            \else
                \pgfsetfillcolor{\@@tmp}%
                \pgfusepath{draw, fill}%
            \fi
        \fi
    \else
        \pgfsetfillcolor{\tikz@fillcolor}%
        \pgfusepath{draw, fill}%
    \fi
}

\pgfcircdeclaretransistor{nmosd}{
        \anchor{inner up}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/nmosd/gate height}\pgf@y
        }
        \anchor{inner down}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/nmosd/gate height}\pgf@y
        }
        \anchor{nobulk}{ %override
            \left
            \pgf@circ@res@temp=\ctikzvalof{tripoles/nmosd/depletion width}\pgf@x
            \pgf@x=\ctikzvalof{tripoles/nmosd/base width}\pgf@x
            \advance\pgf@x by -\pgf@circ@res@temp
        }
    }{%
    % draw depletion channel
    % add the circle if requested (before everything else, so we can fill it)
    \pgfcirc@transistorcircle
    % fill the gap color if requested
    \pgfcirc@fillgategap{nmos}
    \pgfscope
        \pgfpathrectanglecorners
        {\pgfpoint
        {\ctikzvalof{tripoles/nmosd/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/nmosd/gate height}\pgf@circ@res@up}}
        {\pgfpoint
        {(\ctikzvalof{tripoles/nmosd/base width} - \ctikzvalof{tripoles/nmosd/depletion width})*\pgf@circ@res@left}
        {-\ctikzvalof{tripoles/nmosd/gate height}\pgf@circ@res@up}}
        \pgfcirc@filldraw@depletion{nmosd}
    \endpgfscope
    % draw drain and source terminals
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\ctikzvalof{tripoles/nmosd/gate height}\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint
        {\ctikzvalof{tripoles/nmosd/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/nmosd/gate height}\pgf@circ@res@up}}

    \pgfpathmoveto{\pgfpoint
        {\ctikzvalof{tripoles/nmosd/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/nmosd/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/nmosd/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfusepath{draw}

    \pgfscope
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/nmosd/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/nmosd/base height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/nmosd/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/nmosd/base height}\pgf@circ@res@down}}
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/nmosd/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/nmosd/gate height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/nmosd/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/nmosd/gate height}\pgf@circ@res@down}}
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfusepath{draw}
    \endpgfscope
    \ifpgf@circuit@mos@arrows
        \pgfscope
            \ifpgf@circuit@trans@arrowatend
                \pgftransformshift{\pgfpoint
                    {\pgf@circ@res@right}%
                    {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@down}%
                }
                \pgfnode{trarrow}{btip}{}{}{\pgfusepath{stroke}}
            \else
                \pgfslopedattimetrue
                \pgfallowupsidedownattimetrue
                \pgfresetnontranslationattimefalse
                \pgftransformlineattime{\ctikzvalof{tripoles/nmosd/arrow pos}}{%
                    \pgfpoint%
                    {\ctikzvalof{tripoles/nmosd/gate width}\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/nmosd/gate height}\pgf@circ@res@down}%
                    }{%
                    \pgfpoint
                    {\pgf@circ@res@right-\ctikzvalof{tripoles/nmosd/depletion width}*\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/nmosd/gate height}\pgf@circ@res@down}%
                }
                \pgfnode{currarrow}{center}{}{}{\pgfusepath{stroke}}
            \fi
        \endpgfscope
    \fi

    \ifpgf@circuit@bpt@drawgate
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/nmosd/gate width}\pgf@circ@res@left}
        {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}{\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfusepath{draw}
    \fi

    \ifpgf@circuit@bpt@drawbulk % added by Burak Kelleci
        \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/nmosd/base width}\pgf@circ@res@left-\ctikzvalof{tripoles/nmosd/depletion width}*\pgf@circ@res@left}
                {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right+\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}
                {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfusepath{draw}
    \fi

    \pgfcirc@ferroelectric{nmosd}{\ctikzvalof{tripoles/nmosd/depletion width}}
}

\pgfcircdeclaretransistor{pmosd}{
        \anchor{inner up}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/pmosd/gate height}\pgf@y
        }
        \anchor{inner down}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/pmosd/gate height}\pgf@y
        }
        \anchor{nobulk}{ %override
            \left
            \pgf@circ@res@temp=\ctikzvalof{tripoles/pmosd/depletion width}\pgf@x
            \pgf@x=\ctikzvalof{tripoles/pmosd/base width}\pgf@x
            \advance\pgf@x by -\pgf@circ@res@temp
        }
    }{%
    % add the circle if requested (before everything else, so we can fill it)
    \pgfcirc@transistorcircle
    % fill the gap color if requested
    \pgfcirc@fillgategap{nmos}
    % draw depletion channel
    \pgfscope
        \pgfpathrectanglecorners
        {\pgfpoint
        {\ctikzvalof{tripoles/pmosd/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@up}}
        {\pgfpoint
        {(\ctikzvalof{tripoles/pmosd/base width} - \ctikzvalof{tripoles/pmosd/depletion width})*\pgf@circ@res@left}
        {-\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@up}}
        \pgfcirc@filldraw@depletion{pmosd}
    \endpgfscope
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint
        {\ctikzvalof{tripoles/pmosd/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@up}}
    \pgfusepath{draw}

    \ifpgf@circuit@mos@arrows
        \pgfscope
            \ifpgf@circuit@trans@arrowatend
                \pgftransformshift{\pgfpoint
                    {\ctikzvalof{tripoles/pmosd/base width}\pgf@circ@res@left-\ctikzvalof{tripoles/nmosd/depletion width}*\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@up}%
                }
                \pgftransformrotate{180}
                \pgfnode{trarrow}{tip}{}{}{\pgfusepath{stroke}}
            \else
                \pgfslopedattimetrue
                \pgfallowupsidedownattimetrue
                \pgfresetnontranslationattimefalse
                \pgftransformlineattime{\ctikzvalof{tripoles/pmosd/arrow pos}}{%
                    \pgfpoint%
                    {\pgf@circ@res@right-\ctikzvalof{tripoles/nmosd/depletion width}*\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@up}%
                    }{%
                    \pgfpoint
                    {\ctikzvalof{tripoles/pmosd/gate width}\pgf@circ@res@left}%
                    {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@up}%
                }
                \pgfnode{currarrow}{center}{}{}{\pgfusepath{stroke}}
            \fi
        \endpgfscope
    \fi

    \pgfscope
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/pmosd/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/pmosd/base height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/pmosd/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/pmosd/base height}\pgf@circ@res@down}}
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/pmosd/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/pmosd/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@down}}
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfusepath{draw}
    \endpgfscope

    \pgfpathmoveto{\pgfpoint
        {\ctikzvalof{tripoles/pmosd/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/pmosd/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}


    \pgfusepath{draw}
    \ifpgf@circuit@bpt@drawgate
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/pmosd/gate width}\pgf@circ@res@left}
        {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}{\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfusepath{draw}
    \fi

    \ifpgf@circuit@bpt@drawbulk % added by Burak Kelleci
        \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/pmosd/base width}\pgf@circ@res@left-\ctikzvalof{tripoles/nmosd/depletion width}*\pgf@circ@res@left}
                {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right+\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}
                {\pgf@circ@res@up+\pgf@circ@res@down}}
        \pgfusepath{draw}
    \fi

    \ifpgf@circuit@pmos@nocircle\else
        % we are not scaling the circle with the MOS --- I think it's better to have it
        % coherent with the poles/nodes of the rest of the circuit.
        \pgfpathcircle{\pgfpoint
            {\ctikzvalof{tripoles/pmosd/gate width}\pgf@circ@res@left - \ctikzvalof{nodes width}*\pgf@circ@Rlen}
        {\pgf@circ@res@up+\pgf@circ@res@down}}{\ctikzvalof{nodes width}*\pgf@circ@Rlen}
        \ifpgf@circuit@pmos@emptycircle
            \pgfsetfillcolor{white}
        \fi
        \pgfusepath{draw,fill}
    \fi

    \pgfcirc@ferroelectric{pmosd}{\ctikzvalof{tripoles/pmosd/depletion width}}
}
%% HEMT FET Transistor
\def\pgf@circ@hemt@gate@anchor@helper{%
    \northeast
    \pgf@circ@res@up = \pgf@y
    \pgf@circ@res@down = -\pgf@y
    \pgfmathsetlength{\pgf@circ@res@step}{{(1+\gateasym)*\pgf@circ@res@up+(1-\gateasym)*\pgf@circ@res@down}}
    \left
    \pgf@y=\pgf@circ@res@step
}
\pgfcircdeclaretransistor{hemt}{
        \savedmacro{\gateasym}{\edef\gateasym{\ctikzvalof{tripoles/hemt/gate asym}}}
        \anchor{inner up}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/hemt/gate height}\pgf@y
        }
        \anchor{inner down}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/hemt/gate height}\pgf@y
        }
        % override gate/base anchors
        \anchor{G}{\pgf@circ@hemt@gate@anchor@helper}
        \anchor{gate}{\pgf@circ@hemt@gate@anchor@helper}
        \anchor{nogate}{\pgf@circ@hemt@gate@anchor@helper\pgf@x=\ctikzvalof{tripoles/hemt/base width}\pgf@x}
        \anchor{B}{\pgf@circ@hemt@gate@anchor@helper}
        \anchor{base}{\pgf@circ@hemt@gate@anchor@helper}
        \anchor{nobase}{\pgf@circ@hemt@gate@anchor@helper\pgf@x=\ctikzvalof{tripoles/hemt/base width}\pgf@x}
    }{%
    % add the circle if requested (before everything else, so we can fill it)
    \pgfcirc@transistorcircle
    % upper connection
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/hemt/gate height}\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint
        {\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/hemt/gate height}\pgf@circ@res@up}}
    % lower connection
    \pgfpathmoveto{\pgfpoint
        {\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/hemt/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
        {\ctikzvalof{tripoles/hemt/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint
        {\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfusepath{draw}
    \pgfscope
        \edef\@@tmp{\ctikzvalof{tripoles/hemt/source arrow}}
        \ifnum\@@tmp=0 \else
            \ifpgf@circuit@trans@arrowatend
                \ifnum\@@tmp > 0 %
                    \pgftransformshift{\pgfpoint
                        {\pgf@circ@res@right}
                        {\ctikzvalof{tripoles/hemt/gate height}\pgf@circ@res@down}}
                \else
                    \pgftransformshift{\pgfpoint
                        {\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
                        {\ctikzvalof{tripoles/hemt/gate height}\pgf@circ@res@down}}
                        \ifnum\@@tmp < 0 \pgftransformrotate{180}\fi
                \fi
                    \pgfnode{trarrow}{tip}{}{}{\pgfusepath{stroke}}
            \else
                % the position here is a bit strange...
                \pgftransformshift{\pgfpoint
                    {0.5*\pgf@circ@res@right+0.5*\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
                    {\ctikzvalof{tripoles/hemt/gate height}\pgf@circ@res@down}}
                    \ifnum\@@tmp < 0 \pgftransformrotate{180}\fi
                    \pgfnode{trarrow}{center}{}{}{\pgfusepath{stroke}}
            \fi
        \fi
    \endpgfscope
    \pgfscope
        % draw gate (base) bar
        \ifpgf@circ@hemt@split
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
                {\ctikzvalof{tripoles/hemt/base height}\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
                {(1.1+\gateasym)*\pgf@circ@res@up +
                 (0.9-\gateasym)*\pgf@circ@res@down}}
            \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
                {(1.05+\gateasym)*\pgf@circ@res@up +
                 (0.95-\gateasym)*\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
                {(0.95+\gateasym)*\pgf@circ@res@up +
                 (1.05-\gateasym)*\pgf@circ@res@down}}
            \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
                {(0.9+\gateasym)*\pgf@circ@res@up +
                 (1.1-\gateasym)*\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/hemt/base height}\pgf@circ@res@down}}
        \else
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/hemt/base height}\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/hemt/base height}\pgf@circ@res@down}}
        \fi
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfusepath{draw}
    \endpgfscope
    % draw the gate horizontal segment
    \ifpgf@circuit@bpt@drawgate
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/hemt/base width}\pgf@circ@res@left}
        {(1+\gateasym)*\pgf@circ@res@up+(1-\gateasym)*\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}
            {(1+\gateasym)*\pgf@circ@res@up+(1-\gateasym)*\pgf@circ@res@down}}
        \pgfusepath{draw}
    \fi
}
\tikzset{GaN hemt/.style={hemt,
        circuitikz/tripoles/hemt/base height=0.6,%length of the "base" vertical bar
        circuitikz/tripoles/hemt/gate height=0.5,%distance of the S/D terminals
        circuitikz/tripoles/hemt/bodydiode conn=0.85,% attachment point of body diode
        circuitikz/tripoles/hemt/gate asym=-0.1,% slightly down
        circuitikz/tripoles/hemt/split gate=true,% split gate
        circuitikz/tripoles/hemt/source arrow=1,% right-facing arrow
    },
}

\long\def\drawfetcore#1{
    \pgftransformationadjustments
    % add the circle if requested (before everything else, so we can fill it)
    \pgfcirc@transistorcircle
    % fill the gap color if requested
    \pgfcirc@fillgategap{#1}

    %top connection
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint
        {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@up}}
    \pgfusepath{draw}
    \pgfscope
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up}}
        \ifpgf@circuit@trans@depletiontype
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down}}
        \else

            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up*0.45}}
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@up*0.25}}
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down*0.25}}
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/nfet/base height}\pgf@circ@res@down*0.45}}
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/base height}\pgf@circ@res@down}}
        \fi
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfusepath{draw}
    \endpgfscope
    %Bulk connection line
    \pgfpathmoveto{\pgfpoint
        {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
    {\pgf@circ@res@up+\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right+\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}
    {\pgf@circ@res@up+\pgf@circ@res@down}}

    %bottom connection
    \pgfpathmoveto{\pgfpoint
        {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfusepath{draw}

    %draw thick gate line
    \pgfscope%%% gate line
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
        \ifpgf@circuit@fet@doublegate
            \edef\@@gateconnheight{(\ctikzvalof{tripoles/#1/conn height})}
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
                {\@@gateconnheight*\pgf@circ@res@up/3}}
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
                {\@@gateconnheight*\pgf@circ@res@down/3}}
        \fi
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
        \pgfusepath{draw}
    \endpgfscope

    % arrows
    \pgfscope
        \pgfslopedattimetrue
        \pgfallowupsidedownattimetrue
        \pgfresetnontranslationattimefalse
        \ifpgf@circuit@trans@arrowatend
                \ifpgf@circuit@trans@ntype
                    \edef\@@anchor{tip}
                    \edef\@@pos{1.0}
                \else
                    \edef\@@anchor{btip}
                    \edef\@@pos{0.0}
                \fi
        \else
            \edef\@@anchor{center}\edef\@@pos{0.6}
        \fi
        \pgftransformlineattime{\@@pos}{%
            \pgfpoint
            {\pgf@circ@res@right}%
            {\pgf@circ@res@up+\pgf@circ@res@down}%
            }{%
            \pgfpoint%
            {\ctikzvalof{tripoles/#1/base width}\pgf@circ@res@left}%
            {\pgf@circ@res@up+\pgf@circ@res@down}%
        }
        \ifpgf@circuit@trans@ntype
        \else
            \pgftransformrotate{180}
        \fi
        \pgfnode{trarrow}{\@@anchor}{}{}{\pgfusepath{stroke}}
    \endpgfscope

    % GATE CONNECTION
    \ifpgf@circuit@bpt@drawgate
        \pgfscope %% gate connection
            \ifpgf@circuit@trans@ntype
                \edef\@@gateconnheight{(\ctikzvalof{tripoles/#1/conn height})}
            \else
                \edef\@@gateconnheight{(-\ctikzvalof{tripoles/#1/conn height})}
            \fi
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
                {\@@gateconnheight*\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint
                {\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}
                {\@@gateconnheight*\pgf@circ@res@down}}
            \ifpgf@circuit@fet@doublegate
                \pgfpathmoveto{\pgfpoint
                    {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
                    {-\@@gateconnheight*\pgf@circ@res@down/3}}
                \pgfpathlineto{\pgfpoint
                    {\pgf@circ@res@left-\pgfhorizontaltransformationadjustment*.5*\pgflinewidth}
                    {-\@@gateconnheight*\pgf@circ@res@down/3}}
            \fi
            \pgfusepath{draw}
        \endpgfscope
    \fi

    \pgfcirc@ferroelectric{#1}{0}

}

\long\def\pgfdeclaretransistorwrapperaddbulk#1#2#3{
    \pgfcircdeclaretransistor{#1}{
        \anchor{bulk}{\left\pgf@x=0pt}
        \anchor{B}{\left\pgf@x=0pt}%override Base anchor from npn&igbt
        \anchor{inner up}{
            \northeast
            \pgf@y=\ctikzvalof{tripoles/#1/gate height}\pgf@y
        }
        \anchor{inner down}{
            \northeast
            \pgf@y=-\ctikzvalof{tripoles/#1/gate height}\pgf@y
        }
        #2%
    }
    {#3}
}

\pgfdeclaretransistorwrapperaddbulk{nfet}{}{%
	\pgf@circuit@trans@depletiontypefalse
	\drawfetcore{nfet}
}

\pgfdeclaretransistorwrapperaddbulk{pfet}{}{%
    \pgf@circuit@trans@depletiontypefalse
    \drawfetcore{pfet}
}

\pgfdeclaretransistorwrapperaddbulk{nfetd}{}{%
	\pgf@circuit@trans@depletiontypetrue
	\drawfetcore{nfetd}
}

\pgfdeclaretransistorwrapperaddbulk{pfetd}{}{%
    \pgf@circuit@trans@depletiontypetrue
    \drawfetcore{pfetd}
}
% N-CHANNEL IGFET ENHANCEMENT TYPE
\pgfdeclaretransistorwrapperaddbulk{nigfete}{}{%
    \pgf@circuit@trans@depletiontypefalse
    \drawfetcore{nigfete}

    % SOURCE CONNECTION
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down}}
    \pgfusepath{draw}

    % SOLDER DOT at source-bulk connection
    \ifpgf@circuit@fet@solderdot
        \pgfscope
            \pgftransformshift{\pgfpoint{\pgf@circ@res@right}{
            \ctikzvalof{tripoles/nigfete/gate height}\pgf@circ@res@down}}
            \pgftransformscale{\ctikzvalof{transistor solderdot scale}}
            \pgfnode{circ}{center}{}{}{}
    \endpgfscope{}
\fi
}

% N-CHANNEL IGFET ENHANCEMENT TYPE with Bulk connector
\pgfdeclaretransistorwrapperaddbulk{nigfetebulk}{}{%
    \pgf@circuit@trans@depletiontypefalse
    \drawfetcore{nigfetebulk}
    % SOLDER DOT at source-bulk connection
    \ifpgf@circuit@fet@solderdot
        \pgfscope
            \pgftransformshift{\pgfpoint{\pgf@circ@res@right}{
            \ctikzvalof{tripoles/nigfetebulk/gate height}\pgf@circ@res@down}}
            \pgftransformscale{\ctikzvalof{transistor solderdot scale}}
            \pgfnode{circ}{center}{}{}{}
        \endpgfscope{}
    \fi
}

% N-CHANNEL IGFET DEPLETION TYPE
\pgfdeclaretransistorwrapperaddbulk{nigfetd}{}{%
    \pgf@circuit@trans@depletiontypetrue
    \drawfetcore{nigfetd}

    % SOURCE CONNECTION
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down}}
    \pgfusepath{draw}

    % SOLDER DOT at source-bulk connection
    \ifpgf@circuit@fet@solderdot
        \pgfscope
            \pgftransformshift{\pgfpoint{\pgf@circ@res@right}{
            \ctikzvalof{tripoles/nigfete/gate height}\pgf@circ@res@down}}
            \pgftransformscale{\ctikzvalof{transistor solderdot scale}}
            \pgfnode{circ}{center}{}{}{}
        \endpgfscope{}
    \fi
}

% P-CHANNEL IGFET ENHANCEMENT TYPE
\pgfdeclaretransistorwrapperaddbulk{pigfete}{}{%
    \pgf@circuit@trans@depletiontypefalse
    \drawfetcore{pigfete}
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}

    % SOURCE CONNECTION
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
    \pgfusepath{draw}


    % SOLDER DOT at source-bulk connection
    \ifpgf@circuit@fet@solderdot
        \pgfscope
            \pgftransformshift{\pgfpoint{\pgf@circ@res@right}{
            \ctikzvalof{tripoles/pigfete/gate height}\pgf@circ@res@up}}
            \pgftransformscale{\ctikzvalof{transistor solderdot scale}}
            \pgfnode{circ}{center}{}{}{}
        \endpgfscope
    \fi
}

% P-CHANNEL IGFET ENHANCEMENT TYPE with bulk connector
\pgfdeclaretransistorwrapperaddbulk{pigfetebulk}{}{%
    \pgf@circuit@trans@depletiontypefalse
    \drawfetcore{pigfetebulk}
}

% P-CHANNEL IGFET DEPLETION TYPE
\pgfdeclaretransistorwrapperaddbulk{pigfetd}{}{%
    \pgf@circuit@trans@depletiontypetrue
    \drawfetcore{pigfetd}

    % SOURCE CONNECTION
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
    \pgfusepath{draw}


    % SOLDER DOT at source-bulk connection
    \ifpgf@circuit@fet@solderdot
        \pgfscope
            \pgftransformshift{\pgfpoint{\pgf@circ@res@right}{
            \ctikzvalof{tripoles/nigfete/gate height}\pgf@circ@res@up}}
            \pgftransformscale{\ctikzvalof{transistor solderdot scale}}
            \pgfnode{circ}{center}{}{}{}
        \endpgfscope{}
    \fi
}

%%% uni-junction transistors: jfets, ujts

\long\def\pgfcircdeclarejunctiontransistor#1#2#3{% name, extra anchors, extra draw
    \pgfcircdeclaretransistor{#1}{
            \anchor{inner up}{
                \northeast
                \pgf@y=\ctikzvalof{tripoles/#1/gate height 2}\pgf@y
            }
            \anchor{inner down}{
                \northeast
                \pgf@y=-\ctikzvalof{tripoles/#1/gate height 2}\pgf@y
            }
            \anchor{kink}{
                \northeast
                \pgf@ya=-\ctikzvalof{tripoles/#1/gate height 2}\pgf@y
                \left
                \pgf@y=\ctikzvalof{tripoles/#1/curr direction}\pgf@ya
                \pgf@x=\ctikzvalof{tripoles/#1/conn kink}\pgf@x
            }
            % extra anchors (or override)
            #2%
        }{%
        % add the circle if requested (before everything else, so we can fill it)
        \pgfcirc@transistorcircle

        % get direction
        \edef\@@dir{\ctikzvalof{tripoles/#1/curr direction}}

        % draw drain/source connections: up
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
        {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@up}}

        % down
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
        {\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
        \pgfusepath{draw}

        \pgfscope
            \pgfpathmoveto{\pgfpoint
                {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint
                {\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/#1/gate height}\pgf@circ@res@down}}
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgfusepath{draw}
        \endpgfscope

        % arrow
        \pgfscope
            \pgfslopedattimetrue
            \pgfallowupsidedownattimetrue
            \pgfresetnontranslationattimefalse
            \ifpgf@circuit@trans@arrowatend
                \ifnum\@@dir>0
                    \edef\@@anchor{tip}\edef\@@pos{1.0}
                \else
                    \edef\@@anchor{btip}\edef\@@pos{1.0}
                \fi
            \else
                \ifnum\@@dir>0
                    \edef\@@anchor{center}\edef\@@pos{0.6}
                \else
                    \edef\@@anchor{center}\edef\@@pos{0.4}
                \fi
            \fi
            % find x1, x2 and y for the base arrow
            \ifnum\@@dir>0
                \pgf@circ@res@step=\ctikzvalof{tripoles/#1/conn kink}\pgf@circ@res@left
                \pgf@circ@res@other=\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left
                \pgf@circ@res@zero=\ctikzvalof{tripoles/#1/union height}\pgf@circ@res@down
                \pgf@circ@res@temp=\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@down
            \else
                \pgf@circ@res@step=\ctikzvalof{tripoles/#1/gate width}\pgf@circ@res@left
                \pgf@circ@res@other=\ctikzvalof{tripoles/#1/conn kink}\pgf@circ@res@left
                \pgf@circ@res@zero=\ctikzvalof{tripoles/#1/gate height 2}\pgf@circ@res@up
                \pgf@circ@res@temp=\ctikzvalof{tripoles/#1/union height}\pgf@circ@res@up
            \fi
            %
            % gate line (called emitter in UJT)
            %
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@temp}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@zero}}
            %
            % horizontal gate line
            %
            \ifpgf@circuit@bpt@drawgate
                \ifnum\@@dir>0
                    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@temp}}
                    \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@temp}}
                \else
                    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@zero}}
                    \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@zero}}
                \fi
            \fi
            \pgfusepath{draw}
            %
            % gate arrows (called emitter in UJT)
            %
            \pgftransformlineattime{\@@pos}{%
                \pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@temp}
                }{%
                \pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@zero}
            }
            \pgfnode{trarrow}{\@@anchor}{}{}{\pgfusepath{stroke}}
            %
            % gate line (called emitter in UJT)
            %
        \endpgfscope

        % extra drawings
        #3%
    }
}

% NJFET and PJFET

\pgfcircdeclarejunctiontransistor{njfet}{}{}
\pgfcircdeclarejunctiontransistor{pjfet}{}{}

\def\@ujtanchoroverride#1{
    \savedanchor{\ujtrealemitter}{
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/#1/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y %north anchor height
        % kink AND conn height, they're the same
        \pgf@y=-\ctikzvalof{tripoles/#1/gate height 2}\pgf@y
        \pgf@y=\ctikzvalof{tripoles/#1/curr direction}\pgf@y
        % horizontal
        \pgf@x=-\ctikzvalof{tripoles/#1/width}\pgf@circ@scaled@Rlen % left
        \ifpgf@circuit@bpt@drawgate\else
            \pgf@x=\ctikzvalof{tripoles/#1/conn kink}\pgf@x
        \fi
    }
    \anchor{E}{\ujtrealemitter}
    \anchor{emitter}{\ujtrealemitter}
    \anchor{B2}{\northeast\pgf@y=-\pgf@y}
    \anchor{B1}{\northeast}
}


\pgfcircdeclarejunctiontransistor{nujt}{\@ujtanchoroverride{nujt}}{}
\pgfcircdeclarejunctiontransistor{pujt}{\@ujtanchoroverride{pujt}}{}


\pgfdeclaretransistorwrapperaddbulk{isfet}{
    }{%
    % add the circle if requested (before everything else, so we can fill it)
    \pgfcirc@transistorcircle
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up+\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    % DRAIN CONNECTION
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/isfet/gate height}\pgf@circ@res@up}}
    % DRAIN
    \pgfpathlineto{\pgfpoint
        {\ctikzvalof{tripoles/isfet/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/isfet/gate height}\pgf@circ@res@up}}
    \pgfusepath{draw}

    % GATE, DEPLETION TYPE
    \pgfscope %% added
        \pgfpathmoveto{\pgfpoint
            {\ctikzvalof{tripoles/isfet/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/isfet/base height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/isfet/base width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/isfet/base height}\pgf@circ@res@down}}
        \pgf@circ@setlinewidth{tripoles}{\pgflinewidth} %% added
        \pgfusepath{draw} %% added
    \endpgfscope %% added

    % BULK
    \pgfpathmoveto{\pgfpoint
        {\ctikzvalof{tripoles/isfet/base width}\pgf@circ@res@left}
    {\pgf@circ@res@up+\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right+.5\pgflinewidth}
    {\pgf@circ@res@up+\pgf@circ@res@down}}

    % SOURCE
    \pgfpathmoveto{\pgfpoint
        {\ctikzvalof{tripoles/isfet/base width}\pgf@circ@res@left}
    {\ctikzvalof{tripoles/isfet/gate height}\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}
    {\ctikzvalof{tripoles/isfet/gate height}\pgf@circ@res@down}}
    % SOURCE CONNECTION
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down-\pgfverticaltransformationadjustment*.5*\pgflinewidth}}
    \pgfusepath{draw}

    % SOLDER DOT at source-bulk connection
    \ifpgf@circuit@fet@solderdot
        \pgfscope
            \pgftransformshift{\pgfpoint{\pgf@circ@res@right}{
            \ctikzvalof{tripoles/nigfete/gate height}\pgf@circ@res@down}}
            \pgftransformscale{\ctikzvalof{transistor solderdot scale}}
            \pgfnode{circ}{center}{}{}{}
        \endpgfscope{}
    \fi
    % ARROW
    \pgfscope
        \pgfslopedattimetrue
        \pgfallowupsidedownattimetrue
        \pgfresetnontranslationattimefalse
        \ifpgf@circuit@trans@arrowatend
            \edef\@@anchor{tip}\edef\@@pos{1.0}
        \else
            \edef\@@anchor{center}\edef\@@pos{0.6}
        \fi
        \pgftransformlineattime{\@@pos}{%
            \pgfpoint
            {\pgf@circ@res@right}%
            {\pgf@circ@res@up+\pgf@circ@res@down}%
            }{%
            \pgfpoint%
            {\ctikzvalof{tripoles/isfet/base width}\pgf@circ@res@left}%
            {\pgf@circ@res@up+\pgf@circ@res@down}%
        }
        \pgfnode{trarrow}{\@@anchor}{}{}{\pgfusepath{stroke}}
    \endpgfscope
    \pgfusepath{draw}

    % Wavy lines
    \pgfscope
        \pgfpathmoveto{\pgfpoint{-\ctikzvalof{tripoles/isfet/waves x sep}\pgf@circ@res@up}{\ctikzvalof{tripoles/isfet/waves y sep}\pgf@circ@res@up}}
        \pgfpathsine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{-\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfpathcosine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfpathsine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfpathcosine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{-\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfusepath{draw}

        \pgfpathmoveto{\pgfpoint{-\ctikzvalof{tripoles/isfet/waves x sep}\pgf@circ@res@up}{0cm}}
        \pgfpathsine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{-\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfpathcosine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfpathsine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfpathcosine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{-\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfusepath{draw}

        \pgfpathmoveto{\pgfpoint{-\ctikzvalof{tripoles/isfet/waves x sep}\pgf@circ@res@up}{-\ctikzvalof{tripoles/isfet/waves y sep}\pgf@circ@res@up}}
        \pgfpathsine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{-\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfpathcosine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfpathsine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfpathcosine{\pgfpoint{-\ctikzvalof{tripoles/isfet/wave width}\pgf@circ@res@up}{-\ctikzvalof{tripoles/isfet/wave amp}\pgf@circ@res@up}}
        \pgfusepath{draw}
    \endpgfscope
}

% end of transistors
% %>>>

%% Transistor's path definitions%<<<

\pgf@circ@definetranspath{nmos}
\pgf@circ@definetranspath{pmos}
\pgf@circ@definetranspath{nmosd}
\pgf@circ@definetranspath{pmosd}
\pgf@circ@definetranspath{hemt}
\pgf@circ@definetranspath{npn}
\pgf@circ@definetranspath{pnp}
\pgf@circ@definetranspath{nfet}
\pgf@circ@definetranspath{nigfete}
\pgf@circ@definetranspath{nigfetd}
\pgf@circ@definetranspath{nigfetebulk}
\pgf@circ@definetranspath{pfet}
\pgf@circ@definetranspath{pigfete}
\pgf@circ@definetranspath{pigfetd}
\pgf@circ@definetranspath{pigfetebulk}
\pgf@circ@definetranspath{njfet}
\pgf@circ@definetranspath{pjfet}
\pgf@circ@definetranspath{pigbt}
\pgf@circ@definetranspath{nigbt}
\pgf@circ@definetranspath{Lpigbt}
\pgf@circ@definetranspath{Lnigbt}% %>>>

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Amplifiers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Settings for Amplifiers%<<<1
%
% switches for op amps
% changing input polarity
%
\newif\ifpgf@circuit@oa@iplusup\pgf@circuit@oa@iplusupfalse
\pgfkeys{/tikz/noinv input up/.add code={}{\pgf@circuit@oa@iplusuptrue}}
\ctikzset{noinv input up/.add code={}{\pgf@circuit@oa@iplusuptrue}}
\pgfkeys{/tikz/noinv input down/.add code={}{\pgf@circuit@oa@iplusupfalse}}
\ctikzset{noinv input down/.add code={}{\pgf@circuit@oa@iplusupfalse}}
%
% changing output polarity (for fully diff objects)
%
\newif\ifpgf@circuit@oa@oplusup\pgf@circuit@oa@oplusuptrue
\pgfkeys{/tikz/noinv output up/.add code={}{\pgf@circuit@oa@oplusuptrue}}
\ctikzset{noinv output up/.add code={}{\pgf@circuit@oa@oplusuptrue}}
\pgfkeys{/tikz/noinv output down/.add code={}{\pgf@circuit@oa@oplusupfalse}}
\ctikzset{noinv output down/.add code={}{\pgf@circuit@oa@oplusupfalse}}

% Operational amplifier
\ctikzset{tripoles/op amp/width/.initial=1.7}              % Total width
\ctikzset{tripoles/op amp/port width/.initial=.7}          % Terminals length
\ctikzset{tripoles/op amp/height/.initial=1.4}             % Total height
\ctikzset{tripoles/op amp/input height/.initial=.5}        % Input port vertical separation
\ctikzset{tripoles/op amp/up pos/.initial=.45}             % Top and bottom anchor position
\ctikzset{tripoles/op amp/font/.initial=\pgf@circ@font@tenbm}  % Absolute font size needed!

% Fully differential operational amplifier
\ctikzset{tripoles/fd op amp/width/.initial=1.7}           % Total width
\ctikzset{tripoles/fd op amp/port width/.initial=.7}       % Terminals length
\ctikzset{tripoles/fd op amp/height/.initial=1.4}          % Total height
\ctikzset{tripoles/fd op amp/input height/.initial=.5}     % Input port vertical separation
\ctikzset{tripoles/fd op amp/output height/.initial=.5}    % Output port vertical separation
\ctikzset{tripoles/fd op amp/up pos/.initial=.45}          % Top and bottom anchor position
\ctikzset{tripoles/fd op amp/font/.initial=\pgf@circ@font@tenbm}  % Absolute font size needed!

\ctikzset{tripoles/en amp/width/.initial=1.7}
\ctikzset{tripoles/en amp/port width/.initial=.7}
\ctikzset{tripoles/en amp/height/.initial=1.6}
\ctikzset{tripoles/en amp/input height/.initial=.3}
\ctikzset{tripoles/en amp/up pos/.initial=.45}
\ctikzset{tripoles/en amp/font/.initial=\pgf@circ@font@tenbm}   % Absolute font size needed!
\ctikzset{tripoles/en amp/font2/.initial=\pgf@circ@font@twelve}  % Absolute font size needed!
\ctikzset{tripoles/en amp/text/.initial={$\mathstrut{\triangleright}\,{\infty}$}}
\tikzset{
    en amp text/.code = {%
        \ctikzsetvalof{tripoles/en amp/text}{#1}%
    },
    en amp text A/.code = {%
        \ctikzsetvalof{tripoles/en amp/text}{$\mathstrut{\triangleright}\,\mathrm{A}$}%
    },
}

% Transconductance amplifier
\ctikzset{tripoles/gm amp/width/.initial=1.7}              % Total width
\ctikzset{tripoles/gm amp/port width/.initial=.7}          % Terminals length
\ctikzset{tripoles/gm amp/height/.initial=1.4}             % Left side of the trapezoid
\ctikzset{tripoles/gm amp/height 2/.initial=0.5}           % Right side of the trapezoid
\ctikzset{tripoles/gm amp/input height/.initial=.5}        % Input port vertical separation
\ctikzset{tripoles/gm amp/up pos/.initial=.45}             % Top and bottom anchor position
\ctikzset{tripoles/gm amp/font/.initial=\pgf@circ@font@tenbm}  % Absolute font size needed!

% Instrumentation amplifier
\ctikzset{tripoles/inst amp/width/.initial=1.7}            % Total width
\ctikzset{tripoles/inst amp/port width/.initial=.7}        % Terminals length
\ctikzset{tripoles/inst amp/height/.initial=1.4}           % Left side of the trapezoid
\ctikzset{tripoles/inst amp/height 2/.initial=0.6}         % Right side of the trapezoid
\ctikzset{tripoles/inst amp/input height/.initial=.5}      % Input ports vertical separation
\ctikzset{tripoles/inst amp/up pos/.initial=.4}            % Top and bottom anchor position
\ctikzset{tripoles/inst amp/refv pos/.initial=.7}          % Top and bottom voltage reference position
\ctikzset{tripoles/inst amp/font/.initial=\pgf@circ@font@tenbm}  % Absolute font size needed!

% Instrumentation amplifier with differential output
\ctikzset{tripoles/fd inst amp/width/.initial=1.7}         % Total width
\ctikzset{tripoles/fd inst amp/port width/.initial=.7}     % Terminals length
\ctikzset{tripoles/fd inst amp/height/.initial=1.4}        % Left side of the trapezoid
\ctikzset{tripoles/fd inst amp/height 2/.initial=0.6}      % Right side of the trapezoid
\ctikzset{tripoles/fd inst amp/input height/.initial=.5}   % Input ports vertical separation
\ctikzset{tripoles/fd inst amp/output height/.initial=.5}  % Output ports vertical separation
\ctikzset{tripoles/fd inst amp/up pos/.initial=.4}         % Top and bottom anchor position
\ctikzset{tripoles/fd inst amp/refv pos/.initial=.7}       % Top and bottom voltage reference position
\ctikzset{tripoles/fd inst amp/font/.initial=\pgf@circ@font@tenbm}  % Absolute font size needed!

% Instrumentation amplifier with gain resistor terminals
\ctikzset{tripoles/inst amp ra/width/.initial=2.4}         % Total width
\ctikzset{tripoles/inst amp ra/port width/.initial=.7}     % Terminals length
\ctikzset{tripoles/inst amp ra/height/.initial=2.9}        % Left side of the trapezoid
\ctikzset{tripoles/inst amp ra/height 2/.initial=0.4}      % Right side of the trapezoid
\ctikzset{tripoles/inst amp ra/input height/.initial=.7}   % Input ports vertical separation
\ctikzset{tripoles/inst amp ra/up pos/.initial=.4}         % Top and bottom anchor position
\ctikzset{tripoles/inst amp ra/refv pos/.initial=.7}       % Top and bottom voltage reference position
\ctikzset{tripoles/inst amp ra/ra pos/.initial=.6}         % Gain resistor terminals vertical separation
\ctikzset{tripoles/inst amp ra/font/.initial=\pgf@circ@font@tenbm}  % Absolute font size needed!

% Plain amplifier
\ctikzset{tripoles/plain amp/width/.initial=1.7}           % Total width
\ctikzset{tripoles/plain amp/port width/.initial=.7}       % Terminals length
\ctikzset{tripoles/plain amp/height/.initial=1.4}          % Total height
\ctikzset{tripoles/plain amp/input height/.initial=.5}     % Input ports vertical separation
\ctikzset{tripoles/plain amp/up pos/.initial=.45}          % Top and bottom anchor position

% changing fonts and symbols of amplifiers
\ctikzset{amplifiers/symbol font/.code={%
        \ctikzset{tripoles/inst amp ra/font=#1}%
        \ctikzset{tripoles/fd inst amp/font=#1}%
        \ctikzset{tripoles/inst amp/font=#1}%
        \ctikzset{tripoles/gm amp/font=#1}%
        \ctikzset{tripoles/en amp/font=#1}%
        \ctikzset{tripoles/fd op amp/font=#1}%
        \ctikzset{tripoles/op amp/font=#1}%
}}
\ctikzset{amplifiers/plus/.initial={$+$}}
% In the mayority of fonts, the size of - is smaller than +, so we have
% unaligned signs when positioned independently.
% See https://github.com/circuitikz/circuitikz/issues/721
\ctikzset{amplifiers/minus/.initial={$\vphantom{+}-$}}
\tikzset{amp symbol font/.code={%
        \ctikzset{amplifiers/symbol font={#1}}%
    }
}
\tikzset{amp plus/.code={\ctikzsetvalof{amplifiers/plus}{#1}}}
\tikzset{amp minus/.code={\ctikzsetvalof{amplifiers/minus}{#1}}}
\def\pgf@circ@ampli@plus{\ctikzvalof{amplifiers/plus}}
\def\pgf@circ@ampli@minus{\ctikzvalof{amplifiers/minus}}
%%>>>

%% Nodes for amplifiers%<<<
%% operational and instrumentation amplifiers

\pgfdeclareshape{op amp}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{amplifiers}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/op amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \savedanchor\left{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@y=0pt
    }
    \anchor{south}{
        \northwest
        \pgf@y=-\pgf@y
        \pgf@x=0pt
    }
    \anchor{north}{
        \northwest
        \pgf@x=0pt
    }
    \savedanchor\left{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@y=0pt
    }
    \savedanchor\inOneFixed{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/op amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/op amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{in up}{
        \inOneFixed
    }
    \anchor{in down}{
        \inOneFixed
        \pgf@y=-\pgf@y
    }
    \savedanchor\inOne{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/op amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/op amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \anchor{-}{
        \inOne
    }
    \anchor{+}{
        \inOne
        \pgf@y=-\pgf@y
    }
    \savedanchor\up{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/op amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/op amp/up pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/op amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/op amp/port width}\pgf@circ@res@right}{0pt}}
    }
    \anchor{up}{
        \up
    }
    \anchor{down}{
        \up
        \pgf@y=-\pgf@y
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{west}{
        \left
    }
    \anchor{east}{
        \left
        \pgf@x=-\pgf@x
    }

    % support for border anchors
    \savedanchor\leftedge{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@x = \ctikzvalof{tripoles/op amp/port width}\pgf@x
        \pgf@y=0pt
    }
    \anchor{leftedge}{\leftedge}
    \anchor{rightedge}{\leftedge \pgf@x =-\pgf@x }
    \anchor{bin up}{
        \inOneFixed
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bin down}{
        \inOneFixed
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b-}{
        \inOne
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b+}{
        \inOne
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bout}{
        \leftedge
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    % end border anchors
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }

    \anchor{text}{
        \ifpgf@circ@center@text
            \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \else
            \leftedge
            \pgfmathsetlength{\pgf@x}{\pgf@x + \ctikzvalof{left text distance}}
        \fi
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }

    \pgf@circ@draw@component{
        \pgf@circ@setcolor
        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgf@circ@scaled@Rlen=\scaledRlen
				% Triangle
        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{\ctikzvalof{tripoles/op amp/port width}\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=\ctikzvalof{tripoles/op amp/port width}\pgf@circ@res@step
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope
				% Negative input terminal
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
            {\ctikzvalof{tripoles/op amp/input height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/op amp/port width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/op amp/input height}\pgf@circ@res@up}}
				% Positive input terminal
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
            {\ctikzvalof{tripoles/op amp/input height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/op amp/port width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/op amp/input height}\pgf@circ@res@down}}
        % Output terminal
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/op amp/port width}\pgf@circ@res@right}{0pt}}
        \pgfsetrectcap
        \pgfusepath{draw}
        \pgf@circ@text@strokecolor
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/op amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/op amp/input height}\pgf@circ@res@up}]{\ctikzvalof{tripoles/op amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@plus\else\pgf@circ@ampli@minus\fi}
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/op amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/op amp/input height}\pgf@circ@res@down}]{\ctikzvalof{tripoles/op amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@minus\else\pgf@circ@ampli@plus\fi}
    }
}

% Op amp shape as in european standard EN 60617
\pgfdeclareshape{en amp}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{amplifiers}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/en amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/en amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \savedanchor\left{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/en amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@y=0pt
    }
    \anchor{south}{
        \northwest
        \pgf@y=-\pgf@y
        \pgf@x=0pt
    }
    \anchor{north}{
        \northwest
        \pgf@x=0pt
    }
    \savedanchor\inOneFixed{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/en amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/en amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/en amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{in up}{
        \inOneFixed
    }
    \anchor{in down}{
        \inOneFixed
        \pgf@y=-\pgf@y
    }
    \savedanchor\inOne{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/en amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/en amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/en amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \anchor{-}{
        \inOne
    }
    \anchor{+}{
        \inOne
        \pgf@y=-\pgf@y
    }
    \anchor{up}{
        \northwest
        \pgf@x=0pt
    }
    \anchor{down}{
        \northwest
        \pgf@x=0pt
        \pgf@y=-\pgf@y
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{west}{
        \left
    }
    \anchor{east}{
        \left
        \pgf@x=-\pgf@x
    }
    % support for border anchors
    \savedanchor\leftedge{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/en amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@x = \ctikzvalof{tripoles/en amp/port width}\pgf@x
        \pgf@y=0pt
    }
    \anchor{leftedge}{\leftedge}
    \anchor{rightedge}{\leftedge \pgf@x =-\pgf@x }
    \anchor{bin up}{
        \inOneFixed
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bin down}{
        \inOneFixed
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b-}{
        \inOne
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b+}{
        \inOne
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bout}{
        \leftedge
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    % end border anchors
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \anchor{text}{
        \ifpgf@circ@center@text
            \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \else
            \leftedge
            \pgfmathsetlength{\pgf@x}{\pgf@x + \ctikzvalof{left text distance}}
        \fi
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }

    \pgf@circ@draw@component{
        \pgf@circ@setcolor
        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgf@circ@scaled@Rlen=\scaledRlen

        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/en amp/port width}\pgf@circ@res@left}{\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/en amp/port width}\pgf@circ@res@left}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/en amp/port width}\pgf@circ@res@right}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/en amp/port width}\pgf@circ@res@right}{\pgf@circ@res@down}}
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope

        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
            {\ctikzvalof{tripoles/en amp/input height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/en amp/port width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/en amp/input height}\pgf@circ@res@up}}

        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
            {\ctikzvalof{tripoles/en amp/input height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/en amp/port width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/en amp/input height}\pgf@circ@res@down}}

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@right}{0pt}}
        \pgfsetrectcap
        \pgfusepath{draw}
        \pgf@circ@text@strokecolor
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/en amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/en amp/input height}\pgf@circ@res@up}]{\ctikzvalof{tripoles/en amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@plus\else\pgf@circ@ampli@minus\fi}
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/en amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/en amp/input height}\pgf@circ@res@down}]{\ctikzvalof{tripoles/en amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@minus\else\pgf@circ@ampli@plus\fi}
        \pgftext[top, y=-.5ex, at=\pgfpoint{0pt}{\pgf@circ@res@up}]{\hbox{\ctikzvalof{tripoles/en amp/font2}\ctikzvalof{tripoles/en amp/text}}}
    }
}

% Fully differential output op amp
% Contributed by Kristofer M. Monisit
\pgfdeclareshape{fd op amp}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{amplifiers}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd op amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \savedanchor\outline{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd op amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    \ifpgf@circuit@oa@oplusup\else\pgf@y=-\pgf@y\fi
    }
    \anchor{south}{
        \northwest
        \pgf@y=-\pgf@y\pgf@x=0pt\relax
    }
    \anchor{north}{
        \northwest\pgf@x=0pt\relax
    }
    \savedanchor\left{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@y=0pt
    }
    \savedanchor\inOneFixed{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd op amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/fd op amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{in up}{
        \inOneFixed
    }
    \anchor{in down}{
        \inOneFixed
        \pgf@y=-\pgf@y
    }
    \savedanchor\inOne{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd op amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/fd op amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \anchor{-}{
        \inOne
    }
    \anchor{+}{
        \inOne
        \pgf@y=-\pgf@y
    }
    \savedanchor\up{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd op amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/fd op amp/up pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/fd op amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/fd op amp/port width}\pgf@circ@res@right}{0pt}}
    }
    \anchor{up}{
        \up
    }
    \anchor{down}{
        \up
        \pgf@y=-\pgf@y
    }
    \anchor{out up}{
        \northwest
        \pgf@y=\ctikzvalof{tripoles/fd op amp/output height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/port width}\pgf@x
    }
    \anchor{out down}{
        \northwest
        \pgf@y=-\ctikzvalof{tripoles/fd op amp/output height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/port width}\pgf@x
    }
    \anchor{out +}{
        \outline
        \pgf@y=\ctikzvalof{tripoles/fd op amp/output height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/port width}\pgf@x
    }
    \anchor{out -}{
        \outline
        \pgf@y=-\ctikzvalof{tripoles/fd op amp/output height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/port width}\pgf@x
    }
    % support for border anchors
    \savedanchor\leftedge{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@x = \ctikzvalof{tripoles/fd op amp/port width}\pgf@x
        \pgf@y=0pt
    }
    \anchor{leftedge}{\leftedge}
    \anchor{rightedge}{\leftedge \pgf@x =-\pgf@x }
    \anchor{bin up}{
        \inOneFixed
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bin down}{
        \inOneFixed
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b-}{
        \inOne
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b+}{
        \inOne
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bout}{
        \leftedge
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{bout +}{
        \outline
        \pgf@xa=\pgf@x\pgf@ya=\pgf@y
        \pgfpointlineattime{\ctikzvalof{tripoles/fd op amp/output height}}
            {\pgfpoint{\pgf@xa}{0pt}}
            {\pgfpoint{0pt}{\pgf@ya}}
    }
    \anchor{bout -}{
        \outline
        \pgf@xa=\pgf@x\pgf@ya=\pgf@y
        \pgfpointlineattime{\ctikzvalof{tripoles/fd op amp/output height}}
            {\pgfpoint{\pgf@xa}{0pt}}
            {\pgfpoint{0pt}{\pgf@ya}}
        \pgf@y=-\pgf@y
    }
    \anchor{bout up}{
        \northwest
        \pgf@xa=\pgf@x\pgf@ya=\pgf@y
        \pgfpointlineattime{\ctikzvalof{tripoles/fd op amp/output height}}
            {\pgfpoint{\pgf@xa}{0pt}}
            {\pgfpoint{0pt}{\pgf@ya}}
    }
    \anchor{bout down}{
        \northwest
        \pgf@xa=\pgf@x\pgf@ya=\pgf@y
        \pgfpointlineattime{\ctikzvalof{tripoles/fd op amp/output height}}
            {\pgfpoint{\pgf@xa}{0pt}}
            {\pgfpoint{0pt}{\pgf@ya}}
        \pgf@y=-\pgf@y
    }
    % end border anchors
    \anchor{west}{
        \left
    }
    \anchor{east}{
        \left
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/port width}\pgf@x
    }
    \anchor{out}{% should not be used
        \left
        \pgf@x=-\ctikzvalof{tripoles/fd op amp/port width}\pgf@x
    }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\ctikzvalof{tripoles/fd op amp/port width}\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\ctikzvalof{tripoles/fd op amp/port width}\pgf@x \pgf@y=-\pgf@y }
    \anchor{text}{
        \ifpgf@circ@center@text
            \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \else
            \leftedge
            \pgfmathsetlength{\pgf@x}{\pgf@x + \ctikzvalof{left text distance}}
        \fi
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }
    \pgf@circ@draw@component{
        \pgf@circ@setcolor

        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgf@circ@scaled@Rlen=\scaledRlen

        % Triangle
        % Includes output terminals in the traingular shape
        % to ensure that diagonal joins are properly displayed
        % we first draw the main triangle and the leads with normal thickness,
        % and the redraw the main triangle with the component shape
        \pgfscope
            % shift origin a bit to ease calculations
            \pgftransformxshift{\ctikzvalof{tripoles/fd op amp/port width}\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=\ctikzvalof{tripoles/fd op amp/port width}\pgf@circ@res@step

            % Initial point (right vertex)
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{0pt}}

            % Negative output terminal
            \pgfpathlineto{\pgfpointlineattime{\ctikzvalof{tripoles/fd op amp/output height}}{\pgfpoint{\pgf@circ@res@step}{0pt}}{\pgfpoint{0pt}{\pgf@circ@res@up}}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{\ctikzvalof{tripoles/fd op amp/output height}\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpointlineattime{\ctikzvalof{tripoles/fd op amp/output height}}{\pgfpoint{\pgf@circ@res@step}{0pt}}{\pgfpoint{0pt}{\pgf@circ@res@up}}}

            % Top vertex
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}

            % Bottom vertex
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}

            % Positive output terminal
            \pgfpathlineto{\pgfpointlineattime{\ctikzvalof{tripoles/fd op amp/output height}}{\pgfpoint{\pgf@circ@res@step}{0pt}}{\pgfpoint{0pt}{\pgf@circ@res@down}}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{\ctikzvalof{tripoles/fd op amp/output height}\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpointlineattime{\ctikzvalof{tripoles/fd op amp/output height}}{\pgfpoint{\pgf@circ@res@step}{0pt}}{\pgfpoint{0pt}{\pgf@circ@res@down}}}

            % Right vertex
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{0pt}}

            \pgfpathclose
            \pgfusepath{stroke}

            % ok, now we'll redraw the triangle with the class specific
            % thickness and optionally fill

            \pgfscope
                \pgf@circ@setlinewidth{quadpoles}{\pgflinewidth}

                % Initial point (right vertex)
                \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{0pt}}
                % Top vertex
                \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
                % Bottom vertex
                \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}

                \pgfpathclose
                \pgf@circ@draworfill
            \endpgfscope % thick and fill
        \endpgfscope % shift

        % Negative input terminal
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
            {\ctikzvalof{tripoles/fd op amp/input height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/fd op amp/port width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/fd op amp/input height}\pgf@circ@res@up}}

        % Positive input terminal
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
            {\ctikzvalof{tripoles/fd op amp/input height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/fd op amp/port width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/fd op amp/input height}\pgf@circ@res@down}}
        \pgfsetrectcap
        \pgfusepath{draw}
        \pgf@circ@text@strokecolor
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/fd op amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/fd op amp/input height}\pgf@circ@res@up}]{\ctikzvalof{tripoles/fd op amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@plus\else\pgf@circ@ampli@minus\fi}
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/fd op amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/fd op amp/input height}\pgf@circ@res@down}]{\ctikzvalof{tripoles/fd op amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@minus\else\pgf@circ@ampli@plus\fi}
        % Negative output terminal label
        \pgftext[right, bottom, x=3pt, y=1pt, at=\pgfpoint{0pt}{0.425\pgf@circ@res@down}]{\ctikzvalof{tripoles/fd op amp/font} \ifpgf@circuit@oa@oplusup\pgf@circ@ampli@minus\else\pgf@circ@ampli@plus\fi}
        % Positive output terminal label
        \pgftext[right, top, x=3pt, y=-1pt, at=\pgfpoint{0pt}{0.425\pgf@circ@res@up}]{\ctikzvalof{tripoles/fd op amp/font} \ifpgf@circuit@oa@oplusup\pgf@circ@ampli@plus\else\pgf@circ@ampli@minus\fi}
    }
}


% Instrumentation amplifier with differential output
\pgfdeclareshape{fd inst amp}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{amplifiers}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    % when tikz calls the anchor it wants the relative position in the lengths
    % \pgf@x  \pgf@y
    % \pgfpoint* functions set that variables
    % anchors are visible outside and run on use
    \anchor{center}{\pgfpointorigin}
    % savedanchors are internals and run on node creation (not use)
    % bounding-box top left
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd inst amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{nw}{
        \northwest
    }
    \anchor{south}{
        \northwest
        \pgf@y=-\pgf@y\pgf@x=0pt\relax
    }
    \anchor{north}{
        \northwest\pgf@x=0pt\relax
    }
    \savedanchor\left{%
        \pgf@y=0pt
    }
    \anchor{leftedge}
    {\left
        \pgf@x = \ctikzvalof{tripoles/fd inst amp/port width}\pgf@x
    }
    \savedanchor\inOneFixed{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
          \pgf@y=\ctikzvalof{tripoles/fd inst amp/height}\pgf@circ@scaled@Rlen
          \pgf@y=.5\pgf@y
          \pgf@y=\ctikzvalof{tripoles/fd inst amp/input height}\pgf@y
          \pgf@x=-\ctikzvalof{tripoles/fd inst amp/width}\pgf@circ@scaled@Rlen
          \pgf@x=.5\pgf@x
    }
    \anchor{in up}{
        \inOneFixed
    }
    \anchor{in down}{
        \inOneFixed
        \pgf@y=-\pgf@y
    }
    \savedanchor\inOne{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd inst amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/fd inst amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \anchor{-}{
        \inOne
    }
    \anchor{+}{
        \inOne
        \pgf@y=-\pgf@y
    }
    \savedanchor\up{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd inst amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/fd inst amp/up pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/fd inst amp/height 2}\pgf@circ@res@up}}
    }
    \anchor{up}{
        \up
    }
    \anchor{down}{
        \up
        \pgf@y=-\pgf@y
    }
    % reference voltage input anchors.
    \savedanchor\refv{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd inst amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/fd inst amp/refv pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/fd inst amp/height 2}\pgf@circ@res@up}}
    }
    % we need both because they are normally drawn under the amp, and if you
    % mirror it vertically you need them
    \anchor{refv up}{
        \refv
    }
    \anchor{refv down}{
        \refv
        \pgf@y=-\pgf@y
    }
    \savedanchor\outport{
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd inst amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/fd inst amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=-.5\pgf@x
        \ifpgf@circuit@oa@oplusup\else\pgf@y=-\pgf@y\fi
    }
    \anchor{out}{
        \outport
        \pgf@y=0pt
    }
    \anchor{out +}{
        \outport
    }
    \anchor{out -}{
        \outport
        \pgf@y=-\pgf@y
    }
    \savedanchor\outportfixed{
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/fd inst amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/fd inst amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/fd inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=-.5\pgf@x
    }
    \anchor{out up}{
        \outportfixed
    }
    \anchor{out down}{
        \outportfixed
        \pgf@y=-\pgf@y
    }
    % support for border anchors
    \savedanchor\leftedge{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/fd inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@x = \ctikzvalof{tripoles/fd inst amp/port width}\pgf@x
        \pgf@y=0pt
    }
    \anchor{leftedge}{\leftedge}
    \anchor{rightedge}{\leftedge \pgf@x =-\pgf@x }
    \anchor{bin up}{
        \inOneFixed
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bin down}{
        \inOneFixed
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b-}{
        \inOne
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b+}{
        \inOne
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bout}{
        \leftedge
        \pgf@x=-\pgf@x \pgf@y=0pt
    }
    \anchor{bout +}{
        \outport
        \pgf@ya=\pgf@y \leftedge \pgf@x=-\pgf@x \pgf@y=\pgf@ya
    }
    \anchor{bout -}{
        \outport
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@x=-\pgf@x \pgf@y=\pgf@ya
    }
    \anchor{bout up}{
        \outportfixed
        \pgf@ya=\pgf@y \leftedge \pgf@x=-\pgf@x \pgf@y=\pgf@ya
    }
    \anchor{bout down}{
        \outportfixed
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@x=-\pgf@x \pgf@y=\pgf@ya
    }
    % end border anchors
    %
    \anchor{west}{
        \left
    }
    \anchor{east}{
        \left
        \pgf@x=-\pgf@x
    }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \anchor{text}{
        \ifpgf@circ@center@text
            \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \else
            \leftedge
            \pgfmathsetlength{\pgf@x}{\pgf@x + \ctikzvalof{left text distance}}
        \fi
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }

    % let's start drawing the component
    \pgf@circ@draw@component{
        \pgf@circ@setcolor
        %
        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgf@circ@scaled@Rlen=\scaledRlen
        % main component, normally in thicker lines
        \pgfscope
            \newdimen\pgf@circ@res@right@double
            \pgf@circ@res@right@double=2\pgf@circ@res@right
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@step
            %first point (near output)
            \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@right@double}{0}}
            %from the exit to the top (short side)... (note that the .6 must be copied in \up and \refv anchors
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@right@double}{\ctikzvalof{tripoles/fd inst amp/height 2}\pgf@circ@res@up}}
            % and then to the input "front up", "down", to the output short side "down"
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@right@double}{\ctikzvalof{tripoles/fd inst amp/height 2}\pgf@circ@res@down}}
            % ...and close
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope

        % input terminal up
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\ctikzvalof{tripoles/fd inst amp/input height}\pgf@circ@res@up}}
        %
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/fd inst amp/input height}\pgf@circ@res@up}}
        %

        % input terminal down
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\ctikzvalof{tripoles/fd inst amp/input height}\pgf@circ@res@down}}
        %
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/fd inst amp/input height}\pgf@circ@res@down}}

        % output leads down and up
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\ctikzvalof{tripoles/fd inst amp/output height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/fd inst amp/output height}\pgf@circ@res@down}} %

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\ctikzvalof{tripoles/fd inst amp/output height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/fd inst amp/output height}\pgf@circ@res@up}} %
        %
        \pgfsetrectcap
        \pgfusepath{draw}
        \pgf@circ@text@strokecolor
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/fd inst amp/input height}\pgf@circ@res@up}]{\ctikzvalof{tripoles/fd inst amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@plus\else\pgf@circ@ampli@minus\fi}
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/fd inst amp/input height}\pgf@circ@res@down}]{\ctikzvalof{tripoles/fd inst amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@minus\else\pgf@circ@ampli@plus\fi}
        \pgftext[right, at=\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/fd inst amp/output height}\pgf@circ@res@down}]{\ctikzvalof{tripoles/fd inst amp/font}\ifpgf@circuit@oa@oplusup\pgf@circ@ampli@minus\space\else\pgf@circ@ampli@plus\space\fi}
        \pgftext[right, at=\pgfpoint{\ctikzvalof{tripoles/fd inst amp/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/fd inst amp/output height}\pgf@circ@res@up}]{\ctikzvalof{tripoles/fd inst amp/font}\ifpgf@circuit@oa@oplusup\pgf@circ@ampli@plus\space\else\pgf@circ@ampli@minus\space\fi}
    }
}

% Transconductance amplifier (Transkonduktanzverstärker)
\pgfdeclareshape{gm amp}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{amplifiers}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/gm amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/gm amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{south}{
        \northwest
        \pgf@y=-\pgf@y
    }
    \anchor{north}{
        \northwest
    }
    \savedanchor\left{%
        \pgf@y=0pt
    }
    \anchor{leftedge}
    {\left
        \pgf@x = \ctikzvalof{tripoles/op amp/port width}\pgf@x
    }
    \savedanchor\inOneFixed{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/gm amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/gm amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/gm amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{in up}{
        \inOneFixed
    }
    \anchor{in down}{
        \inOneFixed
        \pgf@y=-\pgf@y
    }
    \savedanchor\inOne{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/gm amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/gm amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/gm amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \anchor{-}{
        \inOne
    }
    \anchor{+}{
        \inOne
        \pgf@y=-\pgf@y
    }
        \savedanchor\up{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/gm amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/gm amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/gm amp/up pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/gm amp/height 2}\pgf@circ@res@up}}
    }
    \anchor{up}{
        \up
    }
    \anchor{down}{
        \up
        \pgf@y=-\pgf@y
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    % support for border anchors
    \savedanchor\leftedge{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/gm amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@x = \ctikzvalof{tripoles/gm amp/port width}\pgf@x
        \pgf@y=0pt
    }
    \anchor{leftedge}{\leftedge}
    \anchor{rightedge}{\leftedge \pgf@x =-\pgf@x }
    \anchor{bin up}{
        \inOneFixed
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bin down}{
        \inOneFixed
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b-}{
        \inOne
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b+}{
        \inOne
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bout}{
        \leftedge
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    % end border anchors
    \anchor{west}{
        \left
    }
    \anchor{east}{
        \left
        \pgf@x=-\pgf@x
    }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }

    \anchor{text}{
        \ifpgf@circ@center@text
            \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \else
            \leftedge
            \pgfmathsetlength{\pgf@x}{\pgf@x + \ctikzvalof{left text distance}}
        \fi
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }
    \pgf@circ@draw@component{
        \pgf@circ@setcolor

        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgf@circ@scaled@Rlen=\scaledRlen

        \pgfscope
            \newdimen\pgf@circ@res@right@double
						\pgf@circ@res@right@double=2\pgf@circ@res@right

            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@step
            %Umrandung:
            \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@right@double}{0}} %geändert startpunkt neu am ausgangsstrich
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@right@double}{\ctikzvalof{tripoles/gm amp/height 2}\pgf@circ@res@up}}%vom Ausgang nach oben
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}} %neu ecke links oben nach rechts oben
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}} %bei deneigängen runter
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@right@double}{\ctikzvalof{tripoles/gm amp/height 2}\pgf@circ@res@down}}%ecke links unten nach rechts unten
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope

        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
            {\ctikzvalof{tripoles/gm amp/input height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/gm amp/input height}\pgf@circ@res@up}}
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
            {\ctikzvalof{tripoles/gm amp/input height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@left}
            {\ctikzvalof{tripoles/gm amp/input height}\pgf@circ@res@down}}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@right}{0pt}} %
        \pgfsetrectcap
        \pgfusepath{draw}
        \pgf@circ@text@strokecolor
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/gm amp/input height}\pgf@circ@res@up}]{\ctikzvalof{tripoles/gm amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@plus\else\pgf@circ@ampli@minus\fi}
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/gm amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/gm amp/input height}\pgf@circ@res@down}]{\ctikzvalof{tripoles/gm amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@minus\else\pgf@circ@ampli@plus\fi}
    }
}

% Instrumentation amplifier
\pgfdeclareshape{inst amp}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{amplifiers}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    % when tikz calls the anchor it wants the relative position in the lengths
    % \pgf@x  \pgf@y
    % \pgfpoint* functions set that variables
    % anchors are visible outside and run on use
    \anchor{center}{\pgfpointorigin}
    % savedanchors are internals and run on node creation (not use)
    % bounding-box top left
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/inst amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{nw}{
        \northwest
    }
    \anchor{south}{
        \northwest
        \pgf@y=-\pgf@y
    }
    \anchor{north}{
        \northwest
    }
    \savedanchor\left{%
        \pgf@y=0pt
    }
    \anchor{leftedge}
    {\left
        \pgf@x = \ctikzvalof{tripoles/op amp/port width}\pgf@x
    }
    \savedanchor\inOneFixed{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
          \pgf@y=\ctikzvalof{tripoles/inst amp/height}\pgf@circ@scaled@Rlen
          \pgf@y=.5\pgf@y
          \pgf@y=\ctikzvalof{tripoles/inst amp/input height}\pgf@y
          \pgf@x=-\ctikzvalof{tripoles/inst amp/width}\pgf@circ@scaled@Rlen
          \pgf@x=.5\pgf@x
    }
    \anchor{in up}{
        \inOneFixed
    }
    \anchor{in down}{
        \inOneFixed
        \pgf@y=-\pgf@y
    }
    \savedanchor\inOne{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/inst amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/inst amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \anchor{-}{
        \inOne
    }
    \anchor{+}{
        \inOne
        \pgf@y=-\pgf@y
    }
    \savedanchor\up{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/inst amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/inst amp/up pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/inst amp/height 2}\pgf@circ@res@up}}
    }
    \anchor{up}{
        \up
    }
    \anchor{down}{
        \up
        \pgf@y=-\pgf@y
    }
    % reference voltage input anchors.
    \savedanchor\refv{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/inst amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/inst amp/refv pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/inst amp/height 2}\pgf@circ@res@up}}
    }
    % we need both because they are normally drawn under the amp, and if you
    % mirror it vertically you need them
    \anchor{refv up}{
        \refv
    }
    \anchor{refv down}{
        \refv
        \pgf@y=-\pgf@y
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    % support for border anchors
    \savedanchor\leftedge{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/inst amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@x = \ctikzvalof{tripoles/inst amp/port width}\pgf@x
        \pgf@y=0pt
    }
    \anchor{leftedge}{\leftedge}
    \anchor{rightedge}{\leftedge \pgf@x =-\pgf@x }
    \anchor{bin up}{
        \inOneFixed
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bin down}{
        \inOneFixed
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b-}{
        \inOne
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b+}{
        \inOne
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bout}{
        \leftedge
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    % end border anchors
    \anchor{west}{
        \left
    }
    \anchor{east}{
        \left
        \pgf@x=-\pgf@x
    }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }

    \anchor{text}{
        \ifpgf@circ@center@text
            \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \else
            \leftedge
            \pgfmathsetlength{\pgf@x}{\pgf@x + \ctikzvalof{left text distance}}
        \fi
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }

    % let's start drawing the component
    \pgf@circ@draw@component{
        \pgf@circ@setcolor
        %
        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgf@circ@scaled@Rlen=\scaledRlen

        % main component, normally in thicker lines
        \pgfscope
            \newdimen\pgf@circ@res@right@double
						\pgf@circ@res@right@double=2\pgf@circ@res@right

            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@step
            %first point (near output)
            \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@right@double}{0}}
            %from the exit to the top (short side)... (note that the .6 must be copied in \up and \refv anchors
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@right@double}{\ctikzvalof{tripoles/inst amp/height 2}\pgf@circ@res@up}}
            % and then to the input "front up", "down", to the output short side "down"
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@right@double}{\ctikzvalof{tripoles/inst amp/height 2}\pgf@circ@res@down}}
            % ...and close
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope

        % Negative input terminal
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\ctikzvalof{tripoles/inst amp/input height}\pgf@circ@res@up}}
        %
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/inst amp/input height}\pgf@circ@res@up}}
        %

        % Positive input terminal
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\ctikzvalof{tripoles/inst amp/input height}\pgf@circ@res@down}}
        %
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/inst amp/input height}\pgf@circ@res@down}}

        % Output terminal
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@right}{0pt}} %
        %
        \pgfsetrectcap
        \pgfusepath{draw}
        \pgf@circ@text@strokecolor
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/inst amp/input height}\pgf@circ@res@up}]{\ctikzvalof{tripoles/inst amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@plus\else\pgf@circ@ampli@minus\fi}
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/inst amp/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/inst amp/input height}\pgf@circ@res@down}]{\ctikzvalof{tripoles/inst amp/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@minus\else\pgf@circ@ampli@plus\fi}
    }
}

% Instrumentation amplifier with terminals for gain resistance between inputs
\pgfdeclareshape{inst amp ra}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{amplifiers}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    % bounding-box top left
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/inst amp ra/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/inst amp ra/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{nw}{
        \northwest
    }
    \anchor{south}{
        \northwest
        \pgf@y=-\pgf@y\pgf@x=0pt\relax
    }
    \anchor{north}{
        \northwest\pgf@x=0pt\relax
    }
    \savedanchor\left{%
        \pgf@y=0pt
    }
    \anchor{leftedge}
    {\left
        \pgf@x = \ctikzvalof{tripoles/op amp/port width}\pgf@x
    }
    % inputs (+-)
    \savedanchor\inOneFixed{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
          \pgf@y=\ctikzvalof{tripoles/inst amp ra/height}\pgf@circ@scaled@Rlen
          \pgf@y=.5\pgf@y
          \pgf@y=\ctikzvalof{tripoles/inst amp ra/input height}\pgf@y
          \pgf@x=-\ctikzvalof{tripoles/inst amp ra/width}\pgf@circ@scaled@Rlen
          \pgf@x=.5\pgf@x
    }
    \anchor{in up}{
        \inOneFixed
    }
    \anchor{in down}{
        \inOneFixed
        \pgf@y=-\pgf@y
    }
    \savedanchor\inOne{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/inst amp ra/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/inst amp ra/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/inst amp ra/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \anchor{-}{
        \inOne
    }
    \anchor{+}{
        \inOne
        \pgf@y=-\pgf@y
    }
    % R ampli anchors. They are by default at 20% more than R-length distance
    % you can change that with the `ra pos` key (use 0.5 for one-R).
    \savedanchor\raOneFixed{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\pgf@circ@scaled@Rlen
        \pgf@y=\ctikzvalof{tripoles/inst amp ra/ra pos}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/inst amp ra/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{ra up}{
        \raOneFixed
    }
    \anchor{ra down}{
        \raOneFixed
        \pgf@y=-\pgf@y
    }
    \savedanchor\raOne{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\pgf@circ@scaled@Rlen
        \pgf@y=\ctikzvalof{tripoles/inst amp ra/ra pos}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/inst amp ra/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \anchor{ra-}{
        \raOne
    }
    \anchor{ra+}{
        \raOne
        \pgf@y=-\pgf@y
    }
    % power supplies
    \savedanchor\up{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/inst amp ra/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/inst amp ra/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/inst amp ra/up pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/inst amp ra/height 2}\pgf@circ@res@up}}
    }
    \anchor{up}{
        \up
    }
    \anchor{down}{
        \up
        \pgf@y=-\pgf@y
    }
    % reference voltage input anchors.
    \savedanchor\refv{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/inst amp ra/height}\pgf@circ@scaled@Rlen
        \pgf@y=0.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/inst amp ra/width}\pgf@circ@scaled@Rlen
        \pgf@x=0.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/inst amp ra/refv pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@right}{\ctikzvalof{tripoles/inst amp ra/height 2}\pgf@circ@res@up}}
    }
    % we need both because they are normally drawn under the amp, and if you
    % mirror it vertically you need them
    \anchor{refv up}{
        \refv
    }
    \anchor{refv down}{
        \refv
        \pgf@y=-\pgf@y
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    % support for border anchors
    \savedanchor\leftedge{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/inst amp ra/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@x = \ctikzvalof{tripoles/inst amp ra/port width}\pgf@x
        \pgf@y=0pt
    }
    \anchor{leftedge}{\leftedge}
    \anchor{rightedge}{\leftedge \pgf@x =-\pgf@x }
    \anchor{bin up}{
        \inOneFixed
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bin down}{
        \inOneFixed
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b-}{
        \inOne
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b+}{
        \inOne
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bra up}{
        \raOneFixed
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bra down}{
        \raOneFixed
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bra-}{
        \raOne
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bra+}{
        \raOne
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bout}{
        \leftedge
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    % end border anchors
    \anchor{west}{
        \left
    }
    \anchor{east}{
        \left
        \pgf@x=-\pgf@x
    }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }

    \anchor{text}{
        \ifpgf@circ@center@text
            \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \else
            \leftedge
            \pgfmathsetlength{\pgf@x}{\pgf@x + \ctikzvalof{left text distance}}
        \fi
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }

    % drawing of the component
    \pgf@circ@draw@component{
        \pgf@circ@setcolor

        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgf@circ@scaled@Rlen=\scaledRlen

        \newdimen\pgf@circ@res@right@double
				\pgf@circ@res@right@double=2\pgf@circ@res@right

        % main component, normally in thicker lines
        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@step
            %primer punto: la linea de salida (lado componente)
            \pgfpathmoveto{\pgfpoint{\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@right@double}{0}}
            %from the exit to the top (short side)... (note that the .6 must be copied in \up anchor
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@right@double}{\ctikzvalof{tripoles/inst amp ra/height 2}\pgf@circ@res@up}}
            % and then to the input "front up", "down", to the output short side "down"
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@right@double}{\ctikzvalof{tripoles/inst amp ra/height 2}\pgf@circ@res@down}}
            % ...and close
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope

        % ra terminal -
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\ctikzvalof{tripoles/inst amp ra/ra pos}\pgf@circ@scaled@Rlen}}
        %
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/inst amp ra/ra pos}\pgf@circ@scaled@Rlen}}
        % ra terminal +
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {-\ctikzvalof{tripoles/inst amp ra/ra pos}\pgf@circ@scaled@Rlen}}
        %
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@left}
        {-\ctikzvalof{tripoles/inst amp ra/ra pos}\pgf@circ@scaled@Rlen}}

        % Negative input terminal
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\ctikzvalof{tripoles/inst amp ra/input height}\pgf@circ@res@up}}
        %
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/inst amp ra/input height}\pgf@circ@res@up}}
        %

        % Positive input terminal
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\ctikzvalof{tripoles/inst amp ra/input height}\pgf@circ@res@down}}
        %
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/inst amp ra/input height}\pgf@circ@res@down}}

        % Output terminal
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@right}{0pt}} %
        %
        \pgfsetrectcap
        \pgfusepath{draw}
        \pgf@circ@text@strokecolor
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/inst amp ra/input height}\pgf@circ@res@up}]{\ctikzvalof{tripoles/inst amp ra/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@plus\else\pgf@circ@ampli@minus\fi}
        \pgftext[left, at=\pgfpoint{\ctikzvalof{tripoles/inst amp ra/port width}\pgf@circ@res@left}{\ctikzvalof{tripoles/inst amp ra/input height}\pgf@circ@res@down}]{\ctikzvalof{tripoles/inst amp ra/font} \ifpgf@circuit@oa@iplusup\pgf@circ@ampli@minus\else\pgf@circ@ampli@plus\fi}
    }
}

% Buffer
% Contributed by Danilo Piazzalunga
\pgfdeclareshape{buffer}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{amplifiers}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{bipoles/buffer/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{bipoles/buffer/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \savedanchor\left{%
        \pgf@y=0pt
    }
    \anchor{in}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{bin}{
        \northwest
        \pgf@y=0pt
        \pgf@x=0.7\pgf@x
    }
    \anchor{bout}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-0.7\pgf@x
    }

    \anchor{text}{
        \ifpgf@circ@center@text
            \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \else
            \northwest
            \pgfmathsetlength{\pgf@x}{0.7*\pgf@x + \ctikzvalof{left text distance}}
        \fi
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }

    \pgf@circ@draw@component{
        \pgf@circ@setcolor

        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgf@circ@scaled@Rlen=\scaledRlen
        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{.7\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=.7\pgf@circ@res@step

            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@left}{0pt}}

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{.7\pgf@circ@res@right}{0pt}}

        \pgfusepath{draw}
    }
}

% plain amplifier, no symbols
\pgfdeclareshape{plain amp}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{amplifiers}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/plain amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/plain amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \savedanchor\left{%
        \pgf@y=0pt
    }
    \savedanchor\inOne{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/plain amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/plain amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/plain amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \ifpgf@circuit@oa@iplusup\pgf@y=-\pgf@y\fi
    }
    \savedanchor\inOneFixed{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/plain amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@y=\ctikzvalof{tripoles/plain amp/input height}\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/plain amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{-}{
        \inOne
    }
    \anchor{+}{
        \inOne
        \pgf@y=-\pgf@y
    }
    \anchor{in up}{
        \inOneFixed
    }
    \anchor{in down}{
        \inOneFixed
        \pgf@y=-\pgf@y
    }
    % support for border anchors
    \savedanchor\leftedge{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/plain amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@x = \ctikzvalof{tripoles/plain amp/port width}\pgf@x
        \pgf@y=0pt
    }
    \anchor{leftedge}{\leftedge}
    \anchor{rightedge}{\leftedge \pgf@x =-\pgf@x }
    \anchor{bin up}{
        \inOneFixed
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bin down}{
        \inOneFixed
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b-}{
        \inOne
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{b+}{
        \inOne
        \pgf@y=-\pgf@y
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bout}{
        \leftedge
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    % end border anchors
    \savedanchor\up{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/plain amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/plain amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/plain amp/up pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@right}{0pt}}
    }
    \anchor{up}{
        \up
    }
    \anchor{down}{
        \up
        \pgf@y=-\pgf@y
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }

    \anchor{text}{
        \ifpgf@circ@center@text
            \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \else
            \leftedge
            \pgfmathsetlength{\pgf@x}{\pgf@x + \ctikzvalof{left text distance}}
        \fi
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }

    \pgf@circ@draw@component{
        \pgf@circ@setcolor

        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x

        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@step

            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\ctikzvalof{tripoles/plain amp/input height}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/plain amp/input height}\pgf@circ@res@up}}


        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {\ctikzvalof{tripoles/plain amp/input height}\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@left}
        {\ctikzvalof{tripoles/plain amp/input height}\pgf@circ@res@down}}

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@right}{0pt}}

        \pgfusepath{draw}
    }
}

% plain amplifier, no symbols, one input
\pgfdeclareshape{plain mono amp}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{amplifiers}}
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \anchor{center}{\pgfpointorigin}
    \savedanchor\northwest{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/plain amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/plain amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{east}{ \northwest \pgf@y=0pt \pgf@x=-\pgf@x  }
    \anchor{west}{ \northwest \pgf@y=0pt }
    \anchor{south}{ \northwest \pgf@x=0pt \pgf@y=-\pgf@y }
    \anchor{north}{ \northwest \pgf@x=0pt }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \savedanchor\left{%
        \pgf@y=0pt
    }
    \savedanchor\inOne{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/plain amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@y=0pt\relax
    }
    \anchor{in}{
        \inOne
    }
    % support for border anchors
    \savedanchor\leftedge{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@x=-\ctikzvalof{tripoles/plain amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@x = \ctikzvalof{tripoles/plain amp/port width}\pgf@x
        \pgf@y=0pt
    }
    \anchor{leftedge}{\leftedge}
    \anchor{rightedge}{\leftedge \pgf@x =-\pgf@x }
    \anchor{bin}{
        \inOne
        \pgf@ya=\pgf@y \leftedge \pgf@y=\pgf@ya
    }
    \anchor{bout}{
        \leftedge
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    % end border anchors
    \savedanchor\up{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/plain amp/height}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/plain amp/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfpointlineattime{
            \ctikzvalof{tripoles/plain amp/up pos}}{
            \pgfpoint{
            \ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@left}
        {\pgf@circ@res@up}}
        {\pgfpoint{\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@right}{0pt}}
    }
    \anchor{up}{
        \up
    }
    \anchor{down}{
        \up
        \pgf@y=-\pgf@y
    }
    \anchor{out}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }

    \anchor{text}{
        \ifpgf@circ@center@text
            \pgf@x=\dimexpr -.6\wd\pgfnodeparttextbox\relax
        \else
            \leftedge
            \pgfmathsetlength{\pgf@x}{\pgf@x + \ctikzvalof{left text distance}}
        \fi
        \pgf@y=\dimexpr.5\dp\pgfnodeparttextbox-.5\ht\pgfnodeparttextbox\relax
    }
    \pgf@circ@draw@component{
        \pgf@circ@setcolor

        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x

        \pgfscope
            \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
            \pgftransformxshift{\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@left}
            \pgf@circ@res@step=\pgf@circ@res@right
            \advance\pgf@circ@res@step by -\pgf@circ@res@left
            \pgf@circ@res@step=\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@step

            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{0pt}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
            \pgfpathclose
            \pgf@circ@draworfill
        \endpgfscope
        \pgfpathmoveto{\pgfpoint
            {\pgf@circ@res@left}
        {0pt}}
        \pgfpathlineto{\pgfpoint
            {\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@left}
        {0pt}}

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tripoles/plain amp/port width}\pgf@circ@res@right}{0pt}}

        \pgfusepath{draw}
    }
}% %>>>

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Tubes and magnetron
%%%%%%%%%%%%%%%%%%%%%%%%

% Settings Electronic tubes: diodetube, triode, tetrode and pentode%<<<1
\ctikzset{tubes/width/.initial=1}                    % relative width
\ctikzset{tubes/height/.initial=1.4}                 % relative height
\ctikzset{tubes/tube radius/.initial=0.40}           % radius of tube circle
\ctikzset{tubes/anode distance/.initial=0.40}        % distance from center
\ctikzset{tubes/anode width/.initial=0.40}           % width of an anode/plate
\ctikzset{tubes/grid protrusion/.initial=0.25}       % distance from center
\ctikzset{tubes/grid dashes/.initial=5}              % number of grid dashes
\ctikzset{tubes/grid separation/.initial=0.2}        % separation between grids
\ctikzset{tubes/grid shift/.initial=0.0}             % y shift grids from center
\ctikzset{tubes/cathode distance/.initial=0.40}      % distance from grid
\ctikzset{tubes/cathode width/.initial=0.40}         % width of an cathode
\ctikzset{tubes/cathode corners/.initial=0.06}       % corners of the cathode wire
\ctikzset{tubes/cathode right extend/.initial=0.075} % extension at the right side
\ctikzset{tubes/filament distance/.initial=0.1}      % distance from cathode
\ctikzset{tubes/filament angle/.initial=15}          % Angle from centerpoint
% partial borders styles
% this can be "none" or 6 numbers saying the style for each part:
% 0 --- nothing, 1 --- solid, 2 --- dashed
\ctikzset{tubes/partial borders/.initial=none}       % Value none for normal borders
\ctikzset{tubes/partial border dash/.initial={{2pt}{2pt}}}

\ctikzset{/tikz/circuitikz/tripoles/magnetron/width/.initial=1}

\newif\ifpgf@circuit@tubes@filament\pgf@circuit@tubes@filamentfalse
\pgfkeys{/tikz/filament/.add code={}{\pgf@circuit@tubes@filamenttrue}}
\ctikzset{tubes/filament/.add code={}{\pgf@circuit@tubes@filamenttrue}}
\newif\ifpgf@circuit@tubes@nocathode\pgf@circuit@tubes@nocathodefalse
\pgfkeys{/tikz/nocathode/.add code={}{\pgf@circuit@tubes@nocathodetrue}}
\ctikzset{tubes/nocathode/.add code={}{\pgf@circuit@tubes@nocathodetrue}}
\newif\ifpgf@circuit@tubes@fullcathode\pgf@circuit@tubes@fullcathodefalse
\pgfkeys{/tikz/fullcathode/.add code={}{\pgf@circuit@tubes@fullcathodetrue}}
\ctikzset{tubes/fullcathode/.add code={}{\pgf@circuit@tubes@fullcathodetrue}}%
%>>>

%% Node shapes for tubes & co%<<<

%%%%%%%%%%%%%%%%%%%
%% Magnetron
%%%%%%%%%%%%%%%%%%%

\pgfdeclareshape{magnetron}
{
    \savedmacro{\ctikzclass}{\edef\ctikzclass{tubes}}  % class of these components
    \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
    \savedanchor\northwest{
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@y=\ctikzvalof{tripoles/magnetron/width}\pgf@circ@scaled@Rlen
        \pgf@y=.5\pgf@y
        \pgf@x=-\ctikzvalof{tripoles/magnetron/width}\pgf@circ@scaled@Rlen
        \pgf@x=.5\pgf@x
    }
    \anchor{center}{
        \pgfpointorigin
    }
    \anchor{anode}{
        \northwest
        \pgf@x=0pt
        \pgf@y=-\pgf@y
    }
    \anchor{cathode1}{
        \northwest
        \pgf@circ@res@step=\pgf@y
        \pgfmathparse{cos(105)}
        \pgf@x=\pgfmathresult\pgf@circ@res@step
        \pgfmathparse{sin(105)}
        \pgf@y=\pgfmathresult\pgf@circ@res@step
        %\pgfpointorigin
        %\pgfpathmoveto{\pgfpointpolar{105}{\pgf@circ@res@step}}%not working in a scaled tikzpicture
    }
    \anchor{cathode2}{
        \northwest
        \pgf@circ@res@step=\pgf@y
        \pgfmathparse{cos(75)}
        \pgf@x=\pgfmathresult\pgf@circ@res@step
        \pgfmathparse{sin(75)}
        \pgf@y=\pgfmathresult\pgf@circ@res@step
        %\pgfpointorigin
        %\pgfpathmoveto{\pgfpointpolar{75}{\pgf@circ@res@step}}%not working in a scaled tikzpicture
    }
    \anchor{text}{
        \pgfpointorigin
        \advance \pgf@x by -.5\wd\pgfnodeparttextbox
        \advance \pgf@y by -.5\ht\pgfnodeparttextbox
    }
    \anchor{left}{%
        \northwest
        \pgf@y=0pt
    }
    \anchor{right}{%
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{top}{
        \northwest
        \pgf@x=0pt
    }
    \anchor{pathstart}{
        \northwest
        \pgf@x=0pt
    }
    \anchor{pathend}{
        \northwest
        \pgf@x=0pt
        \pgf@y=-\pgf@y
    }
    \anchor{bottom}{
        \northwest
        \pgf@x=0pt
        \pgf@y=-\pgf@y
    }
    \anchor{center}{
        \pgf@y=0pt
        \pgf@x=0pt
    }
    \anchor{east}{
        \northwest
        \pgf@y=0pt
        \pgf@x=-\pgf@x
    }
    \anchor{west}{
        \northwest
        \pgf@y=0pt
    }
    \anchor{south}{
        \northwest
        \pgf@x=0pt
        \pgf@y=-\pgf@y
    }
    \anchor{north}{
        \northwest
        \pgf@x=0pt
    }
    \anchor{south west}{ \northwest \pgf@y=-\pgf@y }
    \anchor{north east}{ \northwest \pgf@x=-\pgf@x }
    \anchor{north west}{ \northwest }
    \anchor{south east}{ \northwest \pgf@x=-\pgf@x \pgf@y=-\pgf@y }
    \pgf@circ@draw@component{
        \pgf@circ@setcolor
        \pgf@circ@scaled@Rlen=\scaledRlen
        \pgf@circ@res@step=\ctikzvalof{tripoles/magnetron/width}\pgf@circ@scaled@Rlen
        \northwest
        \pgf@circ@res@up = \pgf@y
        \pgf@circ@res@down = -\pgf@y
        \pgf@circ@res@right = -\pgf@x
        \pgf@circ@res@left = \pgf@x
        \pgfmathsetlength{\pgf@circ@res@other}{sin(15)*\pgf@circ@res@up}

        \pgfscope
            \pgfstartlinewidth=\pgflinewidth
            \pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
            %create outer circle
            \pgfpathcircle{\pgfpoint{0}{0}} {\pgf@circ@res@right}
            \pgf@circ@draworfill
            %create chambers
            \foreach \angle in {45,135,225,315}{
                \pgfpathmoveto{ \pgfpointpolar{\angle}{0.6\pgf@circ@res@right}}
                \pgfpathlineto{ \pgfpointpolar{\angle}{\pgf@circ@res@right}}
            }
            \pgfsetroundcap
            \pgfusepath{draw}
            \pgfscope
                %draw connection from outside
                %anode
                \pgfsetlinewidth{\pgfstartlinewidth}
                \pgfpathmoveto{\pgfpoint{0\pgf@circ@res@left}{\pgf@circ@res@down}}
                \pgfpathlineto{\pgfpoint{0\pgf@circ@res@right}{.5\pgf@circ@res@down}}
                %cathodes
                \pgfpathmoveto{\pgfpointpolar{105}{\pgf@circ@res@up}}
                \pgfpathlineto{\pgfpoint{-\pgf@circ@res@other}{.5\pgf@circ@res@up}}
                \pgfpathmoveto{\pgfpointpolar{75}{\pgf@circ@res@up}}
                \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{.5\pgf@circ@res@up}}
                \pgfsetbuttcap
                \pgfusepath{draw}
            \endpgfscope
            %create cathode
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other}{.5\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{0}{.15\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{-\pgf@circ@res@other}{.5\pgf@circ@res@up}}
            \pgfusepath{draw}
            %create anode
            \pgfpathmoveto{\pgfpoint{0.3\pgf@circ@res@left}{.5\pgf@circ@res@down}}
            \pgfpathlineto{\pgfpoint{0.3\pgf@circ@res@right}{.5\pgf@circ@res@down}}

            \pgfsetbuttcap
            \pgfusepath{draw}
        \endpgfscope
    }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Electronic tubes, submitted by J. op den Brouw
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% draw partial tube outline
%
\def\pgf@circ@tubes@split#1#2#3#4#5#6\relax{%split the six numbers
    \edef\@@a{#1}\edef\@@b{#2}\edef\@@c{#3}\edef\@@d{#4}\edef\@@e{#5}\edef\@@f{#6}%
}
\def\pgf@circ@tubes@setdash{%
    \edef\@@dash{\ctikzvalof{tubes/partial border dash}}%
    % \typeout{DASH:\@@dash}%
    \expandafter\pgfsetdash\expandafter{\@@dash}{0pt}%
}
% this is the same as the transistor circles' one, but let's keep it separated
\def\pgf@circ@tubes@draw@style#1{%
    \ifcase#1
        \pgfusepath{discard}% case 0, will discard the path
    \or
        \pgfsetdash{}{0pt}\pgfusepath{draw}% case 1, solid
    \or
        \pgf@circ@tubes@setdash\pgfusepath{draw}% case 2, dashed
    \else
        \pgfutil@packagewarning{circuitikz}{Tube border draw style not known!}%
        \pgfusepath{draw}%
    \fi
}
\def\pgf@circ@tubes@draw@partial#1{%
    \expandafter\pgf@circ@tubes@split#1\relax
    % \typeout{PARTIAL: \@@a:\@@b:\@@c:\@@d:\@@e:\@@f}
    \ifdim\ctikzvalof{tubes/width}pt>\ctikzvalof{tubes/height}pt\relax
        % horizontal tube
        \pgfutil@tempdima=\pgf@circ@res@right
        \advance\pgfutil@tempdima by -\pgf@circ@res@up
        \pgfscope % left side upper arc
            \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{0pt}}
            \pgfpatharc{180}{90}{\pgf@circ@res@up}
            \pgf@circ@tubes@draw@style{\@@a}
        \endpgfscope
        \pgfscope % upper horizontal part
            \pgfpathmoveto{\pgfpoint{-\pgfutil@tempdima}{\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\pgfutil@tempdima}{\pgf@circ@res@up}}
            \pgf@circ@tubes@draw@style{\@@b}
        \endpgfscope
        \pgfscope % right side upper arc
            \pgfpathmoveto{\pgfpoint{\pgfutil@tempdima}{\pgf@circ@res@up}}
            \pgfpatharc{90}{0}{\pgf@circ@res@up}
            \pgf@circ@tubes@draw@style{\@@c}
        \endpgfscope
        \pgfscope % right side lower arc
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
            \pgfpatharc{0}{-90}{\pgf@circ@res@up}
            \pgf@circ@tubes@draw@style{\@@d}
        \endpgfscope
        \pgfscope % lower horizontal part
            \pgfpathmoveto{\pgfpoint{\pgfutil@tempdima}{-\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{-\pgfutil@tempdima}{-\pgf@circ@res@up}}
            \pgf@circ@tubes@draw@style{\@@e}
        \endpgfscope
        \pgfscope % left side lower arc
            \pgfpathmoveto{\pgfpoint{-\pgfutil@tempdima}{-\pgf@circ@res@up}}
            \pgfpatharc{270}{180}{\pgf@circ@res@up}
            \pgf@circ@tubes@draw@style{\@@f}
        \endpgfscope
    \else
        % vertical tube
        \pgfutil@tempdima=\pgf@circ@res@up
        \advance\pgfutil@tempdima by -\pgf@circ@res@right
        \pgfscope % left side upper arc
            \pgfpathmoveto{\pgfpoint{0pt}{\pgf@circ@res@up}}
            \pgfpatharc{90}{0}{\pgf@circ@res@right}
            \pgf@circ@tubes@draw@style{\@@a}
        \endpgfscope
        \pgfscope % right vertical part
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgfutil@tempdima}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{-\pgfutil@tempdima}}
            \pgf@circ@tubes@draw@style{\@@b}
        \endpgfscope
        \pgfscope % right side lower arc
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{-\pgfutil@tempdima}}
            \pgfpatharc{0}{-90}{\pgf@circ@res@right}
            \pgf@circ@tubes@draw@style{\@@c}
        \endpgfscope
        \pgfscope % right side lower arc
            \pgfpathmoveto{\pgfpoint{0pt}{-\pgf@circ@res@up}}
            \pgfpatharc{-90}{-180}{\pgf@circ@res@right}
            \pgf@circ@tubes@draw@style{\@@d}
        \endpgfscope
        \pgfscope % left vertical part
            \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{-\pgfutil@tempdima}}
            \pgfpathlineto{\pgfpoint{-\pgf@circ@res@right}{\pgfutil@tempdima}}
            \pgf@circ@tubes@draw@style{\@@e}
        \endpgfscope
        \pgfscope % left side upper arc
            \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{\pgfutil@tempdima}}
            \pgfpatharc{180}{90}{\pgf@circ@res@right}
            \pgf@circ@tubes@draw@style{\@@f}
        \endpgfscope
    \fi
}
% Draw tube outline
\def\pgf@circ@tubes@drawtube{%
    \ifdim\ctikzvalof{tubes/width}pt>\ctikzvalof{tubes/height}pt\relax
        % horizontal tube
        \pgfpathmoveto{\pgfpoint{0pt}{\pgf@circ@res@up}}
        \pgfutil@tempdima=\pgf@circ@res@right
        \advance\pgfutil@tempdima by -\pgf@circ@res@up
        \pgfpathlineto{\pgfpoint{\pgfutil@tempdima}{\pgf@circ@res@up}}
        \pgfpatharc{90}{-90}{\pgf@circ@res@up}
        \pgfpathlineto{\pgfpoint{-\pgfutil@tempdima}{-\pgf@circ@res@up}}
        \pgfpatharc{270}{90}{\pgf@circ@res@up}
    \else
        % vertical tube
        \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{0pt}}
        \pgfutil@tempdima=\pgf@circ@res@up
        \advance\pgfutil@tempdima by -\pgf@circ@res@right
        \pgfpathlineto{\pgfpoint{-\pgf@circ@res@right}{\pgfutil@tempdima}}
        \pgfpatharc{180}{0}{\pgf@circ@res@right}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{-\pgfutil@tempdima}}
        \pgfpatharc{180}{0}{-\pgf@circ@res@right}
    \fi
    \pgfpathclose
    \edef\@@partial{\ctikzvalof{tubes/partial borders}}
    \ifx\@@partial\pgf@nonetext
        % Tube fill and draw if it's not partial... for speed
        \pgf@circ@draworfill
    \else
        % otherwise, just fill and do the partial fill
        \pgf@circ@maybefill
        \pgf@circ@tubes@draw@partial{\@@partial}
        %
    \fi
}

\long\def\pgfcirctubedrawanodestd{% Standard anode code (one anode)
    % Anode (or plate)
    \pgfpathmoveto{\pgfpoint{0pt}{\pgf@circ@res@up}} % north
    \pgfpathlineto{\pgfpoint{0pt}{\ctikzvalof{tubes/anode distance}\pgf@circ@res@up}}
    \pgfpathmoveto{\pgfpoint{-\ctikzvalof{tubes/anode width}\pgf@circ@res@right}{\ctikzvalof{tubes/anode distance}\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\ctikzvalof{tubes/anode width}\pgf@circ@res@right}{\ctikzvalof{tubes/anode distance}\pgf@circ@res@up}}
}
\long\def\pgfcirctubedrawcathodestd{% Standard cathode code (one cathode)
    % Cathode
    \ifpgf@circuit@tubes@nocathode
        \pgf@circuit@tubes@nocathodefalse
    \else
        \pgfsetcornersarced{\pgfpoint{\ctikzvalof{tubes/cathode corners}\pgf@circ@res@up}{\ctikzvalof{tubes/cathode corners}\pgf@circ@res@up}}
        \pgfpathmoveto{\pgfpoint{-\ctikzvalof{tubes/cathode width}\pgf@circ@res@right}{-\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{-\ctikzvalof{tubes/cathode width}\pgf@circ@res@right}{-\ctikzvalof{tubes/cathode distance}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tubes/cathode width}\pgf@circ@res@right}{-\ctikzvalof{tubes/cathode distance}\pgf@circ@res@up}}
        \ifpgf@circuit@tubes@fullcathode
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tubes/cathode width}\pgf@circ@res@right}{-\pgf@circ@res@up}}
            \pgf@circuit@tubes@fullcathodefalse
        \else
            \pgfpathlineto{\pgfpoint{\ctikzvalof{tubes/cathode width}\pgf@circ@res@right}{-\ctikzvalof{tubes/cathode distance}\pgf@circ@res@up-\ctikzvalof{tubes/cathode right extend}\pgf@circ@res@up}}
        \fi
    \fi
}

%% generic macro to create a tube. Most of them will use the standard macro for
%% anode and cathode (see above)
\long\def\pgfcircdeclaretubegeneric#1#2#3#4#5#6{% name, anchors, grid, anode, cathode variable anchors
    \pgfdeclareshape{#1}{
        \savedmacro{\ctikzclass}{\edef\ctikzclass{tubes}}
        \saveddimen{\scaledRlen}{\pgfmathsetlength{\pgf@x}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}}
        \savedanchor\northwest{%
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgf@circ@res@up=\ctikzvalof{tubes/height}\pgf@circ@scaled@Rlen
            \pgf@circ@res@right=\ctikzvalof{tubes/width}\pgf@circ@scaled@Rlen
            % x and y should be half the Rlen
            \pgf@y=\pgf@circ@res@up
            \pgf@y=.5\pgf@y
            \pgf@x=-\pgf@circ@res@right
            \pgf@x=.5\pgf@x
        }
        \savedanchor\tubene{%
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgf@circ@res@temp=\ctikzvalof{tubes/height}\pgf@circ@scaled@Rlen
            \pgf@circ@res@other=\ctikzvalof{tubes/width}\pgf@circ@scaled@Rlen
            \pgf@circ@res@up=\ctikzvalof{tubes/tube radius}\pgf@circ@res@temp
            \pgf@circ@res@right=\ctikzvalof{tubes/tube radius}\pgf@circ@res@other
            \pgf@y=\pgf@circ@res@up
            \pgf@x=\pgf@circ@res@right
        }
        \savedanchor\tubesquarene{%
            \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
            \pgf@circ@res@temp=\ctikzvalof{tubes/height}\pgf@circ@scaled@Rlen
            \pgf@circ@res@other=\ctikzvalof{tubes/width}\pgf@circ@scaled@Rlen
            \pgf@circ@res@up=\ctikzvalof{tubes/tube radius}\pgf@circ@res@temp
            \pgf@circ@res@right=\ctikzvalof{tubes/tube radius}\pgf@circ@res@other
            \ifdim\ctikzvalof{tubes/width}pt>\ctikzvalof{tubes/height}pt\relax
                % horizontal
                \pgf@y=\pgf@circ@res@up
                \pgf@x=\pgf@circ@res@right\advance\pgf@x by -\pgf@circ@res@up
            \else
                % vertical
                \pgf@x=\pgf@circ@res@right
                \pgf@y=\pgf@circ@res@up\advance\pgf@y by -\pgf@circ@res@right
            \fi
        }
        \savedmacro{\tubes@filament@angle}{\edef\tubes@filament@angle{\ctikzvalof{tubes/filament angle}}}
        \savedmacro{\tubes@filament@distance}{\edef\tubes@filament@distance{\ctikzvalof{tubes/filament distance}}}
        \savedmacro{\tubes@tube@radius}{\edef\tubes@tube@radius{\ctikzvalof{tubes/tube radius}}}
        \savedmacro{\tubes@cathode@width}{\edef\tubes@cathode@width{\ctikzvalof{tubes/cathode width}}}
        \savedmacro{\tubes@cathode@distance}{\edef\tubes@cathode@distance{\ctikzvalof{tubes/cathode distance}}}
        \anchor{tube ne}{\tubene}
        \anchor{tubesq ne}{\tubesquarene}
        % tube shape anchors: geo
        \anchor{tube top}{\tubene\pgf@x=0pt\relax}
        \anchor{tube bottom}{\tubene\pgf@x=0pt\pgf@y=-\pgf@y}
        \anchor{tube right}{\tubene\pgf@y=0pt\relax}
        \anchor{tube left}{\tubene\pgf@y=0pt\pgf@x=-\pgf@x}
        % tube shape straight part anchors
        \anchor{tube top right}{\tubesquarene}
        \anchor{tube bottom right}{\tubesquarene\pgf@y=-\pgf@y}
        \anchor{tube top left}{\tubesquarene\pgf@x=-\pgf@x}
        \anchor{tube bottom left}{\tubesquarene\pgf@y=-\pgf@y\pgf@x=-\pgf@x}
        % tube shape centers
        \anchor{tube top center}{\tubesquarene\pgf@x=0pt\relax}
        \anchor{tube bottom center}{\tubesquarene\pgf@y=-\pgf@y\pgf@x=0pt\relax}
        \anchor{tube right center}{\tubesquarene\pgf@y=0pt\relax}
        \anchor{tube left center}{\tubesquarene\pgf@x=-\pgf@x\pgf@y=0pt\relax}
        % rest of anchors
        \anchor{center}{\pgfpointorigin}
        % geo anchors based on north-west
        \pgfcirc@northwest@symmetric@geoanchors
        % text anchor
        \anchor{text}{%
            \northwest\pgf@x=-\pgf@x
            \pgf@y=-.5\ht\pgfnodeparttextbox
        }
        % specific anchors
        \anchor{anode} {%
            \northwest
            \pgf@x=0pt
        }
        \anchor{cathode}{%
            \northwest
            \pgf@y=-\pgf@y
            \pgf@x=\tubes@cathode@width\pgf@x
        }
        \anchor{cathode 1}{%
            \northwest
            \pgf@y=-\pgf@y
            \pgf@x=\tubes@cathode@width\pgf@x
        }
        \anchor{cathode 2}{%
            \northwest
            \pgf@y=-\pgf@y
            \pgf@x=-\tubes@cathode@width\pgf@x
        }
        \anchor{filament 1}{%
            \northwest
            \pgfmathparse{(\tubes@tube@radius*sin(\tubes@filament@angle)}
            \pgf@x=\pgfmathresult\pgf@x
            \pgf@y=-\pgf@y
        }
        \anchor{filament 2}{%
            \northwest
            \pgfmathparse{(\tubes@tube@radius*sin(\tubes@filament@angle)}
            \pgf@x=-\pgfmathresult\pgf@x
            \pgf@y=-\pgf@y
        }
        \anchor{filament center}{%
            \northwest
            \pgf@ya=0.5\pgf@y
            \pgf@ya=-\tubes@cathode@distance\pgf@y
            \advance\pgf@ya by -\tubes@filament@distance\pgf@y
            \pgf@x=0pt\pgf@y=\pgf@ya
        }

        % Extra anchors
        #2%

        \pgf@circ@draw@component{
                % Line width for tripoles
                \pgf@circ@setlinewidth{tripoles}{\pgflinewidth}
                \pgf@circ@scaled@Rlen=\scaledRlen

                % Setup to draw tube
                \pgf@circ@res@up=\ctikzvalof{tubes/height}\pgf@circ@scaled@Rlen
                \pgf@circ@res@right=\ctikzvalof{tubes/width}\pgf@circ@scaled@Rlen
                \pgf@circ@res@up=\ctikzvalof{tubes/tube radius}\pgf@circ@res@up
                \pgf@circ@res@right=\ctikzvalof{tubes/tube radius}\pgf@circ@res@right

                % Tube outline
                \pgf@circ@tubes@drawtube

                % Setup to draw grid, filament, anode and cathode
                \pgf@circ@res@up=\ctikzvalof{tubes/height}\pgf@circ@scaled@Rlen
                \pgf@circ@res@right=\ctikzvalof{tubes/width}\pgf@circ@scaled@Rlen
                \pgf@circ@res@up=0.5\pgf@circ@res@up
                \pgf@circ@res@right=0.5\pgf@circ@res@right

                % Grid drawing
                #3%

                % Filament (is not drawn by default)
                \ifpgf@circuit@tubes@filament
                    \pgf@circ@res@temp=-\ctikzvalof{tubes/cathode distance}\pgf@circ@res@up
                    \advance\pgf@circ@res@temp by -\ctikzvalof{tubes/filament distance}\pgf@circ@res@up
                    \pgfmathparse{(\ctikzvalof{tubes/tube radius}*sin(\ctikzvalof{tubes/filament angle})}
                    \pgf@xa=\pgfmathresult\pgf@circ@res@right
                    \pgfmathparse{\ctikzvalof{tubes/tube radius}+\ctikzvalof{tubes/tube radius}*cos(\ctikzvalof{tubes/filament angle}}
                    \pgf@ya=\pgfmathresult\pgf@circ@res@up
                    \pgfpathmoveto{\pgfpoint{0pt}{\pgf@circ@res@temp}}
                    \pgfpathlineto{\pgfpoint{-\pgf@xa}{-\pgf@ya}}
                    \pgfpathlineto{\pgfpoint{-\pgf@xa}{-\pgf@circ@res@up}}
                    \pgfpathmoveto{\pgfpoint{0pt}{\pgf@circ@res@temp}}
                    \pgfpathlineto{\pgfpoint{\pgf@xa}{-\pgf@ya}}
                    \pgfpathlineto{\pgfpoint{\pgf@xa}{-\pgf@circ@res@up}}
                    \pgf@circuit@tubes@filamentfalse
                \fi

                % Anode (or plate)
                #4
                % Cathode
                #5
                % Draw the background
                \pgfusepath{draw}
        }
        #6
    }
}

%% The diode (tube), triode, tetrode and pentode only differ in the
%% number of grids. So we construct a generic declare function in
%% which we can put code for the grid anchors and grid drawing code
%% \pgfcircdeclaretube{tube name}{grid anchors}{grid drawing code}
\long\def\pgfcircdeclaretube#1#2#3{% name, anchors, grid
    \pgfcircdeclaretubegeneric{#1}{#2}{#3}{\pgfcirctubedrawanodestd}{\pgfcirctubedrawcathodestd}{}
}

\pgfcircdeclaretube{diodetube}{}{} % shape diode already exists

\pgfcircdeclaretube{triode}
{
	\anchor{grid} {% should not be used
		\northwest
		\pgf@y=\ctikzvalof{tubes/grid shift}\pgf@y
	}
	\anchor{control} {%
		\northwest
		\pgf@y=\ctikzvalof{tubes/grid shift}\pgf@y
	}
}
{
	% Grid protrusion
	\pgf@xa=-\ctikzvalof{tubes/tube radius}\pgf@circ@res@right
	\advance\pgf@xa by -\ctikzvalof{tubes/grid protrusion}\pgf@circ@res@right
	\pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{\ctikzvalof{tubes/grid shift}\pgf@circ@res@up}}
	\pgfpathlineto{\pgfpoint{\pgf@xa}{\ctikzvalof{tubes/grid shift}\pgf@circ@res@up}}
	% Grid dashes: calculations
	\pgf@xb=2\pgf@circ@res@right
	\pgf@circ@res@step=\ctikzvalof{tubes/tube radius}\pgf@xb
	\pgf@circ@count@a=\ctikzvalof{tubes/grid dashes}  % dashes*2+1
	\multiply\pgf@circ@count@a by 2\relax
	\advance\pgf@circ@count@a by 1\relax
	\advance\pgf@circ@res@step by -\pgf@xa
	\divide\pgf@circ@res@step by \pgf@circ@count@a
	% Grid dashes: draw
	\pgf@circ@res@temp=\pgf@xa
	\pgf@circ@count@a=\ctikzvalof{tubes/grid dashes}
	\loop
	\advance\pgf@circ@res@temp by\pgf@circ@res@step
	\pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\ctikzvalof{tubes/grid shift}\pgf@circ@res@up}}
	\advance\pgf@circ@res@temp by\pgf@circ@res@step
	\pgfpathlineto{\pgfpoint{\pgf@circ@res@temp}{\ctikzvalof{tubes/grid shift}\pgf@circ@res@up}}
	\advance\pgf@circ@count@a by-1
	\ifnum\pgf@circ@count@a>0\relax
	\repeat
}

\pgfcircdeclaretube{tetrode}
{
	\anchor{grid} {% should not be used
		\northwest
		\pgfutil@tempdima=\pgf@y
		\pgf@y=-\ctikzvalof{tubes/grid separation}\pgf@y
		\pgf@y=0.5\pgf@y
		\advance\pgf@y by \ctikzvalof{tubes/grid shift}\pgfutil@tempdima
	}
	\anchor{control} {%
		\northwest
		\pgfutil@tempdima=\pgf@y
		\pgf@y=-\ctikzvalof{tubes/grid separation}\pgf@y
		\pgf@y=0.5\pgf@y
		\advance\pgf@y by \ctikzvalof{tubes/grid shift}\pgfutil@tempdima
	}
	\anchor{screen} {%
		\northwest
		\pgfutil@tempdima=\pgf@y
		\pgf@y=\ctikzvalof{tubes/grid separation}\pgf@y
		\pgf@y=0.5\pgf@y
		\advance\pgf@y by \ctikzvalof{tubes/grid shift}\pgfutil@tempdima
	}
}
{
	% Grid x/y points
	\pgf@xa=-\ctikzvalof{tubes/tube radius}\pgf@circ@res@right
	\advance\pgf@xa by -\ctikzvalof{tubes/grid protrusion}\pgf@circ@res@right
	\pgfutil@tempdima=\ctikzvalof{tubes/grid separation}\pgf@circ@res@up
	\pgfutil@tempdimb=-\pgfutil@tempdima
	\pgfutil@tempdima=0.5\pgfutil@tempdima
	\advance\pgfutil@tempdima by \ctikzvalof{tubes/grid shift}\pgf@circ@res@up
	\pgfutil@tempdimb=0.5\pgfutil@tempdimb
	\advance\pgfutil@tempdimb by \ctikzvalof{tubes/grid shift}\pgf@circ@res@up
	% Grid protrusion
	\pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{\pgfutil@tempdimb}}
	\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgfutil@tempdimb}}
	\pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{\pgfutil@tempdima}}
	\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgfutil@tempdima}}
	% Grid dashes: calculations
	\pgf@xb=2\pgf@circ@res@right
	\pgf@circ@res@step=\ctikzvalof{tubes/tube radius}\pgf@xb
	\pgf@circ@count@a=\ctikzvalof{tubes/grid dashes}  % dashes*2+1
	\multiply\pgf@circ@count@a by 2\relax
	\advance\pgf@circ@count@a by 1\relax
	\advance\pgf@circ@res@step by -\pgf@xa
	\divide\pgf@circ@res@step by \pgf@circ@count@a
	% Grid dashes: draw
	\pgf@circ@res@temp=\pgf@xa
	\pgf@circ@count@a=\ctikzvalof{tubes/grid dashes}
	\loop
	\advance\pgf@circ@res@temp by\pgf@circ@res@step
	\pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgfutil@tempdima}}
	\pgfpathlineto{\pgfpoint{\pgf@circ@res@temp+\pgf@circ@res@step}{\pgfutil@tempdima}}
	\pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgfutil@tempdimb}}
	\pgfpathlineto{\pgfpoint{\pgf@circ@res@temp+\pgf@circ@res@step}{\pgfutil@tempdimb}}
	\advance\pgf@circ@res@temp by\pgf@circ@res@step
	\advance\pgf@circ@count@a by-1
	\ifnum\pgf@circ@count@a>0\relax
	\repeat
}

\pgfcircdeclaretube{pentode}
{
	\anchor{grid} {% should not be used
		\northwest
		\pgfutil@tempdima=\pgf@y
		\pgf@y=-\ctikzvalof{tubes/grid separation}\pgf@y
		\advance\pgf@y by \ctikzvalof{tubes/grid shift}\pgfutil@tempdima
	}
	\anchor{control} {%
		\northwest
		\pgfutil@tempdima=\pgf@y
		\pgf@y=-\ctikzvalof{tubes/grid separation}\pgf@y
		\advance\pgf@y by \ctikzvalof{tubes/grid shift}\pgfutil@tempdima
	}
	\anchor{screen} {%
		\northwest
		\pgf@y=\ctikzvalof{tubes/grid shift}\pgf@y
	}
	\anchor{suppressor} {%
		\northwest
		\pgfutil@tempdima=\pgf@y
		\pgf@y=\ctikzvalof{tubes/grid separation}\pgf@y
		\advance\pgf@y by \ctikzvalof{tubes/grid shift}\pgfutil@tempdima
	}
}
{
	% Grid x/y points
	\pgf@xa=-\ctikzvalof{tubes/tube radius}\pgf@circ@res@right
	\advance\pgf@xa by -\ctikzvalof{tubes/grid protrusion}\pgf@circ@res@right
	\pgfutil@tempdima=\ctikzvalof{tubes/grid separation}\pgf@circ@res@up
	\pgfutil@tempdimb=-\pgfutil@tempdima
	\advance\pgfutil@tempdima by \ctikzvalof{tubes/grid shift}\pgf@circ@res@up
	\advance\pgfutil@tempdimb by \ctikzvalof{tubes/grid shift}\pgf@circ@res@up
	\pgf@circ@res@other=\ctikzvalof{tubes/grid shift}\pgf@circ@res@up
	% Grid protrusion
	\pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{\pgfutil@tempdimb}}
	\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgfutil@tempdimb}}
	\pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{\pgfutil@tempdima}}
	\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgfutil@tempdima}}
	\pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{\pgf@circ@res@other}}
	\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@circ@res@other}}
	% Grid dashes: calculations
	\pgf@xb=2\pgf@circ@res@right
	\pgf@circ@res@step=\ctikzvalof{tubes/tube radius}\pgf@xb
	\pgf@circ@count@a=\ctikzvalof{tubes/grid dashes}  % dashes*2+1
	\multiply\pgf@circ@count@a by 2\relax
	\advance\pgf@circ@count@a by 1\relax
	\advance\pgf@circ@res@step by -\pgf@xa
	\divide\pgf@circ@res@step by \pgf@circ@count@a
	% Grid dashes: draw
	\pgf@circ@res@temp=\pgf@xa
	\pgf@circ@count@a=\ctikzvalof{tubes/grid dashes}
	\loop
	\advance\pgf@circ@res@temp by\pgf@circ@res@step
	\pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgfutil@tempdima}}
	\pgfpathlineto{\pgfpoint{\pgf@circ@res@temp+\pgf@circ@res@step}{\pgfutil@tempdima}}
	\pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgfutil@tempdimb}}
	\pgfpathlineto{\pgfpoint{\pgf@circ@res@temp+\pgf@circ@res@step}{\pgfutil@tempdimb}}
	\pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@other}}
	\pgfpathlineto{\pgfpoint{\pgf@circ@res@temp+\pgf@circ@res@step}{\pgf@circ@res@other}}
	\advance\pgf@circ@res@temp by\pgf@circ@res@step
	\advance\pgf@circ@count@a by-1
	\ifnum\pgf@circ@count@a>0\relax
	\repeat
}

\pgfcircdeclaretube{pentode suppressor to cathode}
{
	\anchor{grid} {% should not be used
		\northwest
		\pgfutil@tempdima=\pgf@y
		\pgf@y=-\ctikzvalof{tubes/grid separation}\pgf@y
		\advance\pgf@y by \ctikzvalof{tubes/grid shift}\pgfutil@tempdima
	}
	\anchor{control} {%
		\northwest
		\pgfutil@tempdima=\pgf@y
		\pgf@y=-\ctikzvalof{tubes/grid separation}\pgf@y
		\advance\pgf@y by \ctikzvalof{tubes/grid shift}\pgfutil@tempdima
	}
	\anchor{screen} {%
		\northwest
		\pgf@y=\ctikzvalof{tubes/grid shift}\pgf@y
	}
}
{
	% Grid x/y points
	\pgf@xa=-\ctikzvalof{tubes/tube radius}\pgf@circ@res@right
	\advance\pgf@xa by -\ctikzvalof{tubes/grid protrusion}\pgf@circ@res@right
	\pgfutil@tempdima=\ctikzvalof{tubes/grid separation}\pgf@circ@res@up
	\pgfutil@tempdimb=-\pgfutil@tempdima
	\advance\pgfutil@tempdima by \ctikzvalof{tubes/grid shift}\pgf@circ@res@up
	\advance\pgfutil@tempdimb by \ctikzvalof{tubes/grid shift}\pgf@circ@res@up
	\pgf@circ@res@other=\ctikzvalof{tubes/grid shift}\pgf@circ@res@up
	% Grid protrusion
	\pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{\pgfutil@tempdimb}}
	\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgfutil@tempdimb}}
	\pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{\pgf@circ@res@other}}
	\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@circ@res@other}}
	% Grid dashes: calculations
	\pgf@xb=2\pgf@circ@res@right
	\pgf@circ@res@step=\ctikzvalof{tubes/tube radius}\pgf@xb
	\pgf@circ@count@a=\ctikzvalof{tubes/grid dashes}  % dashes*2+1
	\multiply\pgf@circ@count@a by 2\relax
	\advance\pgf@circ@count@a by 1\relax
	\advance\pgf@circ@res@step by -\pgf@xa
	\divide\pgf@circ@res@step by \pgf@circ@count@a
	% Grid dashes: draw
	\pgf@circ@res@temp=\pgf@xa
	\pgf@circ@count@a=\ctikzvalof{tubes/grid dashes}
	\loop
	\advance\pgf@circ@res@temp by\pgf@circ@res@step
	\ifnum\pgf@circ@count@a>1\relax
	\pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgfutil@tempdimb}}
	\pgfpathlineto{\pgfpoint{\pgf@circ@res@temp+\pgf@circ@res@step}{\pgfutil@tempdimb}}
	\pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgf@circ@res@other}}
	\pgfpathlineto{\pgfpoint{\pgf@circ@res@temp+\pgf@circ@res@step}{\pgf@circ@res@other}}
	\fi
	\pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\pgfutil@tempdima}}
	\pgfpathlineto{\pgfpoint{\pgf@circ@res@temp+\pgf@circ@res@step}{\pgfutil@tempdima}}
	\advance\pgf@circ@res@temp by\pgf@circ@res@step
	\advance\pgf@circ@count@a by-1
	\ifnum\pgf@circ@count@a>0\relax
	\repeat
	% Grid: connection from suppressor to cathode
	\pgfsetcornersarced{\pgfpoint{\ctikzvalof{tubes/cathode corners}\pgf@circ@res@up}{\ctikzvalof{tubes/cathode corners}\pgf@circ@res@up}}
	\pgfpathlineto{\pgfpoint{\pgf@circ@res@temp}{\pgfutil@tempdima-2*\ctikzvalof{tubes/grid separation}\pgf@circ@res@up}}
	\pgfpathlineto{\pgfpoint{\ctikzvalof{tubes/cathode width}\pgf@circ@res@right-0.4142136*\ctikzvalof{tubes/cathode corners}\pgf@circ@res@right}{-\ctikzvalof{tubes/cathode distance}\pgf@circ@res@up-0.4142136*\ctikzvalof{tubes/cathode corners}\pgf@circ@res@up}}

}%
%
% multi-anode tube, added by Romano for version 1.6.8, suggested by @bogger33 on GitHub
% see https://github.com/circuitikz/circuitikz/issues/785
%
\ctikzset{tubes/anodes/.initial=3}  % number of anodes. Ignored in most tubes.
\newif\ifpgf@circuit@tubes@anodedot\pgf@circuit@tubes@anodedotfalse
\pgfkeys{/tikz/anodedot/.add code={}{\pgf@circuit@tubes@anodedottrue}}
\ctikzset{tubes/anodedot/.add code={}{\pgf@circuit@tubes@anodedottrue}}
\newif\ifpgf@circuit@tubes@nogrid\pgf@circuit@tubes@nogridfalse
\pgfkeys{/tikz/nogrid/.add code={}{\pgf@circuit@tubes@nogridtrue}}
\ctikzset{tubes/nogrid/.add code={}{\pgf@circuit@tubes@nogridtrue}}
\newif\ifpgf@circuit@tubes@nixieanode\pgf@circuit@tubes@nixieanodefalse
\pgfkeys{/tikz/nixieanode/.add code={}{\pgf@circuit@tubes@nixieanodetrue}}
\ctikzset{tubes/nixieanode/.add code={}{\pgf@circuit@tubes@nixieanodetrue}}
\long\def\pgfcirctubedraw@multi@anode{% Separate to override in matubes
    % Anodes (or plates)
    % Use saved len/anchors to find the first drawn position.
    \pgf@circ@res@temp=\anodelen\divide\pgf@circ@res@temp by 2 % half the anode length
    \firstanodepos\pgf@circ@res@other=\pgf@x
    \advance\pgf@circ@res@other by \pgf@circ@res@temp
    \pgf@circ@count@a=0
    \loop
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@up}} % north
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\ctikzvalof{tubes/anode distance}\pgf@circ@res@up}}
        \ifpgf@circuit@tubes@anodedot
            \pgfpathcircle{\pgfpoint{\pgf@circ@res@other}{\ctikzvalof{tubes/anode distance}\pgf@circ@res@up-0.5*\circlelen}}{\circlelen}
        \else
            % this is the horizontal line of the anode
            \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other-\pgf@circ@res@temp}{\ctikzvalof{tubes/anode distance}\pgf@circ@res@up}}
            \pgfpathlineto{\pgfpoint{\pgf@circ@res@other+\pgf@circ@res@temp}{\ctikzvalof{tubes/anode distance}\pgf@circ@res@up}}
        \fi
        \advance\pgf@circ@res@other by 4\pgf@circ@res@temp
        \advance\pgf@circ@count@a by 1
    \ifnum\pgf@circ@count@a<\Nanodes\relax
    \repeat
}
\long\def\pgfcirctubedraw@cathode@or@nixie{%
    \ifpgf@circuit@tubes@nixieanode
        \pgfpathmoveto{\pgfpoint{0pt}{-\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{-\ctikzvalof{tubes/cathode distance}\pgf@circ@res@up}}
        \pgfpathmoveto{\pgfpoint{-\ctikzvalof{tubes/cathode width}\pgf@circ@res@right}{-\ctikzvalof{tubes/cathode distance}\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\ctikzvalof{tubes/cathode width}\pgf@circ@res@right}{-\ctikzvalof{tubes/cathode distance}\pgf@circ@res@up}}
    \else
        \pgfcirctubedrawcathodestd
    \fi
}
\pgfcircdeclaretubegeneric{matube}
{% anchors
    \savedmacro{\Nanodes}{\edef\Nanodes{\ctikzvalof{tubes/anodes}}}
    \savedanchor\firstanodepos{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@circ@res@up=\ctikzvalof{tubes/height}\pgf@circ@scaled@Rlen
        \pgf@circ@res@right=\ctikzvalof{tubes/width}\pgf@circ@scaled@Rlen
        \pgf@circ@res@right=\ctikzvalof{tubes/anode width}\pgf@circ@res@right
        % x and y are one half
        \pgf@y=\pgf@circ@res@up
        \pgf@y=.5\pgf@y
        \pgf@x=-\pgf@circ@res@right
        \pgf@x=.5\pgf@x
    }
    \saveddimen\anodelen{%
        \pgfmathsetlength{\pgf@circ@scaled@Rlen}{\ctikzvalof{\ctikzclass/scale}\pgf@circ@Rlen}
        \pgf@circ@res@right=\ctikzvalof{tubes/width}\pgf@circ@scaled@Rlen % full width, left to right
        % the length of one of the N anodes is the full width divided by 2*N+1
        \pgfmathsetlength\pgf@x{\ctikzvalof{tubes/anode width}*\pgf@circ@res@right/(2*\Nanodes-1)}
    }
    \saveddimen\circlelen{%
        \pgf@x=\ctikzvalof{nodes width}\pgf@circ@Rlen
    }
    \anchor{grid} {% should not be used
        \northwest
        \pgf@y=\ctikzvalof{tubes/grid shift}\pgf@y
    }
    \anchor{control} {%
        \northwest
        \pgf@y=\ctikzvalof{tubes/grid shift}\pgf@y
    }
    \anchor{nixie a}{%
        \northwest\pgf@x=0pt\pgf@y=-\pgf@y
    }
}
{% grid
    \ifpgf@circuit@tubes@nogrid\else
    % Grid protrusion
    \pgf@xa=-\ctikzvalof{tubes/tube radius}\pgf@circ@res@right
    \advance\pgf@xa by -\ctikzvalof{tubes/grid protrusion}\pgf@circ@res@right
    \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@right}{\ctikzvalof{tubes/grid shift}\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\pgf@xa}{\ctikzvalof{tubes/grid shift}\pgf@circ@res@up}}
    % Grid dashes: calculations
    \pgf@xb=2\pgf@circ@res@right
    \pgf@circ@res@step=\ctikzvalof{tubes/tube radius}\pgf@xb
    \pgf@circ@count@a=\ctikzvalof{tubes/grid dashes}  % dashes*2+1
    \multiply\pgf@circ@count@a by 2\relax
    \advance\pgf@circ@count@a by 1\relax
    \advance\pgf@circ@res@step by -\pgf@xa
    \divide\pgf@circ@res@step by \pgf@circ@count@a
    % Grid dashes: draw
    \pgf@circ@res@temp=\pgf@xa
    \pgf@circ@count@a=\ctikzvalof{tubes/grid dashes}
    \loop
    \advance\pgf@circ@res@temp by\pgf@circ@res@step
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@temp}{\ctikzvalof{tubes/grid shift}\pgf@circ@res@up}}
    \advance\pgf@circ@res@temp by\pgf@circ@res@step
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@temp}{\ctikzvalof{tubes/grid shift}\pgf@circ@res@up}}
    \advance\pgf@circ@count@a by-1
    \ifnum\pgf@circ@count@a>0\relax
    \repeat
    \fi
}
{\pgfcirctubedraw@multi@anode}% anodes
{\pgfcirctubedraw@cathode@or@nixie}% cathode
{% extra dynamics anchors
    \pgfutil@g@addto@macro\pgf@sh@s@matube{%
        % Start with the maximum pin number and go backwards.
        \pgf@circ@count@a=\Nanodes\relax
        \pgfmathloop%
        \ifnum\pgf@circ@count@a>0
        \expandafter\xdef\csname pgf@anchor@matube@anode\space\the\pgf@circ@count@a\endcsname{%
            \noexpand\pgf@circ@ma@anode@anchor{\the\pgf@circ@count@a}%
        }
        \expandafter\xdef\csname pgf@anchor@matube@nixie\space k\the\pgf@circ@count@a\endcsname{%
            \noexpand\pgf@circ@ma@anode@anchor{\the\pgf@circ@count@a}%
        }
        \advance\pgf@circ@count@a by -1\relax%
        \repeatpgfmathloop%
    }%
}
\def\pgf@circ@ma@anode@anchor#1{
    \firstanodepos
    \pgf@circ@res@temp=\pgf@x
    \pgfmathsetlength\pgf@x{\pgf@circ@res@temp + (0.5 + 2*(#1 -1))*\anodelen}
}



%>>>

\endinput
% vim: set fdm=marker fmr=%<<<,%>>>:
