8

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?

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

2 Answers2

9

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}
jub0bs
  • 58,916
3

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).