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 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?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

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}
share|improve this answer
    
Addendum: Since v3.3 of the caption package all six load orders should work fine now. See also: latex-community.org/forum/viewtopic.php?f=46&t=20075 –  Axel Sommerfeldt May 1 '13 at 18:05
add comment

I can't see any problems?

%pdflatex --shell-escape     
\documentclass{article}
\usepackage{caption}
\usepackage{minted}
\usepackage{hyperref}

\begin{document}
text
\clearpage
text \ref{verb:1}
\begin{listing}
\begin{minted}{xml}
<?xml version="1.0" encoding="UTF-8"?>
\end{minted}
\caption{test minted}\label{verb:1}
\end{listing}
text
\clearpage
text \ref{verb:1}
\end{document}

Versions:

caption: v3.1m
minted: v1.7
hyperref: v6.82g
share|improve this answer
    
Thank you for answering my question Marco! Your code works fine for me too so I made more tests and edited the question according to my results. Can you reproduce the behaviour I'm having (see my edit)? –  albert May 30 '11 at 14:55
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.