[Tex/LaTex] Hyperref and the float package

cross-referencingfloatshyperref

There is an issue when using the hyperref package, which provides links and bookmarks, together with the float package, which implements changes in float placement in the document.

Loading both has the side effect of making all links generated by hyperref lead to the first page of the document, instead of the appropriate page. The following MWE reproduces the issue:

\documentclass{article}

\usepackage{lipsum}

\usepackage[pdftex,
            bookmarks=true,
            breaklinks,
            pagebackref=true]{hyperref}
\usepackage{bookmark}

\usepackage{float}      
   \restylefloat{figure}
   \restylefloat{table} 


\begin{document}

\lipsum[20-27]

\begin{figure}
\centering
A figure.
\caption{Figure caption}
\label{label}
\end{figure}

\lipsum[3-8]

Link to figure: \ref{label}.
\lipsum[17]

\end{document}

In this example, the label reference leads to the first page, and not to the page where the figure actually is.

I haven't found any mentions to this in either the hyperref or the float documentations.

Best Answer

The solution to this issue is to load the float package before hyperref, issuing commands that affect floats after loading hyperref:

(...)
\usepackage{float}      

\usepackage[pdftex,
            bookmarks=true,
            breaklinks,
            pagebackref=true]{hyperref}
\usepackage{bookmark}

\restylefloat{figure}
\restylefloat{table} 

(...)

I'm not sure about why this behavior occurs, but I can assume that there is some command redefinition in float that breaks links generated by hyperref.

Related Question