[Tex/LaTex] minted’s listing environment with hyperref and caption package together

captionshyperrefincompatibilityminted

I use the minted package for code highlight. I use its listing environment.

I also use the hyperref package to provide links all through my document: figures, tables, listings, etc.

If I import the caption package the listing links do not work properly. They take you to the begining of the document. All other links except listing links work fine.

If I do not import the caption package, all links work fine.

There is an issue opened at the project home page: Incompatibility with caption package but it seems to point to a compilation problem.

Is there any way to make the three packages work together?

EDIT:

I compiled Marco's answer and It worked like a charm so it kept me thinking about it. I have made some tests and I have come to the conclusion that it's in the order of the imports. These are my results:

\documentclass{report}

% it's all in the order of the imports

% this works
\usepackage{caption}
\usepackage{minted}
\usepackage{hyperref}

% this works
%\usepackage{minted}
%\usepackage{hyperref}
%\usepackage{caption}

% this works
%\usepackage{minted}
%\usepackage{caption}
%\usepackage{hyperref}    

%this does not work
%\usepackage{hyperref}
%\usepackage{caption}
%\usepackage{minted}

%this does not work
%\usepackage{caption}
%\usepackage{hyperref}
%\usepackage{minted}

%this does not work
%\usepackage{hyperref}
%\usepackage{minted}
%\usepackage{caption}

% I'm thinking that if hyperref is imported before minted it doesn't work

%however this does work (not importing caption at all, yet hyperref is imported before minted)
%\usepackage{hyperref}
%\usepackage{minted}

\begin{document}

\chapter{test}

\clearpage

\begin{listing}[H] \mint{cl}/(car (cons 1 2))/
\caption{Example of a listing.}
\label{lst:example}
\end{listing}
\clearpage

Listing \ref{lst:example} contains an example of a listing.

\end{document}

So, my question is… Why is this happening?

EDIT 2:

In theory, the order of loading packages with \usepackage in principle doesn't matter. Real life, however…

Hyperref has the nasty property
that sometimes it conflicts when it is
put earlier, sometimes when it is put
later than other widely used packages

So, is it fair to blame Hyperref for this?

Best Answer

As you already have found out the problem occurs every time the hyperref package will be loaded prior to the minted package. This is happening here:

  1. The hyperref package will be loaded. It does not detect the float package (because the float package is not loaded yet), and therefore it does not patch the float package code.

  2. The minted package will be loaded which loads the float package, and does a \newfloat.

  3. The caption package will be loaded. It does detect the float and hyperref packages, and assumes that the float package code was patched by hyperref. But this assumtion is false in this case.

So as solution you could load the float package prior to the hyperref package and the minted package after the hyperref package - which BTW is the correct load order anyway, see hyperref's README for details.

\documentclass{report}

% it's all in the order of the imports

% this one is the correct one:

% 1. Load the float package
\usepackage{float}

% 2. Load the hyperref package; hyperref will patch the float package
\usepackage{hyperref}

% 3. Load the minted package which will use the patched \newfloat now
\usepackage{minted}

% The load order of the caption package is (or should be) irrelevant now
\usepackage{caption}

\begin{document}

\chapter{test}

\clearpage

\begin{listing}[H] \mint{cl}/(car (cons 1 2))/
\caption{Example of a listing.}
\label{lst:example}
\end{listing}
\clearpage

Listing \ref{lst:example} contains an example of a listing.

\end{document}