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:
commentstyle
has an RGB value of (2,112,10)keywordstyle
has an RGB value of (108,48,130)stringstyle
has the color red- The keywords (
int
,char
,double
,float
,unsigned
,void
,bool
) are blue - The keywords (
<
,>
,.
,;
,,
,-
,!
,=
,~
) have an RGB value of (255,165,0) - 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}[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}
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!
>
etc. aren't recognized as keywordslistings
package. I leave this to the experts