3

My objective is exactly the same as in this question, however, I would like to achieve this same effect with \newtcbtheorem from the tcolorbox package. For example

\documentclass{article}

\usepackage{nameref}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}

\newtcbtheorem{thm}{Theorem}{}{thm}

\begin{document}

\begin{thm}{My awesome theorem}{awesome}
Isn't this theorem so awesome?
\end{thm}

This is a reference to \nameref{thm:awesome}.

\end{document}

should print "This is a reference to My awesome theorem.".

Unfortunately, the answer to the linked question no longer works if I use \newtcbtheorem instead of \newtheorem. Is there a way to modify that answer to work in this case?

(Note that wrapping the \begin{thm}\end{thm} created by a \newtheorem call in a \tcbenvironment is not an option for reasons not mentioned here.)

2 Answers 2

4

The reason is that \@currentlabelname is not defined which is written to the .aux by nameref's version of \label.

The easiest way to solve this is either a patch or slight redefinition, setting \@currentlabelname with {##2}, which expands later on to contain the theorem name. (A patch would also do!)

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{theorems}

\makeatletter


\renewcommand{\new@tcbtheorem}[5][]{%
  \@@newtcolorbox[auto counter,#1]{#2}[3][]{#4,%
    title={\tcb@theo@title{#3}{\thetcbcounter}{##2}},%
    list entry={\protect\numberline{\thetcbcounter}##2},%
    code={\gdef\@currentlabelname{##2}\tcb@theo@label{#5}{##3}},%
    ##1}%
  \@@newtcolorbox[#1,no counter,list inside=]{#2*}[2][]{#4,%
    title={\tcb@theo@title{#3}{\@empty}{##2}},%
    ##1}%
}
\makeatother




\usepackage{nameref}
\usepackage{hyperref}

\newtcbtheorem{thm}{Theorem}{}{thm}


\begin{document}

This is a reference to \nameref{thm:awesome} and \ref{thm:awesome}%

\clearpage
\begin{thm}{My awesome theorem}{awesome}
Isn't this theorem so awesome?
\end{thm}



\end{document}

enter image description here

4
  • Awesome. This seems to work. What is the \usepackage{xpatch} used for though (I removed this and it still seemed to work). Jun 21, 2016 at 8:05
  • 1
    @JonathanGleason: A remainder of a previous version -- it's not used here. Basically, it can be used for injecting code into other code with redefinition, but it would not be really shorter than the \renewcommand.
    – user31729
    Jun 21, 2016 at 8:08
  • I wonder if I should add \@currentlabelname or \NR@gettitle as standard for \new@tcbtheorem. It wouldn't hurt much to have it there, if nameref is not used. Making it an selectable option would be a little bit ugly... Jun 22, 2016 at 8:07
  • @ThomasF.Sturm: I would have written to you about this anyway. No, I think an option is not useful here.
    – user31729
    Jun 22, 2016 at 8:15
4

Credits go to Christian Hupfer for solving the requests in his answer.

I can add a shorter way to implement Christians solution:

\documentclass{article}

\usepackage{nameref}
\usepackage{hyperref}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}

\makeatletter
 \newtcbtheorem{thm}{Theorem}{code={\edef\@currentlabelname{#2}}}{thm}
%\newtcbtheorem{thm}{Theorem}{code={\NR@gettitle{#2}}}{thm}  % alternatively, if nameref is loaded
\makeatother

\begin{document}

This is a reference to \nameref{thm:awesome} and \ref{thm:awesome}%

\clearpage
\begin{thm}{My awesome test theorem}{awesome}
Isn't this theorem so awesome?
\end{thm}

\end{document}
3
  • Hm, I thought I had tried the code=... option too. It failed, perhaps there's was another error in my trials!
    – user31729
    Jun 22, 2016 at 8:16
  • @ChristianHupfer Possibly the #2/##2 trap? ;-) Jun 22, 2016 at 8:25
  • Possibly, yes ;-) I can't remember and don't have the older code at hand.
    – user31729
    Jun 22, 2016 at 8:26

You must log in to answer this question.

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