Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

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}
share|improve this answer
add comment

Works for me. I do get an undefined control sequence error, but for \color!

Using

\usepackage{color}

makes this work for me.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.