我定义了一个名为 的生物极设备galvanometer
,它是经过旋转校正的。这意味着它显示一个带圆圈的“G”字母,无论设备以何种方向绘制,该字母始终可读。
我希望此代码生成myammeter
(将打印“A”)或myvoltmeter
(将打印“V”)。但是,我想重复使用相同的代码作为模板并发出类似\newcircleddevice{myvoltmeter}{V}
或的指令\newcircleddevice{myammmeter}{A}
,而不是简单地复制粘贴代码并将“G”更改为“V”或“A”(我目前正在使用这种方法)。
仅使用简单定义\newcommand
不起作用。如何实现模板化定义?
这是我的 MWE。这是完美可编译的代码。
\documentclass[tikz]{standalone}
\usepackage[siunitx]{circuitikz}
% rotation corrected galvanometer
\ctikzset{bipoles/galvanometer/height/.initial=.60}
\ctikzset{bipoles/galvanometer/width/.initial=.60}
\makeatletter
\def\pgf@circ@galvanometer@path#1{\pgf@circ@bipole@path{galvanometer}{#1}}
\tikzset{galvanometer/.style = {\circuitikzbasekey, /tikz/to
path=\pgf@circ@galvanometer@path}}
\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/voltmeter/height}}{galvanometer}
{\ctikzvalof{bipoles/galvanometer/height}}{\ctikzvalof{bipoles/galvanometer/width}}
{
\pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@zero}}
\pgfpointorigin \pgf@circ@res@other = \pgf@x
\advance \pgf@circ@res@other by -\pgf@circ@res@up
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@zero}}
\pgfusepath{draw}
\pgfgetlastxy{\tempx}{\tempy}
\pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}
\pgfstartlinewidth}
\pgfscope
\pgfpathcircle{\pgfpointorigin}{\pgf@circ@res@up}
\pgfusepath{draw}
\endpgfscope
\pgfsetlinewidth{\pgfstartlinewidth}
\pgfgetlastxy{\XCoord}{\YCoord}
\ifdim \XCoord < \tempx
\ifdim \YCoord < \tempy
\def\pgf@circ@temp{UP} % these are debgging variables
\pgftransformrotate{180}
\else
\def\pgf@circ@temp{RIGHT}
\pgftransformrotate{-90}
\fi
\else
\ifdim \YCoord < \tempy
\def\pgf@circ@temp{LEFT}
\pgftransformrotate{90}
\else
\def\pgf@circ@temp{DOWN}
%\pgftransformrotate{90}
\fi
\fi
%\pgfnode{circle}{center}{\textbf{G}\pgf@circ@temp}{}{}% for testing
\pgfnode{circle}{center}{\textbf{G}}{}{}
}
\makeatother
\begin{document}
\begin{circuitikz}
\draw (0,0) to[galvanometer] ++(5,0) to[galvanometer] ++(0,5) to[galvanometer] ++(-5,0) to[galvanometer] ++(0,-5);
\draw (2.5,0.5) node[above] {A};
\draw (7,0) to[galvanometer] ++(5,0) to[galvanometer] ++(0,5) to[galvanometer] ++(-5,0) to[galvanometer] ++(0,-5);
\draw (7.5,2.5) node[right] {B};
\draw (0,-7) to[galvanometer] ++(5,0) to[galvanometer] ++(0,5) to[galvanometer] ++(-5,0) to[galvanometer] ++(0,-5);
\draw (7,-7) to[galvanometer] ++(5,0) to[galvanometer] ++(0,5) to[galvanometer] ++(-5,0) to[galvanometer] ++(0,-5);
\draw (7,-20) to[galvanometer] ++(10,0) to[galvanometer] ++(0,10) to[galvanometer] ++(-10,0) to[galvanometer] ++(0,-10);
%\draw (17,30) to[galvanometer] ++(5,0) to[galvanometer] ++(0,5) to[galvanometer] ++(-5,0) to[galvanometer] ++(0,-5);
\end{circuitikz}
\end{document}
答案1
从 0.9.1 版本开始,您就有了一个通用的旋转不变仪器rmeter
;我不明白为什么不能直接使用它……:
\documentclass[border=10pt]{standalone}
\usepackage[siunitx, RPvoltages]{circuitikz}
\begin{document}
\begin{circuitikz}[
galvanometer/.style={rmeter, t=G},
ammeter/.style={rmeter, t=A},
]
\draw (0,0) to[galvanometer] ++(5,0) to[galvanometer] ++(0,5) to[galvanometer] ++(-5,0) to[galvanometer] ++(0,-5);
\draw (7,0) to[ammeter] ++(5,0) to[ammeter] ++(0,5) to[ammeter] ++(-5,0) to[ammeter] ++(0,-5);
\end{circuitikz}
\end{document}
...或者用计量箭头,改变风格
galvanometer/.style={rmeterwa, t=G},
ammeter/.style={rmeterwa, t=A},
无论如何,您的代码的一个解决方案可能是使用密钥(就像我在这里做的那样);做类似的事情
\ctikzset{myletter/.initial=G}
然后\ctikzvalof{myletter}
在代码中使用而不是简单的“G”;当你想改变它时,你可以使用
to[galvanometer, /circuitikz/myletter=A]
...或者创建一种风格。