Posts Tagged 'beamer'

Syntax Highlighted Code in LaTeX Beamer

I’m currently preparing a small presentation about Python and functional programming and need to put syntax highlighted source code on my latex beamer foils. Here is a small summary how I did it:

Step1
Prepare a file code.py containing some Python code (or code from any other language):

print "Hello World!"

Step2
Install Pygments via easy_install Pygments

Step3
Create TeX code for the code file with
pygmentize -O full -f latex code.py > code.tex
This produce a complete LaTeX document code.tex with some usepackage directives and command definitions for different colours as well as a verbatim block which can be integrated in a LaTeX Beamer frame.

Step4
Put the newcommand and usepackage stuff in your beamer tex file and create a new frame to include the code snippet:

\begin{frame}[fragile]
   \begin{Verbatim}[commandchars=@\[\]]
    @PYay[print] @PYaB["]@PYaB[Hello World!]@PYaB["]
   \end{Verbatim}
\end{frame}

Step5
You need to run pygmentize only once with the -O full parameter. For every additional code snippet this parameter can be omitted to produce the latex verbatim block only.