# Problem with includestandalone of a tikz graphics

From my post (Circuitikz: arrows on the path and changing directions of arrow with variable resistor), Gonzalo Medina helped correct a variable resistor.

However, when I use include standalone in my main doc, it won't compile. The separate tikz document compiles fine.

The main doc is set up like:

\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
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
\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}


Then the standalone do is

\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
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
\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}


When I compile the main document, I receive the following messages:

! 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\$}'.


And it goes on and on.

• It can't change the catcode of @ in the body of the document. – percusse Apr 18 '14 at 3:07
• A quick workaround is to create a file mycode.sty starting with \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{mycode} and then move there all the code I gave you (without \makeatletter nor \makeatother). Save this file somewhere TeX can find it (local TDS tree of current working directory) and then use \usepackage{mycode} in your document. (I didn't test it but it should work). – Gonzalo Medina Apr 18 '14 at 3:11
• @dustin the easiest location is your current working directory (the same where your .tex file is). Another possible location would be in a local TDS tree (this will depend on your OS, but there are some question in this site addressing this). – Gonzalo Medina Apr 18 '14 at 3:37
• @GonzaloMedina problem solved. Do you want to create an answer. – dustin Apr 18 '14 at 3:40

A quick workaround is to create a little package with the code. Create a file, say mycode.sty, starting with

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mycode}


and then move there all the code I gave you (without \makeatletter nor \makeatother). Save this file somewhere TeX can find it (local TDS tree of current working directory) and then use \usepackage{mycode} in your document after loading circuitikz.

The file mycode.sty then would be something like

\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
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@up}}
\pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\myfrac\pgf@circ@res@down}}

• @dustin as I said, I am not familiar with \includestandalone, but I guess that (as percusse) mentioned in his comment, the @ character causes problems; apparently the necessary change of category code cannot be performed directly in the document. – Gonzalo Medina Apr 18 '14 at 3:59