# \autoref doesn't work correctly when used in tufte-book

i want to use the \autoref command (from the hyperref package) in the document-class tufte-book.

mini-example:

\documentclass{tufte-book}
\usepackage{hyperref}

\begin{document}

\section{Testsection}
\label{sec:test}

\begin{figure}
\rule{20pt}{20pt}
\caption{Testfigure}
\label{fig:test}
\end{figure}

Cross Reference to \autoref{fig:test}

\end{document}


The Output is

Cross Reference to section 1

and not

Cross Reference to figure 1

what one could expect. any ideas how to fix this?

-
one ugly way to fix this is load \usepackage[compatibility=false]{caption} before the hyperref package. The problem seems to be that tufte and hyperref battle for supremacy. I'm sure a guru can explain the details :) – cmhughes Mar 8 '13 at 20:09
it seems to work :) thank you – Christian Mar 8 '13 at 20:13
Did you try \caption{Testfigure\label{fig:test}}, i.e. putting the \label into the caption text? – Axel Sommerfeldt Mar 8 '13 at 20:44
makes no difference – Christian Mar 8 '13 at 22:14
@cmhughes Could you make that an answer? – Joseph Wright Aug 10 '13 at 7:10

One ugly way to fix this is load

 \usepackage[compatibility=false]{caption}


before the hyperref package. The problem seems to be that tufte and hyperref battle for supremacy.

-
Alas, this results in larger fonts used for the captions, and they're no longer done in the same justification style, for instance. – torek Jan 1 at 18:33

I have a somewhat elaborate patch for this, which works for my test cases (they are decidedly not thorough, though). The problem ultimately boils down to the internal use of the \@caption macro. I borrowed a patch idea from someone else (the link has escaped me already) and this two part patch is also not pretty, although you can put it into your tufte-common-local.tex file to keep it out of the way and avoid the need for \makeatletter.

(This also should really check whether hyperref was not disabled, in which case it could be put directly into the tufte-common.def file, if it doesn't have additional bugs.)

Here's the two-part patch:

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox

% tufte \@caption doesn't invoke the hyperref bits.
% The fix is two parts, one to set up the type and
% one to provide the anchor.  It takes two patches
% to put this in...
\pretocmd{\@caption}%
{\expandafter\ifx\csname if@capstart\expandafter\endcsname
\csname iftrue\endcsname
\global\let\@currentHref\hc@currentHref
\else
\hyper@makecurrent{\@captype}%
\fi
\@ifundefined{NR@gettitle}{%
\def\@currentlabelname{#2}%
}{%
\NR@gettitle{#2}%
}%
}
{\typeout{patched captioning, part 1}}% success msg
{\typeout{HELP - failed to patch captioning, part 1}}% fail msg
\patchcmd{\@caption}%
{\ignorespaces#3\par}% search
{\ignorespaces
\ifHy@nesting
\expandafter\hyper@@anchor\expandafter{\@currentHref}{#3}%
\else
\expandafter\hyper@@anchor\expandafter{%
\@currentHref
}{\relax}%
}%
#3\fi\par}% replace
{\typeout{patched captioning, part 2}}% success msg
{\typeout{HELP - failed to patch captioning, part 2}}% fail msg


I don't properly understand all the hyperref code in the first place: I cobbled this together by comparing the internal definitions of \@caption in the two files (hyperref.sty and tufte-common.def).

-

It seems to work well with cleveref.

\documentclass{tufte-book}
\usepackage{hyperref}
\usepackage{cleveref}

\crefname{figure}{figure}{figures} % cleveref by default has fig.'
\Crefname{figure}{Figure}{Figures}

\setcounter{secnumdepth}{1}

\begin{document}

\chapter{Testchapter}

\section{Testsection}
\label{sec:test}

\begin{figure}
\rule{20pt}{20pt}
\caption{Testfigure}
\label{fig:test}
\end{figure}

Cross reference to \cref{fig:test}
and to \cref{sec:test}

\end{document}
`

-