帶有化合物標籤的參考

帶有化合物標籤的參考

我使用包創建了一個環境,​​過程tcolorbox。這些程式已編號\thechapter-\theproc,我想cleveref用來引用它們。

這是一個最小的工作範例,其中我無法弄清楚如何\label{proc:One}使用\cref.

    \documentclass{book}

    \usepackage{tcolorbox}
    \newcounter{proc}[chapter]
    \newtcolorbox{Procedure}[1]{title=#1}
    \usepackage{cleveref}

    \begin{document}

    \chapter{First Chapter}
    \label{ch:First}

    \refstepcounter{proc}
    \label{proc:One}%
    \begin{Procedure}%
    {%begin title
    \emph{\textbf{Procedure \thechapter-\theproc. }}A TITLE%
    }
    Some stuff
    \end{Procedure}

    I would like to refer to the above procedure but neither

    \textbf{Procedure \cref{proc:One}} on \cpageref{proc:One}

    nor

    \textbf{Procedure \labelcref{proc:One}} on \cpageref{proc:One}

    give the hoped-for

    \textbf{Procedure 1-1} on page 1

    \end{document}

答案1

Mico的回答很好,還有另一種可能性!

這些cleveref功能已由 提供tcolorbox,使用crefname=Crefname=選項 和label=作為標籤選項。 tcolorboxProcedure可以稍微重新定義。

\documentclass{book}

\newcounter{proc}%[chapter]
\usepackage{tcolorbox}

\usepackage{cleveref}

\newtcolorbox[use counter=proc,number within=chapter,crefname={procedure}{procedures},Crefname={Procedure}{Procedures}]{Procedure}[2][]{%
  title={\textbf{\textit{Procedure \thetcbcounter. }}#2},#1}

\begin{document}


\chapter{First}
%\setcounter{chapter}{1} % just for this example

\begin{Procedure}[label={proc:One}]{A TITLE} 
Some stuff
\end{Procedure}

I would like to cross-reference the above procedure. 

Now, ``\cref{proc:One} on \cpageref{proc:One}'' and
``Procedure \labelcref{proc:One} on \cpageref{proc:One}''
both work and \cref{proc:Two} works two!

\chapter{Second}
\begin{Procedure}[label={proc:Two}]{And now for something completely different}
  Some stuff
\end{Procedure}


\end{document}

答案2

您需要告知cleveref如何「標記」與計數器關聯的項目的交叉引用proc。一種方法是使用指令

\crefname{proc}{Procedure}{Procedures}

指令的第二個和第三個參數\crefname應包含「標籤」的單數和複數形式。

在下面的程式碼中,我還簡化了與每個Procedure環境實例相關的開銷。

在此輸入影像描述

\documentclass{book}

\newcounter{proc}[chapter]
% prefix the 'chapter' counter to 'proc' counter
\renewcommand{\theproc}{\thechapter-\arabic{proc}}

\usepackage{tcolorbox}
% reduce the overhead needed to create title of procedure
\newtcolorbox{Procedure}[1]{%
  title=\textbf{\textit{Procedure \theproc. }}#1}

% automatically increment the 'proc' counter at start of each 'Procedure'
\usepackage{etoolbox}
\BeforeBeginEnvironment{Procedure}{\refstepcounter{proc}}

% Provide the singular and plural forms of label associated with 'proc' counter
\usepackage{cleveref}
\crefname{proc}{Procedure}{Procedures}

\begin{document}

\setcounter{chapter}{1} % just for this example

\begin{Procedure}{A TITLE} \label{proc:One}
Some stuff
\end{Procedure}

I would like to cross-reference the above procedure. 

Now, ``\cref{proc:One} on \cpageref{proc:One}'' and
``Procedure \labelcref{proc:One} on \cpageref{proc:One}''
both work.

\end{document}

相關內容