3

I'm writing a literate Haskell paper, and I'm using lhs2TeX to help make it look pretty. However, lhs2TeX seems to mess up my indentation. I have some source that looks like this:

\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}

Compile with lhs2TeX Example.lhs -o Example.tex, then render with pdflatex Example.tex and check the pdf. Notice that the "I should be indented." line is not indented.

Function locals should be indented, but lhs2TeX loses the indentation. Does anyone know how to convince lhs2TeX to indent function locals when they are separated by text from the beginning of the function definition?

I'm open to quick-and-dirty TeX solutions (like manually marking the code blocks that need indentation) and I'm also open to alternatives to lhs2TeX if they also make the literate haskell file pretty.

2
  • 1
    Could you please provide a full minimal (working) example starting with \documentclass... and ending with \end{document}. It helps the solvers a lot.
    – Malipivo
    Apr 9, 2014 at 15:19
  • Sure thing, added to the inline example above.
    – Nate
    Apr 9, 2014 at 17:29

2 Answers 2

5

Assuming that you are using poly style, one possibility is to use the \savecolumns / \restorecolumns commands described in section 8.8 of Guide2lhs2TeX. However, this requires that there is a token in the first code block that is indented as much as the code in the second code block; e.g., in your example, you could put the where on the next line:

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}

I don't know whether there's a less quick-and-dirty option.

3
  • Is there any way to get this to work with multiple levels of nesting? I can successfully use this to get two levels deep, but then how do I pop only one level back up? I don't suppose there is a \savecolumns stack of any sort, as I couldn't find reference to it in the docs.
    – Nate
    Apr 9, 2014 at 18:07
  • 2
    Just using \savecolumns with the first code block, and then \restorecolumns with all subsequent code blocks, should work.
    – Benya
    Apr 9, 2014 at 18:13
  • Note that \savecolumns and \restorecolumns also take an optional argument, so you can reuse multiple layouts. Plenty of potential for abuse there :)
    – kosmikus
    Jun 25, 2014 at 9:00
2

I can provide you solution at the TeX level. Under normal circumstances I would use the listings package which can format the Haskell language, but this question looked like a challenge to try something new.

The problem is that the indentation of lines starts after one or more regular lines in your blocks of code. I indent that part of code manually by setting the \leftskip dimension without notifying the lhs2TeX bundle. After installing lhs2tex package (and sudo mktexlsr) on Lubuntu 13.10, I run:

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}

mwe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.