Principles Around Writing Beamer Themes
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:
beamercolorthemefoo.stybeamerfontthemefoo.stybeamerinnerthemefoo.stybeamerouterthemefoo.stybeamerthemefoo.sty
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:
~/texmf/tex/latex/beamer/themesoverrides~/texmf/tex/latex/beamer/base/themes;~/texmf/tex/latex/beamer/aa/themesoverrides~/texmf/tex/latex/beamer/base/themes;~/texmf/tex/latex/beamer/old/themesoverrides~/texmf/tex/latex/beamer/base/themes.- However,
~/texmf/tex/latex/beamer/.old/themesdoesn't override~/texmf/tex/latex/beamer/base/themes.
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:
wd={⟨width⟩}: width, which can be a function of\paperwidthdp={⟨depth⟩}: depth below the baselineht=⟨height⟩: height above the baselineleft: flush leftright: flush rightcenter: centreleftskip=⟨left skip): add left skiprightskip=⟨right skip): add right skip
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:
fg=⟨color⟩: foreground colourbg=⟨color⟩: background colourparent=⟨parent beamer-color(s)⟩: a shortcut to usingfgandbgto use other colours.use=⟨another beamer-color⟩: if you setfgorbgto an existing colour, you should refer to it first inuseto make sure you're up to date.
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:
- Set the
footlinetemplate to not draw anyfootline. - Call the (undocumented)
\usebackgroundtemplatecommand. - Make an empty frame.
- Set the
footlinetemplate draw afootlineagain.
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
- The The Beamer Class will only offer a reference and you'll need to jump all over the PDF guide to find your way.
- There is also a more helpful discussion explaining how to Design a custom Beamer theme from scratch, which summarises the exercise in a somewhat more pragmatic way.
- CERN LATEX Beamer Theme
- Confused with TeX terminology: height, depth, width
- fontsize - Custom Beamer Theme: Set normal font size
- How does \footnotesize map to \fontsize{size}{baselineskip}
- Changing the font size in LaTeX
- How to set a small default font size with beamer?
- Image on full slide in beamer package
- LaTeX conditional expression
