Best Way to Present Functional Programs in a LaTeX Document?

I am interested in putting some of my functional programs (Haskell/OCaml/Coq) in a LaTeX document.

What is the best way to do this?

Options I have considered:

  • Pasting actual Haskell code and using the minted package for syntax highlighting.
    • This is nice for certain pieces of code, but does not appropriately take advantage of LaTeX. For example, if I have a variable named w_1, I would like for it to render it as a subscript. I would also like to be able to render certain symbols (say greek letters) without using Unicode in my LaTeX source.
  • Formatting with algorithms2e
    • This is not particularly helpful since this package is meant for procedural algorithms. So, while there is a mechanism to deal with for, there is no mechanism to deal with where or let
  • Formatting Equations using a \begin{aligned*} ... \end{aligned*} block.
    • This works pretty well when the function is a series of top level pattern matches. However, there is still no support for where syntax, or a way of formatting case ... of ...
1 Like

It sounds like you’re looking for lhs2tex:

And there is a user guide here:

I don’t know how well it works for OCaml and Coq, but I’ve used it for Agda before and you can define your own keywords.

Edit: Apparently there is a pull request for OCaml support: Ocaml syntax by balez · Pull Request #49 · kosmikus/lhs2tex · GitHub

I’m currently using lhs2TeX for a draft paper and am really happy. You can see the results here: Release Latest · sgraf812/abs-den · GitHub

Of course that requires heavy tweaking, see custom.fmt. I’ve developed quite a few hacks to be able to maximise the overlap of code that is type-set and code that is run, inventing hacks such as ifPoly here:

1 Like