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:

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.

Advertisements

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:

PROJECT=
TEX=pdflatex
BIBTEX=bibtex
BUILDTEX=$(TEX) $(PROJECT).tex

all:
    $(BUILDTEX)
    $(BIBTEX) $(PROJECT)
    $(BUILDTEX)
    $(BUILDTEX)
clean-all:
    rm -f *.dvi *.log *.bak *.aux *.bbl *.blg *.idx *.ps *.eps *.pdf *.toc *.out *~

clean:
    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.