[Tex/LaTex] How to add a component to circuitikz

circuitikztikz-pgf

Similar to CircuiTikZ — create new component


I am using circuitikz to create an electric circuit diagram.
I have found the preset symbol for the zener diode to be different to the symbol I wish to use to represent the zener diode.


I have constructed the following code in tikz to display the symbol i wish to use.

My question is, How can I store this as a component, to recalled with [nzD] like the preset symbols, so that it works in a circuit.


\documentclass{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{5mm}
\usepackage{tikz}
\usepackage{circuitikz}

\title{New Zener Diode}

\begin{document}

    \begin{preview}
    \centering
        \begin{circuitikz}

            \draw        (0,0)  --(0.22,0)  to  [open]  (0.78,0)--      (1,0)   ;       % connecting wires          
            \draw [thick](0.22,0)--(0.22,0.35)--(0.78,0)--(0.22,-0.35)--(0.22,0)            % triangle      
                         (0.84,0.35)--(0.78,0.35)--(0.78,-0.35)--(0.72,-0.35)   ;       % vertical

            %\draw       (0,0)              to [zD]                     (1,0)   ;       % preset zener diode

        \end{circuitikz}        
    \end{preview}
\end{document}  

Best Answer

The file circuitikz1.code.tex holds all style definition for the Zener diode zD and its variant zD* and zDo as well as the long forms Zener diode, empty Zener diode and full Zener diode.
(The zD and Zener diode styles switch dynamically to empty or full diodes depending on the styles/options empty diodes/emptydiodes or full diodes/fulldiodes respectively.)

These styles are copied to make a new Zener diode family. As we want to use this shape on paths we also need to define a set of paths. These are simply copied from the old Zener diode family and are adjusted to match the new shapes.

The actual shape re-definition happens with the \pgfcircledeclarebipole macro. Again, these definitions are copied from the pgfcircbipoles.sty file: The names are changed (I added an n).

The first path is the path of the triangle, the second path is the path of the line thing in front of it.

\pgfpathmoveto{\pgfpoint{\pgf@circ@res@right-.8\pgf@circ@res@left}{\pgf@circ@res@down}}

Notice the additional .8 in the \pgfpathmoveto line? That’s it!

Code

\documentclass[tikz]{standalone}
\usepackage{circuitikz}
\makeatletter
\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/diode/height}}{emptynzdiode}{\ctikzvalof{bipoles/diode/height}}{\ctikzvalof{bipoles/diode/width}}{
    \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
    \pgfscope
        \pgftransformxshift{\pgf@circ@res@left}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right-\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right-\pgf@circ@res@left}{0pt}}
        \pgfusepath{draw}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right-.8\pgf@circ@res@left}{\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right-\pgf@circ@res@left}{\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right-\pgf@circ@res@left}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right-1.2\pgf@circ@res@left}{\pgf@circ@res@up}}
        \pgfusepath{draw}
    \endpgfscope
}
\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/diode/height}}{fullnzdiode}{\ctikzvalof{bipoles/diode/height}}{\ctikzvalof{bipoles/diode/width}}{
    \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
    \pgfscope
        \pgftransformxshift{\pgf@circ@res@left}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right-\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{0pt}{\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right-\pgf@circ@res@left}{0pt}}
        \pgfusepath{draw,fill}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right-.8\pgf@circ@res@left}{\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right-\pgf@circ@res@left}{\pgf@circ@res@down}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right-\pgf@circ@res@left}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@right-1.2\pgf@circ@res@left}{\pgf@circ@res@up}}
        \pgfusepath{draw}
    \endpgfscope
}
\tikzset{
    nzD/.style={new Zener diode},
    nzD*/.style={full new Zener diode},
    nzDo/.style={empty new Zener diode},
    new Zener diode/.style={\ifpgf@circuit@fulldiode full \else empty \fi new Zener diode},
    full new Zener diode/.style={\circuitikzbasekey, /tikz/to path=\pgf@circ@fullnzdiode@path},
    empty new Zener diode/.style={\circuitikzbasekey, /tikz/to path=\pgf@circ@emptynzdiode@path},
}
\def\pgf@circ@fullnzdiode@path#1{\pgf@circ@bipole@path{fullnzdiode}{#1}}
\def\pgf@circ@emptynzdiode@path#1{\pgf@circ@bipole@path{emptynzdiode}{#1}}
\makeatother
\begin{document}
\begin{circuitikz}
\foreach \diode[count=\x from 0] in {zD,
                    zD*,
                    zDo,
                    nzD,
                    nzD*,
                    nzDo} {
    \draw[yshift=-\x cm] (0,0) to[\diode] (1,0) node[right] {\diode};
}
\end{circuitikz}
\end{document}

Output

enter image description here