Referindo-se com intelligentef a uma listagem personalizada (\lstnewenvironment)

Referindo-se com intelligentef a uma listagem personalizada (\lstnewenvironment)

Eu uso o comando do intelligentef \crefpara fazer referência e \lstnewenvironmentpara criar um novo tipo de listagem. Meu documento possui vários tipos de listagens, cada uma delas com seu próprio contador.

No entanto, o intelligentef insiste em chamar todos os tipos de listagens listing, apesar da minha definição de a \crefnamepara os tipos de listagens personalizadas.

Como posso me referir com o rótulo correto a uma listagem personalizada?

Exemplo mínimo de trabalho:

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

Responder1

Você pode forçar listingso uso do contador correto em vez do padrão 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}

insira a descrição da imagem aqui


Correção para cooperação comhyperref

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

informação relacionada