[Tex/LaTex] How to define multiple identifier classes and styles in listings

colorlistings

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},
        "login": {string},
        "password": {string},
        "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?

Best Answer

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.

enter image description here

\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},
        "login": {string},
        "password": {string},
        "name": {string},
        "picture": {
            File Resource
        }
    }    
    ...       
    PUT /files/{file_id}
\end{lstlisting}
\end{document}