12

Suppose, there is a large *.tex document, which has many nested \input directives with many Haskell listings surrounded by \begin{code} \end{code} commands.

How can all this code listings be highlighted like all haskell code is highlighted in many haskell papers (functional pearls is a good example)?

I heard, that many haskell papers written in lhs and then transferred to tex via lhs2tex. Yep, this is a nice tool, but it produces whole tex document and it is hard to be usable for the task like mine.


Btw, I've tried lstlistings and fancyvrb - it's far away from ideal.

2
  • 3
    In what way is lstlistings insufficient? I can't help if what I think is the right answer was already dismissed without explanation.
    – TomMD
    May 28 '13 at 2:58
  • I guess what he means is that you do not get an ideal result by just using the package lstlistings (which is true in my experience). You have to spend some effort for the setup, then lstlistings is definitely the/a way to go.
    – chris
    May 28 '13 at 4:45
9

I was pleased with my lstlistings setup which I used in my bachelor thesis:

\lstset{
  frame=none,
  xleftmargin=2pt,
  stepnumber=1,
  numbers=left,
  numbersep=5pt,
  numberstyle=\ttfamily\tiny\color[gray]{0.3},
  belowcaptionskip=\bigskipamount,
  captionpos=b,
  escapeinside={*'}{'*},
  language=haskell,
  tabsize=2,
  emphstyle={\bf},
  commentstyle=\it,
  stringstyle=\mdseries\rmfamily,
  showspaces=false,
  keywordstyle=\bfseries\rmfamily,
  columns=flexible,
  basicstyle=\small\sffamily,
  showstringspaces=false,
  morecomment=[l]\%,
}

You can find some examples in my thesis: https://www.dropbox.com/sh/zu56xcp1mj57ikq/TtFoHKmOLV/thesis.pdf

I can also provide the whole .tex source if needed.

2
  • 1
    Welcome to TeX.SX!
    – mafp
    May 28 '13 at 22:53
  • 3
    Thanks for wanting to share your thesis with examples, but the link is broken.
    – Turion
    Jun 25 '16 at 17:05
14

I'm not sure what you mean by "many nested \input directives with many Haskell listings surrounded by \begin{code} \end{code} commands" but... the minted package supports Haskell syntax highlighting. Here is some sample code, taken from the Wikipedia page on the Haskell programming language.

enter image description here

\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{minted}{haskell}
-- Type annotation (optional)
fib :: Int -> Integer

-- With self-referencing data
fib n = fibs !! n
        where fibs = 0 : scanl (+) 1 fibs
        -- 0,1,1,2,3,5,...

-- Same, coded directly
fib n = fibs !! n
        where fibs = 0 : 1 : next fibs
              next (a : t@(b:_)) = (a+b) : next t

-- Similar idea, using zipWith
fib n = fibs !! n
        where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

-- Using a generator function
fib n = fibs (0,1) !! n
        where fibs (a,b) = a : fibs (b,a+b)
\end{minted}
\end{document}

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