# Thread: howto: syntax highlighted source code in latex beamer

1. ## howto: syntax highlighted source code in latex beamer

I wanted to put some python source code in to a presentation i am doing, using Latex beamer. It took me a while to figure out, so i am posting it here.

I used http://pygments.org/, which claims to take almost any language input, and has many output formatters. it can do python code into latex.

There is a bug in the ubuntu version. Some of the colour commands it defines for latex clash with common latex packages. it is fixed in 0.10, so a grabbed the debian package from

Now from the command line run
Code:
pygmentize -f latex hello.py
(replacing hello.py with the source code you want to put in you latex document).

This outputs (something like)(the forums may mangle this)
Code:
\begin{Verbatim}[commandchars=@]
\end{Verbatim}
Those 'PYaD' type things are commands for colours, but latex needs to be told what they mean. if you run
Code:
pygmentize  -O full -f latex hello.py
you get the full output for a complete document.

The use package, and newcommand lines need to be put in your latex document, between the \documentclass and \begin{document} lines. The UTF-8 bit seems not to be neccessary.

or you can create file called pygments.tex, containing
Code:
\usepackage{fancyvrb}
\usepackage{color}

\newcommand\at{@}
\newcommand\lb{[}
\newcommand\rb{]}
\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\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\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}}
and then just put
Code:
\include{pygments}
in your latex document.

And one more thing. If you are using beamer, then putting a \Verbatim section in a frame can cause trouble. so you need to make a fagile frame with:
Code:
\begin{frame}[fragile]

2. ## Re: howto: syntax highlighted source code in latex beamer

Thanks!

Hey, thanks a lot. This was a really nifty post about how to use pygmentize. I'm using it now rather contently. Cheers.

3. Spilled the Beans
Join Date
Jan 2007
Beans
12

## Re: howto: syntax highlighted source code in latex beamer

Thanks ssam, works perfectly. You've saved me a lot of time

4. First Cup of Ubuntu
Join Date
Jul 2009
Beans
3

## Re: howto: syntax highlighted source code in latex beamer

Thanks ssam, that works perfectly.

5. First Cup of Ubuntu
Join Date
Jun 2007
Location
Bogotá
Beans
11
Distro
Ubuntu Jaunty Jackalope (testing)

## Re: howto: syntax highlighted source code in latex beamer

Thanks, it take me a lot of time to get the <fragile> thing.
It works great!

6. First Cup of Ubuntu
Join Date
Mar 2011
Beans
1

## Re: howto: syntax highlighted source code in latex beamer

I use
Code:
pygmentize -f latex file.py
I get

Code:
\begin{Verbatim}[commandchars=\\\{\}]
\PY{c}{#!/usr/bin/env python}

\PY{k}{print} \PY{l+s}{"}\PY{l+s}{Hello World!}\PY{l+s}{"}
\end{Verbatim}
instead of that you say.

¿How can I fix that?

7. First Cup of Ubuntu
Join Date
Mar 2011
Beans
3

## Re: howto: syntax highlighted source code in latex beamer

nice post=D> thanks for shearing

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•