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).