列表包突出显示不同的背景颜色

列表包突出显示不同的背景颜色

我希望使用列表包突出显示不同的代码颜色,如下所示: 在此处输入图片描述

下面的代码突出显示前景,如何改变线条的背景颜色?

\documentclass{standalone}
\usepackage{listings}
\usepackage{xcolor}

\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{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinelanguage{diff}{
    morecomment=[f][\color{blue}]{@@},     % group identifier
    morecomment=[f][\color{red}]-,         % deleted lines 
    morecomment=[f][\color{green}]+,       % added lines
    morecomment=[f][\color{magenta}]{---}, % Diff header lines (must appear after +,-)
    morecomment=[f][\color{rgb,255:red,123; green,6; blue,1}]{+++},
}
\lstdefinestyle{diffstyle}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}
\lstset{style=diffstyle}
\begin{document}
\begin{lstlisting}[language=diff]
--- a/test.cpp
+++ b/test.cpp
@@ -1,6 +1,6 @@
int main() {
    int x = 10;
    int y = 20;
    -    printf("%d + %d = %d\n",x,y,x+y);
    +    printf("%d * %d = %d\n",x,y,x*y);
    return 0;
}   
\end{lstlisting}
%\lstinputlisting[language=diff]{test.diff}
\end{document}

电流输出:

在此处输入图片描述

相关内容