# Adding words to lstlisting for Python langaue

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
}}

-
Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say "thank you" to users who helped you. –  Peter Jansson Apr 4 '13 at 12:58
No problem, appreciate the advice. –  user28462 Apr 4 '13 at 13:01

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}


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

-
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