Sweave and Pweave are programs that let you embed R and Python code respectively into LaTeX files. You can display the source code, the result of running the code, or both.
lhs2TeX is roughly the Haskell analog of Sweave and Pweave. This post takes the sample code I wrote for Sweave and Pweave before and gives a lhs2TeX counterpart.
\documentclass{article} %include polycode.fmt %options ghci \long\def\ignore#1{} \begin{document} Invisible code that sets the value of the variable $a$. \ignore{ \begin{code} a = 3.14 \end{code} } Visible code that sets $b$ and squares it. (There doesn't seem to be a way to display the result of a block of code directly. Seems you have to save the result and display it explicitly in an eval statement.) \begin{code} b = 3.15 c = b*b \end{code} $b^2$ = \eval{c} Calling Haskell inline: $\sqrt{2} = \eval{sqrt 2}$ Recalling the variable $a$ set above: $a$ = \eval{a}. \end{document}
If you save this code to a file foo.lhs
, you can run
lhs2TeX -o foo.tex foo.lhs
to create a LaTeX file foo.tex
which you could then compile with pdflatex
.
One gotcha that I ran into is that your .lhs
file must contain at least one code block, though the code block may be empty. You cannot just have code in \eval
statements.
Unlike R and Python, the Haskell language itself has a notion of literate programming. Haskell specifies a format for literate comments. lhs2TeX is a popular tool for processing literate Haskell files but not the only one.
Pandoc is another useful one. It can output to quite a few more formats.
Have you looked at the org-mode facilities for literate programming? Has an enormous capability of polyglot literate programming, actually. A pretty good video going over it is here. https://www.youtube.com/watch?v=1-dUkyn_fZA
Josh: I use org-mode regularly, and I’ve tinkered with embedding code in org-mode files, but I haven’t used it to for real work. It looks promising.