2

I want to modify the oscope element in circuitikz. It shows a triangle wave by default, but I want it to show a sine wave. How can I do that?

enter image description here

2
  • 1
    Hi, if you think that the solution is ok, please consider accepting it. I also cited you in the new option in the package, I hope you are ok with it.
    – Rmano
    May 1 at 16:22
  • It's for me an honor, @Rmano! And yes, the solution worked. May 2 at 23:55
3

Since v1.3.5 you have the possibility to use a set of standard "preloaded" waveforms for the oscilloscope:

enter image description here

accessible with the syntax \ctikzset{bipoles/oscope/waveform=name.

If you want more, you can define new shapes; see the manual for details (section 4.6.1.1, "Oscilloscope waveforms") but basically:

\ctikzset{%
bipoles/oscope/waveform/mywave/.code={%
    \pgfsetcolor{red}
    \pgfpathmoveto{\pgfpoint{-.75cm}{-.5cm}}
    \pgfpathlineto{\pgfpoint{.75cm}{.5cm}}
    \pgfusepath{draw}
    \pgfsetcolor{green}
    \pgfpathmoveto{\pgfpoint{-.75cm}{.5cm}}
    \pgfpathlineto{\pgfpoint{.75cm}{-.5cm}}
    \pgfusepath{draw}
}}
\begin{circuitikz}
    \ctikzset{bipoles/oscope/waveform=mywave}
    \draw (0,0) node[oscopeshape]{};
\end{circuitikz}

which leads to:

enter image description here

BEFORE v1.3.5, the best option was to follow the instructions about adding a new bipole in the manual - copy the old one, change the name and redefine the graph element. It will not be easy unless you have quite a bit of practice with low-level pgf commands...

\documentclass[border=10pt]{standalone}
\usepackage{circuitikz}

\makeatletter
\pgfcircdeclarebipolescaled{instruments}
{
    \anchor{in 1}{\southwest\pgf@y=0.75\pgf@y\pgf@x=0.4\pgf@x}
    \anchor{in 2}{\southwest\pgf@y=0.75\pgf@y\pgf@x=-0.4\pgf@x}
    % put the node text above and centered
    \anchor{text}{\pgfextracty{\pgf@circ@res@up}{\northeast}
        \pgfpoint{-.5\wd\pgfnodeparttextbox}{
            \dimexpr.5\dp\pgfnodeparttextbox+.5\ht\pgfnodeparttextbox+\pgf@circ@res@up\relax
        }
    }
}
{\ctikzvalof{bipoles/oscope/height}}
{oscopesin}
{\ctikzvalof{bipoles/oscope/height}}
{\ctikzvalof{bipoles/oscope/width}}
{
    \pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
    \pgfextracty{\pgf@circ@res@up}{\northeast}
    \pgfextractx{\pgf@circ@res@right}{\northeast}
    \pgfextractx{\pgf@circ@res@left}{\southwest}
    \pgfextracty{\pgf@circ@res@down}{\southwest}
    \pgfmathsetlength{\pgf@circ@res@step}{0.25*\pgf@circ@res@up}
    \pgfscope
        \pgfsetcornersarced{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@step}}
        \pgfpathrectanglecorners{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
        % this would create a round (analog?) scope...
        % \pgfpathellipse{\pgfpointorigin}{\pgfpoint{0}{\pgf@circ@res@up}}{\pgfpoint{\pgf@circ@res@left}{0}}
        \pgf@circ@draworfill
    \endpgfscope
    % get the rotation
    \ifpgf@circuit@straightinstruments
        \pgfgettransformentries\a\b\temp\temp\temp\temp
        \pgfmathsetmacro{\rot}{-atan2(\b,\a)}
    \else
        \edef\rot{0}
    \fi
    % and unrotate the scope
    \pgfscope
        \pgftransformrotate{\rot}
        % grid
        \pgfscope
            \pgfsetlinewidth{0.5\pgfstartlinewidth}
            \pgfpathmoveto{\pgfpoint{0.75\pgf@circ@res@left}{0.25\pgf@circ@res@down}}
            \pgfpathgrid[stepx=\pgf@circ@res@step, stepy=\pgf@circ@res@step]%
            {\pgfpoint{0.75\pgf@circ@res@left}{0.5\pgf@circ@res@down}}
            {\pgfpoint{0.75\pgf@circ@res@right}{0.5\pgf@circ@res@up}}
            \pgfsetstrokeopacity{0.5}
            \pgfusepath{draw}
        \endpgfscope
        % function displayed
        \pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
        \pgfpathmoveto{\pgfpoint{0.6\pgf@circ@res@left}{0\pgf@circ@res@down}}
        \pgfpathsine{\pgfpoint{-0.3\pgf@circ@res@left}{0.4\pgf@circ@res@up}}
        \pgfpathcosine{\pgfpoint{-0.3\pgf@circ@res@left}{-0.4\pgf@circ@res@up}}
        \pgfpathsine{\pgfpoint{-0.3\pgf@circ@res@left}{-0.4\pgf@circ@res@up}}
        \pgfpathcosine{\pgfpoint{-0.3\pgf@circ@res@left}{0.4\pgf@circ@res@up}}
        \pgfusepath{draw}
    \endpgfscope
}
\pgfcirc@activate@bipole@simple{l}{oscopesin}
\makeatother



\begin{document}
\begin{tikzpicture}[]
    \draw (0,0) node[oscopesinshape]{};
    \draw (1,0) to[oscopesin] ++(0,-2);
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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