TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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, my, yours):
        bla bla bla...

What should I write in my \lstset command in order to highlight 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.

share|improve this question

migrated from stackoverflow.com Nov 23 '12 at 1:16

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

And why is morekeywords={...} not working with lstinputlistings. It does with lstlistings environment, but doesn't with input from a source file. – fmonegaglia Nov 21 '12 at 16:13
I'd recommend using minted instead or pythontex. They produce much better results and also the default highlighting is already coloured (and the colours are nice). Anyway this should really be posted in TeX Exchange as unutbu said. (On a side note: don't add information as comment, edit your question). – Bakuriu Nov 21 '12 at 16:22
up vote 10 down vote accepted

I'd consider running your code through pygments to generate the latex, probably using the minted package. You can get some details here Source code highlighting in LaTeX .

share|improve this answer

Good approach is defining new environments for programming language. Minimal setup can be around this:


% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal

% Custom colors


% Python style for highlighting
otherkeywords={self},             % Add keywords here
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
frame=tb,                         % Any extra options here
showstringspaces=false            % 

% Python environment

% Python for external files

% Python for inline


\section{``In-text'' listing highlighting}

class MyClass(Yourclass):
    def __init__(self, my, yours):
        bla = '5 1 2 3 4'
        print bla

\section{External listing highlighting}


\section{Inline highlighting}

Definition \pythoninline{class MyClass} means \dots



enter image description here

share|improve this answer
very nice, helped me a lot. My suggestion is to add \small to all styles, to make it smaller. Right now the code looks clumsy and big to me – kadir May 11 '14 at 22:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.