2

While \cref{vip:1-4}---where vip refers to a tcolorbox---will typeset as Alert 1-4 ∞ is not a number and \labelcref will typeset as 1-4 ∞ is not a number, occasionally I would like to go one step further and typeset only ∞ is not a number.

Here is a MWE:

\documentclass[11pt]{book}
\usepackage[hyperfootnotes=false]{hyperref}%
\usepackage{cleveref}%
\usepackage{tcolorbox}%
    \newcounter{vip}% 
    \newtcolorbox%
    [%
    use counter= vip,
    number within=chapter,
    number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitlealert},%.
    crefname={Alert}{Alert},
    Crefname={Alert}{Alerts},
    ]%
    {mportant}%
    [2][]%
    {%
    detach title,%
    before upper={\tcbtitle\quad},
    fonttitle=\bfseries,
    coltitle=black,
    code={\gdef\mytitlealert{#2}},%
    title=ALERT \thetcbcounter,%
    #1,
    }%
\begin{document}
\chapter{One}
\begin{mportant}[label={vip:1-4}]{$\infty$ is not a number}
\hspace{-3mm}and we cannot compute with $\infty$.
\end{mportant}
\chapter{Two}
We cannot input $\infty$ because \labelcref{vip:1-4}  (\Cpageref{vip:1-4})
\end{document}

and, occasionally, I would like not to have the reference number 1.1 at the beginning of the reference in:

enter image description here

1 Answer 1

3

I suggest to use the nameref= option of tcolorbox (introduced on 2016/6/22 (the CHANGES file says 2016/7/14, however), version 3.94) and \nameref instead of \labelcref or \cref here since the cleveref feature is not necessary for this special title.

I used nameref={#2} since basically the title should be displayed and nothing else.

\documentclass[11pt]{book}
\usepackage[hyperfootnotes=false]{hyperref}%
\usepackage{tcolorbox}%
\newcounter{vip}% 
\usepackage{cleveref}%


\newtcolorbox%
[%
use counter= vip,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitlealert},%.
crefname={Alert}{Alert},
Crefname={Alert}{Alerts},
]%
{mportant}%
[2][]%
{%
  detach title,%
  before upper={\tcbtitle\quad},
  fonttitle=\bfseries,
  coltitle=black,
  nameref={#2},
  code={\gdef\mytitlealert{#2}},
  title={ALERT \thetcbcounter},%
  #1,
}%
\begin{document}
\chapter{One}
\begin{mportant}[label={vip:1-4}]{$\infty$ is not a number}
\hspace{-3mm}and we cannot compute with $\infty$.
\end{mportant}
\chapter{Two}
We cannot input $\infty$ because \nameref{vip:1-4}  (\Cpageref{vip:1-4})
\end{document}

enter image description here

Just in case the tcolorbox version should be blatantly outdated, following by-pass solution by defining the \@currentlabelname (which is used by \nameref) in the code={} option can be applied -- of course, \makeatletter...\makeatother is necessary unless the definition of the box is written in a .sty or .cls file.

\documentclass[11pt]{book}
\usepackage[hyperfootnotes=false]{hyperref}%
\usepackage{tcolorbox}%
\newcounter{vip}% 
\usepackage{cleveref}%


\makeatletter
\newtcolorbox%
[%
use counter= vip,
number within=chapter,
number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}~\noexpand\mytitlealert},%.
crefname={Alert}{Alert},
Crefname={Alert}{Alerts},
]%
{mportant}%
[2][]%
{%
  detach title,%
  before upper={\tcbtitle\quad},
  fonttitle=\bfseries,
  coltitle=black,
  code={\gdef\mytitlealert{#2}\edef\@currentlabelname{#2}},%
  title={ALERT \thetcbcounter},%
  #1,
}%
\makeatother
\begin{document}
\chapter{One}
\begin{mportant}[label={vip:1-4}]{$\infty$ is not a number}
\hspace{-3mm}and we cannot compute with $\infty$.
\end{mportant}
\chapter{Two}
We cannot input $\infty$ because \nameref{vip:1-4}  (\Cpageref{vip:1-4})
\end{document}
3
  • Nice but my version is 2014/03/31 so I am first going to download the 2016/6/22 version but in a way so as to make sure I can go back if I break too much stuff. (I am using TeXLive-2013 under 10.6.8!) Will let you know.
    – schremmer
    Aug 17, 2017 at 20:11
  • 1
    @schremmer: Sigh! There's a 'harder' way to get the same result, but I can remember you wanted to update your system a long time ago ;-) What is 10.6.8.? MacOS?
    – user31729
    Aug 17, 2017 at 20:35
  • Thanks for your "just in case solution" but, and I can't really believe it, tcolorbox 2017-07-05 did not break the magnum opus which is still under MacOS 10.6.8 =Snow Leopard. And of course your "easy" solution does it. Still can't believe it.
    – schremmer
    Aug 17, 2017 at 20:59

You must log in to answer this question.

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