Displaying Code in LaTeX

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.

Code in LaTeX using verbatim
\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:

Code in LaTeX using 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}
Code in LaTeX using pygmentize

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.

LaTeX
Programmer

Comments (8)

Permalink

Interesting Links (08-01-23)

I think it’s supposed to be some sort of blogging shortcut but I kind of like when a blog I read posts interesting links they’ve found recently. So I thought I would start doing a few posts like that of my own. I’ll gather up links I think are especially interesting and once I get five or so dump them in to a post. Feel free to read or delete as you please.

MESSENGER Images of Mercury
The Messenger space probe passed by Mercury recently. I hadn’t realized that most of Mercury has never been seen. It’s pretty cool that we get to see images of a new world almost as quickly as the scientists working on it.
That Stupid Bigfoot on Mars
This one has been going around the internet. If you missed it, there’s a rock on Mars near one of the rovers that looks like Bigfoot. The “Bigfoot” thing is pretty silly (although Sasquatch was the first thing I thought when I saw the picture) but that post shows the really cool and huge panorama it came from.
Donald Knuth and LaTeX
I like LaTeX so I found this bit of history about Donald Knuth coming up with the software pretty interesting.
Bioluminescence and Squid Video
I just found out about all these TED talks being online. Pretty handy when you don’t have a TV. This one is about five minutes long and has a bunch of videos of squid, octopuses and things that glow in the depths.
Pulgasari: The North Korean Godzilla
This is another one resulting from not having a TV. Definitely a less than B grade monster movie but it does provide a good comparison to Cloverfield. The story of Kim Jong-Il kidnapping the director and his wife and forcing them to make the thing sounds like a better story than the movie itself (not that it’d take much). For the impatient, there’s decent monster bits around 27:30, 47:30 and 1:03:00.
Soldering Tiny Components
This is a great video tutorial on how to solder tiny electronic components. Really nicely filmed and very closeup. You can really see what’s going on and the guy sure makes it look easy.
NerdKits
A nice idea by a couple college students to sell kits for learning how to use microcontrollers. They “guarantee that you’ll get your first program written and running”. Unfortunately they don’t have a USB version yet. Sort of a homegrown alternative to EasyPic4.

That’s it for now. That was pretty quick and fun to put together so I’ll probably do some more of these in the future. I hope something on there is interesting for other people too.

Blogger

Comments (0)

Permalink

LaTeX: Document Creation Alternative

I’ve been using LaTeX a lot recently and I thought I would write a quick post since I wish I would have found out about it earlier. LaTeX is a really powerful document (pdf and others) creation program. It’s sort of like HTML and CSS for paper publishing. As a first warning, LaTeX, like HTML, is not WYSIWYG. You have to code in things like \textbf{This will be bold}. This takes some getting used to after programs like MS Word but after using LaTeX, I really can’t stand working in Word for anything longer than a page or two.

Continue Reading »

Grad Student
LaTeX
Programmer

Comments (3)

Permalink