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.
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 }
26
…
42 \begin{document}
43
44 \lstset{language=Java,style=Java}
…
43 \begin{frame}[fragile]
44 \nameslide{An Objekt}
45 \frametitle{An Objekt}
46 \begin{lstlisting}
47 class Human{
48
49 public String name;
50 public int age;
51
52 public Human(String name, int age){
53 this.name = name;
54 this.age = age;
55 }
56
57 public void describe(){
58 System.out.println(“Name: “+this.name);
59 System.out.println(“Alter: “+this.age);
60 }
61
62 }
63 \end{lstlisting}
64 \end{frame}
I’ve got following output. What can I to do?:
\begin{Verbatim}[commandchars=\\\{\}]
\PY{k}{print} \PY{l+s}{“}\PY{l+s}{Hello World!}\PY{l+s}{“}
\end{Verbatim}