5

So I have a section of code that looks like this

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{verbatim}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\usepackage{booktabs}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\label{collecting_the_spectra}
\subsection{}
\label{usb}
If the screen in Figure \ref{4_specs} 

\begin{figure}[htp]
\centering
\includegraphics[width=5in]{4_specs.png}
\hspace{1.5in}\parbox{5in}{\caption{All spectrometers.}}
\label{4_specs}
\end{figure}

\end{document}

and should compile if you point 4_specs.png at an actual picture but for some reason it outputs stuff like this

enter image description here

Notice the spots where it says "Figure 0.1". No where else is a label made called 4_specs. Those are the only references broken in the entire paper. Why is LaTeX doing this?

Werner
  • 603,163
  • 2
    Welcome to the site! This is a frequent mistake: the label needs to go after your caption. It takes a lot to confuse LaTeX :) – cmhughes Nov 07 '13 at 00:41
  • @cmhughes Thank you for your quick response, but that is actually the way I originally had it. I just switched it back and the error persists. – zzembower Nov 07 '13 at 00:44
  • You can have a look on our starter guide to familiarize yourself further with our format. In particular, please provide a complete minimal working example (MWE) to help us diagnose the issue – cmhughes Nov 07 '13 at 00:46
  • Don't put your caption inside a \parbox... Not sure why you do that in the first place. Can you explain? – Werner Nov 07 '13 at 01:00
  • @Werner Well you are correct in why it was not working, however I was using a \parbox because my caption is really long and is too wide when compared to both my figure and paragraph beneath it. Can you suggest another solution? – zzembower Nov 07 '13 at 01:05
  • Nevermind, the solution is to put the \label in the \parbox after the \caption . Thanks everyone for your help – zzembower Nov 07 '13 at 01:08
  • On another note: It is not a good idea to have very long captions (it disturbs the reading); give the long explanation outside the caption and then have a short one in it. – Svend Tveskæg Nov 07 '13 at 05:42

1 Answers1

5

The following MWE replicates your problem:

enter image description here

\documentclass{article}
\begin{document}
\section{A section}\subsection{A subsection}
See Figures~\ref{fig:A} and~\ref{fig:B}.
\begin{figure}
  % <your figure>
  \caption{A caption}\label{fig:A}
\end{figure}
\begin{figure}
  % <your figure>
  {\caption{A caption}}\label{fig:B}
\end{figure}
\end{document}

Note that Figure 2 has its caption placed inside a group {...}. That similar to putting a \caption inside a \parbox. When you issue a \label, LaTeX updates a macro called \@currentlabel that contains the last-updated counter. After a group is closed, macro-updates contained within are usually restored, leaving the reference to be incorrect. For a general discussion regarding the \label-\ref system, see Understanding how references and labels work.

If the motivation for setting the \caption in a \parbox is to fix the width, consider using the caption package instead. It provides a width key-value that may provide exactly what you're after (see section 2.4 Margins and further paragraph options, p 11 or the caption documentation). Of course, it also provides a host of other functionality.

Here is a caption-approach to the MWE:

\begin{figure}
  % <your figure>
  \captionsetup{width=<len>}% Constrain caption width to <len>
  \caption{<your caption>}\label{fig:B}
\end{figure}

Or you could set the key-values globally in your document preamble.

Werner
  • 603,163