3

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: incorrect numbering 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?

1
  • Found a workaround: disable "numberfirstline". Not perfect (I'd like the first line to be numbered...), but better than nothing. Leaving the question open in case anyone has a real fix :)
    – Schnouki
    Nov 4, 2011 at 11:04

2 Answers 2

5

Try this:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\lstset{
  language=Java,
  numbers=left,
  stepnumber=5,
  numberfirstline=true,
  numbersep=5pt,
  escapechar=\`
}

\makeatletter% ADDED
\newcommand{\ca}[1]{\color{red}{#1}%
  \aftergroup\lst@numberfirstlinefalse% ADDED
}
\makeatother% ADDED

\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}
1
  • Awesome, that's exactly what I needed. Thanks!
    – Schnouki
    Nov 4, 2011 at 12:35
1

I suggest a different method that avoids escaping to TeX code and is adaptable to different situations without marking up the original code.

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\lstset{
  language=Java,
  numbers=left,
  stepnumber=5,
  numberfirstline=true,
  numbersep=5pt,
}

\begin{document}
\begin{lstlisting}[emph={[2]final,static,new},emphstyle={[2]\color{red}}]
public class Color {
  private int R, G, B;
  public final static Color red = new Color(255, 0, 0);
  public final static Color magenta = new Color(255, 0, 255);
  public final static Color lightgray = new Color(192, 192, 192);
  // ...
}
\end{lstlisting}
\end{document}
1
  • Yes, obviously. But in other parts of the document I want to highlight different things, not necessarily keywords (sometimes an int value, sometimes Javadoc), so using emph would be quite troublesome. Thanks for the suggestion though.
    – Schnouki
    Nov 4, 2011 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .