tikz 图形的 includestandalone 问题

tikz 图形的 includestandalone 问题

摘自我的帖子(Circuitikz:路径上的箭头以及使用可变电阻改变箭头的方向),Gonzalo Medina 帮助修正了一个可变电阻器。

但是,当我在主文档中使用 include standalone 时,它​​无法编译。单独的 tikz 文档可以正常编译。

主要文档设置如下:

\documentclass{article}
\usepackage{standalone}
\usepackage[american, cuteinductors]{circuitikz}
\usepackage{siunitx}

\makeatletter
\ctikzset{bipoles/vresistorm/height/.initial=.6}
\ctikzset{bipoles/vresistorm/width/.initial=.8}
\def\pgf@circ@vresistorm@path#1{\ifpgf@circuit@europeanresistor
  \pgf@circ@bipole@path{tgeneric}{#1}\else\pgf@circ@bipole@path{vresistorm}{#1}
  \fi}

\tikzset{variable resistorm/.style = {\circuitikzbasekey,
    /tikz/to path = \pgf@circ@vresistorm@path, l = #1}}
\tikzset{variable american resistorm/.style= {\circuitikzbasekey,
    /tikz/to path = \pgf@circ@bipole@path{vresistorm}{#1}, l = #1}}

\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/vresistorm/height}}{vresistorm}{
  \ctikzvalof{bipoles/vresistorm/height}}{\ctikzvalof{bipoles/vresistorm/width}}
{\pgf@circ@res@step = \ctikzvalof{bipoles/vresistorm/width}\pgf@circ@Rlen
  \divide \pgf@circ@res@step by 12
  \def\myfrac{.5}
  \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}
    \pgfstartlinewidth}

  \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@zero}}
  \pgf@circ@res@other = \pgf@circ@res@left
  \advance\pgf@circ@res@other by \pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
  \advance\pgf@circ@res@other by \pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@zero}}
  \pgfusepath{draw}

  \pgfscope
  \pgfsetarrowsend{latex'}
  \pgfpathmoveto{\pgfpoint{.7\pgf@circ@res@other}{-\pgf@circ@res@up}}
  \pgfpathlineto{\pgfpoint{-.7\pgf@circ@res@other}{-\pgf@circ@res@down}}
  \pgfusepath{draw}
  \endpgfscope
}
\makeatother

\begin{document}

\begin{figure}
  \centering
  \includestandalone[width = 3in]{tikz}
\end{figure}
\end{document}

然后独立做的是

\documentclass[convert = false]{standalone}

\usepackage[american, cuteinductors]{circuitikz}
\usepackage{siunitx}

\makeatletter
\ctikzset{bipoles/vresistorm/height/.initial=.6}
\ctikzset{bipoles/vresistorm/width/.initial=.8}
\def\pgf@circ@vresistorm@path#1{\ifpgf@circuit@europeanresistor
  \pgf@circ@bipole@path{tgeneric}{#1}\else\pgf@circ@bipole@path{vresistorm}{#1}
  \fi}

\tikzset{variable resistorm/.style = {\circuitikzbasekey,
    /tikz/to path = \pgf@circ@vresistorm@path, l = #1}}
\tikzset{variable american resistorm/.style= {\circuitikzbasekey,
    /tikz/to path = \pgf@circ@bipole@path{vresistorm}{#1}, l = #1}}

\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/vresistorm/height}}{vresistorm}{
  \ctikzvalof{bipoles/vresistorm/height}}{\ctikzvalof{bipoles/vresistorm/width}}
{\pgf@circ@res@step = \ctikzvalof{bipoles/vresistorm/width}\pgf@circ@Rlen
  \divide \pgf@circ@res@step by 12
  \def\myfrac{.5}
  \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}
    \pgfstartlinewidth}

  \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@zero}}
  \pgf@circ@res@other = \pgf@circ@res@left
  \advance\pgf@circ@res@other by \pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
  \advance\pgf@circ@res@other by \pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@zero}}
  \pgfusepath{draw}

  \pgfscope
  \pgfsetarrowsend{latex'}
  \pgfpathmoveto{\pgfpoint{.7\pgf@circ@res@other}{-\pgf@circ@res@up}}
  \pgfpathlineto{\pgfpoint{-.7\pgf@circ@res@other}{-\pgf@circ@res@down}}
  \pgfusepath{draw}
  \endpgfscope
}
\makeatother

