4

I noticed that a 'tcolorbox' with auto counter including the section number causes a warning in the log file and the respective hyperref links point only to the first occurence.

MWE:

\documentclass{scrartcl}
\usepackage{tcolorbox}
\newtcolorbox[auto counter,number within=section]{mybox}[1][]{title=MyBox~\thetcbcounter,#1}
\usepackage{hyperref}

\begin{document}
Mybox~\ref{mybox1}; Mybox~\ref{mybox2}

\clearpage
\section{Section}
\begin{mybox}[label=mybox1]
The counter name is \texttt{\tcbcounter}.
\end{mybox}

\clearpage
\section{Section}
\begin{mybox}[label=mybox2]
The counter name is \texttt{\tcbcounter}.
\end{mybox}
\end{document}

The warning in the log file reads

warning  (pdf backend): ignoring duplicate destination with the name 'tcb@cnt@mybox.1'

So somehow the 'tcbcounter' is properly reset for the second section, but the internal counter 'tcb@cnt@mybox' does not contain the section number, so that the reference link to the second box points to the first.

Is this known or maybe a bug? Or can I, as the user, fix this easily? Thanks!

6

hyperref has a built-in mechanism for this. If for a counter the command \theH<counter> is defined, then it is used for internal anchor names instead of \the<counter>.

\documentclass{scrartcl}
\usepackage{tcolorbox}
\usepackage{hyperref}

\newtcolorbox[auto counter,number within=section]{mybox}[1][]{%
  title=MyBox~\thetcbcounter,
  #1
}
\providecommand{\theHtcbcounter}{\thesection.\arabic{tcbcounter}}

\begin{document}

Mybox~\ref{mybox1}; Mybox~\ref{mybox2}

\clearpage
\section{Section}
\begin{mybox}[label=mybox1]
The counter name is \texttt{\tcbcounter}.
\end{mybox}

\clearpage
\section{Section}
\begin{mybox}[label=mybox2]
The counter name is \texttt{\tcbcounter}.
\end{mybox}
\end{document}

This way the two links will have distinct anchor names and all is well.

If you want this to work with later loading of hyperref, you have to plunge in the internals:

\makeatletter
\providecommand\theHtcb@cnt@mybox{\thesection.\arabic{tcb@cnt@mybox}}
\makeatother

because tcolorbox associates that counter to the color box.

  • This seems to require loading hyperref before the definition(s) of tcolorboxs. So I would need to load hyperref pretty early on in my classes/packages. On the other hand, I read one should load hyperrefat the end of the preamble. So what would you recommend? – FlorianL Apr 17 at 10:40
  • 1
    @FlorianL Delay the definitions of the boxes at begin document. The “fault” is in tcolorbox that doesn't actually do \newcounter{tcbcounter}. – egreg Apr 17 at 10:53
  • @FlorianL I added the workaround – egreg Apr 17 at 10:59
  • After playing around a bit, I think delaying the definitions of the boxes with \AtBeginDocument is what matters. The MWE works with loading hyperref before the box definition, even without the \providecommand. Could you point this out in your answer such that I can mark it as such? – FlorianL Apr 17 at 11:15
0

Based on @egreg's answer, it seems that one only needs to define new tcolorboxes after loading package hyperref. If both should be done from inside a custom package or a more complicated preamble structure, the \AtEndPreamble and \AtBeginDocument from the etoolbox package can be used.

Loading package auxhook early prevents an error message when using a koma-scrip class (see etoolbox + koma-script: Warning 'seems someone has broken package auxhook').

So a functional MWE would be:

\documentclass{scrartcl}
\usepackage{tcolorbox}
\usepackage{etoolbox, auxhook}

\AtEndPreamble{
    \usepackage{hyperref}
}

\AtBeginDocument{
    \newtcolorbox[auto counter,number within=section]{mybox}[1][]{%
        title=MyBox~\thetcbcounter,#1}
}

\begin{document}
Mybox~\ref{mybox1}; Mybox~\ref{mybox2}

\clearpage
\section{Section}
\begin{mybox}[label=mybox1]
The counter name is \texttt{\tcbcounter}.
\end{mybox}

\clearpage
\section{Section}
\begin{mybox}[label=mybox2]
The counter name is \texttt{\tcbcounter}.
\end{mybox}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.