I'm using the subcaption
package to define sublistings that I can put in my listing float environments. The listing float is defined by minted
.
This has all been working well so far (see full code example below).
However, when I want to turn my references into hyperlinks with hyperref
, the sublisting references fail with errors like this:
! Undefined control sequence.
<argument> ... \Hy@tempb {sublisting.\theHlisting
.1}\ifx \Hy@tempa \ltx@emp...
l.33 Here are references to Lst.~\ref{lst:foo}
, Lst.~\ref{lst:bar}, and
It seems that there is some interaction between the packages that I don't understand. What do I need to do to get hyperref
references to sublistings?
Self-contained example:
\documentclass{article}
\usepackage{minted} % Defines the listing float
\usepackage{subcaption}
\DeclareCaptionSubType{listing} % Allow sublistings
\usepackage{hyperref} % Including this breaks compilation
\begin{document}
\begin{listing}
\begin{sublisting}{.5\textwidth}
\begin{minted}{Python}
def foo():
pass
\end{minted}
\caption{First function.}
\label{lst:foo}
\end{sublisting}\hfill%
\begin{sublisting}{.5\textwidth}
\begin{minted}{Python}
def bar():
pass
\end{minted}
\caption{Second function.}
\label{lst:bar}
\end{sublisting}
\caption{Two functions.}
\label{lst:foobar}
\end{listing}
Here are references to Lst.~\ref{lst:foo}, Lst.~\ref{lst:bar}, and
Lst.~\ref{lst:foobar}.
\end{document}
(needs pygmentize
to be installed. Compile with latexmk -shell-escape -pdf
.)