[Tex/LaTex] Using different colors for different keywords in lstlisting

codecolorlistings

I've looked through a few answers on here, and either they don't work or don't fully answer my question. I have to format some C++ code and I want it to have these colors for these specific keywords (to simulate some other syntax highlighting), but I don't know if its possible:

  1. commentstyle has an RGB value of (2,112,10)
  2. keywordstyle has an RGB value of (108,48,130)
  3. stringstyle has the color red
  4. The keywords (int,char,double,float,unsigned,void,bool) are blue
  5. The keywords (<,>,.,;,,,-,!,=,~) have an RGB value of (255,165,0)
  6. If possible, function names (which means a word that has a ( immediately after it) have an RGB value of (129,20,83).

So far I have this (which was a solution that I found from another question on here):

\documentclass[11pt]{article} % use larger type; default would be 10pt
\usepackage{titlesec}
\usepackage[utf8]{inputenc} % set input encoding (not needed with XeLaTeX)
\usepackage{float}
\usepackage{geometry} % to change the page dimensions
\geometry{a4paper}
\usepackage{graphicx}

\usepackage{xcolor}
\definecolor{commentgreen}{RGB}{2,112,10}
\definecolor{eminence}{RGB}{108,48,130}
\definecolor{weborange}{RGB}{255,165,0}
\definecolor{frenchplum}{RGB}{129,20,83}

\usepackage{listings}
\usepackage{textcomp}
\lstset {
    language=C++,
    frame=tb,
    tabsize=4,
    showstringspaces=false,
    numbers=left,
    upquote=true,
    commentstyle=\color{commentgreen},
    keywordstyle=\color{eminence},
    stringstyle=\color{red},
    basicstyle=\small\ttfamily, % basic font setting
    emph={int,char,double,float,unsigned,void,bool},
    emphstyle={\color{blue}},
    escapechar=\&,
    % keyword highlighting
    classoffset=1, % starting new class
    morekeywords={>,<,.,;,,,-,!,=,~},
    keywordstyle=\color{weborange},
    classoffset=0,
}


\newcommand{\code}[1]{\texttt{#1}} % inline code setting
\usepackage{mathtools}         % used for math symbols

\title{Testing C++ Styles}
\author{Joe}
\date{}

\begin{document}
\maketitle

\section{C++ STUFF}
Hi there!

\begin{lstlisting}
/* Comment1 */
// comment 2
// , !- should still be green

bool Data::DoStuff(int enum_type){
    std::vector<Obj*>::iterator object;
    Param* par=NULL;

    for(object=obj.begin();obj<obj.end();++obj){
        if(par->GetEnum()==enum_type){
            return true;
        }
    }
    _error_("string here!" << strx(enum_type));
    return false;
}
\end{lstlisting}
\end{document}

But my classoffset solution doesn't work. Nothing is being highlighted in orange. And I also don't know if its possible to do #6 in the list above. If anyone has ideas let me know!

Best Answer

This is a partial answer.

I met this dirty trick once before: add otherkeywords={>,<,.,;,-,!,=,~} alongside morekeywords={>,<,.,;,-,!,=,~}.

As for comma, there are other posts giving completely different approach. It is up to you how fancy the C++ code should be. But more fancy the code is, more fragile Listings is.

\documentclass[11pt]{article} % use larger type; default would be 10pt

\usepackage{xcolor}
\definecolor{commentgreen}{RGB}{2,112,10}
\definecolor{eminence}{RGB}{108,48,130}
\definecolor{weborange}{RGB}{255,165,0}
\definecolor{frenchplum}{RGB}{129,20,83}

\usepackage{listings}
\lstset {
    language=C++,
    frame=tb,
    tabsize=4,
    showstringspaces=false,
    numbers=left,
    %upquote=true,
    commentstyle=\color{commentgreen},
    keywordstyle=\color{eminence},
    stringstyle=\color{red},
    basicstyle=\small\ttfamily, % basic font setting
    emph={int,char,double,float,unsigned,void,bool},
    emphstyle={\color{blue}},
    escapechar=\&,
    % keyword highlighting
    classoffset=1, % starting new class
    otherkeywords={>,<,.,;,-,!,=,~},
    morekeywords={>,<,.,;,-,!,=,~},
    keywordstyle=\color{weborange},
    classoffset=0,
}


\newcommand{\code}[1]{\texttt{#1}} % inline code setting
\usepackage{mathtools}         % used for math symbols

\title{Testing C++ Styles}
\author{Joe}
\date{}

\begin{document}
\maketitle

\section{C++ STUFF}
Hi there!

\begin{lstlisting}[caption={Yo!}]
/* Comment1 */
// comment 2
// , !- should still be green

bool Data::DoStuff(int enum_type){
    std::vector<Obj*>::iterator object;
    Param* par=NULL;

    for(object=obj.begin();obj<obj.end();++obj){
        if(par->GetEnum()==enum_type){
            return true;
        }
    }
    _error_("string here!" << strx(enum_type));
    return false;
}
\end{lstlisting}
\end{document}
Related Question