# How can I define multiple identifier classes and styles in listings?

I'm trying to add different styles (color, etc.) to different groups of words in the listings package. Note that my question is different from How can I use multiple declarations (\textbf, \emph) in listings' emphstyle?.

Let me show you some code:

\documentclass{article}
\usepackage{listings}
\begin{document}
...
\begin{lstlisting}
{
"id": {integer},
"name": {string},
"picture": {
File Resource
}
}
...
PUT /files/{file_id}
\end{lstlisting}
\end{document}


I use the \lstset command to define the following:

...
emph={  % HTTP Request
GET,POST,PUT,DELETE
},
emphstyle={\color{green}},
...


but I would like to add more words with a different style (color, etc.), like so

...
emph2={  % Variable Types
integer,string,blob,datetime
},
emphstyle2={\color{darkBlue}},
...


How can I do that?

• Thanks for this question! It actually led me to solve a problem I was having with the formatting of my listings :) – jubobs Dec 3 '13 at 18:19

You're halfway there. The listings manual (p.31 in v1.5b) tells you that multiple classes of identifiers and associated styles can be defined by using

\emph=[<number>]{<identifier list>}
\emphstyle=[<number>]{<identifier style>}


where <number> is some integer of your choice that gets associated with the class of identifiers in question.

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}

\lstset%
{%
emph=[1]%
{%
DELETE,
GET,
POST,
PUT,
},
emphstyle=[1]{\color{green}},
%
emph=[2]% Variable Types
{%
blob,
datetime,
integer,
string,
},
emphstyle=[2]{\color{blue}},
}

\begin{document}
...
\begin{lstlisting}
{
"id": {integer},
"name": {string},
"picture": {
File Resource
}
}
...
PUT /files/{file_id}
\end{lstlisting}
\end{document}

• minutes after the question, I found this in the manual! Thanks anyway! – Guilherme Dec 4 '13 at 2:50
• @Guilherme No problem. And I learned something in the process of posting this answer. Two birds with one stone! – jubobs Dec 4 '13 at 3:16
• Great answer! But how to use special characters like '#' as keyword? I try to add an #endif keyword – Passe Aug 5 '18 at 11:31

A supplement to @Jubobs's answer as a reminder for users of listings v1.6 (maybe also for versions later on, I haven't checked yet).

In listings v1.6, the valid syntax for specifying group in emph and keywords has been changed into

emph={[2]...}, emphstyle={[2]...}
keywords={[1]...}, keywordstyle={[1]...}


These can be found in the listings manual (v1.6, p22).