# Add coloring rules for specified language keywords in lstinputlisting [duplicate]

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},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=2
}

\begin{document}
\lstinputlisting[label={lst:sourcefile}]{codefile.java}
\end{document}

-

## marked as duplicate by Jubobs, Charles Stewart, Peter Jansson, ChrisS, SperavirJan 14 '14 at 20:56

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 '14 at 19:23
@Jubobs Done :). I would like to add coloring rule for all annotations (starting with @). – Patryk Jan 14 '14 at 19:31

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.

\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},