TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

Is there any way to include Python code in a LaTeX file and automatically color its syntax? Is it possible to do syntax coloring with any other language? If yes what packages can I use to do that?

share|improve this question
up vote 43 down vote accepted

The standard way of doing this is to use the listings package. It allows a wide range of formatting for the output, can choose to display only part of an input file and so on. The package also comes with a number of predefined languages it understands, including Python.

share|improve this answer
Listings is nice and I use it a lot- but it is getting a little long in the tooth. The last update was in 2007. – Sharpie Aug 3 '10 at 18:51
Well, LaTeX2e was release in 1994 :-) I believe that there is some testing going on with a new version, although news has been sporadic. – Joseph Wright Aug 3 '10 at 19:56
Elaborating a bit on Sharpie's comment -- the Listings v.1 package can't really change very much at all without breaking backwards compatibility, so there isn't much updating that can be done other than relatively minor bug-fixes. (I say this as the current maintainer, who is embarrassed to be so far behind on these "minor" bug-fixes, but unfortunately the fixes are far less minor than the bugs.) The original author has mentioned a v.2 of the Listings package, about a year ago, but I know of no more recent news. – Brooks Moses Sep 4 '10 at 17:30
Is listings defunct? The link seems dead – soandos Jan 29 '15 at 20:20
@soandos Link fixed – Joseph Wright Jan 29 '15 at 20:26

While listings package is good, it has some deficiencies, especially in Unicode support. I'd recommend trying out minted, which uses the output of Pygments, which is more likely to get grammar updates than listings.

share|improve this answer
This package is awesome. Plus, it's made by a fellow SO-er: stackoverflow.com/questions/1966425/…. – Ben Alpert Aug 5 '10 at 5:16
If I just had time to push a new release already … :-( At the moment, it still has got quite a few problems. – Konrad Rudolph Aug 5 '10 at 9:25
Yeah, it's great, I particularly like code coloring straight out of the box and Scala support. – nietaki Sep 8 '13 at 22:40

I use matlab code in my papers. To get syntax coloring in matlab, put the following in the header:


Then use the highlight.m function in matlab to generate the code to put in the latex file.

Then just enclose the matlab code inside

%% code generated by highlight.m %%
share|improve this answer
Mark up code using either PRE HTML blocks or by indenting with four spaces. Or online with back ticks. – Will Robertson Aug 4 '10 at 4:23
Thanks Konrad for reformatting. – Dash Sep 1 '10 at 15:24
Consider using the matlab-prettifier package; see this answer. – Jubobs Feb 11 '14 at 7:51

Another package to consider is fancyvrb. It is a bit more general than listings as it provides an expanded verbatim environment where listings focuses specifically on formatting source code- this means you can pull some tricks with listings that are not possible with fancyvrb. However, fancyvrb is the package used by the Sphinx documentation generator to highlight source code in PDF output.

If you are documenting some Python code, you may want to look into using Sphinx anyway- it is the bee's knees.

Another package that looks promising is minted. This package allows you to format code using Pygments which is a very sophisticated syntax highlighter written in Python.

share|improve this answer
fancyvrb is nice. With time I lean towards minimalist highlighting, but I still need some basic formatting support. Depending on the case, I also use listings with a very simple configuration (just tt, and serif italic for comments or meta-syntactic stuff). – Damien Pollet Aug 19 '10 at 16:34

For technical documentation my preferred method is to use Sphinx. The output LaTeX looks ugly (well, machine generated) but the output looks nice. It uses Pygments behind the scenes.

I've not used minted, but from what people say it should work just as well, and allow you to author a LaTeX document. (But if you use Sphinx, you'll get HTML and other outputs. They're working on epub.)

share|improve this answer

Here's some code I made to put listings of results and code in some homework i turned in recently. Results and code listings each get their own counter so they are numbered, and my paper can reference them.

Here's the stuff for the header:

\usepackage{fancyvrb, listings, color}


Now, throughout my paper I include results from estimations without any language defined

\VerbatimInput[firstline=6,lastline=27,label=\fbox{\textbf{Results \arabic{resultsin}:} OLS labor supply}]{"./results/ps3_stata_c.log"}

And at the end I change the language to R to show some code

\section*{Appendix: Code}
\VerbatimInput[firstline=70,lastline=90,label=\fbox{\textbf{Code \arabic{codein}:} function for IV estimation}]{"ps3.R"}
\VerbatimInput[firstline=42,lastline=53,label=\fbox{\textbf{Code \arabic{codein}:} makeresiduals (called by IVregress)}]{"ps3.R"}
\VerbatimInput[firstline=53,lastline=58,label=\fbox{\textbf{Code \arabic{codein}:} addcons (called by IVregress)}]{"ps3.R"}
share|improve this answer

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.