# Unable to reference figures

I have inserted the following figure into my latex document

\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{Figures/magnetic-sandwich.jpg}\\
\caption{A magnetic multilayer (a trilayer or sandwich'') that exhibits the GMR effect.  Magnetic iron (red) and chromium (grey).}\label{setup}
\end{figure}


later on in the document I want to reference this figures which I am trying to do like so:

 By constructing multilayers as depicted in Fig \ref{setup} devices....


however when I typeset the document I get the following warning:

LaTeX Warning: Reference setup' on page 2 undefined on input line 64.

which persists even after I typeset the document again.

The text where the reference is contained is within a subsection of the section where the figure is inserted. I also have this same problem with another figure in the document but all others work ok. I am not sure on the cause of this issue and any help troubleshooting / fixing it would be appreciated.

Edit:

Here is a sample document which should replicate the problem:

\documentclass[10pt]{iopart}
\usepackage{graphicx}
%Uncomment next line if AMS fonts required
\usepackage{iopams}
\usepackage{placeins}
\usepackage{url}
\usepackage{multicol}
\usepackage{blindtext}

\begin{document}

\section{Giant Magnetoresistance}
\begin{multicols}{2}

%\blindtext

\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{Figures/magnetic-sandwich.jpg}\\
\caption{A magnetic multilayer (a trilayer or sandwich'') that exhibits the GMR     effect.  Magnetic iron (red) and chromium (grey).}\label{setup}
\end{figure}

%\blindtext

\end{multicols}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{Figures/initial-results.png}
\caption{The initial results displaying GMR effect by Gr\"unberg (left) and Fert (right).\\
Left: To the far right and left the iron layers are parallel to the external magnetic field.  In the middle the iron layers are antiparallel to the external magnetic field.\\
Right:  To the far left and right the magnetisation of the iron layers are parallel to the external magnetic field.  In the middle every second iron layer is magnetised perpendicular to the external magnetic field.} \label{fig-initial-results}
\end{figure}

\begin{multicols}{2}

%\blindtext

\subsection{GMR cause and effect}
By constructing multilayers as depicted in Fig \ref{setup} devices such as spin-valves can be constructed which depend on being able to switch between different resistivity, this resistivity can be understood by analysing the following diagram.

\end{multicols}

\end{document}


The source files for iopart class can be found here: http://authors.iop.org/atom/usermgmt.nsf/AuthorServices

select latex guidelines and class file on the left hand side, which should start the download.

-
For references two work, you have to compile twice. –  A.Ellett Feb 24 at 17:13
Sorry, I should have added that I have done this and it is still an issue. –  Aesir Feb 24 at 17:16
As presented, I don't get any errors (other than for the citation which I don't have). –  A.Ellett Feb 24 at 17:21
If you're breaking your document up into pieces and then loading them, you might want to check to see whether you somehow neglected to \input this particular file containing the figure. –  A.Ellett Feb 24 at 17:22
I've added a sample document which you should be able to use to reproduce the error. –  Aesir Feb 24 at 18:02

I don't have the iopart class. When I ran this under article, there were a couple of things I noticed.

1. You're using figures within a multicols environment. Perhaps this is allowed in iopart.
2. You've embedded newline breaks in your caption. You should either \protect them (ie., \protect\\), or you should include a minipage environment with the extra text to be placed after a shorter caption.

Here's an example of what I'm suggesting regarding the caption:

\begin{figure}
\centering
\includegraphics[width=\textwidth]{example-image-b}
\caption{The initial results displaying GMR effect by Gr\"unberg (left)
and Fert (right).}
\begin{minipage}{\linewidth}\vspace{1ex}
%%
Left: To the far right and left the iron layers are parallel to the
external magnetic field.  In the middle the iron layers are
antiparallel to the external magnetic field.\\
%%
Right:  To the far
left and right the magnetisation of the iron layers are parallel to
the external magnetic field.  In the middle every second iron layer
is magnetised perpendicular to the external magnetic field.
\end{minipage}
\label{fig-initial-results}
\end{figure}


Otherwise, I still don't get any errors with the labels, but I am using a different class. Unfortunately, I don't have the time available today to load that class and learn enough about it to see whether there might be an issue there.

But, my guess is that you should correct the issue with the multicols and caption content and see whether that resolves the issue. I strongly suspect that your iopart class might be suppressing the multicols errors and consequently not writing out the necessary label. Just move the figure to immediately before or immediately after the call to multicols. I doubt the caption formatting has anything to do with the labeling issues.

So try removing the multicols about the figures. Post a response to this answer whether that works or not. If it doesn't work, then I'll remove this answer since the issue is something beyond what I have time to research right now.

I hope this helps.

Here's a complete MWE showing what I'm suggesting:

\documentclass[10pt]{iopart}
\usepackage{graphicx}
%Uncomment next line if AMS fonts required
\usepackage{iopams}
\usepackage{placeins}
\usepackage{url}
\usepackage{multicol}
\usepackage{blindtext}
\begin{document}

\section{Giant Magnetoresistance}

\begin{figure}
\centering
\includegraphics[width=0.5\textwidth]{example-image-a}\\
\caption{A magnetic multilayer (a trilayer or sandwich'') that
exhibits the GMR effect.  }
\begin{minipage}{\linewidth}
\vspace{1ex}
Magnetic iron (red) and chromium (grey).
\end{minipage}
\label{setup}
\end{figure}

\begin{multicols}{2}

\blindtext

\blindtext

\blindtext

\subsection{GMR cause and effect}

By constructing multilayers as depicted in Fig \ref{setup} devices such as
spin-valves can be constructed which depend on being able to switch between
different resistivity, this resistivity can be understood by analysing the
following diagram.

\end{multicols}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{example-image-b}
\caption{The initial results displaying GMR effect by Gr\"unberg (left)
and Fert (right).}
\begin{minipage}{\linewidth}\vspace{1ex}
%%
Left: To the far right and left the iron layers are parallel to the
external magnetic field.  In the middle the iron layers are
antiparallel to the external magnetic field.\\
%%
Right:  To the far
left and right the magnetisation of the iron layers are parallel to
the external magnetic field.  In the middle every second iron layer
is magnetised perpendicular to the external magnetic field.
\end{minipage}
\label{fig-initial-results}
\end{figure}

\end{document}
`
-
Ah ok, I didn't realise that you couldn't put figures within a multicol part. I will go through and remove them and then hopefully this should resolve my problem, thanks for spending time on this. –  Aesir Feb 24 at 19:11
+1 @AEllett for spending a fair amount of time on the OP's long MWE (possibly nonminimal). –  Ethan Bolker Feb 24 at 19:12