参考复合标签

参考复合标签

我使用包创建了一个环境,即过程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=作为标签的选项。Proceduretcolorbox 可以稍微重新定义。

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

相关内容