4

I used \autoref throughout my thesis and when doing corrections and needing to keep track of them using the todonotes package, I noticed that the \autoref function generates wrong labels.

To show what I mean, here's MWE:

\documentclass{article}
\usepackage{hyperref}
\usepackage{todonotes}

\begin{document}
\section{ABC}
\section{DEF}
\section{EFG}
\section{HIJ}

\begin{figure}
\centering
XXX
\caption{Three Xs}
\label{fig:XXX}
\end{figure}

\begin{figure}
\centering
YYY
\caption{Three Y's \todo[inline]{remove the apostrophe}}
\label{fig:YYY}
\end{figure}

Should be Figure 1: \autoref{fig:XXX}


Should be Figure 2: \autoref{fig:YYY}

\end{document}

This is what it generates: MWE problem with autoref

I read around and it seems that it may be doing that because as soon as you insert any todonotes in the caption, it thinks that it's the main body and then the following label then refers to the section (but somehow keeps the figure's counter). This is clearly wrong.

Putting label before \caption doesn't work. Putting all todonotes after \caption is not suitable as the corrections are WITHIN caption.

Could anybody suggest a solution, please?

Many thanks, Nickolai.

2

Here's a suggestion that's not a direct solution to the problem with \autoref but, instead, a robust alternative to using \autoref.

Load the cleveref package with the options noabbrev, nameinlink, and capitalize (capitalise works too...), and issue the instruction

\cref{fig:YYY}

instead of \autoref{fig:YYY}.

(The options noabbrev, nameinlink, and capitalize serve to make the output of \cref look like that of \autoref.)

enter image description here

\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage{todonotes}
\usepackage[noabbrev,nameinlink,capitalize]{cleveref}
\begin{document}
\begin{figure}
\centering
XXX
\caption{Three Xs}
\label{fig:XXX}
\end{figure}

\begin{figure}
\centering
YYY
\caption{Three Y's \todo[inline]{remove the apostrophe}}
\label{fig:YYY}
\end{figure}
Should be Figure 1: \autoref{fig:XXX}, \cref{fig:XXX}

Should be Figure 2: \autoref{fig:YYY}, \cref{fig:YYY}
\end{document}
  • Awesome!!! I should have used the cleverref package to start with (there were few places with multiple references where I had to put the names in manually and when my supervisor said that section should be in capital Section - I had renew the label as per manual and to be careful throughout the whole thesis). What I did now is a cheat: \usepackage[noabbrev, capitalise, nameinlink]{cleveref} \renewcommand{\autoref}{\cref} Seems to be working ;-) – Nickolai Jan 29 '15 at 9:58
  • 1
    @Nickolai - On redefining \autoref to make it work like \cref: I would suggest either \let\autoref\cref or \renewcommand{\autoref}[1]{\cref{#1}}. Alernatively, in your text editor, do a global search-and-replace of "autoref" for "cref", making a redefinition of \autoref unnecessary. – Mico Jan 29 '15 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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