# Label and use custom reference for a colorbox

I am trying to reference a tcolorbox that I have created within my latex document. Ideally the in-text reference will have the form BOX 1.1, BOX 1.3, BOX, 3.1 etc. based on the section.

To create my box I followed the example N 2014-10-10 on page 21 of their document. Also, on page 108, of the same document, there is an example of how to label the box but I cannot really make it work.

Can someone please help? Thank you very much.

\usepackage{wrapfig}
\usepackage{tcolorbox}

\begin{wrapfigure}{r}{0.5\textwidth}
\begin{tcolorbox}[title=Box 1 $\mid$ Timeline,
colback=blue!5!white,
colframe=blue!75!black,
colbacktitle=cyan!50!green,
coltitle=blue!25!black,
fonttitle=\bfseries,
subtitle style={boxrule=0.4pt,colback=cyan!50!red!25!white}]
\tcbsubtitle{1690}
TEXT.
\tcbsubtitle{1896}
TEXT 1.
\tcbsubtitle{1925}
TEXT 2.
\tcbsubtitle{1967}
TEXT 3.
\tcbsubtitle{2000}
TEXT 4.
\end{tcolorbox}
\end{wrapfigure}


## 1 Answer

I would suggest the following:

\documentclass{article}

\usepackage{wrapfig}
\usepackage{tcolorbox}
\usepackage{cleveref}

\newtcolorbox[auto counter,number within=section,crefname={box}{boxes}]{pabox}[2][]{%
title=Box 1 $\mid$ Timeline,
colback=blue!5!white,
colframe=blue!75!black,
colbacktitle=cyan!50!green,
coltitle=blue!25!black,
fonttitle=\bfseries,
subtitle style={boxrule=0.4pt,colback=cyan!50!red!25!white},title=Box ~\thetcbcounter $\mid$ #2, label={#1}}
\begin{document}

\section{section title}
\begin{wrapfigure}{r}{0.5\textwidth}
\begin{pabox}[mybox]{Timeline}
\tcbsubtitle{1690}
TEXT.
\tcbsubtitle{1896}
TEXT 1.
\tcbsubtitle{1925}
TEXT 2.
\tcbsubtitle{1967}
TEXT 3.
\tcbsubtitle{2000}
TEXT 4.
\end{pabox}
\end{wrapfigure}
Here is a reference with cleveref \cref{mybox} and a capitalized reference \Cref{mybox} and another without cleveref: \ref{mybox}

\end{document}


In the preamble of the document I have used the \newtcolobox command to define a new type of colored box named pabox. The definition of the layout is the same as in the question. I have additionally added the options auto counter and number within=sections that allow automated numbering of the boxes according to the numbering scheme, that was requested.

Additionally, the title of the box is set 'semi-automatically' with Box and the current number being printed automatically and the text behind the | that can be added manually for every box.

In the first optional argument of pabox one can add a label that can be used with the usual referencing command like ref.

EDIT: To automatically print the 'type' in the reference as well (box 1.1 instead of 1.1), I would recomment the package cleveref, that I have included in the edited MWE above. Additionally, I have used the option crefname={box}{boxes} to tell cleveref how to 'call' the boxes in the reference.

To reference the boxes in the text, including their type, one can use \cref{<label>} while \ref{<label>} still only prints their number. Please also keep in mind the command \Cref{<label>} that capitalizes the first letter of the type (usefull for beginnings of sencences).

Addition: hyperref can as well be incorporated in this solution to turn references into clickabe hyperlinks. Please keep in mind the correct loading order, as cleverref is one of the exceptions that has to be loaded after hyperref.

• Ah, I see. So it's better to use a common design before I start the main article and then call the design every time I want to use a colorbox. Also, could you please let me know if there is a way to use custom reference. In your example to look like 'Here is a reference: BOX 1.1'. Thank you very much. Nov 26 '17 at 14:32
• @Jespar: If you want to use the same design over and over again, it is indeed easier to define it only once in the preamble. I have also edited my answer according to your question. Nov 26 '17 at 14:51
• Your answer, through my 4 years across SE, easily is amongst the top-5 I have seen. Informative and to the point. Nice work! Nov 26 '17 at 14:53