Posts Tagged 'latex'

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.

Compiling LaTeX Documents using Makefiles

I usually don’t use a clumsy IDE for creating documents with LaTeX, but rather write them with a simple text editor like VIM or SciTe. To avoid typing the same commands for compiling a PDF file over and over again, I’ using a very simple and primitive Makefile:


    rm -f *.dvi *.log *.bak *.aux *.bbl *.blg *.idx *.ps *.eps *.pdf *.toc *.out *~

    rm -f *.log *.bak *.aux *.bbl *.blg *.idx *.toc *.out *~

To use this Makefile you need to define a project name, which should be the name of your LaTeX file (without the .tex suffix) containing the \begin{document} and end{document} tags. Just add this name to the first line of the make script. If you don’t use BibTeX, you’ll need to uncomment the line $(BIBTEX) $(PROJECT).
Now run the “make” command to create a PDF file and “make clean” to remove the temporary files created by pdflatex.