As may know, I am a -fan-boy! To me , is just the perfect way to get documents well structured and with a professional layout. More, with you can write to someone in the other side of the planet mathematic formulas, or whatever because is a standard. It’s also fast, stable, flexible, free, etc.
Well, from the very beginning of my course I have to be able to write documents in , so after I learn it, I start to do my template document. To import code to my document I start to use verbatim package, but it really suck’s!
So, I found listings, great output! And even better, I could make my own style to one language. After reading the manual I did it for Haskell. I love the output.
You can see an example of that here(.pdf).
A simple definition of some colors to highlight our code:
\definecolor{gray_ulisses}{gray}{0.55} \definecolor{castanho_ulisses}{rgb}{0.71,0.33,0.14} \definecolor{preto_ulisses}{rgb}{0.41,0.20,0.04} \definecolor{green_ulises}{rgb}{0.2,0.75,0}
And here it is, the thing that do things 🙂
\lstdefinelanguage{HaskellUlisses} { basicstyle=\ttfamily\tiny, sensitive=true, morecomment=[l][\color{gray_ulisses}\ttfamily\tiny]{--}, morecomment=[s][\color{gray_ulisses}\ttfamily\tiny]{\{-}{-\}}, morestring=[b]", stringstyle=\color{red}, showstringspaces=false, numberstyle=\tiny, numberblanklines=true, showspaces=false, breaklines=true, showtabs=false, emph= {[1] FilePath,IOError,abs,acos,acosh,all,and,any,appendFile,approxRational,asTypeOf,asin, asinh,atan,atan2,atanh,basicIORun,break,catch,ceiling,chr,compare,concat,concatMap, const,cos,cosh,curry,cycle,decodeFloat,denominator,digitToInt,div,divMod,drop, dropWhile,either,elem,encodeFloat,enumFrom,enumFromThen,enumFromThenTo,enumFromTo, error,even,exp,exponent,fail,filter,flip,floatDigits,floatRadix,floatRange,floor, fmap,foldl,foldl1,foldr,foldr1,fromDouble,fromEnum,fromInt,fromInteger,fromIntegral, fromRational,fst,gcd,getChar,getContents,getLine,head,id,inRange,index,init,intToDigit, interact,ioError,isAlpha,isAlphaNum,isAscii,isControl,isDenormalized,isDigit,isHexDigit, isIEEE,isInfinite,isLower,isNaN,isNegativeZero,isOctDigit,isPrint,isSpace,isUpper,iterate, last,lcm,length,lex,lexDigits,lexLitChar,lines,log,logBase,lookup,map,mapM,mapM_,max, maxBound,maximum,maybe,min,minBound,minimum,mod,negate,not,notElem,null,numerator,odd, or,ord,otherwise,pi,pred,primExitWith,print,product,properFraction,putChar,putStr,putStrLn,quot, quotRem,range,rangeSize,read,readDec,readFile,readFloat,readHex,readIO,readInt,readList,readLitChar, readLn,readOct,readParen,readSigned,reads,readsPrec,realToFrac,recip,rem,repeat,replicate,return, reverse,round,scaleFloat,scanl,scanl1,scanr,scanr1,seq,sequence,sequence_,show,showChar,showInt, showList,showLitChar,showParen,showSigned,showString,shows,showsPrec,significand,signum,sin, sinh,snd,span,splitAt,sqrt,subtract,succ,sum,tail,take,takeWhile,tan,tanh,threadToIOResult,toEnum, toInt,toInteger,toLower,toRational,toUpper,truncate,uncurry,undefined,unlines,until,unwords,unzip, unzip3,userError,words,writeFile,zip,zip3,zipWith,zipWith3,listArray,doParse }, emphstyle={[1]\color{blue}}, emph= {[2] Bool,Char,Double,Either,Float,IO,Integer,Int,Maybe,Ordering,Rational,Ratio,ReadS,ShowS,String, Word8,InPacket }, emphstyle={[2]\color{castanho_ulisses}}, emph= {[3] case,class,data,deriving,do,else,if,import,in,infixl,infixr,instance,let, module,of,primitive,then,type,where }, emphstyle={[3]\color{preto_ulisses}\textbf}, emph= {[4] quot,rem,div,mod,elem,notElem,seq }, emphstyle={[4]\color{castanho_ulisses}\textbf}, emph= {[5] EQ,False,GT,Just,LT,Left,Nothing,Right,True,Show,Eq,Ord,Num }, emphstyle={[5]\color{preto_ulisses}\textbf} }
And now, to use it, we define a new environment:
\lstnewenvironment{code} {\textbf{Haskell Code} \hspace{1cm} \hrulefill \lstset{language=HaskellUlisses}} {\hrulesmallskip}
Now to write Haskell code in our document with great output, just write:
\begin{code} module Main(main) where main :: IO() main = putStrLn $ "LaTeX listings is cool!" \end{code}
Because I’m not so good at web design I use this blog layout (that I like), but some of the code may not be seen, if you want to see an example of a source go here (.lhs).
Have you come across lhs2TeX (http://people.cs.uu.nl/andres/lhs2tex/)? It’s a preprocessor for LaTeX-based literate Haskell. You can use it to typeset your code in a proportional font with code aligned in multiple columns, but still executable (at least with ghc). I used it to typeset the second to last chapter of my dissertation. (You can get other layout styles as well to suit your taste.)
The major problem with it is that it’s a preprocessor rather than a package, but if you use a build system like make for your LaTeX, that shouldn’t matter too much.
LaTeX, the most powerful typesetting tool in the world!
We have to find a piece of time to (re)make our pretty little workshop, are you in? :p
abraço!
if I use “\begin{code}” like you said, how can I insert captions?
The environment code does not support captions, maybe if you use the environment code inside environment figure.
Anyway, you can do your comments in the normal text, that fallows the listing. Because listings are not float objects. And if you add it to the figure’s list it will be float.
Hi, I tried your LeTeX samples, but it looks like that somehow backslashes fell of. Could you perhaps fix it? Thanks, Petr
Petr, now is working.
Regards.