Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

Principles Around Writing Beamer Themes

1 Mar 2015

Using Beamer for making slides is comfortable if you stick to the many themes it comes with. Things become trickier if you want to write themes of your own.

1 Files

Local themes can locally be installed under a texmf tree, e.g. ~/texmf/tex/latex/beamer/base/themes. A quick check inside /usr/share/texmf/tex/latex/beamer/base/themes shows that themes come in colour, font, inner and outer flavours whose respective purposes are fairly obvious. If you want to write a theme called foo, it's fine to create a theme by writing the following files:

Note that for each flavour, there's always an existing theme called default, which is a fallback, so that if you don't define what a given element of your theme should look like, it'll at least be defined there. These default themes are also a good source of inspiration to get comprehensive lists of which elements are available because I found the documentation a bit lacking on that front.

2 File Overrides

Not that it matters much, and you should probably skip to the next section without further ado, but I took the opportunity to take a quick look at how themes with the same name override each other:

So it doesn't have to do with depth or lexicography, at least.

3 Modes

Most themes, regardless of their flavour, are clamped between:

\mode<presentation>
% ...
\mode
<all>

The \mode command has you enter a so-called gobbling state in which contents are typeset only in the corresponding mode. The last \mode call shown above causes to exit gobbling, which you must do eventually.

4 Defining Templates

What you essentially need to understand when it comes to writing Beamer themes, is that they are in essence a collection of calls to the \defbeamertemplate* command. You could probably call it without a star, but I've never seen anyone do so. The star version immediately calls \setbeamertemplate.

\defbeamertemplate<⟨mode specification⟩>*{⟨element name⟩}{⟨predefined option⟩}
    [⟨argument number⟩][⟨default optional argument⟩]{⟨predefined text⟩}
    [action]{⟨action command⟩}

In practice, you only pass this command 3 arguments. The first argument is the element name. The second one makes it unique. The third one defines it:

\defbeamertemplate*{footline}{foo theme}
{
  % ...
}

5 Colour Boxes

Making colour boxes is a common operation when designing themes. The beamercolorbox environment is used as follows:

\begin{beamercolorbox}[⟨options⟩]{⟨beamer color⟩}
  % ...
\end{beamercolorbox}

The environment comes with many options, and the most common ones are:

The so-called depth refers to everything that lies below the baseline. Interestingly, it seems advisable to set even a depth of 0pt in colour boxes to avoid the effect of the position of text below being affected by depth if there is any:

\begin{beamercolorbox}[dp=0pt]{title}
  \insertframetitle
\end{beamercolorbox}

6 Colours

They are set with the \setbeamercolor command and the star version discards any accumulation such that calling \setbeamercolor{section in toc}{fg=blue}, then \setbeamercolor{section in toc}{bg=white} comes down to the same as \setbeamercolor{section in toc}{fg=blue,bg=white}:

\setbeamercolor*{⟨beamer-color name⟩}{⟨options⟩}

Options:

It is fine to have \definecolor in presentation mode; some official themes do so too.

7 Including Images

I've seen images included in some official inner themes with \pgfdeclareimage, which it is fine to call in a presentation mode. The files are referred to without extension and are expected to be looked up in the art directory (e.g. /usr/share/texmf/tex/latex/beamer/base/art). Before they can be used, they must be declared:

\pgfdeclareimage{foo}{foo}

8 Fonts

The \setbeamerfont command lets you choose fonts:

\setbeamerfont*{⟨beamer-font name⟩}{⟨attributes⟩}

However, changing the default size of fonts when they aren't in any particular environment or element is problematic. Setting the font for elements such as normal text or even structure won't work, not least because many other font elements don't have structure as parent, and even fixing this doesn't solve the problem. One way is to just use the \fontsize command instead of \setbeamerfont. You would typically do so in your beamerfontthemefoo.sty file. The trick is to know what to set the baseline skip to. Some say that it's just 1.2× the font size:

\mode<presentation>
\AtBeginDocument{\fontsize{15}{18}\selectfont}

Another approach to setting the default font size is by using the scrextend package. Again, in your beamerfontthemefoo.sty file:

\usepackage{scrextend}
\mode<presentation>
\changefontsizes{15pt}
% ...

9 Providing Commands and the Case of Cover Slides

I don't know if it's common practice, but I like defining new commands right in the presentation themes. I do so for instance to add cover slides. These cover slides had some interesting logic in that they would set conditionals to:

  1. Set the footline template to not draw any footline.
  2. Call the (undocumented) \usebackgroundtemplate command.
  3. Make an empty frame.
  4. Set the footline template draw a footline again.

And similarly, you can define conditionals and lengths in the presentation theme, which I think makes sense especially if they're used in several of the themes it refers to.

10 References