# Multiple colour themes in one beamer presentation

I'm preparing a single seminar talk on three more-or-less unrelated results. In order to visually separate these results, I wanted to give each one a different (custom) colour theme for the corresponding beamer slides. I know I could make three different presentations and stitch their pdf output together, but I'd prefer not to do this. So, my question is:

Is there any way to switch colour themes partway through a beamer document?

-

This sort of works. \usecolortheme{foo} uses a package beamercolorthemefoo.sty, which is why it can only be used in the preamble. But if you neuter the commands in the .sty file which make it a package, and \input it instead, you can get those commands anywhere you want. So:

\documentclass{beamer}

\newenvironment{colortheme}[1]{
\def\ProvidesPackageRCS $##1${\relax}
\renewcommand{\DeclareOption[2]}{\relax}
\renewcommand{\ProcessOptions}{\relax}
\makeatletter
\input beamercolortheme#1.sty
\makeatother
}{}

\begin{document}

\begin{frame}
\begin{theorem}[Pythagoras]
If $a$ and $b$ are the legs of a right triangle and $c$ is the hypotenuse, then
$a^2 + b^2 = c^2$
\end{theorem}
\end{frame}

\begin{colortheme}{albatross}
\begin{frame}
\begin{theorem}[Pythagoras]
If $a$ and $b$ are the legs of a right triangle and $c$ is the hypotenuse, then
$a^2 + b^2 = c^2$
\end{theorem}
\end{frame}
\end{colortheme}

\begin{colortheme}{crane}
\begin{frame}
\begin{theorem}[Pythagoras]
If $a$ and $b$ are the legs of a right triangle and $c$ is the hypotenuse, then
$a^2 + b^2 = c^2$
\end{theorem}
\end{frame}
\end{colortheme}

\end{document}


The trouble is that some of the themes have optional arguments and this is going to ignore them. You would have to reprogram \DeclareOption and \ProcessOptions to do what you want outside of a package file. That could be possible with the pgfkeys package.

Also, there might be other commands that are specific to packages and need to be neutered. Grepping a few of the other color theme packages I see \DeclareBeamerOption and \ProcessBeamerOption.

I think a nice enhancement to beamer would be to change the theme implementation so that this could be done easily. You could have \loadcolortheme{foo} in the preamble which sets up and configures the theme, but doesn't call any of the \setbeamercolor commands until \usecolortheme{foo} is found. For backwards compatibility \usecolortheme can check if the theme is loaded first.

-
I like the idea of your proposed enhancement. Something like the geometry package's "savegeometry" and "restoregeometry". – Loop Space Nov 11 '10 at 16:54
@Andrew Stacey: I would prefer something analogous to the template commands \defbeamertemplate, \setbeamertemplate, \usebeamertemplate. Now that I think about it, it might be possible to implement color themes as templates! – Matthew Leingang Nov 11 '10 at 19:22

A hacky solution is just to copy the contents of the colour theme .sty files (they aren't long) into a new tex file, cutting out the top and the bottom parts. So for example, for the whale theme just having a file containing:

\setbeamercolor*{palette primary}{use=structure,fg=white,bg=structure.fg}
\setbeamercolor*{palette secondary}{use=structure,fg=white,bg=structure.fg!75!black}
\setbeamercolor*{palette tertiary}{use=structure,fg=white,bg=structure.fg!50!black}
\setbeamercolor*{palette quaternary}{fg=white,bg=black}

\setbeamercolor*{sidebar}{use=structure,bg=structure.fg}

\setbeamercolor*{palette sidebar primary}{use=structure,fg=structure.fg!10}
\setbeamercolor*{palette sidebar secondary}{fg=white}
\setbeamercolor*{palette sidebar tertiary}{use=structure,fg=structure.fg!50}
\setbeamercolor*{palette sidebar quaternary}{fg=white}

\setbeamercolor*{titlelike}{parent=palette primary}

\setbeamercolor*{separation line}{}
\setbeamercolor*{fine separation line}{}


And then \input this just before you want the change to happen. Between frames.

One problem with this solution is that if you then change themes again, weird things will happen if theme 2 sets a colour, but theme 3 doesn't set it (because it leaves it as the default), you'll get the theme 2 colour showing through. So you'd need to add lines to your excised .sty files in order to reset it to default.

I'm sure there is a neater solution, however. It really should be easy to allow beamer to change colour themes (and other themes) on the fly. The only reason it can't is that the colour themes are in .sty files: the actual commands they define are, for the most part, valid in the body of the text...

Edit There is now a package on CTAN (which I maintain) that allows you to change the colour of a beamer presentation "on the fly". It doesn't, strictly speaking, change the colour theme, just the base colour of the structural elements. Here's an MWE:

\documentclass{beamer}
\usepackage{colourchange}
\useoutertheme{infolines}
\begin{document}
\selectmanualcolour{red}
\begin{frame}
This is a frame
\end{frame}
\selectmanualcolor{green}
\begin{frame}
This is a frame
\end{frame}
\end{document}

-