1

I'd like to have different theorem environments share the same counter using tcolorbox in beamer. However cleveref doesn't seem to pick up their names correctly. Can anyone point me to what I'm doing wrong?

Here's a MWE:

\documentclass[10pt,aspectratio=169]{beamer}

\usepackage{amsmath}
\usepackage{amsthm}

\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}

\usepackage{cleveref}
\crefname{lemma}{lemma}{lemmas}
\crefname{theorem}{theorem}{theorems}

\renewtcolorbox[use counter=theorem]{theorem}[1][]{
    title={\thetheorem. Theorem.},
    before upper={\tcbtitle\;\itshape},
    #1
    }

\renewtcolorbox[use counter=theorem]{lemma}[1][]{
    title={\thetheorem. Lemma.},
    before upper={\tcbtitle\;\itshape},
    #1
    }

\begin{document}

\begin{frame}{Blah}
    \begin{theorem}[label=bar]
        Blah blah.
    \end{theorem}
    \cref{bar}

    \begin{lemma}[label=foo]{}{}
        Blah blah.
    \end{lemma}
    \cref{foo}
\end{frame}

\end{document}

Which produces: output

I would like "lemma 2" instead of "theorem 2". I tried adding label type = lemma as suggested here, but without any success.

3
  • 2
    Since beamer loads hyperref with package option implicit=false, cleveref doesn't work with beamer (as can be seen from the Package cleveref warning in log) and there seems to be no easy workaround. Similar: tex.stackexchange.com/a/481055. Commented Jan 5, 2023 at 8:13
  • @muzimuzhiZ As far as I know, the issue you mentioned affects the hyperref's anchors (the links will all point to DocStart). See tex.stackexchange.com/a/266109/105447 for a (partial) work-around. However, are your sure it affects the reference counter/type too?
    – gusbrs
    Commented Jan 5, 2023 at 9:30
  • I think what's happening is that beamer can't handle the optional argument to \label which cleveref adds and tcolorbox uses to implement label type...
    – gusbrs
    Commented Jan 5, 2023 at 9:54

1 Answer 1

2

beamer is quite peculiar in it's treatment of cross-referencing but, if you are willing to use zref-clever, and do some setup, you may get away with the following:

\documentclass[10pt,aspectratio=169]{beamer}

\usepackage{amsmath}
\usepackage{amsthm}

\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}

% Create 'zlabel' and 'zlabel type' options for tcolorbox.
\tcbset{%
  zlabel/.style={phantom={\zlabel{#1}}},%
  zlabel type/.code={\zcsetup{reftype={#1}}},%
}

\usepackage{zref-clever}

% From "Work-around 2: beamer" in zref-clever's User manual
\makeatletter
\RenewDocumentCommand{\zlabel}{ D<>{1} m }{%
  \ifx\label\ltx@gobble
  \else
    \zref@wrapper@babel{\zref@label<#1>}{#2}%
  \fi
}
\NewCommandCopy\beamer@old@zref@label\zref@label
\RenewDocumentCommand{\zref@label}{ D<>{1} m }{%
  \alt<#1>{%
    \zref@ifpropundefined{anchor}{}{\zref@setcurrent{anchor}{#2}}%
    \beamer@old@zref@label{#2}%
    \beamer@nameslide{#2}%
  }{%
    \beamer@dummynameslide%
  }%
}
\makeatother

\renewtcolorbox[use counter=theorem]{theorem}[1][]{
    title={\thetheorem. Theorem.},
    before upper={\tcbtitle\;\itshape},
    #1
    }

\renewtcolorbox[use counter=theorem]{lemma}[1][]{
    title={\thetheorem. Lemma.},
    zlabel type={lemma},
    before upper={\tcbtitle\;\itshape},
    #1
    }

\begin{document}

\begin{frame}
  \zcref{bar,foo}
\end{frame}

\begin{frame}{Blah}
  \begin{theorem}[zlabel=bar]
    Blah blah.
  \end{theorem}
  \zcref{bar}

  \begin{lemma}[zlabel=foo]
    Blah blah.
  \end{lemma}
  \zcref{foo}
\end{frame}

\end{document}

enter image description here

1
  • This worked great – thanks!
    – samjindani
    Commented Jan 9, 2023 at 9:22

You must log in to answer this question.

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