[Tex/LaTex] adding keywords to be highligted in the lstlistings

keywordslistings

The recent version of C++ has added a some new keywords e.g. decltype etc..
I want to highlight them in my report.
I saw this post but it didn't work for me.

Extend a language with additional keywords?

So i added the following code

\usepackage{listings}
\lstset{emph={%  
    decltype%
    },emphstyle={\color{black}}%
}

But then I got the error message:
undefined control sequence decltype

when used in the following way:

\begin{lstlisting}
auto f(T1 X, T2 Y) -> 
  decltype(X<Y)
{}
\end{lstlisting}

Best Answer

Expanding on to Stephan's post. To make it easy to add keywords to emphasize, it is always best to define a new environment and a command such a \emphasis. Here is a MWE.

![enter image description here][1]

\documentclass{article}
\usepackage{listings,xcolor}
\begin{document}

%% Emphasis
\newcommand\emphasis[2][red]{%
   \lstset{emph={#2},
   emphstyle={\ttfamily\textcolor{#1}}}}%

\lstnewenvironment{teX}[1][]
  {\lstset{language=[LaTeX]TeX}\lstset{%
      escapeinside={{(*@}{@*)}},
      breaklines=true,
      framesep=5pt,
      basicstyle=\ttfamily,
      showstringspaces=false,
      keywordstyle=\ttfamily\textcolor{blue},
      stringstyle=\color{orange},
       commentstyle=\color{gray!80},
       rulecolor=\color{gray!10},
      breakatwhitespace=true,
      showspaces=false,  % shows spacing symbol
       xleftmargin=0pt,
       xrightmargin=5pt,
       aboveskip=0pt, % compact the code looks ugly in type
       belowskip=0pt,  % user responsible to insert any skips
      backgroundcolor=\color{gray!15}, #1
}}
{}

\emphasis{test,aline}
\begin{teX}
  % test
  \test
  \aline
\end{teX}
and another
\emphasis[blue]{new,aline}
\begin{teX}
  \new\aline
\end{teX}
\end{document}