# mlhutchins/brownbags

Switch branches/tags
Nothing to show
Fetching contributors…
Cannot retrieve contributors at this time
575 lines (426 sloc) 20.5 KB

# An Introduction to LaTeX/Beamer Slides

This is a simple introduction to getting setup and running with LaTeX to generate slide decks. It is by no means comprehensive and really just includes slide types that I use in my work.

LaTeX is a markup language where you separate the content from the styling and layout. For example you say that a chunk of text is to be centered with:

\begin{centering}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nam vitae metus scelerisque, suscipit magna ac, tincidunt massa.
\end{centering}


Several main advantages of LaTeX, specific to our work and slide deck creation are:

1. Images are added by links to the files, so if you update an image you just need to rebuild the PDF - not reinsert into a PowerPoint
2. Since it is plain text you can fully version control the document
3. It outputs consistantly styled PDF slides
4. Watermarking is easy
5. Automatic slide generation and slide reuse is much simpler than word

A few disadvantages compared to PowerPoint are:

1. It can be difficult to make minor styling adjustments
2. Complex slides (overlapping images, floating text, etc) are difficult and sometimes frustrating to add
3. There is a learning curve compared to switching to another PowerPoint like software
4. Once created the PDFs cannot be changed by others (may be a positive)

But once you have a template in place, or have created specific slide styles, it is saves massive amounts of time to recreate or mass-produce the same type of slide deck. It can often be as simple as a find-and-replace on the filepaths for the images.

## Installation

Installing TeX can be a little daunting compared to most other software; it requires installation of LaTeX itself and usually a LaTeX editor. A plain text editor (Notepad, TextWrangler) can be used but it is better to have an editor that allows you to build and preview the documents.

### Linux

Installing TeXLive in Linux is a simple

sudo apt-get install texlive-full


that will take about six hours to install (TeX has a lot of tiny files).

### Windows

For Windows I recommend the full installation of TeXLive.

### Mac

For OS X the best distribution will be MacTeX.

### Text Editor

I am a huge fan of Sublime Text but feel free to use you favorite code editor. Most full featured code editors have a TeX plugin that allows you to build with a button or shortcut (e.g. ctrl+b) and then display the PDF.

## PDF Output

LaTeX is mainly used for producing nicely typeset reports with embedded math equations, for our main use case we will use the Beamer class to produce slide decks. There are other packages and classes such at TikZ that allow you to programmatically create figures or Beamer can be used to make posters. LaTeX is technically Turing Complete so it can be used as a general purpose programming language. I do not recommend that.

# Writing LaTeX

So now that it is all installed and you have a text editor set up (right?) let us see what LaTeX code actually looks like. First a basic slide deck with a single title slide is made with:

\documentclass{beamer}

\mode<presentation> {

\usepackage{graphicx} % Allows including images

%----------------------------------------------------------------------------------------
%	TITLE PAGE
%----------------------------------------------------------------------------------------

\title{RootMetrics Beamer Template}

\date{\today} % Date, can be changed to a custom date

%--------------
%	Begin
%--------------

\begin{document}

\begin{frame}
\titlepage % Print the title page as the first slide
\end{frame}

%--------------
%	Content
%--------------

} \end{document}


The first three lines:

\documentclass{beamer}

\mode<presentation> {

\usepackage{graphicx} % Allows including images


Start the document and specify what LaTeX packages we want to include. Usually I include other packages as well that allow for more flexibility and remove some annoying warnings (at least on my system). So I may add:

\usepackage{media9} % Allows the use of \toprule, \midrule and \bottomrule in tables
\usepackage{lmodern} % Enables additional fonts to remove 2 warnings
\usepackage{amsmath, natbib, multirow} % Math formatting, references, and multirow-table


The next section is the title page with:

\title{RootMetrics Beamer Template}

\date{\today} % Date, can be changed to a custom date


The first is the title, the second the author, and the last is the date to display. The \today puts in the date the PDF was compiled but it can be any date (or delete it for no date).

The document actually startsd with the next command:

\begin{document}


Where everything between this and the last line of } \end{document} are the contents of the presentation.

