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.

2 Responses to “Syntax Highlighted Code in LaTeX Beamer”

  1. 1 florian April 1, 2009 at 9:56 pm

    Well, I am using another approach: lstlistings:

    1 \documentclass{beamer}
    2 \usepackage{color}
    3 \usepackage{german}
    4 \usetheme{Warsaw}
    5 \usepackage{listings}
    6 \setbeamertemplate{headline}{}
    7 \setbeamertemplate{navigation symbols}{}
    8 \usebackgroundtemplate{\includegraphics[width=\paperwidth]{img/background.jpeg}}
    9 \usepackage[german]{babel}
    10 \usepackage[utf8]{inputenc}
    11 \definecolor{mygreen}{rgb}{0,0.4,0}
    12 \definecolor{myid}{rgb}{0.1,0.1,0.1}
    13 \lstdefinestyle{Java}{language=java,
    14 basicstyle=\small,%\ttfamily,
    15 numbers=left,stepnumber=1,numberstyle=\small\ttfamily,
    16 numbersep=5pt,frame=tlbr,extendedchars=true,
    17 commentstyle=\color{mygreen}\ttfamily,
    18 %% stringstyle=\color{red}\ttfamily,
    19 stringstyle=\color{magenta},
    20 keywordstyle=\color{violet}\bfseries,
    21 ndkeywordstyle=\color{yellow}\bfseries,
    22 identifierstyle=\color{myid},
    23 % sensitive=false,
    24 basicstyle=\scriptsize,
    25 }

    42 \begin{document}
    44 \lstset{language=Java,style=Java}

    43 \begin{frame}[fragile]
    44 \nameslide{An Objekt}
    45 \frametitle{An Objekt}
    46 \begin{lstlisting}
    47 class Human{
    49 public String name;
    50 public int age;
    52 public Human(String name, int age){
    53 = name;
    54 this.age = age;
    55 }
    57 public void describe(){
    58 System.out.println(“Name: “;
    59 System.out.println(“Alter: “+this.age);
    60 }
    62 }
    63 \end{lstlisting}
    64 \end{frame}

  2. 2 raul March 27, 2011 at 9:13 am

    I’ve got following output. What can I to do?:

    \PY{k}{print} \PY{l+s}{“}\PY{l+s}{Hello World!}\PY{l+s}{“}

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: