## Haskell + lhs2TeX + LaTeX beamer

May 16, 2008

For a presentation I’m currently building I want to walk the public through a bit of Haskell code. Because I want to make sure my code is runnable too, I decided to write it as Literal Haskell and then use lhs2TeX to convert it to very pretty LaTeX which I use to make a presention PDF with LaTeX Beamer.

This is pretty easy for the common use case (a (research) paper) about Haskell), but I had some more requirements.

• The code is split over several files
• I want to mix code fragments of those files
• I want effects, like uncovering code line by line

It took me a couple of hours to figure out a sensible approach, which I describe here. I’m sure there are more ways to do it, and I’d love to hear them!

Since our lhs2TeX activities will be spread over multiple files, we’ll want to make a simple formatting file to share. Let’s call it ‘talk.fmt’

 %include polycode.fmt

 

% put formatting rules here 

Then we set up our main LaTeX file. Because we might want to use some code examples in there that are not in the files, let’s make it an lhs-file: ‘talk.lhs’. In it’s preamble we define a couple of new LaTeX commands that are going to help us with making slides but referencing them in an arbitrary order.

 \documentclass{beamer}


\end{document} 

First, we have ‘\ignore’ to hide stuff from LaTeX that is only useful in the code and we don’t want to have in our presentation.

The ‘\defslide’ command takes two arguments, a name which is used as a label to reference to and the contents for the slide. The ‘<hidden>’ makes sure that these slides will not be rendered. In this case ‘hidden’ is not a special argument, just a non-existing ‘mode’.

To use a defined slide, we use ‘\slide’, which uses ‘\againslide’ to draw the slide, with all possible subframes (‘<1->’).

In the document body we include all the files which contain slides. Note that this should be .tex file, generated from our .lhs file. For example ‘Code.lhs’ could look like this.

 %include talk.fmt

 \ignore{ module Code where } \defslide{example} { >                     example :: IO () > {-"\uncover<2->{"-} example = putStrLn "Hello!" {-"}"-} } 

\defslide{thisisan}{ \frametitle{Just a normal frame} \begin{code} main :: IO () main = undefined \end{code}} 

I don’t uncourage anybody to mix styles like this, it’s just an a example, okay? 😉 What you also see here is the way to uncover a piece of code line by line. If you want to learn more about LaTeX Beamer I suggest you consult the fine manual.

For easy use, we can make a Makefile to tie everything up. We also use latexmk, a simple tool that automagically processes the LaTeX file as many times as necessary, which saves a ton of time.

 default: talk.pdf

 talk.pdf : Code.tex %.pdf : %.tex     latexmk -pdf $< %.tex : %.lhs talk.fmt lhs2TeX$< > \$@ 

clean:     latexmk -CA talk 

If you want to try it for yourself just copy the above code to the mentioned filenames and run ‘make’ to get the pdf. (Provided you have all the necessary requirements.)

(I think I massaged all the code so that it gets through WordPress’ botching, but you’ll still need to replace the indenting spaces with tabs in the Makefile yourself.)

While in this example only one file with code is used, it easy to see that this way as many files as necessary can be used. As shown, the slides can be in arbitrary order and we can use nice LaTeX Beamer effects like uncovering. Hooray!