TransWikia.com

listings package highlight differ background color

TeX - LaTeX Asked by lucky1928 on February 7, 2021

I wish to highlight differ code color with listings package like below:
enter image description here

Below code highlight the foreground, how to change the line’s background color as well?

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=tinycolor{codegray},
    stringstyle=color{codepurple},
    basicstyle=ttfamilyfootnotesize,
    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 = %dn",x,y,x+y);
    +    printf("%d * %d = %dn",x,y,x*y);
    return 0;
}   
end{lstlisting}
%lstinputlisting[language=diff]{test.diff}
end{document}

Current output:

enter image description here

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP