The listings
package does not have a parser feature to detect decimal numbers. Thus it is very difficult to catch them. Key literate
does not help much, because numbers in identifiers must not be colored.
The other requirements are easier:
- The following example uses the Latin Modern fonts.
- The colors are taken from the IDE's screenshot in the question.
- The color for the brackets and other punctuation/operator symbols
are set by the
literate
feature of listings
. The list can
be extended for other symbols. Exception, if*
and /
are redefined using literate
, then the comment detection breaks.
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[T1]{fontenc}
%\usepackage{tgcursor}
\usepackage{lmodern}
\colorlet{comment}{green!50!black}
\colorlet{cppcomment}{teal}
\colorlet{symb}{blue!50!black}
\colorlet{number}{violet}
\newcommand*{\textcolorsymb}{\textcolor{symb}}
\lstdefinestyle{cpp}{%
language=C++,
columns=flexible,
basewidth=.5em,
tabsize=3,
basicstyle=\ttfamily,
commentstyle={\itshape\color{comment}\let\textcolorsymb\relax},
keywordstyle=\bfseries,
morecomment={[l][\itshape\color{cppcomment}\let\textcolorsymb\relax]//},
literate=%
{\{}{\textcolorsymb{\{}}1
{\}}{\textcolorsymb{\}}}1
{(}{\textcolorsymb{(}}1
{)}{\textcolorsymb{)}}1
{;}{\textcolorsymb{;}}1
{=}{\textcolorsymb{=}}1
{<}{\textcolorsymb{<}}1
{>}{\textcolorsymb{>}}1
{!}{\textcolorsymb{!}}1
{\&}{\textcolorsymb{\&}}1
{|}{\textcolorsymb{|}}1
{?}{\textcolorsymb{?}}1
{:}{\textcolorsymb{:}}1
{+}{\textcolorsymb{+}}1
{-}{\textcolorsymb{-}}1
{,}{\textcolorsymb{,}}1
{\%}{\textcolorsymb{\%}}1
{\^}{\textcolorsymb{\textasciicircum}}1
{~}{\textcolorsymb{\textasciitilde}}1
%% {/}{\textcolorsymb{/}}1
%% {*}{\textcolorsymb{*}}1
% 2 (optionally)
{==}{\textcolorsymb{==}}2
{>=}{\textcolorsymb{=>}}2
{<=}{\textcolorsymb{<=}}2
{!=}{\textcolorsymb{!=}}2
{+=}{\textcolorsymb{+=}}2
{-=}{\textcolorsymb{-=}}2
{*=}{\textcolorsymb{*=}}2
{/=}{\textcolorsymb{/=}}2
{\%=}{\textcolorsymb{\%=}}2
{\&\&}{\textcolorsymb{\&\&}}2
{||}{\textcolorsymb{||}}2
{++}{\textcolorsymb{++}}2
{--}{\textcolorsymb{--}}2
{>>}{\textcolorsymb{>\kern0pt>}}2
{<<}{\textcolorsymb{<\kern0pt<}}2
{::}{\textcolorsymb{::}}2
% 3 (optionally)
{>>=}{\textcolorsymb{>\kern0pt>=}}3
{<<=}{\textcolorsymb{<\kern0pt<=}}3
% Remove byte order mark
{^^ef^^bb^^bf}{}0
}
\lstnewenvironment{cpp}{\lstset{style=cpp}}{}
\begin{document}
\begin{cpp}
long some_function();
int other_function();
/* This is a comment ()={}; */
int calling_function()
{
long test1;
int test2;
a == b <= c >= d;
a & b && c | d || e;
a ? b : c;
a + b++, c - d--;
a %= b % c >> e << f;
a *= *c; d /= e;
a / b ^ c ~ d;
a <<= b, c >>= d;
test1 = some_function();
// this is another comment
if (test1 > 0)
{
test2 = 0;
}
else
{
test2 = other_function();
}
return test2;
}
\end{cpp}
\end{document}

The result with TeX Gyre Cursor (developed from Ghostscript's Cursor variant): \usepackage{lmodern}
is replaced by \usepackage{tgcursor}
.

Removal of the byte order mark
Byte order marks in UTF-8 can be removed by a properly configured editor.
Or the literate
feature can also be used to remove them, see above and here:
literate={^^ef^^bb^^bf{}0}
See Ulrike Fischer's answer to xport's question "How to suppress BOM effect in the output?".