5

I created an environment, Procedure, using the package tcolorbox. The procedures are numbered by \thechapter-\theproc and I would like to use cleveref to refer to them.

Here is a minimum working example in which I can't figure out how to cross-reference \label{proc:One} using \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}

2 Answers 2

6

Mico's answer is good, here is another possibility!

The cleveref features are already provided by tcolorbox, using the crefname= or Crefname= options and label= as option for the label. The Procedure tcolorbox can be slightly redefined.

\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
  • It works on the Magnum Opus, with a zillion packages including Hyperref! Only one question, out of curiosity: I wanted to try and get, say, Procedure 3.4 boldfaced and have only A TITLE in italics. Didn't make it. What happened, I think, is that I have: \newtcolorbox[use counter=proc,number within=chapter,crefname={procedure}{procedures},Crefname={Procedure}{Procedures}]{Procedure}[2][]{% enhanced,arc=0mm,colframe=DarkBlue,colback=LightBlue,coltitle=black,title={\textbf{\textit{Procedure \thetcbcounter. }}#2},#1}
    – schremmer
    Feb 7, 2016 at 2:10
  • @schremmer: Try title={\textbf{Procedure \thetcbcounter. }\textit{#2}}. But the black font on DarkBlue background is hard to read -- I don't recommend this
    – user31729
    Feb 7, 2016 at 10:12
4

You need to inform cleveref how to "label" cross-references to items associated with the counter proc. One way to do this is use the instruction

\crefname{proc}{Procedure}{Procedures}

The second and third arguments of the \crefname directive should contain the singular and plural forms of the "label".

In the code below, I've also streamlined and simplified the overhead associated with each instance of a Procedure environment.

enter image description here

\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}
8
  • I had tried \crefname but while there indeed was no ??, I still did not get "Procedure 1-1", only what you got, namely "Procedure 1".
    – schremmer
    Feb 6, 2016 at 3:58
  • 1
    @schremmer - Sorry, I had missed the fact that you want the chapter number to show up in cross-references to Procedure environments. I've updated the code to meet your objective. I've also streamlined it a bit to reduce the overhead associated with each instance of a Procedure.
    – Mico
    Feb 6, 2016 at 5:26
  • 1
    \newtcolorbox[auto counter, number within=chapter]{Procedure}... will provide the whole counting 'system'. The bad thing about this, that the internal name is different, something like \tcbcounter holds the name.
    – user31729
    Feb 6, 2016 at 7:56
  • @ChristianHupfer - Thanks for this pointer. I knew about the auto-counter option, but decided to stick with (but also streamline) the OP's approach that uses an external counter variable.
    – Mico
    Feb 6, 2016 at 8:08
  • @Mico: No problem -- I even forgot that there's a crefname option etc. already provided by tcolorbox
    – user31729
    Feb 6, 2016 at 8:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .