With the package hyperref
you can use the optional argument of \hyperref
to reference a \label
with arbitrary text:
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\section{Hello World}
\label{sec:hello}
Reference to \hyperref[sec:hello]{this section}.
\end{document}
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
.
Best Answer
Taking the excellent answer by egreg (Labeling a text and referencing it later) and modifying it to take an additional optional argument I could come up with the solution below. It creates a new command
\labeltext
that can be referenced to. It takes two parameters and one optional parameter:If you want to, you can set the markup or highlighting of the label and (optionally) the reference, too. I set it to
\emph
rather than\underline
as underlining looks bad to me and is considered bad practice.MWE: