1. 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!

• 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:

\documentclass{minimal}
\usepackage[pdftex]{xcolor}
\usepackage[a4paper,hmargin=6cm]{geometry}
\usepackage{listings}
\usepackage{framed}
\lstset{numbers=left,
stepnumber=1,
numbersep=1pt,
numberstyle=\color{black},
basicstyle=\ttfamily}
\begin{document}
\begin{lstlisting}
if (a &lt; b)
{
printf(&quot;A is smaller than  B!\n&quot;);
}
a = b;
\end{lstlisting}
\end{document}

Best, Tom.

• 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… 🙁

2. I like the Java theme… could you create one for xml :-)? The existing is not very nice :-/

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

3. Reblogged this on KRIPTUSA´s Blog and commented:
Para presentar algoritmos, códigos en nuestros artículos. Esperar que todo se vea bien.

4. Thank you.

5. Awesome

Thank you!

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

\documentclass{report}
\usepackage{xcolor}
\usepackage{listings}          % for source code snippets.
\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}
\lstdefinestyle{Javastyle}{
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
keywordstyle=\color{pblue},
stringstyle=\color{pred},
basicstyle=\ttfamily\singlespacing,
moredelim=[il][\textcolor{pgrey}]{\$\$},
moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%},
rulecolor= \color{black}
}

\begin{document}

%\lstset{style=Javastyle}
\begin{lstlisting}[language=Java]
@NonNull
public static byte[] longToBytes(long x) {
buffer.clear();
buffer.putLong(0, x);
return buffer.array();
}

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

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

\usepackage{setspace}