# Python code in LaTeX document

I want to include some Python code into LaTex document, so I "imported" the listings package with

\usepackage{listings}


and

\lstdefinelanguage{Python}{
keywords={typeof, null, catch, switch, in, int, str, float, self},
keywordstyle=\color{ForestGreen}\bfseries,
ndkeywords={boolean, throw, import},
ndkeywords={return, class, if ,elif, endif, while, do, else, True, False , catch, def},
ndkeywordstyle=\color{BrickRed}\bfseries,
identifierstyle=\color{black},
sensitive=false,
comment=[l]{#},
morecomment=[s]{/*}{*/},
stringstyle=\color{red}\ttfamily,
}


when compiling, I get:

 Illegal parameter number in definition oof \lstlang@python\$ }


I googled, but I didn't understood how to work the problem out. it speaks about a command or something similar.

-

## migrated from stackoverflow.comJun 12 '12 at 13:48

This question came from our site for professional and enthusiast programmers.

Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Werner Jun 12 '12 at 14:05
–  lockstep Jan 27 '13 at 15:11

In the LaTeX source you posted, you have an error at:

comment=[l]{#},


Change this to:

comment=[l]{\#},


and it compiles for me.

-
Yes, it works to me too. Last question, I indicated keywords. How to get them colored? I just see black line of code, without no color –  FrankBr Jun 11 '12 at 15:18

I found the following in the Reference Guide of the listings package documentation:

### 4.1 How to read the reference

...

Regarding the parameters, please keep in mind the following:

...

5.If you want to enter one of the special characters {}#%\, this character must be escaped with a backslash. This means that you must write } for the single character ‘right brace’—but of course not for the closing paramater character.

and also in the following section there is a note

### 4.18 Language deﬁnitions

...

... Note: If you want to enter \, {, }, %, # or & as (part of ) an argument to the keywords below, you must do it with a preceding backslash!

-