0

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

3
  • Please paste the code here so that others can copy to test. Don't include a screenshot.
    – fractal
    May 19, 2020 at 11:59
  • 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
    May 19, 2020 at 12:17
  • 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.
    – Marijn
    May 19, 2020 at 12:19

1 Answer 1

2

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

4
  • 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"?
    – Felixx2403
    May 19, 2020 at 14:25
  • @Felixx2403 you can use another set of keywords with keywords=[2]{pow} and then assign a color to them. I just updated the code.
    – Pedro
    May 19, 2020 at 14:46
  • Ahh, now I see how the assignment of keywords works. Thanks a lot!
    – Felixx2403
    May 20, 2020 at 8:50
  • You're welcome. You can use many sets of kewords with different colors. Just keep adding indexes: keywords=[n]{name}.
    – Pedro
    May 20, 2020 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.