Question on Beamer Counter

I'm trying to create a new numbered definition environment based on exampleblock, but it doesn't work as is expected.

My code:

% !TEX program = xelatex

\documentclass[serif, compress]{beamer}

\usetheme{Warsaw}
\useoutertheme{miniframes}

\begin{document}

\newcounter{definition}
\renewenvironment{definition}[1][]
{\stepcounter{definition}\begin{exampleblock}{Definition\,\arabic{definition}}}
{\end{exampleblock}}

\begin{frame}
\onslide<1->
\begin{definition}
qwq1 % expected to be "Definition 1"
\end{definition}
\onslide<2->
\begin{definition}
qwq2 % expected to be "Definition 2"
\end{definition}
\end{frame}

\end{document}


However, the slide looks like

Could someone show me how to use counter with \onslide properly?

• welcome to tex.se! please complete your code to compilable small document (missed is document preamble). – Zarko Mar 12 at 14:08

Instead of trying to reinvent the wheel, just use beamers build in definition environment and activate theorem numbers:

\documentclass{beamer}

\setbeamertemplate{theorems}[numbered]

\begin{document}

\begin{frame}
\begin{definition}<1->
qwq1 % expected to be "Definition 1"
\end{definition}
\begin{definition}<2->
qwq2 % expected to be "Definition 2"
\end{definition}
\end{frame}

\end{document}


In case the blue colour of this definition environment is bothering you, it can be changed like this:

\documentclass{beamer}

\setbeamertemplate{theorems}[numbered]

\BeforeBeginEnvironment{definition}{%
\setbeamercolor{block title}{fg=white,bg=example text.fg!75!black}
\setbeamercolor{block body}{fg=black, bg=block title.bg!10!bg}
}
\AfterEndEnvironment{definition}{
\setbeamercolor{block title}{use=structure,fg=white,bg=structure.fg!75!black}
\setbeamercolor{block body}{parent=normal text,use=block title,bg=block title.bg!10!bg}
}

\begin{document}

\begin{frame}
\begin{definition}<1->
qwq1 % expected to be "Definition 1"
\end{definition}
\begin{definition}<2->
qwq2 % expected to be "Definition 2"
\end{definition}
\end{frame}

\end{document}

• Thanks a lot, but I'm still curious about the reason why my code doesn't work as is expected. Could you please tell me the reason? – C.R. Wang Mar 13 at 13:44
• @C.R.Wang The reason that your code did not work is that the code is executed on each slide of the frame. For your code this means that on the second slide the counter starts not at zero but at 2. As a workaround you could manually reset the counter with \begin{frame} \setcounter{definition}{0} – user36296 Mar 13 at 13:49
• Oh...It seems that the number goes wrong again when I replace \onslide with \only. – C.R. Wang Mar 13 at 14:34
• @C.R.Wang That's the difference between "not there" and "not visible" – user36296 Mar 13 at 14:46