Take the 2-minute tour ×
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.

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
    

6 Answers 6

up vote 39 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
4  
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
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 '10 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. –  Brooks Moses Sep 4 '10 at 17:30

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"}
share|improve this answer

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}
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
1  
Consider using the matlab-prettifier package; see this answer. –  Jubobs Feb 11 at 7:51

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

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

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

Your Answer

 
discard

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.