Is there a way to reference the title of the previous Beamer slide? Often, a sequence of slides have the same title, and I'd like to avoid having to repeat the title. I could define a macro for the title of each slide, but is there a way to grab the title of an arbitrary slide?
5 Answers
Beamer has a built-in facility for this: Inserting an explicit frame break using \framebreak
instead of \end{frame}
followed by \begin{frame}
will put the content that follows onto the next frame, and the frame title is repeated automatically with a roman number appended. See this topic.
-
10I think it should be pointed out that the
\framebreak
command only works when theallowframebreaks
option has been given and this disables overlays. Aug 8, 2012 at 14:52 -
1@Andrew. Disabling overlays is a severe restriction I wasn't aware of. Thanks for pointing this out.– AlexGAug 8, 2012 at 15:00
One possible solution is to let \frametitle
do the work for you; in the following example, I modified \beamer@frametitle
in such a way that each invocation of \frametitle
globally defines a command that stores the title; the first time you use \frametitle
, the command \Frametitlei
is defined and containes the title; the second time \frametitle
is used, the command \Frametitleii
is created and stores the tile, and so on (the created command are all of the form \Frametitle<roman numeral>
, where <roman numeral>
is the roman numeral representation of the number of times \frametitle
has been used); then you can use those commands to retrieve the names:
\documentclass{beamer}
\makeatletter
\newcounter{ftitle}
\long\def\beamer@@frametitle[#1]#2{%
\beamer@ifempty{#2}{}{%
\gdef\insertframetitle{{#2\ifnum\beamer@autobreakcount>0\relax{}\space\usebeamertemplate*{frametitle continuation}\fi}}%
\gdef\beamer@frametitle{#2}%
\stepcounter{ftitle}
\expandafter\gdef\csname Frametitle\romannumeral\theftitle\endcsname{#2}
\gdef\beamer@shortframetitle{#1}%
}%
}
\makeatother
\begin{document}
\begin{frame}
\frametitle{Test Frame Title One}
Test
\end{frame}
\begin{frame}
\frametitle{Test Frame Title Two}
Test
\end{frame}
\begin{frame}
\frametitle{\Frametitlei}
Test
\end{frame}
\begin{frame}
\frametitle{Test Frame Title Three}
Test
\end{frame}
\begin{frame}
\frametitle{\Frametitleii}
Test
\end{frame}
\end{document}
Your question is ever-so-slightly ambiguous. You open with:
Is there a way to reference the title of the previous Beamer slide?
and close with:
... is there a way to grab the title of an arbitrary slide?
These are different questions. The first simply wants to get the name of the previous(ly named) Beamer slide. The second wants full random access. Gonzalo has answered the second, here's a slightly cheaper way to get the first. It works on the same principle: we save the previous frame title (and subtitle) but it has a slightly different way of accessing it. The rule that I infer is that "If the frametitle isn't given, use the previous one.". The simplest way to achieve that is to insert the previous frame title (and subtitle) as the new ones at the start of the frame. Then any actual assignment will overrule it, but if no assignment is made then the defaults are there to be used.
Here's the code (with a little help from https://tex.stackexchange.com/a/519/86)
\documentclass{beamer}
%\url{https://tex.stackexchange.com/q/66274/86}
\makeatletter
\def\beamer@checkframetitle{%
\begingroup
\edef\temp{%
\endgroup
\noexpand\frametitle
[\unexpanded\expandafter{\beamer@savedshortframetitle}]%
{\unexpanded\expandafter{\beamer@savedframetitle}}%
}
\temp
\@ifnextchar\bgroup\beamer@inlineframetitle{}}
\long\def\beamer@@frametitle[#1]#2{%
\beamer@ifempty{#2}{}{%
\gdef\insertframetitle{{#2\ifnum\beamer@autobreakcount>0\relax{}\space\usebeamertemplate*{frametitle
continuation}\fi}}%
\gdef\beamer@frametitle{#2}%
\gdef\beamer@shortframetitle{#1}%
\global\let\beamer@savedshortframetitle\beamer@shortframetitle
\global\let\beamer@savedframetitle\beamer@frametitle
}%
}
\global\let\beamer@savedshortframetitle\@empty
\global\let\beamer@savedframetitle\@empty
\makeatother
\begin{document}
\begin{frame}{The Frame Title}
A frame with a title
\end{frame}
\begin{frame}
A frame without a title
\end{frame}
\begin{frame}
\frametitle{The Next Frame Title}
A frame with a title
\end{frame}
\begin{frame}
A second frame without a title
\end{frame}
\end{document}
and the result:
:
-
Thanks. This codes does pretty much what I want (reuse old frame title as long as I don't set a new one), but in its current form it seems broken, because it will also put the frametitle on top of all "section page"s. Sep 29, 2016 at 9:45
This is a very unusual request. Maybe you can do without it.
Here is two solutions. The first two slides (with one frame) use \only
. The last two slides (with two frames) use \section
:
\documentclass{beamer}
\usepackage{lipsum}
\begin{document}
\begin{frame}
\frametitle{My first repeated title}
\only<1>{
\framesubtitle{first subtitle}
\begin{itemize}
\item first item
\item ...
\end{itemize}
}
\only<2>{
\framesubtitle{second subtitle}
\begin{enumerate}
\item \lipsum[2]
\item ...
\end{enumerate}
}
\end{frame}
\section{My long subject}
\begin{frame}
\frametitle{\insertsection}
\framesubtitle{first subtitle}
\begin{itemize}
\item first item
\item ...
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{\insertsection}
\framesubtitle{second subtitle}
\begin{enumerate}
\item \lipsum[2]
\item ...
\end{enumerate}
\end{frame}
\end{document}
I prefer this simple command because it can be easily adapted to your specific needs. And everything else is done as usual with \subsection{}
and \subsubsection{}
. As a nice side effect, every title and subtitle is a clickable reference.
\documentclass{beamer}
\newenvironment{slide}[0]{
\begin{frame}\ifx \insertsubsection \empty
\frametitle{\insertsection} \else
\frametitle{\insertsubsection}\framesubtitle{\insertsection} \fi
}{\end{frame}}
\begin{document}
\begin{frame}{Table of Contents}
\tableofcontents
\end{frame}
\section{my section}
\begin{slide}
first slide
\end{slide}
\subsection{my subsection}
\begin{slide}
second slide
\end{slide}
\end{document}
\end
And the result:
\begin{frame} ... \end{frame}
) where you want to reuse the frame title? If you aren't sure: here's a test question. Will your slides use overlays?