Run a macro before the start of each frame/slide?

Use the patching facilities of etoolbox to patch \frame to do what you want.

Since \frame is parameterless, it'd seem like you want to do

\preto\frame{\foo}

This will cause \foo to happen before the frame.

If you want things to happen after all of the argument processing, it's significantly more complex because it appears that beamer follows lots of different paths. (I sort of gave up after \beamer@@@@frame which then sets \beamer@howtotreatframe in a bunch of different ways depending on some conditions.)

Here's a simple example:

\documentclass{beamer}
\usepackage{etoolbox}
\preto\frame{\typeout{Starting new frame.}}

\begin{document}
\begin{frame}
asdf
\end{frame}
\begin{frame}
asdf
\end{frame}
\end{document}

Sounds like the ideal job for \BeforeBeginEnvironment

\documentclass{beamer}
\usepackage{etoolbox}

\BeforeBeginEnvironment{frame}{\typeout{Starting new frame.}}

\begin{document}
\begin{frame}
asdf
\end{frame}
\begin{frame}
asdf
\end{frame}
\end{document}

Old post, but I’ve been looking for something alike for some time and came up with a nice solution for this without any extra package so I wanted to share it.

For each frame, not slide, you could put in your preamble something like:

\let\oldframe\frame
\def\frame{\yourmacro\oldframe}

The point is that \frame macro is very fragile and it parses the options that comes after it so appending something to \frame will not work. This will let you prepend something to \frame without messing with the usual behaviour of the macro.

Also, note that this will affect both \frame{...} command and \begin{frame}...\end{frame} environment.

Tags:

Beamer