5

Simple task

Suppose there is a example.tex file with Haskell listings:

\documentclass{article}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{code}{Verbatim}{fontsize=\small}

\begin{document}
\begin{code}
sequence :: [IO a] -> IO [a]
sequence      [] = return []
sequnce (c : cs) = do
  x <- c
  xs <- sequence cs
  return (x : xs)
\end{code}
\end{document}

easy before

Let's prepare this file to be processed via lhs2TeX:

\documentclass{article}
%include lhs2TeX.fmt
%include lhs2TeX.sty

\begin{document}
\begin{code}
sequence :: [IO a] -> IO [a]
sequence      [] = return []
sequnce (c : cs) = do
  x <- c
  xs <- sequence cs
  return (x : xs)
\end{code}
\end{document}
  • lhs2TeX example.tex -o example_new.tex
  • pdflatex example_new.tex

What we've got:

easy after

It is exactly how I want sources to look like.

Real-world example

In real world there are often many files with haskell sources collected by \input command in main file:

hardmod.tex:

\documentclass{disser}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{code}{Verbatim}{fontsize=\small}
\begin{document}
\input{first}
\input{second}
\end{document}

first.tex:

Some stuff

\begin{code}
sequence :: [IO a] -> IO [a]
sequence      [] = return []
sequnce (c : cs) = do
  x <- c
  xs <- sequence cs
  return (x : xs)
\end{code}

and more words.

second.tex:

Another example of code:
\begin{code}
ap :: Monad m => m (a -> b) -> m a -> m b
ap mf mx = do
  f <- mf
  x <- mx
  return (f x)
\end{code}
And a conclusion. 

lhs2TeX produces whole latex document, that why it is kinda tricky to compile hardmod.tex with nice sources prepared by lhs2TeX.

Question: how can lhs2TeX could be used in complex structured document like this to get nice source formatting?

9
  • It seems to me that this question is a better explained version of this one? Should we close the latter as a duplicate?
    – jub0bs
    May 29 '13 at 17:50
  • I thought that it will be a bad idea to edit question you mentioned after two days, that why I've tried to create another one with better explanations. Yep, question #116595 could be closed. May 29 '13 at 17:58
  • I understand you want to typeset your Haskell code with syntax highlighting. I'm more familiar with minted than I am with lhs2TeX, which seems to be specific to Haskell. Do you absolutely want to use lhs2TeX or are you open to a minted solution?
    – jub0bs
    May 29 '13 at 18:05
  • lhs2TeX is preferred because of --math and --poly formatting. May 29 '13 at 18:10
  • 1
    Question: Why do you have Haskell listings in .tex files? Wouldn't it be easier to keep them in their original .lhs files and import them in your hardmod.tex file with a dedicated package such as listings or minted?
    – jub0bs
    May 29 '13 at 18:15
3

Actual answer

You have two options:

  1. Use lhs2TeX's %include directive rather than \input. This is the easiest way, and I'd recommend it.

  2. Process each file separately using lhs2TeX and then combine the results. Each file has to have an %include polycode.fmt line then. This option can be useful if you need very different formatting directives for different parts of your code (for example if you use lhs2TeX to process a different programming language).

Note that your question is actually the topmost FAQ in the lhs2TeX manual.

Some additional remarks

  1. You should put your sources in .lhs files, not .tex files. It's the whole point of lhs2TeX that you have literate Haskell sources, and they're still valid Haskell.

  2. You should not use

    %include lhs2TeX.fmt
    %include lhs2TeX.sty
    

    This is long deprecated. Instead, use

    %include polycode.fmt
    
  3. You are saying that your example looks exactly like you want typeset Haskell code to look, but actually, you are not making use of lhs2TeX's alignment features, which is one of the main advantages of using it. To get the = symbols aligned in the output, make sure that you have two spaces in front of them in the source.

0

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.