This page covers the information how to include a Python snippets into your LaTeX presenation
Listings LaTeX package can highlight source code written in many languages including LaTeX.
A minimal Python example is:
\documentclass{article} \usepackage[procnames]{listings} \usepackage{color} \begin{document} \definecolor{keywords}{RGB}{255,0,90} \definecolor{comments}{RGB}{0,0,113} \definecolor{red}{RGB}{160,0,0} \definecolor{green}{RGB}{0,150,0} \lstset{language=Python, basicstyle=\ttfamily\small, keywordstyle=\color{keywords}, commentstyle=\color{comments}, stringstyle=\color{red}, showstringspaces=false, identifierstyle=\color{green}, procnamekeys={def,class}} \begin{lstlisting} def hello(name): print 'Hello', name if __name__=='__main__': hello('Me') \end{lstlisting} \end{document}
The source code can be also available in separate Python files and then included in the document with latex command
\lstinputlisting{example.py}
(which allows you the modify and test the code easily.
LaTeX files can be also generated automatically from Python scripts (pyreport) or from wiki (wiki2beamer)
If you need more detailed highlighting of Python syntax you can use Pygments.
Minimal example:
\documentclass{article} \usepackage{fancyvrb} \usepackage{color} %define style \newcommand\at{@} \newcommand\lb{[} \newcommand\rb{]} \newcommand\PYZlb{[} \newcommand\PYZrb{]} \newcommand\PYbg[1]{\textcolor[rgb]{0.00,0.50,0.00}{\textbf{#1}}} \newcommand\PYbf[1]{\textcolor[rgb]{0.73,0.40,0.53}{\textbf{#1}}} \newcommand\PYbe[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} \newcommand\PYbd[1]{\textcolor[rgb]{0.73,0.13,0.13}{#1}} \newcommand\PYbc[1]{\textcolor[rgb]{0.00,0.50,0.00}{\textbf{#1}}} \newcommand\PYbb[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} \newcommand\PYba[1]{\textcolor[rgb]{0.00,0.00,0.50}{\textbf{#1}}} \newcommand\PYaJ[1]{\textcolor[rgb]{0.73,0.13,0.13}{#1}} \newcommand\PYaK[1]{\textcolor[rgb]{0.00,0.00,1.00}{#1}} \newcommand\PYaH[1]{\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{#1}} \newcommand\PYaI[1]{\textcolor[rgb]{0.69,0.00,0.25}{#1}} \newcommand\PYaN[1]{\textcolor[rgb]{0.00,0.00,1.00}{\textbf{#1}}} \newcommand\PYaO[1]{\textcolor[rgb]{0.00,0.00,0.50}{\textbf{#1}}} \newcommand\PYaL[1]{\textcolor[rgb]{0.73,0.73,0.73}{#1}} \newcommand\PYaM[1]{\textcolor[rgb]{0.74,0.48,0.00}{#1}} \newcommand\PYaB[1]{\textcolor[rgb]{0.00,0.25,0.82}{#1}} \newcommand\PYaC[1]{\textcolor[rgb]{0.67,0.13,1.00}{#1}} \newcommand\PYaA[1]{\textcolor[rgb]{0.00,0.50,0.00}{#1}} \newcommand\PYaF[1]{\textcolor[rgb]{1.00,0.00,0.00}{#1}} \newcommand\PYaG[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}} \newcommand\PYaD[1]{\textcolor[rgb]{0.25,0.50,0.50}{\textit{#1}}} \newcommand\PYaE[1]{\textcolor[rgb]{0.63,0.00,0.00}{#1}} \newcommand\PYaZ[1]{\textcolor[rgb]{0.00,0.50,0.00}{\textbf{#1}}} \newcommand\PYaX[1]{\textcolor[rgb]{0.00,0.50,0.00}{#1}} \newcommand\PYaY[1]{\textcolor[rgb]{0.73,0.13,0.13}{#1}} \newcommand\PYaR[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}} \newcommand\PYaS[1]{\textcolor[rgb]{0.25,0.50,0.50}{\textit{#1}}} \newcommand\PYaP[1]{\textcolor[rgb]{0.49,0.56,0.16}{#1}} \newcommand\PYaQ[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} \newcommand\PYaV[1]{\textcolor[rgb]{0.00,0.00,1.00}{\textbf{#1}}} \newcommand\PYaW[1]{\textcolor[rgb]{0.73,0.13,0.13}{#1}} \newcommand\PYaT[1]{\textcolor[rgb]{0.50,0.00,0.50}{\textbf{#1}}} \newcommand\PYaU[1]{\textcolor[rgb]{0.82,0.25,0.23}{\textbf{#1}}} \newcommand\PYaj[1]{\textcolor[rgb]{0.00,0.50,0.00}{#1}} \newcommand\PYak[1]{\textcolor[rgb]{0.73,0.40,0.53}{#1}} \newcommand\PYah[1]{\textcolor[rgb]{0.63,0.63,0.00}{#1}} \newcommand\PYai[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}} \newcommand\PYan[1]{\textcolor[rgb]{0.67,0.13,1.00}{\textbf{#1}}} \newcommand\PYao[1]{\textcolor[rgb]{0.73,0.40,0.13}{\textbf{#1}}} \newcommand\PYal[1]{\textcolor[rgb]{0.25,0.50,0.50}{\textit{#1}}} \newcommand\PYam[1]{\textbf{#1}} \newcommand\PYab[1]{\textit{#1}} \newcommand\PYac[1]{\textcolor[rgb]{0.73,0.13,0.13}{#1}} \newcommand\PYaa[1]{\textcolor[rgb]{0.50,0.50,0.50}{#1}} \newcommand\PYaf[1]{\textcolor[rgb]{0.25,0.50,0.50}{\textit{#1}}} \newcommand\PYag[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} \newcommand\PYad[1]{\textcolor[rgb]{0.73,0.13,0.13}{#1}} \newcommand\PYae[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} \newcommand\PYaz[1]{\textcolor[rgb]{0.00,0.63,0.00}{#1}} \newcommand\PYax[1]{\textcolor[rgb]{0.60,0.60,0.60}{\textbf{#1}}} \newcommand\PYay[1]{\textcolor[rgb]{0.00,0.50,0.00}{\textbf{#1}}} \newcommand\PYar[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}} \newcommand\PYas[1]{\textcolor[rgb]{0.73,0.13,0.13}{\textit{#1}}} \newcommand\PYap[1]{\textcolor[rgb]{0.00,0.50,0.00}{#1}} \newcommand\PYaq[1]{\textcolor[rgb]{0.53,0.00,0.00}{#1}} \newcommand\PYav[1]{\textcolor[rgb]{0.00,0.50,0.00}{\textbf{#1}}} \newcommand\PYaw[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} \newcommand\PYat[1]{\textcolor[rgb]{0.10,0.09,0.49}{#1}} \newcommand\PYau[1]{\textcolor[rgb]{0.40,0.40,0.40}{#1}} \begin{document} \begin{Verbatim}[commandchars=@\[\]] %code generated with pygmentize -f latex example.py @PYaz[def] @PYaL[hello](name): @PYaz[print] @PYad[']@PYad[Hello]@PYad['], name @PYaz[if] __name__@PYbf[==]@PYad[']@PYad[__main__]@PYad[']: hello(@PYad[']@PYad[Me]@PYad[']) \end{Verbatim} \end{document}