Circuitikz - 定义可变电容器的箭头位置

Circuitikz - 定义可变电容器的箭头位置

我正在尝试定义可变电容器元件的箭头位置,但坦率地说,我不知道如何去做。

据我所知,箭头总是从左下角的锚点指向右上角的锚点 - 取决于绘图方向。我想在组件 y 轴处翻转电容器组件,以便箭头从右下角的锚点开始指向左上角的锚点。yscale 没有帮助我,因为它翻转了整个电路图 y 轴处的组件。编辑:不改变箭头的方向。

我的问题有解决办法吗?

谢谢!

编辑:

\documentclass{article}
\usepackage{tikz}
\usepackage{circuitikz}
\begin{document}
\begin{figure}[h!]
  \begin{center} % Center figure
    \begin{circuitikz} % Begin add-on environment
      \draw (0,0)
      to[V,v=$U(t)$] (0,2);
      \draw (2,2)
      to[vC=$C_1$, o-] (0,2);
      \draw (2,2)
      to[open] (2,0)
      to[short, o-] (0,0);
    \end{circuitikz}
  \end{center}
\end{figure}
\end{document}

在此处输入图片描述

答案1

如果你总是想要从左到右带有箭头的形状,你可以简单地使用以下方法重新定义与可变电容器相关的形状

\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/vcapacitor/height}}{vcapacitor}{\ctikzvalof{bipoles/vcapacitor/height}}{\ctikzvalof{bipoles/vcapacitor/width}}{
        %\pgf@circ@res@step = \ctikzvalof{bipoles/vcapacitor/width}\pgf@circ@Rlen
        \pgf@circ@res@step = \ctikzvalof{bipoles/vcapacitor/capacitor width} \pgf@circ@res@right

        \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
        \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@step}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{-\pgf@circ@res@step}{\pgf@circ@res@down}}

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@down}}
        \pgfusepath{draw}

        \pgfscope
        \pgfsetarrowsend{latex'}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}
        \pgfusepath{draw}
        \endpgfscope

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{-\pgf@circ@res@step}{0pt}}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{0pt}}
        \pgfsetlinewidth{\pgfstartlinewidth}
        \pgfusepath{draw}
}

这只是来自circuitikzv0.8.1 包含以下行

\pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}

用来代替

\pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@down}}

说从左下角到右上角绘制,而不是从右下角到左上角绘制。

或者你可以定义一个新键Cv为(我只是按照这里似乎调用的命令链vC,并复制它们以制作一个Cv变体,最终调用与上面相同的定义,并使用相同的左/右交换)同时使用CvvC

\compattikzset{Cv/.style = {\comnpatname capacitor variable = #1}}
\compattikzset{capacitor variable/.style = {\circuitikzbasekey, /tikz/to path=\pgf@circ@capacitorv@path, l=#1}}
\def\pgf@circ@capacitorv@path#1{\pgf@circ@bipole@path{capacitorv}{#1}}
\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/vcapacitor/height}}{capacitorv}{\ctikzvalof{bipoles/vcapacitor/height}}{\ctikzvalof{bipoles/vcapacitor/width}}{
        %\pgf@circ@res@step = \ctikzvalof{bipoles/vcapacitor/width}\pgf@circ@Rlen
        \pgf@circ@res@step = \ctikzvalof{bipoles/vcapacitor/capacitor width} \pgf@circ@res@right

        \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
        \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@step}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{-\pgf@circ@res@step}{\pgf@circ@res@down}}

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@down}}
        \pgfusepath{draw}

        \pgfscope
        \pgfsetarrowsend{latex'}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}
        \pgfusepath{draw}
        \endpgfscope

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{-\pgf@circ@res@step}{0pt}}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{0pt}}
        \pgfsetlinewidth{\pgfstartlinewidth}
        \pgfusepath{draw}
}

将其汇总到一个示例中

\documentclass{article}
\usepackage{tikz}
\usepackage{circuitikz}

\makeatletter%Create new circuitikz key Cv for variable capacitor with arrow in other direction
\compattikzset{Cv/.style = {\comnpatname capacitor variable = #1}}
\compattikzset{capacitor variable/.style = {\circuitikzbasekey, /tikz/to path=\pgf@circ@capacitorv@path, l=#1}}
\def\pgf@circ@capacitorv@path#1{\pgf@circ@bipole@path{capacitorv}{#1}}
\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/vcapacitor/height}}{capacitorv}{\ctikzvalof{bipoles/vcapacitor/height}}{\ctikzvalof{bipoles/vcapacitor/width}}{
        %\pgf@circ@res@step = \ctikzvalof{bipoles/vcapacitor/width}\pgf@circ@Rlen
        \pgf@circ@res@step = \ctikzvalof{bipoles/vcapacitor/capacitor width} \pgf@circ@res@right

        \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
        \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@step}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{-\pgf@circ@res@step}{\pgf@circ@res@down}}

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@down}}
        \pgfusepath{draw}

        \pgfscope
        \pgfsetarrowsend{latex'}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}
        \pgfusepath{draw}
        \endpgfscope

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{-\pgf@circ@res@step}{0pt}}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{0pt}}
        \pgfsetlinewidth{\pgfstartlinewidth}
        \pgfusepath{draw}
}
\makeatother

\begin{document}
\begin{center}
\texttt{vC} with default definition

\begin{circuitikz}
    \draw (2,0) to[vC=$C_1$, o-] (0,0);
\end{circuitikz}

New \texttt{Cv} key

\begin{circuitikz}
    \draw (2,0) to[Cv=$C_1$, o-] (0,0);
\end{circuitikz}

\makeatletter%Redefine arrow direction used by vC
\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/vcapacitor/height}}{vcapacitor}{\ctikzvalof{bipoles/vcapacitor/height}}{\ctikzvalof{bipoles/vcapacitor/width}}{
        %\pgf@circ@res@step = \ctikzvalof{bipoles/vcapacitor/width}\pgf@circ@Rlen
        \pgf@circ@res@step = \ctikzvalof{bipoles/vcapacitor/capacitor width} \pgf@circ@res@right

        \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}
        \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@step}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{-\pgf@circ@res@step}{\pgf@circ@res@down}}

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{\pgf@circ@res@down}}
        \pgfusepath{draw}

        \pgfscope
        \pgfsetarrowsend{latex'}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@up}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@down}}
        \pgfusepath{draw}
        \endpgfscope

        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{0pt}}
        \pgfpathlineto{\pgfpoint{-\pgf@circ@res@step}{0pt}}
        \pgfpathmoveto{\pgfpoint{\pgf@circ@res@right}{0pt}}
        \pgfpathlineto{\pgfpoint{\pgf@circ@res@step}{0pt}}
        \pgfsetlinewidth{\pgfstartlinewidth}
        \pgfusepath{draw}
}
\makeatother

\texttt{vC} with arrow direction redefined

\begin{circuitikz}
    \draw (2,0) to[vC=$C_1$, o-] (0,0);
\end{circuitikz}
\end{center}
\end{document}

在此处输入图片描述

答案2

为什么不阅读手册并利用镜像/反转选项?

to[vC=$C_1$,invert, o-]

或者

to[vC=$C_1$,mirror, o-]

或者两者结合就可以了。

例子:

 \begin{circuitikz} % Begin add-on environment
  \draw (0,0) to[vC=$C_1$] ++(2,0)
    to[vC=$C_1$,invert] ++(2,0)
    to[vC=$C_1$,mirror] ++(2,0)
    to[vC=$C_1$,mirror,invert] ++(2,0);
 \end{circuitikz}

在此处输入图片描述

此致,

斯蒂芬

相关内容