12

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]{/*}{*/},
 commentstyle=\color{purple}\ttfamily,
 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.

16

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

comment=[l]{#},

Change this to:

comment=[l]{\#},

and it compiles for me.

  • 1
    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
3

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 definitions

...

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

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.