使用 smartef 引用自訂清單 (\lstnewenvironment)

使用 smartef 引用自訂清單 (\lstnewenvironment)

我使用 smartef 的\cref命令進行引用,並\lstnewenvironment建立一個新的清單類型。我的文件有多種類型的列表,每種類型都有自己的計數器。

然而,cleveref 堅持調用所有類型的列表,儘管我為自訂列表類型listing定義了 a 。\crefname

如何使用正確的標籤引用自訂清單?

最小工作範例:

\documentclass{article}
\usepackage{listings,cleveref}

% Define listing type for queries
\newcounter{query}
\makeatletter
\lstnewenvironment{query}[1][]
{%
  \lstset{#1}%
  \renewcommand\lstlistingname{Query}%
  \let\c@lstlisting=\c@query%
  \let\thelstlisting=\thequery%
}
{}
\makeatother

% Set up reference label
\crefname{query}{query}{queries}

\begin{document}

\begin{lstlisting}[label=lst:MyListing,caption=My listing]
  # Hello world
\end{lstlisting}

\begin{query}[label=qry:MyQuery,caption=My query]
  SELECT * FROM MyTable;
\end{query}

Reference to \cref{lst:MyListing} and \cref{qry:MyQuery}.
\end{document}

答案1

您可以強制listings使用正確的計數器而不是預設的計數器lstlisting

\documentclass{article}
\usepackage{etoolbox,listings,cleveref}

% Define listing type for queries
\newcounter{query}
\makeatletter
\lstnewenvironment{query}[1][]
 {%
  % make \lst@MakeCaption use query instead of lstlisting
  \patchcmd{\lst@MakeCaption}{{lstlisting}}{{query}}{}{}%
  \lstset{basicstyle=\ttfamily,columns=fullflexible,#1}%
  \renewcommand\lstlistingname{Query}%
  \let\thelstlisting=\thequery
 }
 {}
\makeatother

% Set up reference label
\crefname{query}{query}{queries}

\begin{document}

\begin{lstlisting}[label=lst:MyListing,caption=My listing]
  # Hello world
\end{lstlisting}

\begin{query}[label=qry:MyQuery,caption=My query]
  SELECT * FROM MyTable;
\end{query}

\begin{lstlisting}[label=lst:MyListing2,caption=My listing]
  # Hello world
\end{lstlisting}

\begin{query}[label=qry:MyQuery2,caption=My query]
  SELECT * FROM MyTable;
\end{query}

Reference to \cref{lst:MyListing} and \cref{qry:MyQuery}.

Reference to \cref{lst:MyListing2} and \cref{qry:MyQuery2}.
\end{document}

在此輸入影像描述


修復與合作hyperref

\documentclass{article}
\usepackage{etoolbox,listings,hyperref,bookmark,cleveref}

% Define listing type for queries
\newcounter{query}
\makeatletter
\patchcmd{\lst@MakeCaption}{{lstlisting}}{{\verborgh@counter}}{}{}%
\def\verborgh@counter{lstlisting}
\def\verborgh@prefix{L}
\AtBeginDocument{%
  \patchcmd{\theHlstnumber}{\thelstnumber}{\verborgh@prefix\thelstnumber}{}{}%
}

\lstnewenvironment{query}[1][]
 {%
  % make \lst@MakeCaption use query instead of lstlisting
  \def\verborgh@counter{query}%
  \def\verborgh@prefix{Q}%
  \lstset{basicstyle=\ttfamily,columns=fullflexible,#1}%
  \renewcommand\lstlistingname{Query}%
 }
 {}
\makeatother

% Set up reference label
\crefname{query}{query}{queries}

\begin{document}

\begin{lstlisting}[label=lst:MyListing,caption=My listing]
  # Hello world
\end{lstlisting}

\begin{query}[label=qry:MyQuery,caption=My query]
  SELECT * FROM MyTable;
\end{query}

\begin{lstlisting}[label=lst:MyListing2,caption=My listing]
  # Hello world
\end{lstlisting}

\begin{query}[label=qry:MyQuery2,caption=My query]
  SELECT * FROM MyTable;
\end{query}

Reference to \cref{lst:MyListing} and \cref{qry:MyQuery}.

Reference to \cref{lst:MyListing2} and \cref{qry:MyQuery2}.
\end{document}

相關內容