Skip to content


Pretty printing Haskell code in Latex

This is a small code snippet for Latex which defines some rewriting rules for some operators to generate pretty printed Haskell code. It converts symbols like <- or `elem` into real mathematical symbols and also supports some Monad and Arrow combinators. It’s not complete though. Here is an example output:

You may have to install the “MnSymbol” package which is used for the Arrow combinator -<. It is different from the other Arrow symbols though but it’s the closest one I found the Latex symbol reference.

Update: GHC uses ↢ and ↣ for arrows.

\usepackage{MnSymbol}
\lstloadlanguages{Haskell}
\lstnewenvironment{HaskellCode}
    {\lstset{
}%
      \csname lst@SetFirstLabel\endcsname}
    {\csname lst@SaveFirstLabel\endcsname}
    \lstset{
      basicstyle=\small\ttfamily,
      escapeinside={/+}{+/},
      flexiblecolumns=false,
      basewidth={0.5em,0.45em},
      literate={+}{{$+$}}1 {/}{{$/$}}1 {*}{{$*$}}1
               {=}{{$=$}
}1 %{/=}{{$\neg$}}1
               {>}{{$>$}}1 {<}{{$<$}}1 {\\}{{$\lambda$}}1
               {++}{{$+\!\!\!+$}}1 {::}{{$:\!\!\!:$}}1
               {\\\\}{{\char`\\\char`\\}}1
               {->}{{$\rightarrow$}}2 {>=}{{$\geq$}}2 {<-}{{$\leftarrow$}}2
               {<=}{{$\leq$}}2 {=>}{{$\Rightarrow$}}2
               {\ .\ }{{$\circ$}}2 {(.)}{{($\circ$)}}2
               {<<}{{$\ll$}}2 {>>}{{$\gg$}}2 {>>=}{{$>\!\!>\!\!=$}}2
               {<<<}{{$\lll$}}2 {>>>}{{$\ggg$}}2 {-<}{{$\leftY$}}1 {^<<}{{$\hat{}\!\!\ll$}}2 {^>>}{{$\hat{}\!\!\gg$}}2
               {|}{{$\mid$}}1
               {undefined}{{$\bot$}}1
               {not\ }{{$\neg$}}1
               {`elem`}{{$\in$}}1
               {forall}{{$\forall$}}1
   
}

Tagged with , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.