TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have learned that for labels and their respective references to compile and display correctly (for figures), it is necessary to have the \label{} after \caption{}, such as below:

Figure \ref{k2a}

 % \caption{Isothermal thick film.}
    \includegraphics[width= \columnwidth]{/home/userid/Research/Dissertation/2D/Periodic/k2a}
     \caption{When $\lambda = \lambda_{max}$ and M=35.1, Pr=7.20, S=100, G=1/3, rupture takes place at t=1280.0 (within 1\% or\citet{Krishnamoorthy1995a})}

However, are there any .sty / package dependencies that are not allowing my labels to compile right? I run latex twice but I receive a bunch of warnings such as Reference on page abc undefined on input line xxx. and I get the dreaded ? where the reference should be. The link to the figure through the question mark works though.

Here is my preamble, where I load all my packages. This occurs in a separate file.

does \usepackage{caption} have anything to do with this issue?

% --------
\usepackage{lscape}         % allows for landscape tables and figures
\usepackage{paralist}       % enancements to list environment
\usepackage{array}          % enhancements to array and tabular environments
\usepackage{longtable}      % multipage tables
\usepackage{setspace}       % easy single or doublespacing
\usepackage{listings}       % for listing computer code such as matlab
\usepackage[bw]{mcode}      % for matlab code with listings package
\usepackage[square,comma,sort&compress]{natbib} % enhances bibtex citations
% \usepackage{biblatex}
\usepackage{setspace}       % \singlespacing, \onehalfspacing, \doublespacing, ...


\usepackage[T1]{fontenc} %Useful for file names with underscores in them
\newcommand{\hilight}[1]{\colorbox{yellow}{#1}} %To highlight text
\renewcommand{\thefootnote}{\alph{footnote}} %For footnotes
share|improve this question
I don't know whether it helps, but you may try to put the \label inside \caption like this: \caption{When $\lambda = \lambda_{max}$ and M=35.1, Pr=7.20, S=100, G=1/3, rupture takes place at t=1280.0 (within 1\% or\citet{Krishnamoorthy1995a}).\label{k2a}}. Notice that some of the packages are deprecated: epsfig (graphicx is enough), color (use xcolor). As well, I'm not sure whether the packages subfig and sibfigure are not clashing each other. – yo' Dec 6 '12 at 13:20
@tohecz Thanks for the tip! Also about the subfig and subfigure intereference... On a side note, would you know how I could label each figure in a minipage... or in a subfigure? – drN Dec 6 '12 at 13:22
I had no problems compiling your float and \ref{} using your preamble (using MikTeX). I had to comment out \usepackage{mcode} since I did not have that package installed and also your \includegraphics since I do not have the figure. I would actually try to change the name of the graphics so that it is not the same as the the label. I finally renamed one of my eps-files to k2a.eps and your script still works without issues. So I do not think you have anything wrong in the code you have shown. – Peter Jansson Dec 6 '12 at 13:39
Please, show how your .aux file looks after two compilations. – yo' Dec 6 '12 at 13:51
You really should try to make a minimal self contained example that shows the problem (while trying to make that you will probably find out what is causing the problem) Also, unrelated don't use [h] always use at least [htp] if you want to give latex a chance to avoid taking the float to the end of the document. – David Carlisle Dec 6 '12 at 14:21
up vote 3 down vote accepted

As mentioned in the comments, it is very useful to provide a minimum working example (MWE) rather than copying your entire preamble (you did not include the documentclass in your code, but that's important).

For example, the following compiles fine with LaTeX.





Figure \ref{k2a}.

%     \includegraphics[width= \columnwidth]{/home/userid/Research/Dissertation/2D/Periodic/k2a}
     \caption{When $\lambda = \lambda_{max}$ and M=35.1, Pr=7.20, S=100, G=1/3, rupture takes place at t=1280.0}


Incidentally, your entire preamble compiles fine on my machine with the caption package and the undefinend reference errors only occur because I don't have your citations.

A few other things, mentioned in the comments above and summarised here:

  • Load graphicx instead of epsfig
  • Load xcolor instead of color
  • Load only subfig and not the depreciated subfigure
  • You load setspace twice
  • Don't use the center environement in the float. centering is enough
  • Don't use [h] for placement unless you really need to, in which case [h!] is preferred (I think). Otherwise use something like [htbp], which instructs LaTeX to try an place the float here, top, bottom, page in that priority.

The l2tabu document might provide an interest read, which discusses some of LaTeX' obsolote packages and options.

share|improve this answer
Instead of subfigure one could also use subcaption which should play very nicely with caption (it's from the same author). – clemens Dec 6 '12 at 18:18
I accept this as an answer but I had the issue with non-compiling labels and references until I removed the \usepackage{caption}. – drN Dec 7 '12 at 15:56
@drN Did you try updating your distribution? – Jörg Dec 7 '12 at 15:58
@Jörg I am scared stiff that if I updated my latex distribution it would rewrite style files and put my dissertation (due quite soon) in jeopardy. What I generally do is, if I find that I lack a certain style file or package, I just put it in my dissertation folder... – drN Dec 7 '12 at 16:24
@drN I am not sure that copying .sty files into your folder is good practice. One way to find out if your distribution is your issue is to install TeXLive (probably in portable mode), make a copy of your dissertation folder (without the sty files!) and try how that works. – Jörg Dec 7 '12 at 16:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.