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?


6 Answers 6


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.

  • 5
    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, 2010 at 18:51
  • 3
    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, 2010 at 19:56
  • 2
    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. Sep 4, 2010 at 17:30
  • Is listings defunct? The link seems dead
    – soandos
    Jan 29, 2015 at 20:20
  • @soandos Link fixed
    – Joseph Wright
    Jan 29, 2015 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.

  • 1
    This package is awesome. Plus, it's made by a fellow SO-er: stackoverflow.com/questions/1966425/…. Aug 5, 2010 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. Aug 5, 2010 at 9:25
  • Yeah, it's great, I particularly like code coloring straight out of the box and Scala support.
    – nietaki
    Sep 8, 2013 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 %%
  • Mark up code using either PRE HTML blocks or by indenting with four spaces. Or online with back ticks. Aug 4, 2010 at 4:23
  • 2
    Consider using the matlab-prettifier package; see this answer.
    – jub0bs
    Feb 11, 2014 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.

  • 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). Aug 19, 2010 at 16:34

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"}

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.)

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.