8

I wish to add a few keywords to some python code inserted into a LaTeX document. I want to make True & False appear yellow and also some module calls such as ttk appear red. I have tried using the morekeywords call, however, it is not working for me. Below is my LaTeX code:

\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
otherkeywords={self},             
keywordstyle=\ttb\color{deepblue},
morekeywords={ttk}
emph={MyClass,__init__},          
emphstyle=\ttb\color{deepred},    
stringstyle=\color{deepgreen},

showstringspaces=false            
}}
5

You can use

keywords=[<number>]{<list of keywords>}

to add another set of keywords and then

keywordstyle={[<number>]<style commands>},

to give a style for this new set.

Since I didn't have the original definitions for your colors nor of the \ttb command, in the example below I used some own settings, but you can easily use your own settings; the code was randomly taken, just for this example.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{bera}% optional; just for the example

\lstset{
language=Python,
basicstyle=\ttfamily,
otherkeywords={self},             
keywordstyle=\ttfamily\color{blue!90!black},
keywords=[2]{True,False},
keywords=[3]{ttk},
keywordstyle={[2]\ttfamily\color{yellow!80!orange}},
keywordstyle={[3]\ttfamily\color{red!80!orange}},
emph={MyClass,__init__},          
emphstyle=\ttfamily\color{red!80!black},    
stringstyle=\color{green!80!black},
showstringspaces=false            
}

\begin{document}

\begin{lstlisting} 
def under_attack(col, queens):
    left = right = col
    for r, c in reversed(queens):
        left, right = left - 1, right + 1

        if c in (left, col, right):
            return True
    return False
    print 'This generation has {0} babies'.format(babies)
ttk.Button(buttonframe,width = 3,
           textvariable=guivars["%s %s" %(current, item)],
           command=lambda: remoteButton(current, item))
\end{lstlisting}

\end{document}

enter image description here

Please notice that "ttk" is being treated as a keyword with red color, as was also requested.

| improve this answer | |
  • This is great, sorry for the lack of not mentioning the \ttb deceleration. This will help me greatly. – user28462 Apr 4 '13 at 18:46

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.