Color of Python Comment Keywords in Latex

1,062

Use \lstset. Here is a MWE:

\documentclass{article}
\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}

\begin{document}

\lstset{language=Python, 
        basicstyle=\ttfamily\small, 
        keywordstyle=\color{keywords},
        commentstyle=\color{comments},
        stringstyle=\color{red},
        showstringspaces=false,
        identifierstyle=\color{green},
        keywords=[2]{pow},
        keywordstyle=[2]{\color{orange}},
}

\begin{lstlisting}
#This is a comment with keywords: and or for in range
n = 5
for i in range(0, n): 
    print( pow(i,3) )
#Over and out
\end{lstlisting}    

\end{document}

The above code will give you this: enter image description here

Share:
1,062

Related videos on Youtube

Felixx2403
Author by

Felixx2403

Updated on May 20, 2020

Comments

  • Felixx2403
    Felixx2403 over 3 years

    if have the following problem.

    I have to insert Python Code in Latex. I copied a nice code for Latex which creates a good looking Python output for my purposes.

    Now I have the problem that I defined a certain color for programming keywords like "for, and,..." But Latex, of course, displays also the and's and for's that I used in the comment section in Python after the # sign in this color. Is there any way to change this?

    Best

    Felix

    enter image description here

    • fractal
      fractal over 3 years
      Please paste the code here so that others can copy to test. Don't include a screenshot.
    • Marijn
      Marijn over 3 years
      You are missing the definition of the normal comment (only triple quote comments ''' and """ are defined), so you should add morecomment=[l]\#,%. However, Python is a language that is defined by default for listings, so you don't need this definition at all, remove all this code and just use \lstset{language=Python} and you are done.
    • Marijn
      Marijn over 3 years
      and if you do want additional settings that are different from the default then use \lstset for this and not \lstdefinelanguage, with \lstset the extra settings are applied on top of the defaults, whereas with \lstdefinelanguage the defaults are discarded.
  • Felixx2403
    Felixx2403 over 3 years
    Thanks for your help. Is there an opportunity to define the style of functions in Python? Functions such as "pow" I would like Latex to display in a bright orange instead of pink. How can I define the function style without changing the pink color of keywords like "import"?
  • Pedro
    Pedro over 3 years
    @Felixx2403 you can use another set of keywords with keywords=[2]{pow} and then assign a color to them. I just updated the code.
  • Felixx2403
    Felixx2403 over 3 years
    Ahh, now I see how the assignment of keywords works. Thanks a lot!
  • Pedro
    Pedro over 3 years
    You're welcome. You can use many sets of kewords with different colors. Just keep adding indexes: keywords=[n]{name}.