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.
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.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With