\begin{document}
\begin{circuitikz}[scale = 2]
  \draw                                                 (0, 0)
        to[american voltage source, v^ = $e(t)$]        (0, 3)
        to[R, l^ = $R_1$, -*]                           (1.5, 3)
        to[variable american resistorm, l_ = $R_2(t)$]  (1.5, 1.5)
        to[L, l^ = $i_L(t)$, i = $ \ $, -*]             (1.5, 0)
        to[short]                                       (0, 0);
  \draw                                                 (1.5, 0)
        to[short, -*]                                   (3, 0)
        to[C, l^ = $v_c(t)$, -*]                        (3, 3)
        to[short]                                       (1.5, 3);
  \draw                                                 (3, 3)
        to[short]                                       (4.5, 3)
        to[R]                                           (4.5, 0)
        to[short]                                       (3, 0);        
  \draw                                                 (4.5, 2.75)
        to[short, i = $f_v(c)$]                         (4.5, 2);      

  \node at (1.25, .75) {$L$};
  \node at (3.35, 1.5) {$C$};
  \node at (3.1, 1.65) {$+$};
  \node[align = left] at (5.1, 1.5) {Nonlinear \\ resistor};
\end{circuitikz}
\end{document}

当我编译主文档时,收到以下消息:

! Undefined control sequence.
<argument> ...ipole@path{tgeneric}{##1}\else \pgf 
                                                  @circ@bipole@path{vresisto...
l.65 \begin{document}

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! You can't use `macro parameter character #' in restricted horizontal mode.
<argument> ...pgf @circ@bipole@path{vresistorm}{##
                                                  1} \fi } \par \tikzset {va...
l.65 \begin{document}

Sorry, but I'm not programmed to handle this case;
I'll just pretend that you didn't ask for it.
If you're in the wrong mode, you might be able to
return to the right one by typing `I}' or `I$' or `I\par'.

! Extra }, or forgotten \endgroup.
<argument> ...c@bipole@path{vresistorm}{##1} \fi }
                                                   \par \tikzset {variable r...
l.65 \begin{document}

I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

并且这样的情况一直持续下去。

答案1

一种快速的解决方法是创建一个包含代码的小包。创建一个文件,例如mycode.sty,以

\NeedsTeXFormat{LaTeX2e} 
\ProvidesPackage{mycode} 

然后将我给你的所有代码(不带\makeatletternor \makeatother)移到那里。将此文件保存在 TeX 可以找到的地方(当前工作目录的本地 TDS 树),然后\usepackage{mycode}在你的文档中使用加载中circuitikz

那么文件mycode.sty将会是这样的

\NeedsTeXFormat{LaTeX2e} 
\ProvidesPackage{mycode} 

\ctikzset{bipoles/vresistorm/height/.initial=.6}
\ctikzset{bipoles/vresistorm/width/.initial=.8}
\def\pgf@circ@vresistorm@path#1{\ifpgf@circuit@europeanresistor
  \pgf@circ@bipole@path{tgeneric}{#1}\else\pgf@circ@bipole@path{vresistorm}{#1}
  \fi}

\tikzset{variable resistorm/.style = {\circuitikzbasekey,
    /tikz/to path = \pgf@circ@vresistorm@path, l = #1}}
\tikzset{variable american resistorm/.style= {\circuitikzbasekey,
    /tikz/to path = \pgf@circ@bipole@path{vresistorm}{#1}, l = #1}}

\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/vresistorm/height}}{vresistorm}{
  \ctikzvalof{bipoles/vresistorm/height}}{\ctikzvalof{bipoles/vresistorm/width}}
{\pgf@circ@res@step = \ctikzvalof{bipoles/vresistorm/width}\pgf@circ@Rlen
  \divide \pgf@circ@res@step by 12
  \def\myfrac{.5}
  \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}
    \pgfstartlinewidth}

  \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@zero}}
  \pgf@circ@res@other = \pgf@circ@res@left
  \advance\pgf@circ@res@other by \pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
  \advance\pgf@circ@res@other by 2\pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
  \advance\pgf@circ@res@other by \pgf@circ@res@step
  \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@zero}}
  \pgfusepath{draw}

  \pgfscope
  \pgfsetarrowsend{latex'}
  \pgfpathmoveto{\pgfpoint{.7\pgf@circ@res@other}{-\pgf@circ@res@up}}
  \pgfpathlineto{\pgfpoint{-.7\pgf@circ@res@other}{-\pgf@circ@res@down}}
  \pgfusepath{draw}
  \endpgfscope
}

相关内容