Each frame is bracketed with a

\begin{frame}
Frame content here
\end{frame}


And the only frame we have is the title frame that looks like:

\begin{frame}
\titlepage
\end{frame}


Let us start to look at other types of frames we can have!

## Plain Text

There are two important considerations when writing simple text in LaTeX:

1. Whitespace and newlines
2. Special characters

A single newline character does not create a newline the document, two newlines are required. So the two sections produce the same output:

The first line.
The second line.

No line break.  Renders the same.


The advantage of the ignore single newline is that it can clean up the text and allow for easier version control.

The force a new line without the double newline is to use the \\ command. You may want to use this in a title for example, it is easier to write

\title{Beamer Brownbag \\ It is the bee's knees}


The common special characters to watch out for are \$, %, _, and &. They can all be escapeed with a slash, so AT&T would be written AT\&T.

A simple text slide would look like:

\begin{frame}
\frametitle{My First Real Slide}
This is a simple text slide.
It has a very lines in it.
Not to mention an escaped \%.

I do want to include a new paragraph, mostly to not talk about AT\&T.
\end{frame}


## Lists and Bullets

While plain text is all well and good the most used text format for slides are the beloved bullet list and numbered lists. I mean, what presentation can get by without 21 bullet point paragraphs on a single slide?

Fortunately they are easy to do in Beamer: bullet points are itemize and numbered list are enumerate. Each item in the list begins with an \item  command and everything that follows is part of that bullet. This means that a sublist will just require you to begin a new list in a current item and TeX will take care of the auto-indenting, numbering, subnumbering, etc. It also intelligently updates the numbers to the first level will be 1, 2, 3 and then the next a, b, c and then i, ii, iii, and then something else. I have never gone that deep before, it is unexplored territory where no one should venture.

So here are few different list frames:

\begin{frame}
\frametitle{Frametitle}
\begin{itemize}
\item Itemized lists are good
\item Use consistent punctuation
\item Include differing \texttt{typesetting}
\end{itemize}
\end{frame}

\begin{frame}
\frametitle{Frametitle}
\begin{enumerate}
\item First
\item Second
\item Third
\end{enumerate}
\end{frame}

\begin{frame}
\frametitle{Frametitle}
\begin{itemize}
\item The first points is simple
\item The second has some caveats
\begin{itemize}
\item There were not enough samples for this point!
\item Not to mention the phones were in tinfoil boxes the entire time
\end{itemize}
\item The third item is again easy and simple
\end{itemize}
\end{frame}


## Figures

Figure slides are probably the must important part of any slide deck; why would you even bother with a slide deck with some sort of graphic to include? The basic command for adding a figure (png, jpg, pdf, etc) is \includegraphics{path-to-file.png}. However this lacks two things that I find indispensible to inserting figures:

1. It does not automatically escale _ characters
2. It does not auto center the plots!

Well that is now annoying. Luckily you can write macros and define new commands in TeX to cope with situations just like these, and I have already written one. In the TeX document you can define commands, macors, colors, watermarks, and other features in the area before the \begin{document} command. The common macro I use for new figures is:

%-----------------
%	Macros
%-----------------

\newcommand{\PlotFrameB}[2]{%
\begin{center}
\includegraphics[scale=#1]{#2}
\end{center}\endgroup}

\def\PlotFrame{\begingroup
\catcode\_=12
\PlotFrameB}


Now this may look scary but what it does is allow figures to be specified with just:

\PlotFrame{0.35}{path/to/file_name.png}


Where the first argument, 0.35, defines how to scale the corresponding image. If you are using my utilities package, the default plotSave size is full framed in the slide at a scaling of 0.35. Once this is defined (or feel free to use \includegraphics) a frame with a figure looks like:

\begin{frame}
\frametitle{Sweet Figure Brah}
\PlotFrame{0.35}{barchart.png}
\end{frame}


## Frame decorations

The following are what I am unofficially calling frame decorations. They are things that you can add to a frame or ways to structure them that are not that necessary but may be useful.

### Frame and Subtitles

You may have noticed that the examples have included frame title and frame subtitle commands. These are, respectively given by:

\begin{frame}
\frametitle{I Am The Best Frame!}
\framesubtitle{Clearly not a Sprint Frame}
\end{frame}


That was easy!

### Only

The biggest flaw with TeX slides is the lack of awesome utransitions. What we have instead of only sections of a frame. These sections will only display at the given time markers so you can have the rest of the slide remain the same. A common use case is when you have the same frame title or same title and subtitle but want to change the figures in each slide. The basic only transitions look like

\begin{frame}
\frametitle{This Wont Change}
\only<1> {
}
\only<1> {
}
\end{frame}


The number in the angled braces designations which phase of the frame the contents should be displayed. This can be a simple number, <1> or designation a range <2-3>, or until end / from beginning <2->, <-4>. It is important to note that numbering begins at 1 and empty slides can be made.

\begin{frame}
\frametitle{This Wont Change}
Some Text On Every Slide
\PlotFrame{0.01}{a_tiny_image.png}
\only<4> {
}
\only<5-7> {
}
\end{frame}


This will produce a set of frames with the same text and tiny image at the top of each one with frames 1 - 3 duplicates of each other, frame 4 with a download plot, and 5-7 with the same upload plot.

### Section

Sections can be defined outside of frames with the \section{name} command and subsections with \subsection(name2}. You can even go several levels deeper with \subsubsubsection{name4} if you so desire. But what can we do with sections?

\begin{frame}
\tableofcontents
\end{frame}


That wasn't so hard. LaTeX will automatically update it as you add, move, remove, or rename sections.

You can also add section frames that are styled frames with just the section name on them. Depending on theme you are using (more on this later) it can be as simple as:

\sectionframe


With no \begin or \end needed!

### Forced Whitespace

Sometimes you just want some whitespace! You can force spaces with a ~, especially useful as it prevents text wrapping, such as units from a number. For larger whitespaces you can specify vertical whitespace with \vspace{0.2in} and horizontal whitespace with \hspace{0.4cm}. The distance can be given in different units for inches in, centimeters cm, pixels px, and default LaTeX units (no specifier). The default units correspond to some magical unit system I would not recommend learning.

## Tables

I knew this section was coming and I feared it from the outset. Tables in LaTeX are hard. This is because the document needs to contain all of the formatting code along with all of the data code. My best suggestion it to start with excel2latex and use Excel to make table (this will not propagate most formatting). Or use an online tool like Tables Generator.

For example here is a simple LaTeX table:

\begin{tabular}{ | l | l | l |}
\hline
{\bf Title 1} & {\bf Title 2} & {\bf Title 3} \\ \hline
A & B & C \\ \hline
A & B & C \\ \hline
A & B & C \\ \hline
\end{tabular}


Rows are separated with \\ and columns with &. The number of columns is given right after \begin{tabular} with a string like {c c c} for all centered columns, or right or left depending on the justification. In the column specifier the pipe | says whether to add a border between those columns. Likewise an \hline will add a line between rows.

It can get horrifyingly complex fast.

But if you make the same type of table often I recommend make a function/macro in your analysis software that takesa common format and spits out the corresponding LaTeX table.

Or avoid tables, no one wants to see them anyway!

## Slide Layout

Okay so far, you may be thinking, but what about slide layouts? I have made no mention about things like two columns, quadrants, two rows, etc. Beamer can do these things - they just require the right commands since there are no buttons to press.

Columns can be specified with the native column environment (what a crazy naming scheme):

\begin{frame}
\frametitle{Indoor National}
\begin{columns}
\column{0.6\textwidth}
\PlotFrame[0.35]{indoor_rollup.png}
\column{0.4\textwidth}
\begin{itemize}
\item{I heard Neal is in charge}
\item{We want indoor national performance}
\item{It should be easy!}
\end{itemize}
\end{columns}
\end{frame}


Where the whole thing is wrapped inside \begin{columns} ... \end{columns} and the column width is given in fractional frame widths \column{0.6\textwidth}. Inside each column you can treat it just like any other frame content. I also prefer to add tabs (tabs are ignored) to make it clear where each column begings/ends.

Rows are harder to define and may require manual adjustment with \hspace or empty lines. I tend to stick with simple frame layouts so this is mostly out of my realm of experience.

## Includes

A super near (in my mind) feature of LaTeX are \include statements. These let you write portions of a document in different files and the compiler will pull them all together for you. So if you have some standard text that always goes into a presentation you do not need to copy/paste it in everytime, instead write it to a separate .tex file and link it in!

\begin{frame}
The final results of the product
\end{frame}

\include{path/to/methodology.tex}
\include{path/to/legal_stuff.tex}

} \end{document}


This has the advantage of only requiring slide updates in one place that propagate on the next compile. They can also be used to abstract out unruly code sections such as tables:

\begin{frame}
\frametitle{Call Failure Rates}
\include{call_failure_table.tex}
\end{frame}


This can create a lot easier to read presentations and allows faster reuse of code.

## Themes

Beamer Themes. I have a love/hate/hate relationship with beamer themes. They are horrible to set up and sometimes awful to install, but once they work they are the greatest thing since machine sliced bread. The default available Beamer themes are politely referred to as ugly. Lucky for you (and unlucky for me once you want an edit) I have a RootMetrics Beamer Theme already created! This has 100% not been validated by anyone, but no one has noticed it was different so it all works out.

The theme and coarse installation instructions are available on Gitlab and is called RootTeX.

Here is where TeX begins to shine. To completely convert over to the beamer theme you add:

\usetheme{Root}


To the start of your file right after \mode<presentation> {. That is it!

There are two options to the theme (given in square brackets, comma separated):

\usetheme[RootFont]{Root}
\usetheme[Halloween]{Root}


If you have Proxima Nova (the official Root font) installed in a place TeX can find it the [RootFont] option will use it throughout the presentation. If it is the month of October you are required to add the [Halloween] option that will change the Root logo in the corner of the slide to a Halloween themed logo that I stole from Tia. Of course I will drop everything to add alternative logos for other holidays.

### Aspect Ratio

The default Beamer aspect ratio is 4:3, the standard for computer displays for a long time. However the world has seen the light of the widescreen formats of 16:10 and 16:9 with the official (last I checked) PowerPoint template for Root having a 16:10 aspect ratio. To get that aspect ratio for Beamer just change the first line of the document from:

\documentclass{beamer}


To:

\documentclass[aspectratio=1610]{beamer}


All components fo the Root theme should be invariant under this change in aspect ratio, if not let me know! Other aspect ratios are also available: 169, 149, 54, 43 and 32 for 16:9, 14:9, etc.

## Templates

In this last section I want to highly encourage the use and development of Beamer templates for your own personal use. I use Sublime Text 3 as my TeX Text editor of choice and have developed a host of snippets to use for common Beamer structures, including a blank presentation. Doing this will save time and reduce a lot of frustrations, especially with tables and complex slide formats. A couple are:

1. Basic presentation (won't compile due to non-existant figures)
2. Watermark every slide (goes in the header)
3. Syntax highlight SQL code (listings package)

# Examples

Here are some presentations and documents that I have written in LaTeX:

1. us-device-selection.tex - PDF
2. phase2-us.tex - A monstrously long PDF
3. saturation.tex -PDF A simple experiment report
4. long_call.tex - PDF A written report about an experiment
5. For a longer document: Thesis.tex - PDF A written thesis (note use of \includes!)