# Are there any dependencies causing my \ref to not compile?

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}

\begin{figure}[h]
% \caption{Isothermal thick film.}
\begin{center}
\centering
\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})}
\label{k2a}
\end{center}
\end{figure}


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{graphicx}
%\usepackage{float}
\usepackage[section]{placeins}
\usepackage{caption}
\captionsetup{margin=10pt,font=small,labelsep=period,labelfont=bf}
% --------
\usepackage{lscape}         % allows for landscape tables and figures
\usepackage{paralist}       % enancements to list environment
\usepackage{array}          % enhancements to array and tabular environments
\usepackage[usenames]{color}
\usepackage{url}
\usepackage{longtable}      % multipage tables
\usepackage{ifthen}
\usepackage{ifpdf}
\usepackage{setspace}       % easy single or doublespacing
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{rotate}
\usepackage{subfigure}
\usepackage{subfig}
\usepackage{wrapfig}
\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, ...

%%%%%%%%xyz%%%%%%%%%%%%%%%
\usepackage{epsfig}
\usepackage{epstopdf}
\usepackage{textcomp}
\usepackage{multirow}
%\usepackage{subfig}
\usepackage{paralist}
\usepackage{changepage}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%11022012-START%%%%%%%%%%%%%%
\usepackage[T1]{fontenc} %Useful for file names with underscores in them
\usepackage{float}
\usepackage{grffile}
\newcommand{\hilight}[1]{\colorbox{yellow}{#1}} %To highlight text
\renewcommand{\thefootnote}{\alph{footnote}} %For footnotes
%%%%%%%%%%11022012-STOP%%%%%%%%%%%%%%

-
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

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.

\documentclass{article}

\usepackage[T1]{fontenc}

\usepackage{caption}
\captionsetup{margin=10pt,font=small,labelsep=period,labelfont=bf}

\begin{document}

Figure \ref{k2a}.

\begin{figure}[htp]
\centering
%     \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}
\label{k2a}
\end{figure}

\end{document}


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.

-
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