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}
CarLaTeX
  • 62,716
hochoi
  • 193

1 Answers1

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

  • Thanks, @Schrodinger's cat. It works and yet a very simple solution! – hochoi Sep 30 '19 at 05: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 '19 at 05: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. –  Sep 30 '19 at 12:42
  • It works wonderfully! Thank you very much, @Schrodinger's cat! – hochoi Sep 30 '19 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 01 '19 at 01:14
  • @hochoi OK OK, I tried to fix it once and for all by slightly redefining an internal command of tcolorbox. It adds the counter to the reset list if the \resetcounteronoverlays command is defined, which should be a very good check whether or not that's a beamer document. –  Oct 01 '19 at 02:58
  • Thank again, Schrodinger's cat. You are always a big help. I will test this one tomorrow and confirm :) – hochoi Oct 01 '19 at 05:42
  • Things work as intended now. Thank you so much for your effort, Schrodinger's cat. – hochoi Oct 02 '19 at 01:26
  • @hochoi You are welcome! I think you raised an important point, see https://github.com/T-F-S/tcolorbox/issues/80#issuecomment-536969328. –  Oct 02 '19 at 01:28
  • I am glad I did without real intention :) I am curious on the \makeatletter and \makeatother. It seems that you can remove the \makeatother? – hochoi Oct 02 '19 at 01:36
  • @hochoi Any attempt of me answering this will be pale compared to https://tex.stackexchange.com/a/8353/194703, so just read Alan's answer on this. ;-) –  Oct 02 '19 at 01:40
  • I see. Thank again, @Schordinger's cat. – hochoi Oct 02 '19 at 01:41
  • Hi Schrodinger'scat, there is a need for an improvement in the codes. That is, when I try to refer certain box, the label of the box is not listed for me to choose from when I type \ref{. Is it possible to make that happen? – hochoi Oct 07 '19 at 22:26
  • @hochoi Could you add a concrete example to the question? Or, perhaps even better, ask a new question with that examples which many users will see? –  Oct 07 '19 at 22:31
  • Will do so. Thanks, Schrödinger's cat. – hochoi Oct 08 '19 at 00:21