I am trying to generate a table of contents in beamer, but instead of displaying the page number on the right side, I would like to access/display the frame number. So my question is:
How do I create a table of contents that displays the frame number (and not the page number)?
Alternatively: How can I access the command that instructs the (standard latex) table of contents to use the page number (\thepage
)? I have to assume that if I get access, then I can replace the page number with the frame number (\theframenumber
)). Right?
I understand that this would not be an issue in a simple presentation, because the page number is identical to the frame number. However, I am creating presentations for lectures and I simultaneously develop intricate handouts using beamerarticle
(in the same file). For example, some frames do not show up in the handout (e.g. A picture that helps me to make a point during the lecture, do not need to be included in the handout. Or some headline slides \AtBeginSection
would only take up space in the handout without adding any important information). So I would like for my table of contents in the article version (handout) to be identical to the table of contents that I show on the wall (my beamer presentation) - though the included frames are different. In other words, I would need my beamerarticle
to count the frames. Or maybe beamerarticle
(or standard latex) has an option that allows me to implement \theframenumber
in a regular doc.
I understand that it is possible that this is a difficult problem to solve, because one of the goals of the beamerarticle
package is to ignore the \begin{frame}
, \end{frame}
, and other frame related commands. So I expect that it is not easy to count frames in article mode. But then again, that's why I am here to ask you. ... Maybe a new command can be created that I can globally place before/after each frame.
Here is a very much simplified MWE for a presentation:
%\documentclass{beamer} %uncomment to produce presentation
\documentclass{article} %comment out to produce presentation
\usepackage{beamerarticle} %comment out to produce presentation
\begin{document}
\begin{frame}
\frametitle{Title 1}
\tableofcontents % Works well in beamer, but does not show correct frame numbers in article mode
In article mode, I would like this list to read as follows:
1 Sec One .... Frame 2
2 Sec Two .... Frame 4
3 Sec Three .... Frame 6
\end{frame}
\section{Sec One}
\begin{frame}
\frametitle{Title 2}
\end{frame}
\begin{frame}
\frametitle{Title 3}
\end{frame}
\section{Sec Two}
\begin{frame}
\frametitle{Title 4}
\end{frame}
\section{Sec Three}
\mode<presentation>{ % Only shows up in presentation, so list of frames must make a jump here
\begin{frame}
\frametitle{Title 5}
\end{frame}
}
\begin{frame}
\frametitle{Title 6}
\end{frame}
\end{document}
I really appreciate any input or pointers in the right directions. Anything that can help me solve this issue would be great. Thanks.
Update:
Based on SimonDispa's post, I was able to make some tweaks, but it still did not get me to the full solution. I believe that either the refcount
or the zref
package can get me over the finish line.
First, because I have so many presentations/handouts in which I will use this type of approach, I would like the solution to be more automated (or more global). Based on this post (diabonas' answer), it is possible to make \atBeginSection
work when I compile the beamerarticle
% Make atBeginSection work in article mode
\mode<article>
\makeatletter
\expandafter\let\expandafter\originalsection\expandafter=\csname @orig\string\section\endcsname
\def\sectionwithhook{\@ifstar\@sectionwithhook\@@sectionwithhook}
\newcommand{\@sectionwithhook}[1]{\originalsection*{#1}}
\newcommand{\@@sectionwithhook}[2][]{\beamer@ifempty{#1}{\originalsection{#2}}{\originalsection[#1]{#2}}\beamer@atbeginsection}
\renewcommand<>{\section}{\alt#1{\sectionwithhook}{\beamer@secgobble}}
\makeatother
\mode<all>
This allows me to set a counter (MyFrameNumber) to the current frame number plus 1, at the beginning of each section,
\AtBeginSection[]
{
\mode<article>{\setcounter{MyFrameNumber}{\theframenumber+1}}
}
Thanks to the tocloft
package, I can manipulate the appearance of the toc and I would like to do so as follows:
\mode<article>{
\usepackage{tocloft}
\renewcommand{\cftsecleader}{\dotfill{} Starts at Frame
% How to make \theMyFrameNumber work and update properly in toc?
% It currently produces a 0
\theMyFrameNumber{}
% Can it be done with the refcount package ?
% Can it be done with the zref package ?
}}
But it leaves an issue with \theMyFrameNumber{}
. I have to speculate a little bit, but there are a few potential issues prevent this from working properly:
- The toc is generated before the rest of the document (but proper handling of the aux file could probably fix this issue).
- The counter
MyFrameNumber
is not written to the aux file, and the toc cannot access the information. Hence, it prints a zero for all instances. - Even if the counter
MyFrameNumber
would be written to the aux file as it is, it would currently only have a single value (the last/highest one)
Accordingly, I believe that I could make this work, if I had a method that allows me to cross reference \theMyFrameNumber{}
. In other words, I would need a method that writes the current number (or label) of \theMyFrameNumber{}
to the aux file and a method to read that number or label back into my table of contents.
I have tried the refcount
and the zref
package, as I believe that those package could hold the solution. However, I am not familiar enough with those commands and I seem unable to piece it together. Am I wrong? Can it be done with the tools provided in those packages? Or would a different approach be better?
Here is the updated full MWE, it feels so close:
%\documentclass{beamer} %uncomment to produce presentation
\documentclass{article} %comment out to produce presentation
\usepackage{beamerarticle} %comment out to produce presentation
\usepackage{calc}
%\usepackage{refcount}
%\usepackage{zref}
\global\newcounter{MyFrameNumber}
\mode<article>{
\usepackage{tocloft}
\renewcommand{\cftsecleader}{\dotfill{} Starts at Frame
% How to make \theMyFrameNumber work properly in toc?
% It currently produces a 0
\theMyFrameNumber{}
% Can it be done with the refcount package ?
% Can it be done with the zref package ?
}}
% Make atBeginSection work in article mode
\mode<article>
\makeatletter
\expandafter\let\expandafter\originalsection\expandafter=\csname @orig\string\section\endcsname
\def\sectionwithhook{\@ifstar\@sectionwithhook\@@sectionwithhook}
\newcommand{\@sectionwithhook}[1]{\originalsection*{#1}}
\newcommand{\@@sectionwithhook}[2][]{\beamer@ifempty{#1}{\originalsection{#2}}{\originalsection[#1]{#2}}\beamer@atbeginsection}
\renewcommand<>{\section}{\alt#1{\sectionwithhook}{\beamer@secgobble}}
\makeatother
\mode<all>
\AtBeginSection[]
{
\mode<article>{\setcounter{MyFrameNumber}{\theframenumber+1}}
%
%\begin{frame}
%\frametitle{Table of Contents}
%\tableofcontents[currentsection]
%\end{frame}
}
\begin{document}
\begin{frame}
\frametitle{Title 1}
\pagenumbering{gobble} % suppress page numbering in TOC
\tableofcontents % Works well in beamer, but does not show correct frame number in article
\bigskip
In article mode, I would like this list to read as follows:
\bigskip
1 Sec One \dotfill{} Starts at Frame 2\\
2 Sec Two \dotfill{} Starts at Frame 4\\
3 Sec Two \dotfill{} Starts at Frame 5\\
\end{frame}
\section{Sec One}
\begin{frame}
\frametitle{Title 2}
% Here \theMyFrameNumber works flawlessly
This is frame \theMyFrameNumber{}
% Check correctness
(\theframenumber)
\end{frame}
\begin{frame}
\frametitle{Title 3}
% Here \theMyFrameNumber does not work
% because it only incremented/updated atBeginSection
This is frame \theMyFrameNumber{}
% Check correctness
(\theframenumber)
\end{frame}
\section{Sec Two}
\begin{frame}
\frametitle{Title 4}
% Here \theMyFrameNumber works flawlessly
This is frame \theMyFrameNumber{}
% Check correctness
(\theframenumber)
\end{frame}
\section{Sec Three}
\begin{frame}
\frametitle{Title 5}
% Here \theMyFrameNumber works flawlessly
This is frame \theMyFrameNumber{}
% Check correctness
(\theframenumber)
\end{frame}
\end{document}
Only needed for resolution. Everything below will be deleted after fix.
The solution provide by @SimonDispa is great, and the final outcome looks exactly like what I am looking for. However, when I compile the code on my computer, it looks like this:
As you can see, the blue table of contents does not compile correctly. I have tried to manipulate the code (in beamerarticleETOC.tex, which I copied directly into the main document), but without any success.
\theMyFrameNumber{}
and the default\theframenumber
. II believe that can be fixed through assigning a different/new "helper counter". 2) I believe once I understand point 3), I can handle presentation and article mode. 3) That is exactly the remaining issue. That is what I would like to fix, but I do not know how. It appears challenging (to me) because it requires writing a counter to the aux file and reading it back into the tocloft command.