## Sunday, February 3, 2008

### Shiny Presentations with Beamer

In the not too distance past, I was asked to convert a set of notes for a taught module to a set of presentation slides. The work presentation has become synonymous with the words Microsoft PowerPoint within recent years and virtually every presentation you see or hear about is created using PowerPoint. Fortunately, there's a group of people (mostly academics or those that enjoy the challenge (read: have free time)) who prefer to use a (free) cross-platform solution to the presentation problem (everything to me is considered to be a problem) and that solution is: LaTeX.

With LaTeX, one is able to do a multitude of things but, on its own, is mainly restricted to nicely formatted documents without having the hassle of needing to position everything to your liking. Yes, formatting is automatic... to a large extent. There are times when its complex formatting algorithms get is slightly wrong, so you have to enter some commands to get it back on track. I won't go into great detail with the LaTeX side of things as there are many examples around that will illustrate its power to make a professional looking document look... professional.

Beamer is a LaTeX class that allows you to create a Beamer presentation. Whilst this description is pretty vague (and hasn't answered the question "what is beamer?", I can assure you that it can produce professional looking presentations with great ease. If, however, you're looking to replace PowerPoint completely with all the flashy transitions and effects (and you are one of the few that enjoy the peeling text and/or the typewriter effect), this may not be for you.

Additionally, a Beamer presentation is created as an Adobe PDF file. Whilst this may not entirely be a bad thing, for slide transitions and basic animations, Adobe Reader is really the only software available that will display these correctly. You will, otherwise, have to move frame-by-frame through each animation which is almost as bad as the typewriter effect in PowerPoint. The nice thing is that by having the output as a PDF file, most people will be able to view the presentation without having to pay for extremely expensive software. To view a Beamer presentation, neither LaTeX nor Beamer need be installed - only when creating the presentation are these packages needed. Like most good things, there is one downside: you have to install most of this yourself. Beamer does not normally come with LaTeX and if you use a Mac or a Windows machine (or even some *nix distributions), you will have to install the packages yourself. Luckily, there are some distributions of LaTeX that come complete with Beamer (which saves the hassle of installing it manually), so I suggest you read the TUG and Beamer resources for more information on what to do.

So on the assumption that you've managed to install both LaTeX and Beamer, we'll jump into the deep end by attempting to describe what everything does. Beamer, by default, has a few presentation styles which are predefined and, by no means, restricted. You can set up your own template with its own colour set once you're familiar with the layout and syntax of both LaTeX and Beamer. To get started, I'll firstly define a basic slide.


The header information opens by telling LaTeX what type of document we want to create which, in this case is beamer. We have the option to create handouts or slides - I've added both (but commented out one so it will compile) and can be switched easily. On the \usepackage{...} side, we can specify what font packages (if any) we wish to use. As an engineer, I write documents with some equations and, more frequently, linear algebra. As a result, I've added the American Mathematical Society's mathematics class and font package. The AMS package allows me to (easily) create nicely formatted matrices and vectors, providing I remember the command. We then continue with the normal LaTeX preamble which specifies the document's owner and the document's title.

Creating a slide is fairly easy. You declare that you want to start a new frame by typing \begin{frame}, declare a frame title by using the subtly named tag \frametitle{My Title}, write some words down before finally closing the frame with the code \end{frame}. Easy. A title frame can be created without much fuss by using the code \titlepage between a \begin{frame} and \end{frame}. Table of contents? No problem: declare a frame as before and then use the tag \tableofcontents. LaTeX does the rest.

So, at this point, you could create some basic slides and handouts. Pictures can be added using the tag \includegraphics[options]{loction/of/file.ext}. Options are optional (so don't include the square brackets if you don't wish to specify anything) or you can specify exact measurements such as height=4cm and LaTeX will do just that without deforming the image (i.e. it keeps the same aspect ratio). Additionally, should you create handouts, the slides are rescaled meaning that you don't have to alter any of the measurements in order to cater for the smaller area. Graphics should be of certain file types: PDF files work as do .png files (I'm certain JPEG will work too, but others won't be as forthcoming due to the nature of the PDFLaTeX compiler).

There are numerous templates available (as I mentioned earlier) which are pre-installed with the Beamer class (on *nix and on Macs, the files are stored within your LaTeX path under tex/latex/beamer/themes/; MS Windows-wise... I'm unsure but shouldn't be difficult to locate). Try \usetheme{Berlin} which, in fact, utilises the beamerthemeBerlin.sty file within the theme directory. The theme files specify which colour, inner and outer themes to apply. The colour theme is, as its name suggests will alter the colour settings of the template whereas the inner theme alters the internal layout such as bullet points. The outer theme dictates the overall layout of the slides, so if you want slide numbers and other miscellaneous information on all your slides (e.g. logos), then the outer style is what you'll need to edit. When I first started playing with these files, I copied them to my local directory (so as not to corrupt the original files (again)) and then renamed the themes such that they wouldn't interfere with the predefined template styles.

The easiest and, quite possibly, simplest edit is the colour scheme. As every business entity knows, a corporate colour scheme using garish and often clashing colours is the route to success. The University is no exception, so I set about altering the colour style sheet to match the University's colour scheme. With a palette of baby blues and hot pink, I was half way to converting the University endorsed PowerPoint template with a Beamer template (which would, of course, save the patience and perhaps lives of the few that prefer Beamer). Colours can be defines with a Style file using RGB controls: \definecolor{sheffieldblue}{rgb}{0.16, 0.1, 0.44} where each value is between 0 and 1 represents the elements red, green and blue respectively. Remember that HTML and other scripting languages normally require colour values between 00 and FF (that's 00 and 255 in decimal) for each RGB value.

Unfortunately, the master template can be difficult to edit if you want to dramatically redesign it. Your best bet is to try to find a template that is close to the end product (or as close as you can get to it) and then begin butchering it. It's what I did in order to get a slide that resembles the University's own (no matter how painful it looks). Patience and a lot of looking up TeX and LaTeX commands will help you along your way. By this point, you should have a good template that you can now use for your presentations. I mentioned transitions and animations earlier - none of which I have experimented with but I have seen them in action elsewhere. As a result, I won't go into details.

So what about fonts? Do you miss the Windows fonts that you were so used to seeing in Microsoft PowerPoint presentations? Luckily, there's plenty of information here... plus you'll have the added joy of facilitating ligatures (yes, I know, it's a link to Wikipedia *shudder*) and other typographical features. The fonts that are provided with LaTeX already have these features enabled by default. The problem with ligatures arises when you've imported a TTF (TrueType Font) into LaTeX. First thing's first, how do you import a TTF font? There's a utility called ttf2tfm which should exist on *nix and Mac LaTeX installations and should come with the MikTeX distribution for Windows users. You'll also need a copy of the T1 encoding file T1-WGL4.enc which you can get from either Rakityansky's or Cheong's site. This file is basically a font descriptor file for the TrueType font layout. You will need to then run the command:

ttf2tfm times.ttf -q -T T1-WGL4.enc -v ectimes.vpl rectimes.tfm >> timesfont.mapttf2tfm timesi.ttf -q -T T1-WGL4.enc -v ectimesi.vpl rectimesi.tfm >> timesfont.mapttf2tfm timesbd.ttf -q -T T1-WGL4.enc -v ectimesbd.vpl rectimesbd.tfm >> timesfont.mapttf2tfm timesbi.ttf -q -T T1-WGL4.enc -v ectimesbi.vpl rectimesbi.tfm >> timesfont.map

For the above example, it takes Times New Roman's normal, italics, bold and bold & italics TTF files and parses them individually. For some fonts, you may only have the one TTF file. Rakityansky details how to create slanted versions of the Times New Roman font, so check that out if you wish to do just that. At this point, you have the font files which can be used as they are, but currently are ligature-less. Despite the ligatures existing in the converted TFM file (providing the TTF file had the ligatures), the ttf2tfm utility did not generate the necessary associations so we will have to manually add them (oh joy). To do this, we need to convert the TFM file to a .pl file using the tftopl utility which is used as follows:

tftopl rectimes.tfm rectimes.pl

The resulting .pl file will contain a list of ligature and character information which all seems quite strange at first. As pointed out by Cheong, it's simply a case of adding some lines along the lines of (pardon the pun):

(LIGTABLE(LABEL C f)(LIG C i D 28)(STOP)(LABEL C -)(LIG C - D 21)(STOP)(LABEL D 21)(LIG C - D 22)(STOP))

Line 2: If the character starts with "f"
Line 3: The next character is "i", use character number 28 (in decimal) (which should be the ligature for the combined fi - check the character font map of the TTF file)
Line 3:No more for this character
Line 4: If the character starts with "-"
Line 5: And the next character is also a "-", use the character number 21 (in decimal) which should give an en-dash
Line 6: No more for this ligature
Line 7: If the character is decimal 21 (i.e. line 5)
Line 8: And the proceeding character is a "-", use character number 22 (which should be em-dash).

For a font with more ligatures, you may be able to define ff, ffl and ffi:

(LIGTABLE(LABEL C f)(LIG C i D 28)(LIG C f D 27)(LIG C l D 29)(STOP)(LABEL D 27)(LIG C l D 31)(LIG C i D 30)(STOP)(LABEL C -)(LIG C - D 21)(STOP)(LABEL D 21)(LIG C - D 22)(STOP))

Check that you have these ligatures first otherwise whenever one types ffl or ffi in a document, it'll be replaced with a professional looking blank space. So once you've got your .pl file sorted out, you can recompile it back into the TFM file it once was. To do this, use the utility pltotf which is used as follows:

pltotf rectimes.pl rectimes.tfm

Whilst you could try using your new fonts, you may find that it won't work correctly as LaTeX will try and look for a font descriptor file (they have .fd as their file extension0. The font descriptor file tells LaTeX which fonts to use depending on the setting. In some cases, it may not be possible to define a font in particular condition (e.g. slanting), so by telling LaTeX to use a substitute, the text will still display without any major issues. For every font style, you specify a {n}ormal, {sl}anted and {it}alics definition. This can be done to each of the categories bold type {b}, normal {m} and bold-extended {bx}. The file descriptor file can look like:

\ProvidesFile{t1MyRomanFont.fd}[Description of font]\DeclareFontFamily{T1}{MyRomanFont}{}\DeclareFontShape{T1}{MyRomanFont}{m}{n}{<-> rectimes}{}\DeclareFontShape{T1}{MyRomanFont}{bx}{n}{<-> rectimesbd}{}\DeclareFontShape{T1}{MyRomanFont}{m}{it}{<-> rectimesi}{}\pdfmapline{+rectimes\space <times.ttf\space><t1-wgl4.enc} space=""><timesbd.ttf\space><t1-wgl4.enc} space=""><timesi.ttf\space><t1-wgl4.enc}></t1-wgl4.enc}></timesi.ttf\space>

At this point, you can use your fonts by using any one of two methods (possibly more, but these should suffice). The first is to renew either the RM (Roman) or SF (Serif) font set used by LaTeX by adding the following code to the header of your LaTeX file:


Whereas the second method is to simply call the font as and when you require it using something like:

\DeclareTextFontCommand{\textmysf}{\fontencoding{T1}\fontfamily{MySerifFont}\selectfont}\DeclareTextFontCommand{\textmyrm}{\fontencoding{T1}\fontfamily{MyRomanFont}\selectfont}

Again, this should go in the preamble of your document (i.e. before \begin{document} but after the \documentclass{...} tag).

You've got a lot of files now that were used in the transfer process of TTF to TFM. Unfortunately, some of these will have to remain. The TTF file will have to stay (as will the TFM file). I didn't go into detail as to what the .map file did but will now briefly mention it: the .map file pieces the italics, bold and italics & bold fonts together so when you request any of these options, LaTeX will automatically use the correct font. This is in addition to your font descriptor file .fd as described earlier.

Whilst this will work, the fonts will be compiled as bitmaps which aren't particularly useful. Rakityansky describes that one can create Adobe Font Metrics to overcome this problem. I won't go regurgitate what he has already written (as I doubt I can do a better job). If you do use this method, you'll notice that new TFM files will be created (which is something we already have), so this will mean you'll have to redefine the ligatures should you decide to overwrite the files.

### Cleaning up

As I mentioned earlier, at this point, you'll probably have lots of files. Some of these have to stay, but other don't. Those that stay should be the Font Descriptor files (.fd), the encoding file (T1-WGL4.enc), the TrueType Font file (.ttf), the font metric file (.tfm), the Adobe Font Metric File (if you proceeded down this route) (.afm) and the font MAP files (.map). The rest can go... with the exception of any LaTeX document, of course!