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:
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.
The minted package will be loaded which loads the float package, and does a \newfloat
.
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}
Here is a proposal using tcolorbox
. Colors, shadows, etc. can be adjusted to your liking. You may replace style=tcblatex
by any listings setting you want to have as default for your environment.
\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\newenvironment{CenteredShadowboxListing}[1][]{%
\tcbset{listing options={style=tcblatex,#1}}\tcbwritetemp}%
{\endtcbwritetemp%
\tcbox[enhanced,arc=0pt,outer arc=0pt,top=1mm,bottom=1mm,left=1mm,right=1mm,
boxrule=0.6pt,drop fuzzy shadow,before=\begin{center},after=\end{center}]%
{\tcbusetemplisting}}
\begin{document}
\lipsum[4]
\begin{CenteredShadowboxListing}
line of code
line of code
\end{CenteredShadowboxListing}
\lipsum[4]
\end{document}
UPDATE: With tcolorbox
version 2.41
of 2013/07/23
, the code to typset the example above can be written more compact with the same result:
\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}% version 2.41 or newer
\newtcblisting{CenteredShadowboxListing}[1][]{%
listing options={style=tcblatex,#1},hbox,listing only,
enhanced,arc=0pt,outer arc=0pt,top=1mm,bottom=1mm,left=1mm,right=1mm,
boxrule=0.6pt,drop fuzzy shadow,before=\begin{center},after=\end{center}}
\begin{document}
\lipsum[4]
\begin{CenteredShadowboxListing}
line of code
line of code
\end{CenteredShadowboxListing}
\lipsum[4]
\end{document}
Best Answer
The caption format will be used internally after applying the justification & font setting, and therefore the
\colorbox
will be centered but not the text inside the\colorbox
.One can fix that by applying the justification setting inside the colorbox (again) using the internal command
\caption@hj
:(Please note that I replaced
\textwidth
with\captionwidth
so the margin & width settings will be used here, too.)But a more naturally way of fixing this is changing the internal code which actually draws the box around the caption, i.e. redefining
\caption@parbox
:(Although both
\caption@hj
and\caption@parbox
are not documented this will work with future versions of thecaption
package, too.)When using version 3.3 of the
caption
package this can be realized without using internal commands at all:Addendum 2013-01-09: Since the version 3.3 of the
caption
package is available now the last solution is preferable.