Syntax Coloring in LaTeX
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
.
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.
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}