gioby of Bioinfo Blog! (an interesting read by the way) left a comment asking about displaying code in LaTeX documents. I’ve sort of been cludging around using \hspace
‘s and \textcolor
but I’ve always meant to figure out the right way to do things so this seemed like a good chance to figure out how to do it right.
LaTeX tends to ignore white space. This is good when you’re writing papers but not so good when you’re trying to show code where white space is an essential part (e.g. Python). Luckily there’s a builtin verbatim
environment in LaTeX that is equivalent to html’s <pre>
. So something like the following should preserve white space.
\begin{verbatim}
for i in range(1, 5):
print i
else:
print "The for loop is over"
\end{verbatim}
Unfortunately, you can’t use any normal LaTeX commands inside verbatim (since they’re displayed verbatim). But luckily there a handy package called fancyvrb that fixes this (the color package is also useful for adding colors). For example, if you wanted to highlight “for” in the above code, you can use the Verbatim
(note the capital V) environment from fancyvrb:
\newcommand\codeHighlight[1]{\textcolor[rgb]{1,0,0}{\textbf{#1}}}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{for} i in range(1, 5):
print i
else:
print "The for loop is over"
\end{Verbatim}
If you really want to get fancy, the Pygments package in Python will output syntax highlighted latex code with a command like: pygmentize -f latex -O full test.py >py.tex
The LaTeX it outputs is a bit hard to read but it’s not too bad (it helped me figure out the fancyvrb package) and it does make nice syntax highlighted output.
Here’s an example LaTeX file with the three examples above and the pdf it generates if you’re curious.
gioby | 24-Apr-09 at 10:00 am | Permalink
Really, thank you a lot for this!!
I have been trying by myself, but I don’t know very much of LaTeX, so once I spent a lot of time to put a sample of python code in a document, and didn’t succed.
Thank you also for the link :)
rkoopmann | 24-Apr-09 at 11:25 am | Permalink
i’m a fan of listings http://www.ctan.org/tex-archive/macros/latex/contrib/listings/
it pulls straight from the source file, so the code and the documentation are always in sync.
ScottS-M | 25-Apr-09 at 4:25 pm | Permalink
@rkoopman
Good suggestion. That’s a useful package too, especially for long bits of code.
Lorenzo | 26-Apr-09 at 1:47 pm | Permalink
Thank you very much !
After a couple of hours trying to solve this problem I’ve been found your web page. Great !!!!
TNX again,
Lorenzo
Eric Gavaletz | 19-Nov-09 at 12:42 pm | Permalink
Thanks for this, I am glad you were the first hit on google. Oh, and the blog title “Dammit Jim!” is by far the best blog title I have ever come across.
Roy Emmerich | 24-Nov-09 at 12:16 pm | Permalink
Many thanks for this very useful post.
However I thought I’d add a few things for others who may be in need. I wanted to include the pygmentized latex as a sub-document in lyx using a tex block with the following inside:
\input{./pygmentized_source_file.tex}
to do this you must first run the command above using the -O full flag. Then copy and paste all the commands (e.g. \newcommand\PYaL[1]{\textcolor[rgb]{0.00,0.00,1.00}{#1}}) from the the resultant tex file into the Latex preamble section (Documents -> Settings -> Latex Preamble) and then re-run the the pygmentize command without the -O flag.
Now that lyx knows what all those new commands mean you should be able to merrily hit the pdflatex button and all should work just fine.
godfrey | 19-Jan-10 at 4:37 pm | Permalink
I installed MiTeX and I have neve used TeX or LaTeX before. How do I get it to run in Windows 7? Specifically, after I write a test program, what cooand do I give to execute the file,and where can I find that command on the window or otherwis. Thanks