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?
|
|
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. |
|||||||||||||
|
|
While |
|||||
|
|
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. |
|||
|
|
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.) |
|||
|
|
|
I use matlab code in my papers. To get syntax coloring in matlab, put the following in the header:
Then use the Then just enclose the matlab code inside
|
|||||
|
|
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:
Now, throughout my paper I include results from estimations without any language defined
And at the end I change the language to R to show some code
|
|||
|
|
