Collected: LaTeX Beamer Tips, Tricks, Workarounds
I recently compiled two 30+ minute talks in LaTeX Beamer. While the output of LaTeX usually is great, getting there is not. There are many things which can (and will) go wrong; many commands and techniques you only discover after a lengthy workaround; and, anyway, sigh, workarounds…!
The usual way to deal with any LaTeX insufficiency is to discover the solution in a post on StackExchange, you already read last week a couple of times. Or you read through some of the documentation and hope to find the solution in there.
Well, here follows a list of all the neat tricks I searched for, stumbled upon, or discovered myself. All somehow related to creating presentations in LaTeX: LaTeX Beamer, TikZ, and more.
Beamer
Handout from Same File
Apart from producing slides for presentations, Beamer has support for handouts built in. Adding [handout]
as a option to the beamer
document class will initialize the mode.
In handout mode, all overlay specifications are reduced to one same value: 1. If overlays in handout mode are still needed, those can explicitly given by adding | handout: 1
to the overlay specification, i.e. \only<2- | handout: 3>{}
(this will print its argument usually from overlay two on, but in handout mode only on slide three).
To typeset a handout version from the same file as the presentation slides, without adding and removing [handout]
all the time, I created a second file (ah-conference--handout.tex
) and added the following line:
\PassOptionsToClass{handout}{beamer}\input{ah-conference}
This will insert the whole content of ah-conference.tex
into the file and pass the handout
option to beamer
. Typeset as usual 1, done ✓.
Options to Slides
Beamer’s frame
environment accepts a few optional arguments which I did not know of before. Sure, they are all listed in the documentation, but this document also has 250 pages…
All of the following options are to be used as is ARG
in this snippet
\begin{frame}[ARG]
\end{frame}
fragile
: Needed to print source code (or any other verbatim block) onto the slideshrink=2
: Allow Beamer to shrink the content of the slide by the amount specified (in percent). Usually, shrinking means you have a lot of text on one frame – which is bad style. Buuut sometimes…
There’s alsosqueeze
which squeezes vertical space together.allowframebreaks
: Beamer will automatically create a second frame if the content is too large for one frame. The title of the frame is appended by a number. Good for glossaries and bibliographies. For the rest: Usually bad style.plain
: Removes all theme elements and paints the whole frame white.label={something}
: Gives a label to the slide, allowing for later reference (see below).
Overprint
For showing different, but connected images on the same slide, I choose Beamer’s overprint
environment.
\begin{overprint}
\onslide<1>
Something
\onslide<2>
Anything
\end{overprint}
Imagine the \onslide
s as \item
s in a list of overlays. Additionally, the space the overprint
requires on the frame is equal to its largest element (independent of the position in the range of overlays) – so no flipping around if a overlayed pictures is 2pt higher. Caveat: For handout
, overprint
must be manually annotated, since two \onslide
s must not overlap.
Disabling Beamer Features
Beamer produces slides, which should be concise and without cluttered information. It makes sense that the default settings of many themes are optimized in that regard and remove certain pieces of information which available usually in vanilla LaTeX.
For large / lengthy presentations which go into a lot of detail, this might not be the intended outcome, though.
Numbering of Figures
Calling a \caption{}
macro inside a figure
environment usually results in numbered figures. Not so in beamer (because captioning figures in slides usually is stupid).
I wanted to have a numbered figure in my appendix. For this, I needed to put the following into the preamble:
\setbeamertemplate{caption}[numbered]
Numbering in Bibliography
When using a bibliography, Beamer uses an icon of a document to illustrate an entry. This is alright if the bibliography is used as a read further here pool of supplementary information.
I wanted to use the bibliography to note where I took cited information and pictures from. This requires a referable number connecting the picture and bibliography entry.
The usual LaTeX behavior can be regained by adding to the preamble:
Changing Title Page
By means of the \addtobeamertemplate
macro, Beamer allows adding to the title page. I needed this to put a »Preliminary!« disclaimer to my slides.
Exactly this is done by the following snippet.
Short Titles in Table of Contents
LaTeX allows to set a shorter version of a section’s title by given an optional argument to \section
, i.e. \section[short]{Very long}
. This shorter version is for example used in the table of contents. Unfortunately, Beamer has no such option.
Thanks to a StackExchange answer you can redefine the \section
and \subsection
commands to accept short titles. This uses \patchcmd{}
of the etoolbox
package.2
\usepackage{etoolbox}
\makeatletter
\patchcmd{\beamer@section}{ {#2}{\the\c@page}}{ {#1}{\the\c@page}}{}{}
\patchcmd{\beamer@section}{ {\the\c@section}{\secname}}{ {\the\c@section}{ #1}}{}{}
\patchcmd{\beamer@subsection}{ {#2}{\the\c@page}}{ {#1}{\the\c@page}}{}{}
\patchcmd{\beamer@subsection}{ {\the\c@subsection}{#2}}{ {\the\c@subsection}{#1}}{}{}
\makeatother
Placing Things into Background of Slides
Sometimes it’s necessary to place content into the background of a slide. I used this to display a world map in the upper right corner of an introductory slide, which was gently overlapped by the remainders of a text block.
The macro \usebackgroundtemplate
places its arguments into the background of all following slides. The following example limits the scope of the background modification to the next slide by enclosing it in curly braces:
{\setbeamertemplate{sidebar right}{}%
\usebackgroundtemplate{
Background!
}
\begin{frame}
Hi
\end{frame}
}
Additionally I used \setbeamertemplate
to remove all contents of the sidebar right
group of the current scope. In my theme, this holds the logo; it might be called differently for your theme.
Referring to Slides
A frame
can be named as usual by adding \label{frame:abc}
. Unfortunately, this does not work reliably with \hyperref[]{}
.
The easiest solution:
- Add
[label={frame:abc}]
as an argument to the frame and - use
\hyperlink{frame:abc}{On slide ABC}
to link to it.
Horizontal Space
Sometimes, I have troubles with the left margin of slides. This is the case when I deviate from the usual bullet list style of presenting information and, for instance, show two lists next to each other via \begin{columns}
(pro and con lists, for example). The spacing leftwards does not fit anymore in those cases.
For some reason (which I still have to find out) the environments I usually use in those cases ignore a preceding \hspace*{-1em}
. Minipage, the knight in shining armor, does not.
Setting up a two-column layout which is slightly moved to the right becomes
\hspace*{1em}%
\begin{minipage}[t][2.5cm][t]{\textwidth}
\begin{columns}
\begin{column}{0.48\textwidth}
% left text
\end{column}
\begin{column}{0.48\textwidth}
% right text
\end{column}
\end{columns}
\end{minipage}
The same can be achieved by a somewhat simpler \makebox
, capuled into a command:
\newcommand{\shiftright}[2]{\makebox[#1][r]{\makebox[0pt][l]{#2}}}
Other
\againframe
is a neat little macro. It allows to insert a previously used (and named) frame again. The overlay specification can be different from the first appearance of the frame:\begin{frame}<1,2>[label=frame:outline]
and\againframe<2,3>{frame:outline}
.description
environments are great to explain named entities of one group of items. Sometimes, though, one name is quite long. Beamer’sdescription
environment allows to modify the indentation white space quite easily: By giving the longest word as an optional argument to the environment. E.g.\begin{description}[\bfseries\gls{opencl}]
will use the space of the bold, expanded glossary entry of opencl as indentation of all elements of thisdescription
.- FYI: There are
\frame{\sectionpage}
and\frame{\subsectionpage}
to insert logical markers throughout your presentation; this assumes working with\section
and\subsection
, which makes sense alone for the outline. - There’s also
\frame{\tableofcontents}
; I use this in conjunction with\appendix
to print a frame holding solely the ToC of the appendix. The main appendix I personally like to annotate by some additional text, usually some words about me or the talk.
TikZ
There’s a lot of learning involved when using TikZ. The following parts are just two (and some other) useful things. All the rest is sole learning by frustration.
Changing Styles with Overlay
A lot of TikZ’s macros are captured by Beamer’s chevron overlay specifications; i.e. \node<2->
is easy! But sometimes, an already existing node needs to be highlighted individually.
Sometimes, a simple \draw [\only<2->{draw=red}] {Hi!};
works. But I needed something more sophisticated.
The solution: Specify overlay and non-overlay styles outside of the TikZ picture and copy it to an status style overlay-dependent. Like this:
\tikzset{
no highlight/.style={fill=blue},
highlight/.style={fill=red}
}
\tikzset{status/.style={no highlight}}
\only<2->{\tikzset{status/.style={highlight}}}
\tikz{\node [status] {Look!};}
TikZ Mark
A solution to note a specific point in (continuous) text to refer to later on in a TikZ picture is a TikZ mark. It’s an empty, but labeled node at a given point:
\newcommand\tikzmark[1]{\tikz[overlay, remember picture] \node (#1) {};}
The node can then later be referred to: \tikz[overlay, remember picture]{\node [draw=green] at (marked) {Hello!};}
.
Other
|-
can be used to connect two points not by a straight, but by a corner-like line, like ┌.- GPU threads can be visualized by the
decorate, decoration=snake
style on a\path
. The amplitude and ending offsets can be given as well (and more!), see the PGF documentation. - There can be
tikzpicture
s intikzpicture
s. Good for specifying local coordinate systems and general drawing styles. - The
fit
option (from thefit
TikZ library) is super handy for boxing in nodes. So,\draw [fit=(a) (b) (x)];
will draw a rectangle such that it covers all three nodes specified. I discovered this way too late.
Glossaries
Cool: When using LaTeX Beamer, you can work with a glossary. For instance, to give people new to the topic a concise overview of all the technology involved in the topic.
\GlsSetXdyCodePage{utf8}
: I needed to manually set the encoding for xindy; but actually I don’t know if this is still needed for my final version.\renewcommand{\acronymfont}[1]{\somefont{#1}}
: This will change the font for acronyms to the one defined by\somefont{}
; same goes with\glsnamefont
\makenoidxglossaries
,\printnoidxglossary
: I ran out of writes for my document, what ever this means 3. Using the noidx versions of the\makeglossaries
and\printglossary
command solves the issue for me.\glsaddall
will add all defined entries into the glossary.
Other
-
The TeX developers seem to be working on a greatly improved version of
\newcommand
(and all it siblings) via the packagexparse
. Among others, it allows for specifying more than one optional argument with default values.\DeclareDocumentCommand{\command}{ O{blue} O{1cm} m }{ \vspace{#2}\textcolor{#1}{#3} }
The documentation is quite dense, but there’s a lot more to
xparse
. relsize
is a super handy package. It defines\smaller
and\larger
, but also\relsize{-3}
to, as an example, shrink text thrice.-
The correct way to transform a XeTeX /
fontspec
\newfontfamily
to a command which sets given text in a certain font is\DeclareTextFontCommand
:\newfontfamily\dingbats{Zapf Dingbats} \DeclareTextFontCommand{\textding}{\dingbats}
-
You should use
latexmk
for typesetting, by the way. It’s great. Also, it can be steered by a.latexmkrc
file on a per-folder basis. ↩ -
I needed to include space in between to opening curly braces,
{ {
, for my blog to render the code properly. I think the code still works, though. ↩ -
The actual error message is
! No room for a new \write.
. ↩