清单中的代码格式令人沮丧

清单中的代码格式令人沮丧

我一直在尝试为 C++ 代码创建一些自定义格式。但我遇到了以下问题:

  • 右括号)不受格式影响。
  • 格式化type中断typedeftypeid、 和typename
  • 格式化/*中断评论。
  • 如何格式化不同于字符串的字符文字声明?例如,在下面的例子中,字符文字Z应该是洋红色而不是黄色(我正在使用的字符串类型的颜色;而且我没有任何尝试代码)。

我正在从一个文件中加载测试代码,该文件仅包含我想要格式化的所有关键字和字符。以下是我的 TeX 文件代码:

\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}

以下是输入文件代码(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

不管怎样,我正在尝试从 Notepad++ 重新创建“深黑”C++ 主题。另外,我对使用 LaTeX 还不太熟悉(写起来总是很有趣),所以我对代码的理解很少。

我在 Windows 上使用 Texmaker 和 MiKTeX 作为设置,但我也在 Overleaf 上尝试过代码;结果相同。

相关内容