Syntax highlighting in LaTeX using the listings
package is very powerful. Below, I will provide a few examples for your convenience.
First, let me briefly summarize the usage of the listings package.
%Preamble \usepackage{listings} \usepackage{color} \definecolor{name}{rgb}{0.5,0.5,0.5} \lstset{key1=value1,key2=value2} % Define listings style %Document body \begin{lstlisting} % Add code here \end{lstlisting} % Or load from file \lstinputlisting{filename.java}
So below you’ll find a few complete code examples to reuse and modify for your purposes:
“The Pseudo Code Box”
\definecolor{lightpurple}{rgb}{0.8,0.8,1} \lstset{ numbers=left, stepnumber=1, numbersep=5pt, numberstyle=\small\color{black}, basicstyle=\ttfamily, keywordstyle=\color{black}, commentstyle=\color{black}, stringstyle=\color{black}, frame=single, tabsize=2, backgroundcolor=\color{lightpurple}, caption=Fibonacci numbers pseudo code}
This code was taken from here.
“Java/Eclipse style”
\definecolor{javared}{rgb}{0.6,0,0} % for strings \definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments \definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords \definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc \lstset{language=Java, basicstyle=\ttfamily, keywordstyle=\color{javapurple}\bfseries, stringstyle=\color{javared}, commentstyle=\color{javagreen}, morecomment=[s][\color{javadocblue}]{/**}{*/}, numbers=left, numberstyle=\tiny\color{black}, stepnumber=2, numbersep=10pt, tabsize=4, showspaces=false, showstringspaces=false}
\definecolor{lightgrey}{rgb}{0.9,0.9,0.9} \definecolor{darkgreen}{rgb}{0,0.6,0} \lstset{language=[LaTeX]TeX, texcsstyle=*\bf\color{blue}, numbers=none, breaklines=true, keywordstyle=\color{darkgreen}, commentstyle=\color{red}, otherkeywords={$, \{, \}, \[, \]}, frame=none, tabsize=2, backgroundcolor=\color{lightgrey}, caption=LaTeX example}
The LaTeX code was copied from here.
sokai
Hi tom!
Thanks for your code examples. Have you seen that white lines/stripes between the lines of code? – Do you know a solution for disabling them?
Best regards!
tom
Hi Kai,
Thanks for your comment. I am aware of the problem. I found the following solution on stackoverflow and modified it slightly adding line numbers:
Best, Tom.
sokai
Thanks, Tom! – I have seen this on so, too. But the shaded box is (a little bit) ugly… 😐
I have seen some examples that use “backgroundcolor” for the listings _without_ the white lines. Also I found an answer on tex.sx… But my listings are always striped… 🙁
Christian Straube
I like the Java theme… could you create one for xml :-)? The existing is not very nice :-/
tom
Hi Christian,
This question and the answers on stackexchange about XML syntax highlighting may be what you are looking for. The result looks really nice.
Cheers, Tom.
Christian Straube
Thank you for that link! I’ve created a new question concerning this: http://tex.stackexchange.com/q/32767/5065
D555
Reblogged this on KRIPTUSA´s Blog and commented:
Para presentar algoritmos, códigos en nuestros artículos. Esperar que todo se vea bien.
Ade Malsasa Akbar
Thank you.
Awesome
Thank you!
Omar Sweidan
Hi, Tom!
Is it possible to highlight annotations in Java code?
tom
Hi Omar,
Can you provide an example, ideally a minimal working example in LaTeX.
Thanks, Tom
Omar Sweidan
Sorry, I forgot.
When I remove the % before \lstset{}, I get an error message: Undefined control sequence. \begin{lstlisting}[language=Java]
tom
I can’t run your example. It would be useful if you provided a working example. A quick glance suggests that you have to load the setspace package to use
\singlespacing
.Best, Tom
Omar Sweidan
[…]Code removed by Tom[…]
This one is compiling.
tom
Hi Omar,
I removed your code, as there were again some lines missing towards the end. Anyway, as I said before, you’ll have to load the setspace package to use
\singlespacing
in your style definition: