# Accessing other slides' titles in Beamer

Suppose I'm making a custom theme for Beamer presentations, and on each frame I'd like to display not only the frame's title itself, but also the titles of the previous and next frames (de-emphasized in some manner, of course). Does anyone know whether there's some way to construct a macro that I can use to access the title of a neighboring frame? Or whether this has already been done somewhere?

-

Here's a solution to insert access other slides' frame titles (in contrast to other section titles as in my first answer). The procedure is very similar, so I'll just explain the parts which have changed:

\usepackage{etoolbox}
\makeatletter
\newcommand*{\frametitleentry}[3]{\@namedef{frametitleshort#1}{#2}\@namedef{frametitle#1}{#3}}
\AtEndDocument{\if@filesw\newwrite\tf@frm\immediate\openout\tf@frm\jobname.frm\relax\fi}
\@input{\jobname.frm}
\makeatother


As in the first solution, the command capable of the title (\beamer@@frametitle this time) is patched in order to save the frame title. However, there's no storage of the frame titles in the .nav file, so we'll have to create a new auxiliary file .frm for this purpose. The frame titles are written to the .aux file at first and are flushed to the .frm file at the end of the document.

This file is input at the beginning of the next LaTeX run, where it is used to store the frame titles in the macros \frametitle1/\frametitleshort1, ... These values are read by the user macros \insertpreviousframetitle/\insertnextframetitle.

### Usage:

Put the above code into the preamble of your document. Now, you'll be able to insert the previous and next frame titles with the macros \insertpreviousframetitle/\insertnextframetitle. The starred forms \insertpreviousframetitle*/\insertnextframetitle* yield the short frame title you can specify in the optional argument of the \frametitle command. The macros can also take an optional argument: \insertnextframetitle[2] for example inserts the title of the next frame but one.

(Of course, you must supply a frame title in the desired frame, otherwise, the output of the commands will be empty.)

-

You can define commands to insert the previous and next section title like this:

\makeatletter
{%
\def\sectionentry#1#2#3#4#5{{\global\@namedef{secname#1}{#4}\global\@namedef{secnameshort#1}{#2}}}%
\def\slideentry#1#2#3#4#5#6{}%
}%
}
\makeatother


Put this code into the preamble of your document (between \documentclass{beamer} and \begin{document}).

It works as follows: The first few lines execute code at the beginning of the document which processes the entries of the .nav file: It defines \secname1, \secname2, ... and \secnameshort1, \secnameshort2, ... commands storing the long and short section titles of all sections (the processing itself is started with \dohead).

After these values have been saved, you can read them with the macros \insertprevioussection/\insertnextsection. They temporarily de-/increase the counter section containing the current section number in order to read the title of the previous/next section. The starred forms \insertprevioussection*/\insertnextsection* return the short section title you can specify in the optional argument of the \section command.

### A small demonstration:

\documentclass{beamer}
\usetheme{Ilmenau}
\makeatletter
{%
\def\sectionentry#1#2#3#4#5{{\global\@namedef{secname#1}{#4}\global\@namedef{secnameshort#1}{#2}}}%
\def\slideentry#1#2#3#4#5#6{}%
}%
}
\makeatother
\newcommand*{\demo}{\begin{frame}
Previous section: \insertprevioussection \\
Previous section (short): \insertprevioussection* \\
Next section: \insertnextsection \\
Next section (short): \insertnextsection* \\
\end{frame}}
\begin{document}
\section{First section}
\demo
\section[short (2)]{Second section}
\demo
\section{Third section}
\demo
\end{document}


Result:

### Edit:

If you want to know more then the previous and next section title, you can use an optional argument: By defining

\newcommand*{\insertprevioussection}[1][1]{\bgroup\advance\c@section by -#1\relax\@ifstar{\@nameuse{secnameshort\the\c@section}\egroup}{\@nameuse{secname\the\c@section}\egroup}}


you can get the title of the section before the previous section with \insertprevioussection[2], the section title after next with \insertnextsection[2] etc.

-
That could definitely be useful, though ideally I'd like to know if this can be done with frame titles instead of section titles, so that I wouldn't have to put every slide in its own individual subsection. – David Z Apr 27 '11 at 20:02
@David Sorry, I didn't read your question carefully enough :-( I've posted a new answer (in order to have a better distinction) solving the problem for frame titles, but I'll keep this answer in case someone might find it helpful. – diabonas Apr 29 '11 at 11:02

I can think of two possibilities, but I'm not sure this is what is intended in your case. First, there are specific Beamer theme that allows you to display an outline in a dedicated frame, like Bergen, Goettingen, Marburg, Berkeley, or Hannover. Second, you can show your TOC at the beginning of new section, by adding

\AtBeginSubsection[]{
\begin{frame}[plain]{Outline}
\tableofcontents[currentsection,currentsubsection]
\end{frame}
}


to your Beamer template. This has the disadvantage that this not displayed on the current slide. However, maybe you can look around the aforementioned templates and see how sectioning is handled internally.

In both cases, it would mean that every slide should be (sub)sectionned, which I guess might not be very convenient for you although a solution to automate this has been proposed in an earlier thread.

-
Thanks... you're right, that doesn't really give the effect I want, but if/when I have time I'll look around and see if it inspires anything. – David Z Apr 15 '11 at 1:22