[Tex/LaTex] Defining a \newcommand for environment in CircuiTikz

circuitikzmacrostikz-pgf

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

Best Answer

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}