超链接至子列表

超链接至子列表

我正在使用该subcaption包来定义可以放入列表浮动环境中的子列表。列表浮动由 定义minted

到目前为止一切运行良好(请参阅下面的完整代码示例)。

但是,当我想用​​ 将我的参考文献转换为超链接时hyperref,子列表参考文献会失败,并出现如下错误:

! 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

似乎包之间有一些我不理解的交互。我需要做什么才能获取hyperref对子列表的引用?


自包含示例:

\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}

(需要pygmentize安装。用 进行编译latexmk -shell-escape -pdf。)

答案1

定义缺少的命令:

\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}

相关内容