Home > English > LaTeX Listings for Scala

## LaTeX Listings for Scala

To get LaTeX Listings to work for Scala I ended up with the following definition in my .tex file:

\usepackage{listings}

% "define" Scala
\lstdefinelanguage{scala}{
morekeywords={abstract,case,catch,class,def,%
do,else,extends,false,final,finally,%
for,if,implicit,import,match,mixin,%
new,null,object,override,package,%
private,protected,requires,return,sealed,%
super,this,throw,trait,true,try,%
type,val,var,while,with,yield},
otherkeywords={=>,<-,<\%,<:,>:,\#,@},
sensitive=true,
morecomment=[l]{//},
morecomment=[n]{/*}{*/},
morestring=[b]",
morestring=[b]',
morestring=[b]"""
}

NOTE! After writing this I came across this page (with a downloadable style file), which is probably preferred to adding the definition to every single .tex document.

To add a nice IntelliJ-like color/look & feel and a border I added the following configuration:

\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

% Default settings for code listings
\lstset{frame=tb,
language=scala,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
numbers=none,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
stringstyle=\color{mauve},
frame=single,
breaklines=true,
breakatwhitespace=true
tabsize=3
}

Using the listings is then straightforward:

\begin{lstlisting}
val t = "hello" // a string
val x = 42 // an int
\end{lstlisting}

Resulting in the following pretty formatted code example in a Beamer presentation:

Tags:
1. June 7th, 2011 at 11:59 | #1

Do you happen to have a copy of lstlang0.sty? The original link seems to be broken.

2. June 10th, 2011 at 09:49 | #2

@Lars
Sorry, don’t think I have it. Have switched computers, and it must have gotten lost on the way..

3. September 16th, 2011 at 13:34 | #3

I believe it’s the same that you can now find in upstream svn repository at http://lampsvn.epfl.ch/trac/scala/browser/scala-tool-support/trunk/src/latex/scaladoc.sty

1. September 29th, 2010 at 10:53 | #1