Cleveref를 사용하여 사용자 정의 목록 참조(\lstnewenvironment)

Cleveref를 사용하여 사용자 정의 목록 참조(\lstnewenvironment)

\cref나는 참조하고 \lstnewenvironment새로운 목록 유형을 생성하기 위해 Cleveref의 명령을 사용합니다 . 내 문서에는 여러 유형의 목록이 있으며 각 목록에는 자체 카운터가 있습니다.

그러나 Cleveref는 사용자 정의 목록 유형 listing에 대한 정의에도 불구하고 모든 유형의 목록을 호출해야 한다고 주장합니다.\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}

관련 정보