Circuitikz のマクロを使用して要素にラベルを付ける

Circuitikz のマクロを使用して要素にラベルを付ける

($(M1.G)+(1.3,-0.2)$) node [below] {$M_1$}などのマクロを使用して、特定の場所 [ ]にある各 MOS トランジスタ要素にラベルを追加する可能性はありますか?\lbl{1}そうすれば、M1、M2 などの異なる番号のラベルを毎回追加する必要がなくなります。

\begin{figure}[H]
\centering
    \begin{circuitikz}[scale=1]
        \ctikzset{tripoles/mos style/arrows}
        \def\Ba{2,0.5} 
        \def\lbl{1.3,-0.2}
        \draw           
            (\Ba) node [nmos] (M1) {} ($(M1.G)+(\lbl)$) node [below] {$M_1$}
            (M1.S) node [ground] {}
            ($(\Ba)+(0,2)$) node [nmos] (M2) {}($(M2.G)+(1.3,-0.2)$) node [below] {$M_2$} 
            (M1.D) --(M2.S)
            (M1.G) node[circ]{}node[left]{$V_B$}
            (M2.G) node[circ]{}node[left]{$V_{in}$} 
            ($(M1.D)+(0,0.25)$)--($(M1.D)+(1,0.25)$)node[circ]{$V_{out}$}
            (M2.D) node [rground,yscale=-1] (vdd){}   ;
    \end{circuitikz}
    \caption{\Common drain amplifier.}
\end{figure}

ここに画像の説明を入力してください

答え1

例えば、略語を定義することができます

\newcommand\mynmos[1]{node [nmos] (M#1) {} ($(M#1.G)+(\lbl)$) node [below] {$M_{#1}$}}

そしてそれを使って

\draw           
  (\Ba) \mynmos{1}
  (M1.S) node [ground] {}
  ($(\Ba)+(0,2)$) \mynmos{2}

(完全なコードについては下記を参照してください)。

ラベル専用のマクロを導入することもできます。この場合、\lblこの名前はすでに使用されているため、呼び出すことはできません。

\newcommand\lblnmos[1]{($(M#1.G)+(\lbl)$) node [below] {$M_{#1}$}}
...
\draw           
    (\Ba) node [nmos] (M1) {} \lblnmos{1}
    (M1.S) node [ground] {}
    ($(\Ba)+(0,2)$) node [nmos] (M2) {} \lblnmos{2}

ここに画像の説明を入力してください

\documentclass{article}
\usepackage{circuitikz}
\begin{document}
\newcommand\mynmos[1]{node [nmos] (M#1) {} ($(M#1.G)+(\lbl)$) node [below] {$M_{#1}$}}
    \begin{circuitikz}[scale=1]
        \ctikzset{tripoles/mos style/arrows}
        \def\Ba{2,0.5} 
        \def\lbl{1.3,-0.2}
        \draw           
            (\Ba) \mynmos{1}
            (M1.S) node [ground] {}
            ($(\Ba)+(0,2)$) \mynmos{2}
            (M1.D) --(M2.S)
            (M1.G) node[circ]{}node[left]{$V_B$}
            (M2.G) node[circ]{}node[left]{$V_{in}$} 
            ($(M1.D)+(0,0.25)$)--($(M1.D)+(1,0.25)$)node[circ]{$V_{out}$}
            (M2.D) node [rground,yscale=-1] (vdd){}   ;
    \end{circuitikz}
\end{document}

関連情報