# latex thinks reference is undefined

I know that very similar questions have been asked before, but the solutions do not work. I have the following latex code;

\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{enumerate}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
blahblah in figure\ref{diagram1}.

\begin{figure}[H]
\centering
\includegraphics[width=1\textwidth]{images/diagram.png}
\caption{blahblahblah }
\label{diagram1}
\end{figure}

\end{multicols}

\end{document}


and I obtain the following error message;

Reference diagram1' on page 2 undefined


No matter how many times I attempt to recompile the document using pdf latex, I obtain the same error.

Has anyone here encountered this before? Where latex is clearly wrong, but you do not comprehend how/why?

• I'm going to guess that latex is not wrong, but without a complete example that reproduces the problem, I can't say any more. – Ian Thompson Jun 11 '15 at 18:13
• Welcome to TeX.SX! It is easier to help you if you add a minimal working example that takes the form \documentclass{...}\usepackage{....}\begin{document}...\end{document}. The code snippet that you have provided is not enough to trouble shoot your problem. – Andrew Jun 11 '15 at 18:17
• there are any number or reasons that might happen. Impossible to tell unless you show a test case a \nofiles command for example. Or \renewcommand\label[1]{} or .... – David Carlisle Jun 11 '15 at 19:50
• Ian Thompson: I say that latex is wrong as it states that the reference is undefined when it is. Andrew: Thank you, I have added the necessary minimal working example. – user79970 Jun 12 '15 at 10:14
• I bet it has something to do with multi-column! – Pouya Jun 12 '15 at 10:25

LaTeX already told you the reason:

Package multicol Warning: Floats and marginpars not allowed inside multicols' environment!.


And the entire float object is gone including the \label. Therefore the reference is undefined.

Also H is not supported causing an error, which is fixed by adding package float. If you don't want a floating object, then there are other means to add a caption, see the following example.

Also if there are two columns, then there is no place for an image with width of the whole \textwidth. \linewidth is better, because it reflects the current line width.

\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{enumerate}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
blahblah in figure\ref{diagram1}.

\noindent
\begin{minipage}{\linewidth}
\centering
\includegraphics[width=\linewidth]{images/diagram.png}
\captionof{figure}{blahblahblah }
\label{diagram1}
\end{minipage}

\end{multicols}
\end{document}


If you want to have a figure across the full text width, then figure* instead of figure can be used, but only as floating object without H:

\documentclass[a4paper,10pt]{report}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[demo]{graphicx}
\usepackage{enumerate}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}
blahblah in figure\ref{diagram1}.

\begin{figure*}
\includegraphics[width=\linewidth]{images/diagram.png}
\caption{blahblahblah }
\label{diagram1}
\end{figure*}

\end{multicols}
\end{document}

• Fantastic, I didn't receive the "Package multicol Warning: Floats and marginpars not allowed inside multicols' environment!." warning. – user79970 Jun 12 '15 at 11:02
• The first solution does not work, giving the error " ! LaTeX Error: \caption outside float.See the LaTeX manual or LaTeX Companion for explanation.Type H <return> for immediate help.... \caption " However the second works just as you say. – user79970 Jun 12 '15 at 11:12
• @PlokavianNerveGas Therefore the first solution is not using \caption{...} but \captionof{figure}{...}, provided by package caption or capt-of`. – Heiko Oberdiek Jun 12 '15 at 11:20