62

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?

1

6 Answers 6

44

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
  • 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
48

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.

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

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

\usepackage{alltt}
\usepackage{color}
\usepackage{fullpage}
\definecolor{string}{rgb}{0.7,0.0,0.0}
\definecolor{comment}{rgb}{0.13,0.54,0.13}
\definecolor{keyword}{rgb}{0.0,0.0,1.0}

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

\begin{alltt}
%% code generated by highlight.m %%
\end{alltt}
2
  • 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
5

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.

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

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}

\definecolor{gogetit}{HTML}{6C8B9F}
\lstset{
    fancyvrb=true,
    showstringspaces=false,
    tabsize=3,
    keywordstyle=\color{blue}\textbf,
    identifierstyle=,
    commentstyle=\color{gogetit}\textit,
    stringstyle=\color{red},
    }
\begin{document}
\fvset{numbers=left,numbersep=3pt,fontsize=\small,fontfamily=helvetica,frame=lines,resetmargins=true}  

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

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

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

\section*{Appendix: Code}
\lstset{language=R}
\newcounter{codein}
\setcounter{codein}{1}
\VerbatimInput[firstline=70,lastline=90,label=\fbox{\textbf{Code \arabic{codein}:} function for IV estimation}]{"ps3.R"}
\stepcounter{codein}
\VerbatimInput[firstline=42,lastline=53,label=\fbox{\textbf{Code \arabic{codein}:} makeresiduals (called by IVregress)}]{"ps3.R"}
\stepcounter{codein}
\VerbatimInput[firstline=53,lastline=58,label=\fbox{\textbf{Code \arabic{codein}:} addcons (called by IVregress)}]{"ps3.R"}
4

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.