将 circuitikz 标签装入盒子

将 circuitikz 标签装入盒子

我尝试使用以下代码绘制电阻的等效电路:

\documentclass{standalone}
\usepackage[european,cuteinductor]{circuitikz}
\usetikzlibrary{positioning, fit, calc}

\begin{document}
\begin{circuitikz}
    \draw (0,0) to[short,-*] ++(2,0) coordinate(left)
    to[R=$R$, name=r] ++(2,0)
    to[L=$L$,-*] ++(2,0) coordinate(right)
    to[short] ++(2,0)

    (left) to[short] ++(0,1)
    to[C=$C$, name=c] ($(right)+(0,1)$)
    to[short] (right)

    node[fit=(left)(right)(c)(r),draw, dashed, label={Resistor}, inner sep=10pt]{};
\end{circuitikz}
\end{document}

这导致:

Imgur

Fit 不会考虑电容器上方的“C”标签。我通过在电容器上方放置一个额外的坐标来解决这个问题:

\documentclass{standalone}
\usepackage[european,cuteinductor]{circuitikz}
\usetikzlibrary{positioning, fit, calc}

\begin{document}
\begin{circuitikz}
    \draw (0,0) to[short,-*] ++(2,0) coordinate(left)
    to[R=$R$, name=r] ++(2,0)
    to[L=$L$,-*] ++(2,0) coordinate(right)
    to[short] ++(2,0)

    (left) to[short] ++(0,1)
    to[C=$C$, name=c] ($(right)+(0,1)$)
    to[short] (right)

    coordinate[above=10pt of c] (top)
    node[fit=(left)(right)(top)(r),draw, dashed, label={Resistor},
    inner sep=10pt] {};
\end{circuitikz}
\end{document}

看起来合理:

Imgur

有没有更好的方法来告诉它还应该适合电容器上方的“C”标签?

答案1

由于您已经加载了calctikzlibrary,因此您可以使用它来调整 c坐标内部fit

\documentclass{standalone}
\usepackage[european,cuteinductor]{circuitikz}
\usetikzlibrary{positioning, fit, calc}

\begin{document}
\begin{circuitikz}
    \draw (0,0) to[short,-*] ++(2,0) coordinate(left)
    to[R=$R$, name=r] ++(2,0)
    to[L=$L$,-*] ++(2,0) coordinate(right)
    to[short] ++(2,0)

    (left) to[short] ++(0,1)
    to[C=$C$, name=c] ($(right)+(0,1)$)
    to[short] (right)

    node[fit={(left)(right)($(c.north)+(0,.5)$)(r)},draw, dashed, label={Resistor},
    inner sep=10pt] {};

\end{circuitikz}
\end{document}

enter image description here

相关内容