6

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.

fho
  • 6,787
  • 26
  • 71
  • [How can other programming languages and tools be used to create content for TeX documents?](http://tex.stackexchange.com/q/48386/5701) might be of interest. – N.N. Feb 01 '13 at 09:08

5 Answers5

15

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.

kosmikus
  • 19,549
  • 3
  • 51
  • 66
  • That's nice :) Now the only thing that is missing is the ability to create and add images on the fly. – fho Jan 30 '13 at 21:56
  • @Florian If you have Haskell code that can produce the image, then it can be done using `\perform`. – kosmikus Jan 30 '13 at 22:02
  • Ah ... so if I find a way to output something latex compatible from [Charts](http://dockerz.net/twd/HaskellCharts) i'd be done. Yay! – fho Jan 30 '13 at 22:21
  • 1
    @Florian Yes. I've edited the answer to include more info on how you could achieve that. – kosmikus Jan 30 '13 at 23:48
  • Nice :) Honestly I was hoping for a way to embed the image into the produced Latex file. That way there would be no file clutter. But that is nitpicking :) – fho Jan 31 '13 at 00:03
  • 2
    @Florian It's not possible to directly embed images in LaTeX files, except if you produce output for TikZ or similar LaTeX packages. I don't think Chart can do that. – kosmikus Jan 31 '13 at 00:25
  • That doesn't stop me from trying ... maybe i'll spend the weekend to try my hands on a latex backend :) – fho Jan 31 '13 at 10:42
3

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.

Tyler
  • 9,872
  • 2
  • 33
  • 57
  • I've worked with R before and can't say I like it. But then I didn't knew that it supported this kind of output. – fho Jan 30 '13 at 18:02
  • @Florian perhaps this can change your impression about R: http://glimmer.rstudio.com/yihui/knitr/ It is live in the sense that there is an R session running behind it if you open it in your browser. – Yihui Xie Feb 03 '13 at 00:22
2

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.

Joachim Breitner
  • 25,395
  • 6
  • 78
  • 139
0

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

RandomB
  • 3,367
  • 19
  • 30
0

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.

Whil
  • 471
  • 4
  • 6
  • Sadly this requires Emacs :) (And I am a Vi guy) – fho Sep 11 '13 at 07:47
  • Indeed, for a Vi-guy my suggestion isn't the best one :P (Unless you're willing to go [Evil](http://www.emacswiki.org/emacs/Evil)!) – Whil Sep 28 '13 at 16:22
  • Naa ... I keep hitting the borders of usefulness with [vrapper](http://vrapper.sourceforge.net/home/) ... and I can just take so much ;) – fho Oct 02 '13 at 21:11