如何在循環索引上調整 Circuitikz 點?

如何在循環索引上調整 Circuitikz 點?

我試圖僅在循環的某些迭代中使用 Circuitikz 中的連接點,但它失敗了。

\documentclass[border=6mm]{standalone}
\usepackage[siunitx, american]{circuitikz}
\begin{document}
\usetikzlibrary{calc}
\begin{tikzpicture}[
    font=\sffamily,
    every node/.style = {align=center}
]
\foreach[count=\i] \x in {0,3}
{
    \ifnum\i=1
    \tikzstyle{maybedot} = []
   \else
    \tikzstyle{maybedot} = [-*]
    \fi
     \draw (\x,0) to [R, l_={$R_\i$}, maybedot] (\x, 3) to [short, maybedot](\x,5);
}

     \draw (-2,0) to [R, l_={$R_0$ \\ noloop}, -*] (-2, 3) to [short, -*](-2,5);


\end{tikzpicture}
\end{document}

在此輸入影像描述

R1 正確地沒有點,但我希望 R2 像 R0 一樣有點。我怎樣才能讓它發揮作用? (我的真實例子更複雜,做這種事情會節省我很多重複的程式碼)

答案1

這裡的問題是你的\tikzset(儘管使用舊的、最好避免的語法)正在設定鍵/tikz/-*(箭頭),正如你可能在錯誤中註意到的那樣:

prova.tex|20 error| Package pgf Error: Unknown arrow tip kind '*'.
prova.tex|20 error| Package pgf Error: Unknown arrow tip kind '*'.

極點的正確鍵是/tikz/circuitikz/-*,所以這有效:

\documentclass[border=6mm]{standalone}
\usepackage[siunitx, american, RPvoltages]{circuitikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[
    font=\sffamily,
    every node/.style = {align=center},
    ]
    \foreach[count=\i] \x in {0,3}
    {
        \ifnum\i=1
            \ctikzset{maybedot/.style={}}
        \else
            \ctikzset{maybedot/.style={-*}}
        \fi
        \draw (\x,0) to [R, l_={$R_\i$}, maybedot] (\x, 3) 
            to [short,  maybedot](\x,5);
    }
    \draw (-2,0) to [R, l_={$R_0$ \\ noloop}, -*] (-2, 3) to [short, -*](-2,5);
\end{tikzpicture}
\end{document}

在循環中混合\if事物\foreach是相當危險的(儘管問題是這裡的另一個問題)。我會\ifthenelse在這裡使用幾個巨集而不是樣式:

\documentclass[border=6mm]{standalone}
\usepackage{ifthen}
\usepackage[siunitx, american, RPvoltages]{circuitikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[
    font=\sffamily,
    every node/.style = {align=center},
    ]
    \foreach[count=\i] \x in {0,3}
    {
        \ifthenelse{\i = 1}{\edef\maybedot{}}{\edef\maybedot{-*}}
        \draw (\x,0) to [R, l_={$R_\i$}, \maybedot] (\x, 3) 
            to [short,  \maybedot](\x,5);
    }
    \draw (-2,0) to [R, l_={$R_0$ \\ noloop}, -*] (-2, 3) to [short, -*](-2,5);
\end{tikzpicture}
\end{document}

如果不想加載ifthen,則測試

\ifnum\i=1\edef\maybedot{}\else\edef\maybedot{-*}\fi

也有效。

找出三種可能的選擇

相關內容