I'm using the listings package to format some Java code in my document.
I want it to number lines (1 number every 5 lines), and I need to highlight some keywords inside the code with the following command:
\newcommand{\ca}[1]{\color{red}{#1}}
(escaped with "`" inside the lstlisting environment).
However, if I use this command on any line, the next line will be numbered, regardless of its number.
Here is a simplified example:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
language=Java,
numbers=left,
stepnumber=5,
numberfirstline=true,
numbersep=5pt,
escapechar=\`
}
\newcommand{\ca}[1]{\color{red}{#1}}
\begin{document}
\begin{lstlisting}
public class Color {
private int R, G, B;
public `\ca{final static}` Color red = `\ca{new}` Color(255, 0, 0);
public `\ca{final static}` Color magenta = `\ca{new}` Color(255, 0, 255);
public `\ca{final static}` Color lightgray = `\ca{new}` Color(192, 192, 192);
// ...
}
\end{lstlisting}
\end{document}
(in my real document it's much more complex: Beamer, TikZ, etc.)
Here is the result I get:
Lines 4 and 5 should not be numbered, and if I just remove the "`" they are not numbered.
Any idea how to fix this? Did I do something wrong, or is it a bug in listings?