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

    \caption{``Here's a question?'' (by Var1)}
    \caption{``Here's a question?'' (by Var2)}
  \caption{``Here's a question?''}

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


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

      \caption{#1 (by Var 1)}
      \caption{#1 (by Var 2)}

2 Answers 2


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


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


Then your three \label are






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, 2012 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. Nov 27, 2012 at 17:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .