# refstepcounter in tcolorbox option

I want to increment a counter and update the current label in the phantom key of the tcolorbox environment, but it is not working, as can be seen in the example below:

\documentclass{article}
\usepackage[paperwidth=6cm,paperheight=6cm,margin=4mm]{geometry}
\usepackage{tcolorbox}

\makeatletter
\newcommand\currentlabel{\@currentlabel}
\makeatother

\begin{document}

\newcounter{mycounter}

\begin{tcolorbox}[phantom=\refstepcounter{mycounter}]
counter value: \themycounter\ \currentlabel.
\end{tcolorbox}

\begin{tcolorbox}[phantom=\refstepcounter{mycounter}]
counter value: \themycounter\ \currentlabel.
\end{tcolorbox}

\refstepcounter{mycounter}
\begin{tcolorbox}
counter value: \themycounter\ \currentlabel.
\end{tcolorbox}

\refstepcounter{mycounter}
\begin{tcolorbox}
counter value: \themycounter\ \currentlabel.
\end{tcolorbox}

\end{document}


The output follows

.

Observe that \@currentlabel is not updated when the counter is incremented using the phantom key.

Is this a bug or I am missing something here?

-
The manual of tcolorbox clearly states, that the code of the phantom key hook is executed in a LaTeX group, so only global operations can survive. –  Christian Hupfer Aug 27 '14 at 6:21
\@currentlabel is only \protected@edef defined, so it is not global. –  Christian Hupfer Aug 27 '14 at 6:28

I haven't dived into the internals but it looks like tcolorbox is preserving the value of \@currentlabel so that it has the same value that it had before the start of the tcolorbox. On the other hand, it is incrementing your counter and it does allow you to assign a label to it using the label argument. If instead you use:

\begin{tcolorbox}[step=mycounter,label=one]
Counter value: \themycounter, \currentlabel.
This is \ref{one}
\end{tcolorbox}


Then the output is:

That is, using label= correctly sets the reference to your counter. Behind the scenes tcolorbox is probably writing the label directly to the auxfile. (Note that step=... is a shortcut to phantom=\refstepcounter...). So the referencing is working correctly, but to access it you need to use label=...

-

Andrew has provided already a solution, I just want to show, that the \@currentlabel change does not survive outside of phantom code.

The doubled \refstepcounter command for the third box is done on purpose!

\documentclass{article}
\usepackage[paperwidth=6cm,paperheight=6cm,margin=4mm]{geometry}
\usepackage{tcolorbox}

\makeatletter
\newcommand\currentlabel{\@currentlabel}
\makeatother

\begin{document}

\newcounter{mycounter}

\begin{tcolorbox}
\refstepcounter{mycounter}%
counter value: \themycounter\ \currentlabel.
\end{tcolorbox}

\begin{tcolorbox}[phantom={\global\refstepcounter{mycounter}}]
counter value: \themycounter\ \ref{Box::two}% Should not work
\end{tcolorbox}

\refstepcounter{mycounter}
\begin{tcolorbox}
\refstepcounter{mycounter}
counter value: \themycounter\ \ref{Box::three}%
\end{tcolorbox} \label{Box::three}%

\refstepcounter{mycounter}
\begin{tcolorbox}
counter value: \themycounter\ \currentlabel.
\end{tcolorbox}

\end{document}


-