Question: I want to prepare code listing which looks like this:

[![enter image description here](https://i.stack.imgur.com/pQ9G4.png)](https://i.stack.imgur.com/pQ9G4.png)  

I tried this:

```
\lstset{escapeinside={<@}{@>}}
\begin{lstlisting}
<@\textcolor{blue}{void}@> fun(var<<@\textcolor{blue}{long int}@>> list)
\end{lstlisting}

```

But it ended up rendering like this:

[![enter image description here](https://i.stack.imgur.com/WGE6p.png)](https://i.stack.imgur.com/WGE6p.png)

This looks something weird to me. For example the spacing between black characters is different from that of blue characters. Why is this so?

**Edit**

Setting `columns` as `fullflexible` as follows: `\begin{lstlisting}[columns=fullflexible]` solved the problem. More details in [this answer](https://tex.stackexchange.com/a/99451/59722).

Answer: 
You need to keep the verbatim/listing text out of the arguments of other macros, and also out of escaped regions.

[![enter image description here](https://i.stack.imgur.com/ZOsXz.png)](https://i.stack.imgur.com/ZOsXz.png)

```
\documentclass{article}

\usepackage{listings}

\usepackage{color}
\def\z#1{\gdef\zz{\color{#1}}\aftergroup\zz}
\begin{document}

\lstset{escapechar=|}
\begin{lstlisting}
|\z{blue}|void|\z{black}| fun(var<|\z{blue}|long int|\z{black}|> list)
\end{lstlisting}

\end{document}

```
You can define you own listing environment and assign different colors to lists of keyword you define:

```
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[procnames]{listings}
\usepackage{color}

\lstnewenvironment{myCode}[1][]{
\lstset{
    emph={void,long,int},
emphstyle=\color{blue},
emph={[2]fun},
emphstyle=[2]\color{green}
}}{}

\begin{document}

\begin{myCode}
void fun(var<long int> list)
\end{myCode}

\end{document}

```