Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

output of mwe.

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?

share|improve this question
2  
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 at 6:21
    
\@currentlabel is only \protected@edef defined, so it is not global. –  Christian Hupfer Aug 27 at 6:28

2 Answers 2

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:

enter image description here

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=...

share|improve this answer

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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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