2

Suppose I have a beamer presentation with some contiguous frames containing a "special content" that are inside a specialcontent environment. I'd like to introduce this special content with a frame that display both:

  • the numbers of "special" frames,
  • the frame (not page) number of the last "special frame,

as in the MCE below (<total number of frames> and <number of last frame of the special content>).

How could I achieve this?

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\setbeamertemplate{footline}[frame number]
\setbeamertemplate{navigation symbols}{}

\newenvironment{specialcontent}{%
  \begin{frame}
    \frametitle{Special content}
    The following special content:
    \begin{itemize}
    \item has <total number of frames> frames,
    \item ends frame \# <number of last frame of the special content>.
    \end{itemize}
  \end{frame}
}{%
}

\begin{document}
\begin{frame}
  \frametitle{Foo bar}
  Foo\pause{} bar.
\end{frame}

\begin{specialcontent}

  \begin{frame}
    \frametitle{Foo bar (special content)}
    Foo\pause{} bar.
  \end{frame}

  \begin{frame}
    \frametitle{Foo bar (special content)}
    Foo\pause{} bar.
  \end{frame}

\end{specialcontent}

\begin{frame}
  \frametitle{Foo bar}
  Foo\pause{} bar.
\end{frame}
\end{document}
Denis Bitouzé
  • 9,652
  • 4
  • 27
  • 85

2 Answers2

2

Using the totcount package:

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\setbeamertemplate{footline}[frame number]
\setbeamertemplate{navigation symbols}{}


\usepackage{totcount}
\newcounter{endspecial}
\newcounter{startspecial}
\regtotcounter{endspecial}


\newenvironment{specialcontent}{%
  \begin{frame}
    \frametitle{Special content}
    The following special content:
    \begin{itemize}
    \setcounter{startspecial}{\totvalue{endspecial}}
    \addtocounter{startspecial}{1}
    \addtocounter{startspecial}{-\insertframenumber}
    \item has \thestartspecial  <total number of frames> frames,
    \item ends frame \total{endspecial} <number of last frame of the special content>.
    \end{itemize}
  \end{frame}
}{%
  \setcounter{endspecial}{\insertframenumber}
}

\begin{document}
\begin{frame}
  \frametitle{Foo bar}
  Foo\pause{} bar.
\end{frame}

\begin{specialcontent}

  \begin{frame}
    \frametitle{Foo bar (special content)}
    Foo\pause{} bar.
  \end{frame}

  \begin{frame}
    \frametitle{Foo bar (special content)}
    Foo\pause{} bar.
  \end{frame}

\end{specialcontent}

\begin{frame}
  \frametitle{Foo bar}
  Foo\pause{} bar.
\end{frame}
\end{document}

enter image description here

1

For the record, I present here a extended solution to samcarter's one which works also in the case where several occurrences of specialcontents environment are to be used (and, say, this environment is not ours but given by a third party package or class -- in my real use case, it comes from the versions package).

The tricky problem was, when defining many "total counters" on the fly, only the last one was created. Thanks to the egreg's trick, the following is working like a charm:

\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{multido}
\usepackage{totcount}

\usetheme{PaloAlto}
\useoutertheme{infolines}

\setbeamertemplate{navigation symbols}{}

\newenvironment{specialcontent}{%
}{%
}

\AtBeginEnvironment{specialcontent}{%
  \stepcounter{specialcontent@cntr}%
  %
  \begingroup\edef\x{\endgroup
    \noexpand\newtotcounter{startspecial@\roman{specialcontent@cntr}}
    \noexpand\newtotcounter{endspecial@\roman{specialcontent@cntr}}%
  }\x
  %
  \begin{frame}
    \frametitle{Special content (begins)}
    %
    \setcounter{startspecial@\roman{specialcontent@cntr}}{\totvalue{endspecial@\roman{specialcontent@cntr}}}%
    \addtocounter{startspecial@\roman{specialcontent@cntr}}{-\insertframenumber}%
    %
    The following special content:
    \begin{itemize}
    \item \alert{has \total{startspecial@\roman{specialcontent@cntr}} frames},
    \item \alert{ends frame \total{endspecial@\roman{specialcontent@cntr}}}.
    \end{itemize}
  \end{frame}
}

\AtEndEnvironment{specialcontent}{%
  \setcounter{endspecial@\roman{specialcontent@cntr}}{\insertframenumber}%
}

\newcounter{specialcontent@cntr}%

\begin{document}

\multido{\i=1+1}{17}{%
  \begin{frame}
    \frametitle{Foo bar}
    Foo\pause{} bar.
  \end{frame}
  %
  \section{Special \i{}}
  %
  \begin{specialcontent}
    \multido{\I=1+1}{\i}{%
      \begin{frame}
        \frametitle{Foo bar (special content)}
        Foo\pause{} bar.
      \end{frame}
    }
  \end{specialcontent}
  %
  \begin{frame}
    \frametitle{Foo bar}
    Foo\pause{} bar.
  \end{frame}
}

\end{document}

enter image description here

Denis Bitouzé
  • 9,652
  • 4
  • 27
  • 85