0

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.)

1 Answer 1

2

Define the missing command:

\documentclass{article}
\usepackage{minted} % Defines the listing float
\usepackage{subcaption}
\DeclareCaptionSubType{listing} % Allow sublistings
\usepackage{hyperref} % Including this breaks compilation
\newcommand\theHlisting{\arabic{listing}}
\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}
2
  • Works like a charm! Can you explain what happens when importing hyperref that causes this issue? Jun 25, 2019 at 18:01
  • 1
    minted creates the listing environment with the (old) float package which doesn't prepare counters for hyperref, the newer subcaption does it for the subcounter, but doesn't check if the \theH... definition for the main counter exists, and so the definition is missing. Jun 25, 2019 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .