## Latex, Beamer, Python, Beauty

### February 25, 2009

This post quickly shows how to create pretty latex beamer slides, that don’t look anything much like maths seminar slides, and  that can include well formatted (Python) code, as well as the normal stuff that Beamer is so good at (maths, videos, etc). I’m assuming you’ve got a standard’ install of latex (by standard I mean: like mine) that includes beamer and the listings package. I’m also assuming that you already know how to use beamer, so I’ve not included all the documentclass junk and so on. Here’s a taster of what it looks like, how to do it is after the break:

There are three things we need to do to make beamer slides like this that don’t look like those endless maths-y beamer slides that everyone seems to like for some reason.

1. Get rid of all the junk on the top and the bottom! No-one can see it anyway!
2. Make the colours of the presentation very pretty.
3. Make the listings use a better font than courier, and also use pretty colours!

1: get rid of all the junk

This is an awesome command. It’s (maybe) the longest single command ever in the world, sort of like that village in Wales with the huge name:

/usetheme{default}


So now, your beamer presentation is nice and clean and happy.

2: Make Pretty Colours

This is just like knowing the magic words. We define a colour, then we assign it to a magic word. So lets define three colours, a background, a foreground and a title colour:

/definecolor{fore}{RGB}{249,242,215}
/definecolor{back}{RGB}{51,51,51}
/definecolor{title}{RGB}{255,0,90}


These colours are awesome but obviously you can choose your own if you’re a philistine and disagree. I’ve chosen the names (like fore), but they can be whatever you like. You can use other colour specifications instead of RGB, though using RGB for a presentation makes sense to me because it’s probably never going to be read seriously on paper. Now we need to tell beamer to actually use them:

/setbeamercolor{titlelike}{fg=title}
/setbeamercolor{normal text}{fg=fore,bg=back}


there are millions of these magic words like ‘titlelike’ but no-one will tell you what they are.

3: Make the listings pretty!

To include code listing (like Python) we use the listings package because it’s brilliant. We need:

/usepackage{listings,bera}
/definecolor{keywords}{RGB}{255,0,90}
/lstset{language=Python,
keywordstyle=color{keywords},


The packages are for the listsings (clearly) and bera is the font. It’s secretly Bitstream Vera but they had to change the name or something because the world is messed up! Anyway bera is a lovely font in my honest opinion and you should use this and not courier which is ugly and very last century.

The colours are for comments and keywords which you’ll see in a sec, be patient.

The funny lstset command tells the listings package some things. The first is that we’re using Python. There are other languages, I think, but why we wouldn’t be using Python is beyond me. Then we ask listings to make our keywords the colour we defined above (pink!) and our comments to be the comment colour and also emphasised!

Finally, we must make a sample page to demonstrate the lstlisting environment, and because there’s one more important thing that is NOT OBVIOUS! Here’s a sample page:

/begin{frame}[fragile]
/frametitle{A Page}
/begin{lstlisting}
def list_of_vectors(n,T):
return [matlib.zeros((n,1)) for t in T]
/end{lstlisting}
Here is an equation:
/begin{equation}
x_t = Ax_{t-1} + w_t
/end{equation}
/end{frame}
`

The thing to note is the [fragile] command. Here’s what it does. It means that the weird error that happens if you don’t include it DOESN’T HAPPEN! Enjoy!

### 4 Responses to “Latex, Beamer, Python, Beauty”

1. mich Says:

Hi!
I tried Your code but i receivie sth like

“colorkeywordsreturn” instead of pink word return.

2. Jorgen Says:

Nice guide!

Like you say, beamer is full of magic words that no-one explains or are hidden deep inside unreachable documentation. It’s only because guides like this I can use beamer for something useful.

3. ddcampayo Says:

Beautiful. I am definitely using these ideas for my next talk.

I have found your source lacks “\” and other features (\begin{document}, etc). I have taken the liberty of writing some LaTeX code that can be copied and compiled directly. Here it goes (sorry if it’s too long):

\documentclass{beamer}

\usetheme{default}

\definecolor{fore}{RGB}{249,242,215}
\definecolor{back}{RGB}{51,51,51}
\definecolor{title}{RGB}{255,0,90}

\setbeamercolor{titlelike}{fg=title}
\setbeamercolor{normal text}{fg=fore,bg=back}

\usepackage{listings,bera}
\definecolor{keywords}{RGB}{255,0,90}
\lstset{language=Python,
keywordstyle=\color{keywords},

\begin{document}
\begin{frame}[fragile]
\frametitle{A Page}
\begin{lstlisting}
def list_of_vectors(n,T):
return [matlib.zeros((n,1)) for t in T]
\end{lstlisting}
Here is an equation:

x_t = Ax_{t-1} + w_t

\end{frame}
\end{document}

• mikedewar Says:

Yep I think maybe the code formatting thing I’ve used has stripped all the \s from my code. Thanks!