如何在 CircuiTikZ 中绘制方形的从属电压和电流源?

如何在 CircuiTikZ 中绘制方形的从属电压和电流源?

我想创建如图所示的方形相关电压和电流源。我希望仍然能够将它们用作“to”命令的选项,如下所示:

(0,0) to[SQvsource, l=$stuff$] ++(2,0)

在此处输入图片描述

答案1

让我们试着看看如何做到这一点。例如,让我们从电压发生器开始。手册中更相似的对象显然是这样的:

在此处输入图片描述

nodename带的shape是双极子的定义。双极子在 中有定义pgfcircbipoles.tex,因此我们来搜索一下:

% kpsewhere pgfcircbipoles.tex
/home/romano/texlive2021/texmf-dist/tex/generic/circuitikz/pgfcircbipoles.tex

我会打开它并搜索与我们相关的所有东西cvsourceAM,然后将它们放入\makeatletter...other警卫的测试文件中。

\documentclass[border=10pt]{standalone}
\usepackage[siunitx, RPvoltages]{circuitikz}
\makeatletter
\ctikzset{bipoles/cvsourceam/height/.initial=.7}
\ctikzset{bipoles/cvsourceam/width/.initial=.7}
\ctikzset{bipoles/cvsourceam/margin/.initial=.7}
\ctikzset{bipoles/cvsourceam/text scale/.initial=1}
%% To change the internal symbols of the controlled voltage source american style
\ctikzset{bipoles/cvsourceam/inner plus/.initial={$+$}}
\ctikzset{bipoles/cvsourceam/inner minus/.initial={$-$}}
%% Controlled voltage source - American
\pgfcircdeclarebipolescaled{csources}
{}
{\ctikzvalof{bipoles/cvsourceam/height}}
{cvsourceAM}
{\ctikzvalof{bipoles/cvsourceam/height}}
{\ctikzvalof{bipoles/cvsourceam/width}}
{
    \pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@zero}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@zero}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@zero}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@zero}{\pgf@circ@res@down}}
    \pgfpathclose
    \pgf@circ@draworfill

    \pgfsetcolor{\ctikzvalof{color}}
    \ifpgf@circ@oldvoltagedirection
        \pgftext[bottom,rotate=90,y=\ctikzvalof{bipoles/cvsourceam/margin}\pgf@circ@res@left]{\ctikzvalof{bipoles/cvsourceam/inner plus}}
        \pgftext[top,rotate=90,y=\ctikzvalof{bipoles/cvsourceam/margin}\pgf@circ@res@right]{\ctikzvalof{bipoles/cvsourceam/inner minus}}
    \else
        \pgftext[bottom,rotate=90,y=\ctikzvalof{bipoles/cvsourceam/margin}\pgf@circ@res@left]{\ctikzvalof{bipoles/cvsourceam/inner minus}}
        \pgftext[top,rotate=90,y=\ctikzvalof{bipoles/cvsourceam/margin}\pgf@circ@res@right]{\ctikzvalof{bipoles/cvsourceam/inner plus}}
    \fi
}
\pgfcirc@activate@bipole@opt{v}{cvsourceam}{cvsourceAM}{american controlled voltage source}{%
    \circuitikzbasekey/bipole/is voltage=true,
    \circuitikzbasekey/bipole/is voltageoutsideofsymbol=false}
\makeatother

\begin{document}
\begin{tikzpicture}[american]
    \draw (0,0) to[american controlled voltage source] ++(0,-2);
\end{tikzpicture}
\end{document}

...显然什么都没有改变,但请检查它是否仍然可以编译。

在此处输入图片描述

源代码比需要的更完整一些(并且那里有一个大写错误,由于担心向后兼容性,我没有修复它),但基本上相当简单。

现在我将名称更改为我们的新组件。只需全局替换它即可……

