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:

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

print "Hello World!"

Install Pygments via easy_install Pygments

Create TeX code for the code file with
pygmentize -O full -f latex > 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.

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

    @PYay[print] @PYaB["]@PYaB[Hello World!]@PYaB["]

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.