I am using package listings to import my Python source code into my LaTeX document. I use the command \lstinputlistings. I have a Python source like

class MyClass(Yourclass):
    def __init__(self, myvar, yours):
        bla bla bla...

What should I write in my \lstset command in order to enlight words MyClass, init etc.? I wouldn't want to write any word I want to be highlighted. I tried using moredelims=[s][\color{teal}]{class}{(} inside lstset but it doesn't work.

And why is morekeywords={...} not working with lstinputlistings. It does with lstlistings environment, but doesn't with input from a source file.

  • 1
    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. Upvoting is the preferred way here to say "thank you" to users who helped you.
    – Mensch
    Commented Nov 21, 2012 at 16:38
  • There is no moredelims option. Will \lstset{language=Python,morekeywords={as,init}, moredelim=*[s][\color{teal}]{MyClass}{)}, moredelim=*[s][\color{teal}]{__}{)}} work for you? If you provide a minimal working example (MWE) and perhaps post your output or clarify what you are trying to achieve, then you may get better help.
    – hpesoj626
    Commented Nov 22, 2012 at 2:30

2 Answers 2


It is always good to post a minimal and compilable example, not just code snippets. This way, the answerers do not have to guess what's happening with your problem.

I guess that you are looking for something like this.


where guess.py is your sample code snippet. I just added as on the last line to show that morekeywords works.

class MyClass(Yourclass):
    def __init__(self, myvar, yours):
        bla bla bla... as

Here is the output.

enter image description here

You can also remove __init__ from morekeywords option and use the answers in How to I emphazise all words beginning with ` in an lstlisting and Listings language definition keyword suffixes. So you may put the following code snippet into your preamble.


Let me know if this works for you.

  • It does work, thank you. Anyway I was looking for a solution which is not depending on single words I write into my keywords...I found the best the solution in another discussion: package minted Commented Nov 26, 2012 at 17:40

To sidestep your listinputlistings issue, you can always place do


or do a command like so:


As far as adding more keywords, I mucked around with it a while ago and came up with

\lstset{emph={Keyword1, Keyword2, Etc}, emphstyle={\bf}}

I am not sure if this allows you to change the colors, however.

  • 1
    I already tried using emph but not without issues, for example the word as is not recognized by listings. If you try setting in emph, you will have as highlighted in all of your comments and even in the word clASs...anyway the input command seems to work, I haven't actually thought about that before. Commented Nov 21, 2012 at 18:15
  • Well, that's silly. I'll keep looking for a real solution. Commented Nov 21, 2012 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .