0

I have been trying to create some custom formatting for C++ code. But I am having the following problems:

  • The right parentheses ) is immune to formatting.
  • Formatting type breaks typedef, typeid, and typename.
  • Formatting / and * breaks commenting.
  • How to format character literal declarations different from strings? I. e. in the example below, character literal Z should be magenta instead of yellow (color for the string type that I am using; also I do not have any attempt code for this).

I am loading test code from a file that simply has all the keywords and characters I would like formatted. Below is my TeX file code:

\documentclass{report}
\usepackage[utf8]{inputenc}

%Code Formatting
\usepackage{listings}
\usepackage{xcolor}
\usepackage{color}
\usepackage{setspace}

\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{codeyellow}{rgb}{1,1,0}
\definecolor{codeoperator}{rgb}{1,0.8,0}
\definecolor{codeinstuction}{rgb}{1,0.4,0}
\definecolor{codetype}{rgb}{0,1,1}%cyan
\definecolor{backcolour}{rgb}{0,0,0}


\definecolor{main-color}{rgb}{0.6627, 0.7176, 0.7764}
%\definecolor{back-color}{rgb}{0.1686, 0.1686, 0.1686}
\definecolor{back-color}{rgb}{0, 0, 0}
\definecolor{string-color}{rgb}{0.3333, 0.5254, 0.345}
\definecolor{key-color}{rgb}{0.8, 0.47, 0.196}
\usepackage[lighttt]{lmodern} % bold and italic ttfamily
\renewcommand{\ttdefault}{pcr}
\newcommand*{\textcolorsymb}{\textcolor{codeoperator}}

\lstdefinelanguage{Cpp}
{
    basicstyle = {\ttfamily \small \color{main-color}},
    backgroundcolor = {\color{back-color}},
    commentstyle=\color{codegreen},
    stringstyle=\color{codeyellow},
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=3pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2,
   %keywordstyle=\color{codetype} \bfseries,
    keywordstyle={[2]\color{codeoperator}\bfseries},
    keywordstyle={[3]\color{codeinstuction}\bfseries},
    keywordstyle={[4]\color{codetype}\bfseries},
    keywordstyle={[5]\color{codeinstuction}},
    otherkeywords={+,(,),[,],-,<,>,!,~,&,*,/,\%,=,^,|,?,:,sizeof,type},
    morekeywords=[2]{+,(,),[,],-,<,>,!,~,&,*,/,\%,=,^,|,?,:,sizeof,type},
    morekeywords=[3]{if,else,switch,case,default,break,goto,return,for,while,do,continue,typedef,NULL,new,delete,throw,try,catch,namespace,operator,this,const_cast,static_cast,dynamic_cast,reinterpret_cast,true,false,using,typeid,and,and_eq,bitand,bitor,compl,not,not_eq,or,or_eq,xor,xor_eq},
    morekeywords=[4]{void,struct,union,enum,char,short,int,long,double,float,signed,unsigned,const,static,extern,auto,register,volatile,bool,class,private,protected,public,friend,inline,template,virtual,asm,explicit,typename,mutable},
    %morekeywords=[5]{0,1,2,3,4,5,6,7,8,9},
    literate=%
    *{0}{{{\color{codeinstuction}0}}}{1}
    {1}{{{\color{codeinstuction}1}}}{1}
    {2}{{{\color{codeinstuction}2}}}{1}
    {3}{{{\color{codeinstuction}3}}}{1}
    {4}{{{\color{codeinstuction}4}}}{1}
    {5}{{{\color{codeinstuction}5}}}{1}
    {6}{{{\color{codeinstuction}6}}}{1}
    {7}{{{\color{codeinstuction}7}}}{1}
    {8}{{{\color{codeinstuction}8}}}{1}
    {9}{{{\color{codeinstuction}9}}}{1}
    {(}{{{\color{codeoperator}\bfseries{(}}}}{1}
    {)}{{{\color{codeoperator}\bfseries{)}}}}{1}
    {[}{{{\color{codeoperator}\bfseries{[}}}}{1}
    {]}{{{\color{codeoperator}\bfseries{]}}}}{1}
    {.}{{{\color{codeoperator}\bfseries{.}}}}{1}
    {,}{{{\color{codeoperator}\bfseries{,}}}}{1}
    {\{}{{{\color{codeoperator}\bfseries{\{}}}}{1}
    {\}}{{{\color{codeoperator}\bfseries{\}}}}}{1},
    comment=[l]{//},
    morecomment=[s]{/*}{*/},
    morestring=[b]{"},
    morestring=[b]{'},
    morestring=[b]{`},
}

%%%Document Begin%%%
\begin{document}
From the file \verb|Test.cpp|.

\lstinputlisting[language=Cpp]{Test.cpp}
\end{document}

Here is the input file code (cpp):

//Test

"A very clever string."

'Z'

/* Block Comment */

variable name

//Operators
(
)
[
]
->
.
++
--
+
-
!
~
type
&
sizeof
*
/
%
<<
>>
<
<=
>
>=
==
!=
^
|
&&
||
?
:
=
+=
-=
*=
/=
%=
>>=
<<=
&=
~=
|=
{
}
,

//Numbers
0
1
2
3
4
5
6
7
8
9

//Instructions
if
else
switch
case
default
break
goto
return
for
while
do
continue
typedef
NULL
new
delete
throw
try
catch
namespace
operator
this
const_cast
static_cast
dynamic_cast
reinterpret_cast
true
false
using
typeid
and
and_eq
bitand
bitor
compl
not
not_eq
or
or_eq
xor
xor_eq

//Types
void
struct
union
enum
char
short
int
long
double
float
signed
unsigned
const
static
extern
auto
register
volatile
bool
class
private
protected
public
friend
inline
template
virtual
asm
explicit
typename
mutable

For what it's worth, I am trying to recreate the 'Deep Black' C++ theme from Notepad++. Also, I am fairly new at using LaTeX (that's always fun to write), so I have minimal understanding of the code.

I have used Texmaker and MiKTeX as my setup on Windows, but I have also tried the code on Overleaf; same results.

2
  • Found a work around for having multiple comment styles in this thread: here
    – ErMo
    Commented Feb 9, 2020 at 5:57
  • Removing the '/' character from otherkeywords={+,(,),[,],-,<,>,!,~,&,*,/,\%,=,^,|,?,:,sizeof,type}, and morekeywords=[2]{+,(,),[,],-,<,>,!,~,&,*,/,\%,=,^,|,?,:,sizeof,type}, Results in errors. Even trying to highlight the asterisk using literate causes errors.
    – ErMo
    Commented Feb 12, 2020 at 19:14

0

You must log in to answer this question.

Browse other questions tagged .