Question

I am currently writing lots of small reports. Most of them are just value dumps with some graphs and illustrative comments.

Is there a literate programming environment that let's me write my reports in an easy format (preferably markdown/latex and haskell) and then converts to some output format (preferably pdf) that contains results of the calculations done in the original file?

I know that Haskell supports literate programming but I don't think that the output (and possibly whole images) can be captured.

Was it helpful?

Solution

The lhs2TeX preprocessor supports evaluating Haskell expressions via GHCi. Here's a minimal example:

\documentclass{article}

%include polycode.fmt
%options ghci

\begin{document}

Running

> fmap (+1) [1..10]

yields \eval{fmap (+1) [1..10]}.

\end{document}

This will produce output which contains the list [2,3,4,5,6,7,8,9,10,11] in the place where the \eval command is in the input.

You can reference definitions from the current file. I.e., the \eval function works by loading the current file into ghci and then evaluating the expression in that context.

There's also \perform which does the same as \eval, but the output will not be seen as a piece of Haskell code, but rather as a piece of LaTeX code. So you can write Haskell functions that generate parts of your output document.

That being said, there are lots of things that could be improved about this functionality in lhs2TeX, and the implementation is quite a hack.

EDIT: Concluding from the comments, the goal is to include images generated by the Chart library. Here is a proof-of-concept document that shows how to achieve this:

\documentclass{article}

\usepackage{graphicx}

%include polycode.fmt
%options ghci

%if style == newcode
(Stuff here will not be typeset by LaTeX, but seen by Haskell.)

> import Data.Accessor
> import Graphics.Rendering.Chart
>
> renderAndInclude :: Renderable a -> FilePath -> IO ()
> renderAndInclude r filename = do
>   renderableToPDFFile r 200 200 filename
>   putStrLn $ "\\includegraphics{" ++ filename ++ "}"

%endif

%format ^= = "\mathbin{{}^\wedge\!\!=}"

\begin{document}

The image description

> image :: Renderable ()
> image  =  toRenderable
>        $  layout1_title  ^=  "Test"
>        $  layout1_plots  ^=  [ Left (toPlot plot) ]
>        $  defaultLayout1
>   where
>     plot   =  plot_lines_values ^= [[ (x, sin x) | x <- [0, 0.01 .. 10 :: Double] ]]
>            $  defaultPlotLines

yields the following output:

\perform{renderAndInclude image "image.pdf"}.

\end{document}

The central helper function you have to write is something like renderAndInclude above that performs the actual rendering, writes the result to a file, and produces a LaTeX \includegraphics command that reads back the file.

OTHER TIPS

If you don't absolutely have to have Haskell, you can get the rest with Sweave and R. You basically write a LaTeX document, with embedded R code. You can choose to include either the code, the result (including plots), or both in your final PDF output. Knitr is a more recent extension (or simplification?) of Sweave.

If you want to use diagrams instead of chart, then diagrams-builder as described in this blog post is your friend. Works with markdown and LaTeX alike.

You can try http://code.google.com/p/nano-lp/, it supports Markdown/Multimarkdown and other lightweight markups (but also OpenOffice) and it's easy to include several files into one. With OpenOffice, you can export your resulting file to PDF, for example. Also Asciidoc is supported and TeX/LaTeX, so you can use them to produce PDF too

Sorry for being (very) late to the party. The answer comes only as a reference for future visitors to this question.

Org-mode does provide a nice literate programming environment. Albeit a universe in itself, it's well worth looking into! Some more information about the way it handles code can be found in these links:

There is ofc also support for Haskell, else this comment would be superfluous. While the format isn't Markdown, Org-mode has a very simple way of marking up things.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top