<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dammit Jim! &#187; verbatim</title>
	<atom:link href="http://scott.sherrillmix.com/blog/tag/verbatim/feed/" rel="self" type="application/rss+xml" />
	<link>http://scott.sherrillmix.com/blog</link>
	<description>I'm a biologist not a...</description>
	<lastBuildDate>Tue, 12 Jan 2010 05:09:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Displaying Code in LaTeX</title>
		<link>http://scott.sherrillmix.com/blog/programmer/displaying-code-in-latex/</link>
		<comments>http://scott.sherrillmix.com/blog/programmer/displaying-code-in-latex/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 06:28:37 +0000</pubDate>
		<dc:creator>ScottS-M</dc:creator>
				<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[Programmer]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[syntax highlighting]]></category>
		<category><![CDATA[verbatim]]></category>

		<guid isPermaLink="false">http://scott.sherrillmix.com/blog/?p=437</guid>
		<description><![CDATA[gioby of Bioinfo Blog! (an interesting read by the way) left a comment asking about displaying code in LaTeX documents. I&#8217;ve sort of been cludging around using \hspace&#8217;s and \textcolor but I&#8217;ve always meant to figure out the right way to do things so this seemed like a good chance to figure out how to [...]]]></description>
			<content:encoded><![CDATA[<p>gioby of <a href='http://bioinfoblog.it/'>Bioinfo Blog!</a> (an interesting read by the way) left a comment asking about displaying code in LaTeX documents. I&#8217;ve sort of been cludging around using <code>\hspace</code>&#8217;s and <code>\textcolor</code> but I&#8217;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.</p>

<p>LaTeX tends to ignore white space. This is good when you&#8217;re writing papers but not so good when you&#8217;re trying to show code where white space is an essential part (e.g. Python). Luckily there&#8217;s a builtin <code>verbatim</code> environment in LaTeX that is equivalent to html&#8217;s <code>&lt;pre&gt;</code>. So something like the following should preserve white space.</p>
<img src="/res/images/latex_verbatim.png" alt="Code in LaTeX using verbatim" class="right"/><pre><code>\begin{verbatim}
for i in range(1, 5):
  print i
else:
  print &quot;The for loop is over&quot;
\end{verbatim}</code></pre>

<p>Unfortunately, you can&#8217;t use any normal LaTeX commands inside verbatim (since they&#8217;re displayed <em>verbatim</em>). But luckily there a handy package called <a href="http://www.ctan.org/tex-archive/macros/latex/contrib/fancyvrb/">fancyvrb</a> that fixes this (the <a href="http://www.ctan.org/tex-archive/help/Catalogue/entries/color.html">color</a> package is also useful for adding colors). For example, if you wanted to highlight &#8220;for&#8221; in the above code, you can use the <code>Verbatim</code> (note the capital V) environment from fancyvrb:</p>
<img src="/res/images/latex_fancyvrb.png" alt="Code in LaTeX using fancyvrb" class="right"/>
<pre><code>\newcommand\codeHighlight[1]{\textcolor[rgb]{1,0,0}{\textbf{#1}}}
\begin{Verbatim}[commandchars=\\\{\}]
\codeHighlight{for} i in range(1, 5):
  print i
else:
  print &quot;The for loop is over&quot;
\end{Verbatim}
</code></pre>

<img src="/res/images/latex_pygmentize.png" alt="Code in LaTeX using pygmentize" class="right"/>
<p>If you really want to get fancy, the <a href='http://pygments.org/docs/quickstart/'>Pygments package</a> in Python will output syntax highlighted latex code with a command like: <code>pygmentize -f latex -O full test.py &gt;py.tex</code> The LaTeX it outputs is a bit hard to read but it&#8217;s not too bad (it helped me figure out the fancyvrb package) and it does make nice syntax highlighted output.</p>

<p>Here&#8217;s an example LaTeX file with the three examples above and the pdf it generates if you&#8217;re curious.</p>
<ul>
<li><a href="/res/codeInLatex.tex">codeInLatex.tex</a></li>
<li><a href="/res/codeInLatex.pdf">codeInLatex.pdf</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://scott.sherrillmix.com/blog/programmer/displaying-code-in-latex/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
