3

I have recently started working with the fabulous tcolorbox package and have successfully been able to generate the boxes I need with the label as an argument. However, I want to be able to use that label for referencing. Working from the manuals and this site (notably the answer at How to define labels within a tcolorbox?), I have got partway.

The correct box is generated with this code. However, the referencing is not working. In the MWE, I want the text to read "reference the below box as Test Box 1a" as the label I passed is "1a".

Instead, I am getting an error ! Use of \cref@override@label@type doesn't match its definition.

\documentclass[a4paper, twoside]{book}

\usepackage[most]{tcolorbox}
\usepackage{cleveref} 

\newtcolorbox{testbox}[2]{
  enhanced,
  label type=mytype,
  label = #1,
  title = Test~#1:~#2
}
\crefname{mytype}{Test Box}{Test Boxes}

\begin{document}

What I want is to be able to reference the below box as \cref{1a} (Should say \textit{Test Box 1a}).

\begin{testbox}{1a}{Title of first box}
The contents of the box
\end{testbox}

\end{document}

With further investigation (and the excellent explanation in the answer at How to cross-reference an unnumbered theorem using hyperref and cleveref), it looks like \label only works with a counter and not an arbitrary text label, so I might need to change my whole approach. But I would appreciate confirmation if this is true.

JenB
  • 205
  • Search the documentation of tcolorbox for cref, you'll find the appropriate key. Sorry, just checked, but it seems to be a bug as the crefname key of tcolorbox also produces the error. – TeXnician Jul 27 '18 at 20:44

1 Answers1

3

You are missing the counter definition:

\documentclass[a4paper, twoside]{book}

\usepackage[most]{tcolorbox}

\usepackage{cleveref}

\newcounter{mytype}
\crefname{mytype}{Test Box}{Test Boxes}

\newtcolorbox[use counter=mytype]{testbox}[2]{
  enhanced,
  label type=mytype,
  label = #1,
  title = Test~#1:~#2
}

\begin{document}

What I want is to be able to reference the below box as \cref{1a} (Should say \textit{Test Box 1a}).

\begin{testbox}{1a}{Title of first box}
The contents of the box
\end{testbox}

\end{document}

I have no idea how you actually want to number your boxes. The definition above will give 1 in the reference.

enter image description here

Ulrike Fischer
  • 327,261
  • 1
    Thank you for your answer. This had made me realise that I did not properly explain what I want to do. I don't want to number my boxes, I am using labels like {1a, 1b-1, 1b-2, 1c, 2..}, which is why I was passing the label as an argument. I will edit the question, but I'd appreciate you revisiting the answer if possible. – JenB Jul 28 '18 at 09:57