Beamer is a document class that is by far the most practical tool for making presentations involving data science, business analytics, or general research. It is widely used in most conferences and easily lends itself to data intensive reporting and repetitive batch processing.
A custom beamer template is presented that is easy to extend or modify. The benefits of the beamer document are numerous:
- Data and code objects are integrated directly into presentation and reporting objects,
- Costly copy-paste routines are eliminated whenever data or models change,
- Team collaboration can be enhanced through object sharing,
- Reproducible research is achieved since reported results are packaged with supporting data and models.
Switching from PowerPoint is justified when productivity and quality control increase. A complete user guide for coding custom beamer documents is here. Lessons learned are shown below.
Preamble
The preamble defines the document class and loads packages for
, the R programming language and Sweave. Sweave is an integration tool to link R and
. The preamble also serves to define the global defaults for the beamer theme and custom template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
%--- Preamble ---------------------------------------------------------% % Load LaTeX and R packages \documentclass[10pt, xcolor=dvipsnames]{beamer} \usepackage{Sweave} \usepackage[absolute, overlay]{textpos} % supports floating text in any location % Customize theme attributes \useoutertheme{infolines} % adds 3 box footer \usetheme[height=7mm]{Rochester} % choose theme \setbeamertemplate{blocks}[rounded][shadow=true] % rounded theorem box with shadow \setbeamertemplate{caption}[numbered] % enable counting of tables/figures % Define template colors \definecolor{QPblue}{RGB}{0,25,100} % define QP blue using RGB code \definecolor{QPgreen}{RGB}{0,153,110} % define QP green using RGB code \setbeamercolor{title}{fg=white, bg=QPblue} % define title page box color \setbeamercolor{frametitle}{fg=white, bg=QPblue} % define frame title color \setbeamercolor{normal text}{fg=black} % define standard font color \setbeamercolor{author in head/foot}{fg=QPblue, bg=QPgreen!75} % define infoline 1st box color \setbeamercolor{title in head/foot}{fg=QPblue, bg=QPgreen!60} % define info line 2nd box color \setbeamercolor{date in head/foot}{fg=QPblue, bg=QPgreen!30} % define infoline 3rd box color \setbeamercolor{block title}{fg=QPblue!50!QPgreen, bg=QPgreen!30} % define theorem box title color \setbeamercolor{block body}{fg=QPblue!50!QPgreen, bg=gray!10} % define theorem box body color \setbeamercolor{local structure}{fg=QPgreen!75} % define bullet and enumerate list colors % Define global environments \newenvironment{reference}[2]{ % define environment for footnotes \begin{textblock*}{\textwidth}(#1, #2) \tiny\it\bgroup\color{red!70!QPgreen}}{\egroup\end{textblock*}} % Define title page logo and project metadata \titlegraphic{\includegraphics[width=3cm]{QPlogo.png}\hspace*{0cm}~ } \title{The Beamer Document Class} \subtitle{Creating A Custom Presentation Template with \LaTeX{} Code} \author{Bradley J. Horn} \institute[New Energy Dept.]{ \textcolor{QPgreen!75}{Qatar Petroleum \\ Strategy \& Policy Directorate \\ New Energy Dept. \\ Doha, Qatar \\ [1ex] \texttt{b\_horn@qp.com.qa}} } \date{March 2014} |
R integration
Unlike
and R, Sweave is not extensive or complicated.
The basic Sweave syntax consists of a simple << >>= marker indicating that we want to insert and begin an R code section, followed by one or more R commands, followed by a @ marker indicating you are done writing R commands and wish to switch back to the document. The syntax is extremely simple. The only practical twist is the ability to put option commands inside the opening marker.
In the example below, the document and Sweave concordance is started. The first R command is to source an R script from the hard-drive for all data management and analysis. Sweave options launch the R code with console results or echoes turned off. This prevents unwanted script results or error messages from migrating into the
document. Code sourced by the
document runs in a unique environment so presentation data does not mask or corrupt analysis data already present in R.
1 2 3 4 5 6 7 8 9 10 11 |
% --- R integration ---------------------------------------------------% \begin{document} \SweaveOpts{concordance=TRUE} % Initialize and Source R Code <<eval=TRUE, echo=FALSE, results=hide>>= # Source analysis file source("/Users/bjh/@RProjects/LCOE/LCOE_regional.R") @ |
Title Page
Title page and document metadata is also defined in the preamble. The following lines are simple and include the title fields for the beamer theme (Rochester in this case) with the custom attribute that define the presentation template. Code comments provide additional detail.
1 2 3 4 5 |
%--- Title Page -------------------------------------------------------% \begin{frame}[plain] \titlepage \end{frame} |
Bullet Slides
Bulleted slides can include itemized and enumerated lists, as shown. The preamble has already formatted bullet colors, font type and size. Meanwhile, the slide presents bullets in two columns. The colored info-lines at the base of the slide are also formatted in the preamble and all text is controlled by the fields for the title page. Finally, navigation icons and advanced PDF functionality are present (e.g. see the marks along the bottom right edge of the slide). Navigation icons are turned off by inserting \setbeamertemplate{navigation symbols}{} in the preamble.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
%--- Slide 1 ----------------------------------------------------------% \begin{frame}{Bullets} Here is a sample slide that shows what \emph{itemized} and \emph{enumerated} lists look: \begin{columns} \begin{column}{0.45\textwidth} \begin{itemize} \item itemized item 1 \item itemized item 2 \item itemized item 3 \end{itemize} \end{column} \begin{column}{0.45\textwidth} \begin{enumerate} \item enumerated item 1 \item enumerated item 2 \item enumerated item 3 \end{enumerate} \end{column} \end{columns} ~\\[2ex] The sample code also defines 2 columns. \end{frame} |
Customizing Bullets
The command \setbeamertemplate{items}[ball]
changes the markers to simulated 3-dimensional balls. Other options include circle (2d balls), rectangle and default (triangles).
R Data Tables
If Sweave allows R to be run from a document, the R package Stargazer creates
code from within R. In more basic terms, Stargazer wraps R data objects with
code for pretty table formats, eliminating formatting, data alignment and table layout hassles. In this example, a chunk of R code is executed to generate the table and the R print function layers in additional table formats, including table size or scale. The
document will automatically update table labels and numbering.
1 2 3 4 5 6 7 8 9 |
%--- Slide 2 -------------------------------------------------------------% \begin{frame}{R Data Table} Here is a sample slide with a data table generated in R: <<eval=TRUE, echo=FALSE, results=tex>>= test <- xtable(lcoe, caption = "Levelized Cost of Energy (USD/MWh)", digits = 0, label = "tab:coef") print(test, include.rownames = FALSE, scalebox = 0.75) @ \end{frame} |
R Graphics
The ability to generate R graphics inside the beamer template is equally simple: a pre-existing graphic file is loaded directly (the approach used here), or the R graphics code is run directly from the document. Image placement, sizing and labeling remain flexible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
%--- Slide 3 -------------------------------------------------------------% \begin{frame}{R Graphics} An image must be one of PDF, PNG, or JPG. %\begin{center} \begin{figure}[!htbp] \centering \includegraphics[width=3.5in, height=2.25in]{image1.pdf} \caption{LCOE by Region and Technology} \label{fig:coef} \end{figure} %\end{center} \end{frame} |
Text Boxes and Shadows
The custom template also has predefined text boxes, including theorem, corolary and proof. Each of these have fixed or predefined titles and are pre formatted environments defined in the preamble. The theorem text box environment is shown in the example. Default settings used here include rounded corners. Rounded corners with no shadows is achieved with [shadow=false]
in the preamble.
The template also has a generalized text box that offers complete control over titles and body text using the command sequence \begin{block}{title of block} Some text… \end{block}. Finally, the package fancybox provides basic text box highlighting using \doublebox{}, \ovalbox{}, \Ovalbox and \shadowbox{}.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
%--- Slide 4 -------------------------------------------------------------% \begin{frame}{Text Box} Here is a sample slide to show how a "Theorem" text box will appear: \begin{theorem}[with math typesetting] Suppose $\Omega\in\mathbf{R}^n$ is a domain with smooth boundary. Then there exists ....: \[ \int_\Omega |\nabla u|^2 \,dx \ge \lambda \int_\Omega |u|^2 \,dx . \] \end{theorem} ~\\ Here is a more generalized text box: \begin{block}{My Title} My sample text .... \end{block} ~\\ Here are some of the text box examples from the \texttt{fancybox} package: \doublebox{A doublebox} \ovalbox{An ovalbox} \Ovalbox{An Ovalbox} \shadowbox{A shadowbox} \end{frame} |
Floating Text
The final slide in our custom template illustrates floating text, which is useful for footnotes or other ad-hoc needs. The package textpos is used in the preamble to define a reference environment for footnotes (see lines 32 to 34). The environment defines font type, size and color, and the environment is activated as needed using a simple function call. The environment requires two input arguments, the x and y coordinates where text will land on the page. A beamer slide has dimensions 128mm by 98mm with slide coordinates starting in the upper left-hand corner. Trial and error will ensure ad-hoc text lands where you want.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
%--- Slide 5 -------------------------------------------------------------% \begin{frame}{Floating Text} The \LaTeX{} package \texttt{textpos} makes it possible to put text objects in arbitrarily prescribed places.\\[2ex] With floating text, you define the environment name, font size, type, and color.\\[2ex] In this example, The \texttt{reference} environment is created to take two input arguments, which specify \texttt{x} and \texttt{y} text position.\\[2ex] Slide coordinates are defined relative to the top left corner. A Beamer slide has dimensions 128mm by 98mm. Trial and error ensures the ad-hoc text lands where you want.\\[2ex] Example below: \begin{reference}{4mm}{75mm} V. Jikov, S. Kozlov and O. Olenik, Homogenization of differential operators and integral functionals, Springer, 1994. \end{reference} \end{frame} \end{document} |