\documentclass[border=10pt]{standalone}
\usepackage[siunitx, RPvoltages]{circuitikz}
\makeatletter
\ctikzset{bipoles/cvsquaream/height/.initial=.7}
\ctikzset{bipoles/cvsquaream/width/.initial=.7}
\ctikzset{bipoles/cvsquaream/margin/.initial=.7}
\ctikzset{bipoles/cvsquaream/text scale/.initial=1}
%% To change the internal symbols of the controlled voltage source american style
\ctikzset{bipoles/cvsquaream/inner plus/.initial={$+$}}
\ctikzset{bipoles/cvsquaream/inner minus/.initial={$-$}}
%% Controlled voltage source - American
\pgfcircdeclarebipolescaled{csources}
{}
{\ctikzvalof{bipoles/cvsquaream/height}}
{cvsquaream}
{\ctikzvalof{bipoles/cvsquaream/height}}
{\ctikzvalof{bipoles/cvsquaream/width}}
{
    \pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@zero}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@zero}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@zero}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@zero}{\pgf@circ@res@down}}
    \pgfpathclose
    \pgf@circ@draworfill

    \pgfsetcolor{\ctikzvalof{color}}
    \ifpgf@circ@oldvoltagedirection
        \pgftext[bottom,rotate=90,y=\ctikzvalof{bipoles/cvsquaream/margin}\pgf@circ@res@left]{\ctikzvalof{bipoles/cvsquaream/inner plus}}
        \pgftext[top,rotate=90,y=\ctikzvalof{bipoles/cvsquaream/margin}\pgf@circ@res@right]{\ctikzvalof{bipoles/cvsquaream/inner minus}}
    \else
        \pgftext[bottom,rotate=90,y=\ctikzvalof{bipoles/cvsquaream/margin}\pgf@circ@res@left]{\ctikzvalof{bipoles/cvsquaream/inner minus}}
        \pgftext[top,rotate=90,y=\ctikzvalof{bipoles/cvsquaream/margin}\pgf@circ@res@right]{\ctikzvalof{bipoles/cvsquaream/inner plus}}
    \fi
}
\pgfcirc@activate@bipole@opt{v}{cvsquaream}{cvsquaream}{SQvsource}{%
    \circuitikzbasekey/bipole/is voltage=true,
    \circuitikzbasekey/bipole/is voltageoutsideofsymbol=false}
\makeatother

\begin{document}
\begin{tikzpicture}[american]
    \draw (0,0) to[SQvsource] ++(0,-2);
\end{tikzpicture}
\end{document}

注意最后一行 --- 我按照你的建议将其定义为新组件的“别名” SQvsource。现在我们必须更改定义以绘制我们想要的内容……外部菱形由

 \pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@zero}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@zero}{\pgf@circ@res@up}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@zero}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@zero}{\pgf@circ@res@down}}
    \pgfpathclose
    \pgf@circ@draworfill

因此我们将其改为,例如:

\pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
    \pgfpathrectanglecorners{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}
        {\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
    \pgfpathclose
    \pgf@circ@draworfill

在此处输入图片描述

...一切都会按预期进行。最终的电路可能是这样的:

\documentclass[border=10pt]{standalone}
\usepackage[siunitx, RPvoltages]{circuitikz}
\makeatletter
\ctikzset{bipoles/cvsquaream/height/.initial=.7}
\ctikzset{bipoles/cvsquaream/width/.initial=.7}
\ctikzset{bipoles/cvsquaream/margin/.initial=.7}
\ctikzset{bipoles/cvsquaream/text scale/.initial=1}
%% To change the internal symbols of the controlled voltage source american style
\ctikzset{bipoles/cvsquaream/inner plus/.initial={$+$}}
\ctikzset{bipoles/cvsquaream/inner minus/.initial={$-$}}
%% Controlled voltage source - American
\pgfcircdeclarebipolescaled{csources}
{}
{\ctikzvalof{bipoles/cvsquaream/height}}
{cvsquaream}
{\ctikzvalof{bipoles/cvsquaream/height}}
{\ctikzvalof{bipoles/cvsquaream/width}}
{
    \pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
    \pgfpathrectanglecorners{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}
        {\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
    \pgfpathclose
    \pgf@circ@draworfill

    \pgfsetcolor{\ctikzvalof{color}}
    \ifpgf@circ@oldvoltagedirection
        \pgftext[bottom,rotate=90,y=\ctikzvalof{bipoles/cvsquaream/margin}\pgf@circ@res@left]{\ctikzvalof{bipoles/cvsquaream/inner plus}}
        \pgftext[top,rotate=90,y=\ctikzvalof{bipoles/cvsquaream/margin}\pgf@circ@res@right]{\ctikzvalof{bipoles/cvsquaream/inner minus}}
    \else
        \pgftext[bottom,rotate=90,y=\ctikzvalof{bipoles/cvsquaream/margin}\pgf@circ@res@left]{\ctikzvalof{bipoles/cvsquaream/inner minus}}
        \pgftext[top,rotate=90,y=\ctikzvalof{bipoles/cvsquaream/margin}\pgf@circ@res@right]{\ctikzvalof{bipoles/cvsquaream/inner plus}}
    \fi
}
\pgfcirc@activate@bipole@opt{v}{cvsquaream}{cvsquaream}{SQvsource}{%
    \circuitikzbasekey/bipole/is voltage=true,
    \circuitikzbasekey/bipole/is voltageoutsideofsymbol=false}
\makeatother

\begin{document}
\begin{tikzpicture}[american]
    \draw (0,0) -- ++(-2,0) to[SQvsource=$\nu_{sl}(t)$] ++(0,2) 
        to[short, i_=$i_{sl}(t)$] ++(2,0);
\end{tikzpicture}
\end{document}

在此处输入图片描述

当前的生成器留给熟练的读者作为练习 ;-)(它表明我是一名老师,是吗?:-P

相关内容