Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

Currently my document labels figures in a continuous manner (figure 1, figure 2, etc.) but references to these same figures have the section, subsection, etc. number in front of it (3.1.1 for a figure in the 3rd section and 1st subsection). I want the references to my figures to number in the same continuous fashion as the figures themselves (i.e. figure 1, figure 2, no matter what section the figures are in). I tried using \counterwithout{figure}{section} and \counterwithout{figure}{subsection} but the 3.1.1 style of figure referencing continues.

Here is a minimum working example that reproduces the problem:

\documentclass{article}
\usepackage{chngcntr}
\counterwithout{figure}{section}
\counterwithout{figure}{subsection}

\usepackage{graphicx}               

\begin{document}
\section{section}
\subsection{subsection}

Figure~\ref{fig:foo}
\begin{figure}
\label{fig:foo}
\includegraphics[width=6in]{foo.ps}
\caption{sample text}
\end{figure}

\end{document}
share|improve this question
add comment

marked as duplicate by Werner, ChrisS, Guido, Thorsten, Jesse Feb 15 at 8:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 6 down vote accepted

In article that's the default behaviour, so you don't have to do anything special. In your case you experienced an odd result because you were misplacing \label. Always put \label after \caption in floats (or, inside \caption, but never before it.):

\documentclass{article}
\usepackage[demo]{graphicx}               

\begin{document}
\section{section}
\subsection{subsection}

Figure~\ref{fig:foo}
\begin{figure}[!ht]
\includegraphics[width=6in]{foo.ps}
\caption{sample text}
\label{fig:foo}
\end{figure}

\end{document}

enter image description here

The number you are getting is the number of the subsection; you used \label before \caption, so the last anchor (the one of the subsection) was picked to generate the cross-reference.

The demo option for graphicx simply replaces actual figures with black rectangles; do not use that option in your actual document.

share|improve this answer
    
You could also place the \label macro inside the \caption macro. That removes having to remember which order they go in. –  Mike Renfro Feb 15 at 1:53
    
@MikeRenfro true. I've added this remark. Thanks. –  Gonzalo Medina Feb 15 at 1:54
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.