Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does any literate programming environment support on the fly results?

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.

like image 428
fho Avatar asked Jan 30 '13 17:01

fho


2 Answers

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.

like image 150
kosmikus Avatar answered Sep 22 '22 03:09

kosmikus


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.

like image 23
Tyler Avatar answered Sep 21 '22 03:09

Tyler