# Defining a \newcommand for environment in CircuiTikz

I have made the following macro for an op-amp with open connectors in circuitikz:

\newenvironment{opamp5}[5]
{
\begin{circuitikz}
\draw (0,0) node[op amp] (opamp) {};
\draw (-2,-0.49) to[short, o-] (opamp.+) (-2.2,-0.49) node[anchor=east] {${#1}$};
\draw (-2,0.49) to[short, o-] (opamp.-) (-2.2,0.49) node[anchor=east] {${#2}$};
\draw (-0.08,1.5) to[short, o-] (opamp.up) (-0.08,1.6) node[anchor=south] {${#3}$};
\draw (-0.08,-1.5) to[short, o-] (opamp.down) (-0.08,-1.6) node[anchor=north] {${#5}$};
\draw (1,0) to[short, -o] (2,0) -- (opamp.out) (2.1,0) node[anchor=west] {${#4}$};
\end{circuitikz}
}


To draw an op-amp with labels, starting from the positive input terminal and moving clockwise to the negative Vcc terminal you simply enter the environment opamp5 as I have defined above:

\begin{opamp5}
{1}{2}{3}{4}{5}
\end{opamp5}


My question is, instead of making this circuitikz picture an environment, is there any way I can define it as a command so i can link to other elements of a circuit diagram? I tried using the same syntax and defining it as \newcommand instead, but LaTeX did not like that. My goal is to have various new commands that I can link together smoothly instead of drawing an op-amp manually each time I need one in a circuit diagram. Rather than having to begin and end an environment, it is much more convenient if I can have a shorter syntax that will produce the same picture.

Thanks

-
depending on your editor/IDE of choice, i'd recommend some sort of templating/snippet package -- I accomplish this with emacs/yasnippet, but vim, textmate, and a whole bunch of other editors have support for this kind of thing. –  Mica Dec 7 '10 at 1:30
@Mica: Then whenever you change something in the code, you have to change it everywhere instead of at one point. –  Caramdir Dec 7 '10 at 1:42
@Caramdir doesn't your editor support find & replace? My comment was directed more at the part about "define it as a command so i can link it to other elements of the diagram" -- so i would put each element (or group of elements) into a snippet, then call the snippets as i needed them. This would provide the "various new commands that can be linked together." I also made the (possibly poor) assumption that the OP wanted to draw a slightly different circuit each time. Anyway, I'm sure there are several problems with making snippets, I just wanted to give a fresh perspective & offer a new idea –  Mica Dec 7 '10 at 2:08
@Mica: Thanks for advice, I shall play around with snippets and see if they are appropriate for my application, as I have never used them before. Sounds interesting though but as Caramdir mentioned, might be a little unpleasant having to find, replace, and such. Also you were right in your assumption, I would like to draw slightly different diagrams each time. Linking commands within the picture is highly desired. –  Jasper D'Agostino Dec 7 '10 at 2:31

You cannot have numbers in macro names (at least not without some trickery).

The following works fine:

\documentclass{article}
\usepackage{circuitikz}

\begin{document}

\newcommand\opampfive[5]
{
\begin{circuitikz}
\draw (0,0) node[op amp] (opamp) {};
\draw (-2,-0.49) to[short, o-] (opamp.+) (-2.2,-0.49) node[anchor=east] {${#1}$};
\draw (-2,0.49) to[short, o-] (opamp.-) (-2.2,0.49) node[anchor=east] {${#2}$};
\draw (-0.08,1.5) to[short, o-] (opamp.up) (-0.08,1.6) node[anchor=south] {${#3}$};
\draw (-0.08,-1.5) to[short, o-] (opamp.down) (-0.08,-1.6) node[anchor=north] {${#5}$};
\draw (1,0) to[short, -o] (2,0) -- (opamp.out) (2.1,0) node[anchor=west] {${#4}$};
\end{circuitikz}
}

\opampfive{1}{2}{3}{4}{5}
\end{document}

-
Thanks again I appreciate the answer and I will see if I can link each picture (op-amp in this case) to another picture within the CircuiTikz picture itself. The only problem I see at this moment is that when I am working in the CircuiTikz environment, then \begin{circuitikz} and \end{circuitikz} are already defined. If I start \opamp5{}{}{}{}{} then I will be starting another picture within a picture, if you understand what I mean. I'm not sure if LaTeX likes that. –  Jasper D'Agostino Dec 7 '10 at 3:15
@Jasper: You should be able to just take the \begin{circuitikz}...\end{circuitikz} out of \opampfive; then, your source would have \begin{circuitikz}\opampfive{1}{2}{3}{4}{5} <other code here>\end{circuitikz}. (You might also want to add a way to name your op amps in that case. Perhaps an optional argument.) –  Antal S-Z Dec 7 '10 at 6:10
@Antal S-Z: I took the begin and end out and just left it as a new command and I was able to link the opampfive picture to others within the circuit diagram! This is much appreciated. Now I will be more careful when defining the opamp newcommands and I am going to work on a way to keep them organized and named as you were saying. It will much more convenient in the future to reference pictures as commands when linking them in some random configuration. Thanks again! –  Jasper D'Agostino Dec 7 '10 at 13:31