Cross reference figure appears as “??”

I'm using Texmaker for a report and am fairly new to latex.

My cross referenced equations seem to appear just fine, but not my figures. I've labelled them and referenced the label using ref. I ran PDFLatex, then BibTex, then PDFLatex again, then Quick Build, but I keep getting "??"

\documentclass{article}

\usepackage{cite}
\usepackage{harvard}
\usepackage{url}
\renewcommand{\harvardurl}[1]{\textbf{URL:} \url{#1}}
\usepackage{caption}
\usepackage{mathtools}
\citationmode{abbr}
\citationstyle{dcu}
\begin{document}

\section{Results\label{results}}{}
\subsection{Measuring Detection Rate\label{detection}}{}
\subsubsection{Cross-correlation\label{crosscorrdetect}}

{
\begin{figure}[h]
\begin{centering}
\includegraphics[width=\textwidth]{corrDetectTest.png}
\caption{This is the $CrossCorrelation.h5$. $2$ is the value}
\end{centering}
\label{corrdetectdiag}
\end{figure}
}

{\ref{corrdetectdiag} shows the resulting...}

\newpage
\bibliographystyle{dcu}
\bibliography{BIB_FILE}
\end{document}

I liked how math mode formats the text, so I used it every time I mention a file name, value, or equation.

• Remove the center environment and place a \centering instead. Why is all your stuff in groups (between curlyy braces)? – Johannes_B Jun 5 '15 at 11:52
• "I liked how math mode formats the text, so I used it every time I mention a file name, value" For the love of God and all things you hold holy, do not do this!. That said, cross-references require you compile twice. You'll note in the terminal LaTeX saying something like 'some references undefined'. Just run LaTeX again. If that doesn't work, let us know. – Sean Allred Jun 5 '15 at 11:55
• Can you post us a complete-but-minimal compilable example that shows the problem please? Please limit the example to only the code required for your issue to appear. You can have a look at this guide for how to prune your code for this purpose. – Thruston Jun 5 '15 at 11:56
• centering is not an environment that you should \begin{} and \end{}. remove these and replace them with a single \centering after your \begin{figure} – ArTourter Jun 5 '15 at 12:14
• @CaptainObv the biggest reason is that what you're typesetting is not math. If you want to use a special font for filenames and other identifiers, there are other ways to specify that font than abusing math mode. But, anyway, other things that will go wrong: hyphenation, underscores in filenames, loss of kerning, spacing if the identifiers contain characters TeX expects to be math operators, etc. – Paul Gessler Jun 5 '15 at 13:23

I suggest loading the cleveref package, then using the command \cref{} which automatically adds fig. to the referred image number. You can also choose between these options:

• noabbrev, it will change fig. to figure. It must be added as an option to the package. I would advise in favour of this option in case you plan to start a sentence with Figure and not Fig.
• capitalise (or capitalize) will set all references with a capital letter (so Figure), used the same way as above.
• Alternatively, you can select which is capitalised and which is not, by using \cref for lowercase, and \Cref for uppercase. See example:

Code

\documentclass{article}
\usepackage{cite}
\usepackage{harvard}
\usepackage{url}
\usepackage{caption}
\usepackage{mathtools}
\usepackage[noabbrev]{cleveref}

\citationmode{abbr}
\citationstyle{dcu}
\renewcommand{\harvardurl}[1]{\textbf{URL:} \url{#1}}

\begin{document}
\section{Results\label{results}}{}
\subsection{Measuring Detection Rate\label{detection}}{}
\subsubsection{Cross-correlation\label{crosscorrdetect}}

\begin{figure}[h]
\centering
\includegraphics[width=\textwidth]{example-image-a}
\caption{This is the $CrossCorrelation.h5$. $2$ is the value}
\label{corrdetectdiag}
\end{figure}

\Cref{corrdetectdiag} shows the resulting...\\

\cref{corrdetectdiag} shows the resulting...

\newpage
\bibliographystyle{dcu}
\bibliography{BIB_FILE}
\end{document}