4

I have a little problem in my settings. I cannot locate the error and need some additional eyes to tell me where I should look. I use listing to show my c++ code but when I try to add additional keywords they are not highlighted. Here's the setting:

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{dred}{rgb}{0.545,0,0}
\definecolor{dblue}{rgb}{0,0,0.545}
\definecolor{lgrey}{rgb}{0.9,0.9,0.9}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\lstset{ 
      backgroundcolor=\color{lgrey},  
      basicstyle=\footnotesize \ttfamily \color{black} \bfseries,   
      breakatwhitespace=false,       
      breaklines=true,               
      captionpos=b,                   
      commentstyle=\color{dkgreen},   
      deletekeywords={...},          
      escapeinside={\%*}{*)},                  
      frame=single,                  
      keywordstyle=\color{purple},  
      morekeywords={BRIEFDescriptorConfig,string,TiXmlNode,DetectorDescriptorConfigContainer,istringstream,cerr,exit}, 
      identifierstyle=\color{black},
      stringstyle=\color{blue},      
      language=C++,                
      numbers=right,                 
      numbersep=5pt,                  
      numberstyle=\tiny\color{black}, 
      rulecolor=\color{black},        
      showspaces=false,               
      showstringspaces=false,        
      showtabs=false,                
      stepnumber=1,                   
      tabsize=5,                     
      title=\lstname,                 
    }

So nothing special, I guess. Here is an minimal-example using listing:

\begin{lstlisting}[language={C++},caption={Caption}]
void XMLDetDecConfigReader::handleDescriptors(DetectorDescriptorConfigContainer* configContainer, TiXmlNode* descriptorsNode);
\end{lstlisting}

I don't want to use any other packages but to solve this highlighting-problem.

Thank you for your help.

2

Output

enter image description here

I would heed Sašo Živanović's advice about defining your own C++ dialect if you are going to typeset other codes in other languages in your document apart from C++. If not, then write language=C++, before keywordstyle=\color{purple}, in your \lstset, then omit the language={C++} from your optional argument. You do this also for your own flavor of C++ highlighting via the \lstdefinelanguage command.

Here is a minimal working example (MWE) completed from your code snippets. (Please post MWEs next time.)

Code

%http://tex.stackexchange.com/questions/99455/keyword-highlighting-of-c-using-listing#99455
\documentclass[preview,border=5]{standalone}
%\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{dred}{rgb}{0.545,0,0}
\definecolor{dblue}{rgb}{0,0,0.545}
\definecolor{lgrey}{rgb}{0.9,0.9,0.9}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\lstdefinelanguage{cpp}{
      backgroundcolor=\color{lgrey},  
      basicstyle=\footnotesize \ttfamily \color{black} \bfseries,   
      breakatwhitespace=false,       
      breaklines=true,               
      captionpos=b,                   
      commentstyle=\color{dkgreen},   
      deletekeywords={...},          
      escapeinside={\%*}{*)},                  
      frame=single,                  
      language=C++,                
      keywordstyle=\color{purple},  
      morekeywords={BRIEFDescriptorConfig,string,TiXmlNode,DetectorDescriptorConfigContainer,istringstream,cerr,exit}, 
      identifierstyle=\color{black},
      stringstyle=\color{blue},      
      numbers=right,                 
      numbersep=5pt,                  
      numberstyle=\tiny\color{black}, 
      rulecolor=\color{black},        
      showspaces=false,               
      showstringspaces=false,        
      showtabs=false,                
      stepnumber=1,                   
      tabsize=5,                     
      title=\lstname,                 
    }


\begin{document}
\begin{lstlisting}[language=cpp,caption={Caption}]
void XMLDetDecConfigReader::handleDescriptors(DetectorDescriptorConfigContainer* configContainer, TiXmlNode* descriptorsNode);
\end{lstlisting}
\end{document}
3

What you do is define what style you will use and then by specifying language={C++} in the environment effectively undo your specification. Either omit language=... from the optional argument or, probably even better, define your own dialect of C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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