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.

I have a document where labels occur inside of sections, i.e., not only at the beginning of a section. If the section contains environments, then autoref wronlgy uses for all following labels the name of this environment, but still uses the correct numbering of the section. Moving the label definitions to the top of the section is not an option in my setup. Here is a minimal example:

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\section{ABC}
\section{DEF}
\label{sec:DEFup}

Correctly is section 2: \autoref{sec:DEFup}

\begin{figure}
  \centering
  XXX
  \caption{Three X's}
\end{figure}

\label{sec:DEFdown}
\noindent
Should be section 2, but is Figure 2, which does not even exist:
\autoref{sec:DEFdown}

\end{document}
share|improve this question
    
I recommend to insert labels exactly after the item which it is related to. So use \label{} after the \section{} or after the \caption{}. –  Sigur Feb 11 '13 at 20:22
1  
A label far from a place where a counter is stepped should be used only with \pageref –  egreg Feb 11 '13 at 20:31
    
i think this can be fixed by inserting \phantomsection just before the second \label. –  barbara beeton Feb 11 '13 at 20:35
    
\phantomsection did the trick. Thanks! –  berg Feb 11 '13 at 22:08
add comment

1 Answer

The behavior you describe as erroneous is, in fact, entirely correct according to the hyperref syntax rules: the argument of a \label command is "attached" to the most recently encountered entity that can receive a label.

In your example, the second \label command is encountered after a figure environment is set up and is given a caption. Thus, the hyperref package (which provides the autoref command) must assign the label sec:DEFdown to the figure environment. The fact that the label string in the example contains the substring "sec:" does not affect the element to which it will be assigned.

The uptake:

  • Always aim to issue \label directives immediately following the associated sectioning or captioning command. Keep in mind that environments such as equations can also be assigned \labels for cross-referencing purposes.
  • In the case of floats such as figures and tables, be sure to provide the \label command before the float environment ends -- but also, of course, after the float's \caption command is issued.
share|improve this answer
    
I'm not sure the behavior is entirely correct... if it were shouldn't the \autoref{sec:DEFdown} in the example give »Figure 1« instead of »Figure 2«? –  cgnieder Feb 11 '13 at 21:06
    
@cgnieder - If the second \label instruction were contained inside the figure environment -- which is something I believe we can all agree should be done anyway -- then \autoref{sec:DEFdown} would indeed generate "Figure 1" rather than "Figure 2". The problem arises because the OP has placed the second \label command after \end{figure}, leading autoref astray. Whether it's possible (or even desirable) to make autoref robust to such behavior by users is something I won't even attempt to answer... –  Mico Feb 11 '13 at 21:57
    
agreed that the label should be placed immediately after \section or \caption or only be referenced with \pageref and I'm aware why the problem arises... but I still believe that the result »Figure 2« is at least unexpected. But it's probably not worth changing \autoref because of this. –  cgnieder Feb 11 '13 at 22:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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