# How can I refer to label created by new command?

I have several pairs of graphs. I need to display these graphs in latex. I am using the following which works Great.

\begin{figure}
\centering
\begin{subfigure}{.5\textwidth}
\centering
\fbox{\includegraphics[width=.8\linewidth]{4-byVar1.png}}
\caption{Here's a question?'' (by Var1)}
\label{fig:4-byVar1}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
\centering
\fbox{\includegraphics[width=.8\linewidth]{4-byVar2.png}}
\caption{Here's a question?'' (by Var2)}
\label{fig:4-byVar2}
\end{subfigure}
\caption{Here's a question?''}
\label{fig:comboFig-4}
\end{figure}


However, that's a lot of code to repeat for each such pair of figures. I created a

\newcommand


to do the work for me, but it seems that when I try to ref a figure created by my newcommand, i get an error (fig undefined).

Can anyone recommend how I can refer to a label created by a newcommand?

The newcommand I created looks like this

\newcommand{\graphPairs}[2]{
\begin{figure}
\centering
\begin{subfigure}{.5\textwidth}
\centering
\fbox{\includegraphics[width=.8\linewidth]{#2-byCar1.png}}
\caption{#1 (by Var 1)}
\label{fig:#2-byVar1}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
\centering
\fbox{\includegraphics[width=.8\linewidth]{#2-byVar2.png}}
\caption{#1 (by Var 2)}
\label{fig:#2-byVar2}
\end{subfigure}
\caption{#1}
\label{fig:comboFig-#2}
\end{figure}
}

-

It does not matter, that \label is hidden in a \newcommand. For referencing you need the label name that depends on the second argument of \graphPairs. The first example would be generated by:

\graphPairs{Here's a question?''}{4}


And the labels can be referenced by

\ref{fig:4-byVar1}
\ref{fig:4-byVar2}
\ref{fig:comboFig-4}

-

TeX is a macro processor so it just does textual substitution of your command by its definition before \label is even considered. so if your command is called as

 \graphPairs{cap}{myfile}


Then your three \label are

\label{fig:myfile-byVar1}


and

\label{fig:myfile-byVar2}


and

\label{fig:comboFig-myfile}


so can be referenced as \ref{fig:myfile-byVar1}, \ref{fig:myfile-byVar2}, \ref{fig:comboFig-myfile}.

As always, it takes a couple of runs for references to be defined.

-
Thanks for your response. I think the "it takes a couple of runs" is what's holding me up. I had been using an IDE (sublime text 2 with mactex on a OS X) to build the PDF for me (so I have no idea what command to use and etc.). I tried running pdflatex mysourcefile.tex and that seemed to go ok, but even after running it a few times in a row, I am getting this warning: LaTeX Warning: Reference fig:comboFig-5' on page 25 undefined on input line 558. Any further suggestion? –  Michael Nov 27 '12 at 17:29
fig:comboFig-5 would correspond to calling your command with the 2nd argument being {5} so loading images 5-byCar1.png and 5-byVar1.png` is that what you did? it always help if your question contains complete runnable documents that demonstrate the problem. –  David Carlisle Nov 27 '12 at 17:59