我正在画一个电路电路图有许多 SPDT 开关。我想展示两个不同时刻的电路:第一次,所有 SPDT 开关都连接到 out1;第二次,所有 SPDT 开关都连接到 out2。有什么简单的方法可以做到这一点吗?
下面是我正在尝试编写的代码的一个工作示例:
\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{circuitikz}
\begin{document}
\begin{circuitikz}
% Switches
\draw (0,2) node[spdt,xscale=1,anchor=in] (Swp) {};
\draw (0,0) node[spdt,xscale=-1,anchor=in] (Swc) {};
\draw (0,-2) node[spdt,xscale=1,anchor=in] (Swn) {};
% Capacitors
\draw (Swp.in) to[C,l_=$(C_s + \Delta C)$] (Swc.in);
\draw (Swn.in) to[C,l^=$(C_s - \Delta C)$] (Swc.in);
% Voltages
\draw (Swp.out 1) node[anchor=west] {$V_{in}^+$};
\draw (Swn.out 1) node[anchor=west] {$V_{in}^-$};
\draw (Swp.out 2) node[anchor=west] {$V_{cm}$};
\draw (Swn.out 2) node[anchor=west] {$V_{cm}$};
\draw (Swc.out 1) node[anchor=east] {$V_{cm}$};
\draw (Swc.out 2) node[anchor=east] {$V_{ref}$};
\end{circuitikz}
\end{document}
答案1
将适当的值转换为参数相关值并使用条件测试:
\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{circuitikz}
\def\mycircuit{%
% Switches
\draw (0,2) node[spdt,xscale=1,yscale=\value,anchor=in] (Swp) {};
\draw (0,0) node[spdt,xscale=-1,yscale=\value,anchor=in] (Swc) {};
\draw (0,-2) node[spdt,xscale=1,yscale=\value,anchor=in] (Swn) {};
% Capacitors
\draw (Swp.in) to[C,l_=$(C_s + \Delta C)$] (Swc.in);
\draw (Swn.in) to[C,l^=$(C_s - \Delta C)$] (Swc.in);
% Voltages
\draw (Swp.out \myone) node[anchor=west] {$V_{in}^+$};
\draw (Swn.out \myone) node[anchor=west] {$V_{in}^-$};
\draw (Swp.out \mytwo) node[anchor=west] {$V_{cm}$};
\draw (Swn.out \mytwo) node[anchor=west] {$V_{cm}$};
\draw (Swc.out \myone) node[anchor=east] {$V_{cm}$};
\draw (Swc.out \mytwo) node[anchor=east] {$V_{ref}$};
}
\newcommand\swapot[1]{%
\ifnum#1=1\relax
\def\value{1}
\def\myone{2}\def\mytwo{1}
\else
\ifnum#1=-1\relax
\def\value{-1}
\def\myone{1}\def\mytwo{2}
\fi\fi%
}
\begin{document}
\begin{circuitikz}
\swapot{1}
\mycircuit
\begin{scope}[xshift=5cm]
\swapot{-1}
\mycircuit
\end{scope}
\end{circuitikz}
\end{document}