使用 lhs2TeX 缩进

使用 lhs2TeX 缩进

我正在写一篇 Haskell 论文,我正在使用开源TeX让它看起来更漂亮。但是,lhs2TeX 似乎弄乱了我的缩进。我有一些源代码看起来像这样:

\documentclass[a4paper]{article}                                                                                                                                               
%include polycode.fmt
\begin{document}

I'm going to define a big function now.

\begin{code}
function :: Some -> Complicated -> Signature
function s c = some helper variables where
\end{code}

Now I explain the variables and summarize the structure of the function.
Here come the local variables:

\begin{code}
  some = "I should be indented."
  ...
\end{code}
\end{document}

使用 进行编译lhs2TeX Example.lhs -o Example.tex,然后使用 进行渲染pdflatex Example.tex并检查 pdf。请注意,该"I should be indented."行没有缩进。

函数局部变量应该缩进,但 lhs2TeX 会丢失缩进。有谁知道如何让 lhs2TeX 在函数定义开头用文本分隔函数局部变量时缩进它们?

我愿意接受快速而粗糙的 TeX 解决方案(例如手动标记需要缩进的代码块),并且我也愿意接受替代方案,lhs2TeX如果它们也能使文学 haskell 文件变得漂亮。

答案1

假设您正在使用样式,一种可能性poly是使用\savecolumns\restorecolumnsGuide2lhs2TeX但是,这要求第一个code块中有一个标记,其缩进量与第二个代码块中的代码一样多;例如,在您的示例中,您可以将放在where下一行:

I'm going to define a big function now.

\savecolumns
\begin{code}
function :: Some -> Complicated -> Signature
function s c = some helper variables
  where
\end{code}

Now I explain the variables and summarize the structure of the function.
Here come the local variables:

\restorecolumns
\begin{code}
  some = "I should be indented."
  ...
\end{code}

我不知道是否还有其他更快捷、更简便的选择。

答案2

我可以为您提供 TeX 级别的解决方案。在正常情况下,我会使用listings可以格式化 Haskell 语言的包,但这个问题看起来像一个尝试新事物的挑战。

问题在于,行的缩进从代码块中的一个或多个常规行开始。我通过设置尺寸手动缩进该部分代码,\leftskip而不通知软件包。在 Lubuntu 13.10 上lhs2TeX安装lhs2tex软件包 (和) 后,我运行:sudo mktexlsr

lhs2TeX -o mal-haskell.tex mal-haskell.lhs
lualatex mal-haskell.tex

% I am the mal-haskell.lhs file...
%! lhs2TeX -o mal-haskell.tex mal-haskell.lhs
%! lualatex mal-haskell.tex

\documentclass{article}
%- include polycode.fmt
%include lhs2TeX.fmt
\pagestyle{empty}

\begin{document}
I'm going to define a big function now.

\begin{code}
function :: Some -> Complicated -> Signature
function s c = some helper variables where
\end{code}

Now I explain the variables and summarize the structure of the function. Here come the local variables:

\begin{code}
First line of regular code in that block...
  test of some = "I should be indented and I am."
  ...
\end{code}

Text paragraph is here again.

\begingroup
\newbox\malbox
\setbox\malbox=\hbox{\texttt{~~}}%
\leftskip=\wd\malbox
\begin{code}
  test of some = "I should be indented and I am."
  ...
\end{code}
\endgroup

\end{document}

姆韦

相关内容