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.


%include talk.fmt








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

module Code where

>                     example :: IO ()
> {-"\uncover<2->{"-} example = putStrLn "Hello!" {-"}"-}

\frametitle{Just a normal frame}
main :: IO ()
main = undefined

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 $< > $@

    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!



  1. Nice post.

    I have a number of comments though:

    (1) The code blocks look wrong in my browser. Only the first line is framed, the rest is formatted like ordinary text in a proportional font.

    (2) Instead of a command like \ignore, it’s better to use %if directives on lhs2TeX’s side to exclude stuff. The reason is that a command like \ignore in TeX can still have subtle effects. For example, the way you define \ignore, it can easily lead to extra whitespace being included in your TeX output.

    (3) What exactly do you need the construction for that you first define all the frames and then use them? I’ve never needed that in any presentation. I can imagine some situations (where you want to repeat a frame in several situations) where this would be useful, but they’re relatively rare. Am I missing something?

  2. Hi Andres, thanks for your comments!

    (1) You’re right, the HTML is invalid too 😦 Unfortunately I can only blame WordPress and myself for picking WordPress. We will probably try to move this blog. (I hadn’t noticed it, because Opera renders it as ‘intended’)

    (2) Great idea. I choose to use ‘\ignore’ because of a comment on the Haskell wiki, but doing it the lhs2TeX-way seems much more sensible.

    (3) I’m only using parts of the code (which can easily be done), but also from different files which I want to present mixed. Also, I want the code to be runnable. Because there’s Template Haskell involved, I can’t put the code together in one file. So, yeah, it’s a pretty rare problem. If there’s another or better way to do it, I’d love to hear it 🙂

  3. Hi,

    Thanks for your post, it inspired me to write the following, more automated way, to use lhs2tex to translate my .lhs files to .tex


    latexmk -r latexmkrc -pdf -pvc Main.lhs


    $pdflatex = 'lhs2TeX -v --poly Main.tex; pdflatex -file-line-error -halt-on-error -synctex=1 --enable-write18 Main.tex Main.pdf';

    add_cus_dep('lhs', 'tex', 0, 'cus_dep_require_primary_run');

    Main.lhs (‘preamble’):

    {lhs2TeX -v --poly #1.lhs > #1.tex}%

    Main.lhs (‘document body’):


  4. a) To develop professional skills/status among the members.nb) To import technical knowledge among the members, through news-letter/njourn Click http://d2.ae/hool090730

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: