10

Is there any way to execute code in a .lhs file and put the result right below the code itself in the resulting PDF?

For example:

[1,2,3] ++ [4,5,6]
[1,2,3,4,5,6]
Jcao02
  • 794
  • 8
  • 24
  • Do you mean automatically evaluate expressions which aren't declarations, and paste the results back into the source code before it's turned into a pdf, a bit like Mathematica does? – AndrewC May 13 '13 at 02:18
  • Yes, something like that. It's for showing an example of how a function works, and I want to be executed if anyone runs the code and also to be shown if anyone looks at the PDF – Jcao02 May 13 '13 at 02:35
  • Surely, you're gonna need a bit more control about what will be output, or are you not going to use any un-printable definitions? Why not use e.g. [`HaTeX`](http://hackage.haskell.org/package/HaTeX), perhaps together with [a suitable quasi-quoter](http://www.haskell.org/pipermail/haskell-cafe/2008-October/049244.html) to avoid code duplication. – leftaroundabout May 13 '13 at 08:12

1 Answers1

11

If you are using LaTeX, you can use lhs2TeX. Here is a simple example document:

\documentclass{article}

%include polycode.fmt
%options ghci

\begin{document}

< [1,2,3] ++ [4,5,6]

This evaluates to \eval{[1,2,3] ++ [4,5,6]}.

> x = [1 .. 6]

And this evaluates to \eval{x}, too.

\end{document}

This will run GHCi with the source file as input in the background. You can thus evaluate expressions using \eval in the context of the current (literate Haskell) module, and their results will be spliced into the resulting .tex sources.

kosmikus
  • 19,549
  • 3
  • 51
  • 66