# Run macro before each slide in beamer

Following on from this question, is there an easy way to call a macro before each slide? So that question answers how to get at each frame, but I want finer control...

I want a macro that sets a counter to the current fraction of slides we've go through so far. I can't get at it through etoolbox's patching controls, because the slides are controlled more internally than that...

-
What are you going to do with this counter? (It can't be a counter if it's a fraction, but that's not the point) Can you not just compute it at the time of placement? – Matthew Leingang Dec 19 '10 at 23:19
I want it to change the colour of the structure elements, so "time of placement" is the start of the slide. – Seamus Dec 20 '10 at 13:04

\documentclass{beamer}
\newcounter{myctr}
\renewcommand\themyctr{frame~\arabic{myctr}}
\makeatletter
\def\beamer@checkframetitle{\stepcounter{myctr}%
\@ifnextchar\bgroup\beamer@inlineframetitle{}}
\makeatother
\begin{document}

\begin{frame}{foo}{\themyctr}
foo
\end{frame}
\begin{frame}{foo}{\themyctr}
foo
\end{frame}
\begin{frame}{foo}{\themyctr}
foo
\end{frame}
\begin{frame}{foo}{\themyctr}
foo
\end{frame}

\end{document}

-
This works. You could make your MWE neater by adding a couple of \pause s to show what it can do within a frame! Could you explain how it works? Is \beamer@checkframetitle the first thing beamer does with a frame? – Seamus Dec 20 '10 at 13:55

This works for me!

  \makeatletter

\g@addto@macro is defined in LaTeX, etoolbox has an exact clone, but I can't remember the exact command:)
@Matthew is right, this doesn't do what I want it to. The etoolbox version is, I think, \appto – Seamus Dec 20 '10 at 12:34