任何有文化的编程环境是否支持即时结果?

fho*_*fho 6 markdown latex haskell literate-programming

我目前正在写很多小报道.其中大多数只是价值转储,包含一些图表和说明性评论.

是否有一个有文化的编程环境,让我以简单的格式(最好是markdown/latex和haskell)编写我的报告,然后转换为某种输出格式(最好是pdf),其中包含在原始文件中完成的计算结果?

我知道Haskell支持有文化的编程,但我不认为可以捕获输出(可能还有整个图像).

kos*_*kus 15

lhs2TeX预处理器支持通过GHCI评估哈斯克尔表达式.这是一个最小的例子:

\documentclass{article}

%include polycode.fmt
%options ghci

\begin{document}

Running

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

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

\end{document}
Run Code Online (Sandbox Code Playgroud)

这将产生输出,其中包含命令在输入[2,3,4,5,6,7,8,9,10,11]中的位置的列表\eval.

您可以引用当前文件中的定义.即,该\eval函数通过将当前文件加载到ghci该上下文中然后评估表达式来工作.

还有\perform一些相同的\eval,但输出不会被视为一块Haskell代码,而是作为一块LaTeX代码.因此,您可以编写生成输出文档部分的Haskell函数.

话虽这么说,lhs2TeX中有很多关于这个功能可以改进的东西,并且实现非常糟糕.

编辑:从评论结束,目标是包括图表库生成的图像.这是一个概念验证文档,展示了如何实现这一目标:

\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}
Run Code Online (Sandbox Code Playgroud)

您必须编写的中央帮助器函数类似于renderAndInclude上面执行实际渲染,将结果写入文件,并生成\includegraphics读取文件的LaTeX 命令.

  • @Florian除非您为TikZ或类似的LaTeX软件包生成输出,否则无法直接在LaTeX文件中嵌入图像.我不认为Chart可以做到这一点. (2认同)