Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

I would like to add coloring definition for e.g. @Override in java files. How can I do that ?

So far I have the following coloring definitions :

\documentclass[a4paper,11pt]{article}
\usepackage{color}
\usepackage{courier}
\usepackage{listings} % for code snippets

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

\lstset{frame=tb,
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\scriptsize\ttfamily},
  numbers=left,
  numbersep=4pt,
  numberstyle=\scriptsize\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=2
}

\begin{document}
\lstinputlisting[label={lst:sourcefile}]{codefile.java}
\end{document}
share|improve this question

marked as duplicate by Jubobs, Charles Stewart, Peter Jansson, ChrisS, Speravir Jan 14 at 20:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
What is the form of the words you want to highlight? Do they all start by @? What kind of highlight (color, font shape, etc.) are you thinking about? Be more specific. And please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. –  Jubobs Jan 14 at 19:23
    
@Jubobs Done :). I would like to add coloring rule for all annotations (starting with @). –  Patryk Jan 14 at 19:31

1 Answer 1

up vote 2 down vote accepted

To highlight all words starting with @ and followed by a space or newline, you can use the moredelim key like so:

moredelim=[s][⟨style⟩]{@}{\ },

where ⟨style⟩ can be any combination of macros for font family, size, shape, series, colour specification, etc.

enter image description here

\documentclass[a4paper,11pt]{article}
\usepackage{color}
\usepackage{courier}
\usepackage{listings} % for code snippets
\usepackage{filecontents}

% http://stackoverflow.com/questions/11873633/should-i-comment-override-method-in-java
\begin{filecontents*}{codefile.java}
/** Interface */
public Interface IFoo {
      /** method */                 
      public String getFoo();
    }
/** Class implementing interface */        
public Class Foo implements IFoo {

  /** Should be here documentary comment for overrided method getFoo() ???*/      
  @Override    
  public String getFoo() {
      return "Foo";
  }
}
\end{filecontents*}

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

\lstset{frame=tb,
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\scriptsize\ttfamily},
  numbers=left,
  numbersep=4pt,
  numberstyle=\scriptsize\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  moredelim=[s][\color{red}]{@}{\ },
  breaklines=true,
  breakatwhitespace=true,
  tabsize=2
}

\begin{document}
\lstinputlisting[label={lst:sourcefile}]{codefile.java}
\end{document}
share|improve this answer
    
That did the trick :) Thanks –  Patryk Jan 14 at 20:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.