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

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}
\author{Felix Harvey-Rosser} 

\begin{document}

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

            \draw [teal]        (0,0)   --(0.22,0)  to  [open]  (0.78,0)--      (1,0)   ;       % connecting wires          
            \draw [thick, teal] (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}  
share|improve this question
    
You can’t convert from TikZ code to a PGF shape that is used like you want. But we can surely use the original code from circuitikz ’ diode shapes to built our own shape (and to path). –  Qrrbrbirlbel Feb 16 '13 at 13:40
    
How can I do that / how can I learn to do that ? @Qrrbrbirlbel –  Elements in Space Feb 16 '13 at 14:20
    
@Massimo Redaelli –  Elements in Space Feb 16 '13 at 14:28

1 Answer 1

up vote 4 down vote accepted

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

share|improve this answer
    
I can get your code to work fine in the standalone document. However I am having serious difficulty editing the circuitikz1.code.tex file. –  Elements in Space Feb 18 '13 at 5:48
    
@UnkleRhaukus Do not edit the original file. Instead use your own .tex (load with \input) or .sty file (loading with \usepackage, do not include \makeatletter and especially not \makeatother). What is “serious difficulty”? I suspect that you have also included \makeatletter and \makeatother (which is wrong). See for further reference: What do \makeatletter and \makeatother do? –  Qrrbrbirlbel Feb 18 '13 at 5:54
    
@UnkleRhaukus There is one major reason why not to edit an existing file: In the case of an update of the circuitikz package your own code will be overwritten and be lost. Minor reasons: license, maintainability, … –  Qrrbrbirlbel Feb 18 '13 at 6:50

Your Answer

 
discard

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

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