# beamer with numbered theorems not working with cleveref

A want to use cleveref in a beamer presentation. The document defines multiple theorem environments sharing the same counter as follows:

\documentclass{beamer}

\usepackage{cleveref}

\newtheorem{thm}{Theorem}
\newtheorem{cor}[thm]{Corollary}

\begin{document}

\begin{frame}
\begin{cor}
foo bar
\end{cor}
\end{frame}

\end{document}


This results in the following error:

! Undefined control sequence.
\endbeamercolorbox ->\ifdim \beamer@colbox@sep
=0pt\else \vskip \beamer@colb...
l.14 \end{frame}


Note that the example works fine if a separate counter for cor is used, i.e. if the 6th line is replaced by

\newtheorem{cor}{Corollary}


I could not find any useful information about this. Any ideas how to get the original example working?

Note: This is not a duplicate of this question. Cleveref does work together with beamer as long as separate counters are used. I could even add \label to the corollary and \cref it later and everything works fine.

However, if the two theorem environments use the same counter (as in my original example) the error occurs, even if no \label or \cref is used at all.

• To would-be close-this-posting voters: The new question is not a duplicate of the earlier one. In particular, the remedy suggested there does not seem to work with the OP's stated setup. – Mico Apr 13 '16 at 8:20
• Could someone please tell me why my question is considered a duplicate of that other question? Just because it contains "beamer", "cleveref" and "something does not work"? To make it clear: I do not claim that cleveref does not work with beamer. In fact, it does. As long as I do not use a shared counter, everything works fine. I could even add a label to the cor and cref it later and the reference works correctly. The problem is that the shared counter for the numbered theorems does not work with cleveref and causes the error even without using \label or \cref at all. – fifr Apr 13 '16 at 12:31

It's actually not true that cleveref works with beamer; for instance, \label does not accept an optional argument.

Here's a hack that does what you're looking for. However, I don't think it's worthy the pain: your audience will not remember the theorem numbers, so referring to them by number will just be confusing.

\documentclass{beamer}
\usepackage{amsthm}
\usepackage{cleveref}

\newtheorem{thmx}{\theoremname}
\newenvironment{thm}{\def\theoremname{Theorem}\thmx}{\endthmx}
\newenvironment{cor}{\def\theoremname{Corollary}\thmx}{\endthmx}
\providecommand{\theoremname}{Theorem}
\crefname{thm}{Theorem}{Theorems}
\crefname{cor}{Corollary}{Corollaries}

\makeatletter
\AtBeginDocument{
\def\beamer@label<#1>{%
\def\hack@arg{#1}%
\@ifnextchar[\beamer@label@opt\beamer@label@noopt
}%
\def\beamer@label@opt[#1]#2{%
\expandafter\alt\expandafter<\hack@arg>%
{\beamer@origlabel[#1]{#2}\beamer@nameslide{#2}}%
{\beamer@dummynameslide}%
}%
\def\beamer@label@noopt#1{%
\expandafter\alt\expandafter<\hack@arg>%
{\beamer@origlabel{#1}\beamer@nameslide{#1}}%
{\beamer@dummynameslide}%
}%
}
\makeatother

\begin{document}

\begin{frame}
\begin{cor}\label[cor]{A}
foo bar
\end{cor}

\cref{A}

\end{frame}

\end{document}


• Thanks a lot. Unfortunately, it does not work for me in some cases but it should be good enough as a workaround. – fifr Jul 12 '16 at 6:10