2

Please help me to put a simple counter in the tcolorbox environment in beamer. It only needs to go from 1,2,3,...

\begin{tcolorbox}[colback=green!5,colframe=green!40!black, center,title=    ]

\end{tcolorbox}

1 Answer 1

5

There is the key auto counter for that purpose. As it turns out, it does not yet automatically harmonize with beamer \pauses. This can be fixed by redefining \tcb@proc@counter@auto to become

\def\tcb@proc@counter@auto#1{%
  \newcounter{tcb@cnt@#1}%
  \csxdef{tcb@cnt@#1}{tcb@cnt@#1}%
  \tcb@proc@counter@autoanduse{#1}%
  \ifcsname resetcounteronoverlays\endcsname%<-added
  \resetcounteronoverlays{tcb@cnt@#1}%<-added
  \fi%<-added
}

So the MWE becomes

\documentclass{beamer}
\usepackage{tcolorbox}
\makeatletter
\def\tcb@proc@counter@auto#1{%
  \newcounter{tcb@cnt@#1}%
  \csxdef{tcb@cnt@#1}{tcb@cnt@#1}%
  \tcb@proc@counter@autoanduse{#1}%
  \ifcsname resetcounteronoverlays\endcsname%<-added
  \resetcounteronoverlays{tcb@cnt@#1}%<-added
  \fi%<-added
}
\makeatother
\newtcolorbox[auto counter]{numberedbox}[2][]{%
colback=green!5,colframe=green!40!black,center,title=Numbered
box~\thetcbcounter: #2,#1}

\begin{document}
\begin{frame}[t]
\frametitle{tcolorboxes}
\begin{numberedbox}[label={boxA}]{title A}
I am random box and have number~\ref{boxA}
\end{numberedbox}
\pause
\begin{numberedbox}[label={catinbox}]{A cat in a box}
\dots wrote this, see numbered box~\ref{boxA}
\end{numberedbox}
\pause
\begin{numberedbox}[label={yetanotherbox}]{title B}
I'm boring, see numbered box~\ref{catinbox} for something exciting
\end{numberedbox}
\end{frame}
\end{document}

enter image description here

15
  • Thanks, @Schrodinger's cat. It works and yet a very simple solution!
    – hochoi
    Sep 30, 2019 at 5:02
  • Although this solution works, but I encountered another problem when I use it with the \pause command. The counter keeps counting up for the same box. Is there a way to make the counter not to count the same box more than once?
    – hochoi
    Sep 30, 2019 at 5:09
  • @hochoi Good point! I added a working solution. Even though there is a section on beamer support in the tcolorbox manual, I could not find anything on auto counter support. Since tcolorbox gives it some internal name that is not obvious, I added a normal counter here. Maybe that would be something for a feature request to ask to make auto counter do that automatically.
    – user194703
    Sep 30, 2019 at 12:42
  • It works wonderfully! Thank you very much, @Schrodinger's cat!
    – hochoi
    Sep 30, 2019 at 13:29
  • One more problem popping up :) How do I refer to certain box? The label command does not seem o be able to track the counter accurately.
    – hochoi
    Oct 1, 2019 at 1:14

You must log in to answer this question.

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