Search code examples
latexlistingslstlisting

Fontcolor in listing


I want to write a listing for python code. My problem, the zero by x = SUM('ZM_test'[testcol 0AN]) is in black fontcolor and not in green. My style settings for the listing looks like this:

\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\usepackage[left=4cm,right=3cm,top=2.5cm,bottom=2.5cm]{geometry}

\usepackage{listings}
\usepackage{color}
\definecolor{keywords}{RGB}{255,0,90}
\definecolor{comments}{RGB}{0,0,113}
\definecolor{red}{RGB}{160,0,0}
\definecolor{green}{RGB}{0,150,0}
\lstset{language=Python, 
    basicstyle=\ttfamily\footnotesize, 
    keywordstyle=\color{keywords},
    commentstyle=\color{comments},
    stringstyle=\color{red},
    showstringspaces=false,
    identifierstyle=\color{green},
    %procnamekeys={def,class}
    numbers=left,
    xleftmargin=2em,
    frame=single,
    framexleftmargin=1.5em
}

\begin{document}
\begin{lstlisting}[language=Python, caption=xy, label=xy, numbers =left, frame= lines, gobble = 0]
x = SUM('ZM_test'[testcol 0AN])
y = SUM('ZM_test'[testcol bAN])
\end{lstlisting}
\end{document}

Do you know, why the zero has the fontcolor "black" and not "green"? I want the zero green like the "b" in y = SUM('ZM_test'[testcol bAN])

EDIT: That is not python code, but it should be the python style. Best regards Christian


Solution

  • You could try the approach from https://tex.stackexchange.com/a/42895

    \documentclass[12pt,a4paper]{report}
    \usepackage[utf8]{inputenc}
    \usepackage[german]{babel}
    \usepackage[left=4cm,right=3cm,top=2.5cm,bottom=2.5cm]{geometry}
    
    \usepackage{listings}
    \usepackage{xcolor}
    \definecolor{keywords}{RGB}{255,0,90}
    \definecolor{comments}{RGB}{0,0,113}
    \definecolor{red}{RGB}{160,0,0}
    \definecolor{green}{RGB}{0,150,0}
    \lstset{language=Python, 
        basicstyle=\ttfamily\footnotesize, 
        keywordstyle=\color{keywords},
        commentstyle=\color{comments},
        stringstyle=\color{red},
        showstringspaces=false,
        identifierstyle=\color{green},
        %procnamekeys={def,class}
        numbers=left,
        xleftmargin=2em,
        frame=single,
        framexleftmargin=1.5em
    }
    
    
    \lstset{literate=%
       *{0}{{{\color{green}0}}}1
        {1}{{{\color{green}1}}}1
        {2}{{{\color{green}2}}}1
        {3}{{{\color{green}3}}}1
        {4}{{{\color{green}4}}}1
        {5}{{{\color{green}5}}}1
        {6}{{{\color{green}6}}}1
        {7}{{{\color{green}7}}}1
        {8}{{{\color{green}8}}}1
        {9}{{{\color{green}9}}}1
    }
    
    \begin{document}
    \begin{lstlisting}[language=Python, caption=xy, label=xy, numbers =left, frame= lines, gobble = 0]
    x = SUM('ZM_test'[testcol 0AN])
    y = SUM('ZM_test'[testcol bAN])
    \end{lstlisting}
    \end{document}
    

    enter image description here