# Highlighting Source Code

Highlighting source code is easy in LaTeX. There are a number of addon packages available for highlighting source code in LaTeX. We will be looking at some of them

Verbatim

This is the most basic thing one use for listing source code. It is as simple as putting source code in between

\begin{verbatim}

--------------

\end{verbatim}

We don't even need to use any package at all. Here is a sample code and output

\documentclass[]{article}

\begin{document}

This is an example of a C code with $verbatim$ \\

\begin{verbatim}

#include <stdio.h>

int main()

{

printf("Hello world!\n");

return 0;

}

\end{verbatim}

\end{document}

Listings

Listings is the next simple step for a LaTeX user. Listings comes with allmost all LaTeX distrbutions. So you can use it straight away.

\documentclass[]{article}

\usepackage{listings}

\begin{document}

This is an example of a C code with $listings$ package

\lstset{language=C}

\begin{lstlisting}

#include <stdio.h>

int main()

{

printf("Hello world!\n");

return 0;

}

\end{lstlisting}

\end{document}

Minted

Users who want more sophisticated output will go for minted package. Though using minted is easy, its installation is not that easy. Minted depends on pygment, which is a python based generic syntax highlighter. Sample usage and output is:

\documentclass[]{article}

\usepackage{minted}

\begin{document}

This is an example of a C code with $mented$ \\

\begin{minted}{c}

#include <stdio.h>

int main()

{

printf("Hello world!\n");

return 0;

}

\end{minted}

\end{document}

When minted is in use its safe to invoke LaTeX from commandline rather than from IDE since LaTeX is invoked with a special option. (--shell-escape )

# pdflatex --shell-escape test.tex

Installation

We need pygments to run the minted package for Latex. To get pygments, the best way is to use easy_install. For that go to this link to download it.

Now install it with

# sh setuptools-0.6c11-py2.6.egg

This will install easy_install

Then use easy_install to install pygments

# easy_install pygments

When I compiled document using minted, it said it needs the following sty files. Download them and put in the source folder itself

ifplatform.sty

pdftexcmds.sty

infwarerr.sty

ifluatex.sty

ltxcmds.sty

catchfile.sty

etexcmds.sty

After that another error popped up though I have pygment installed: Package minted Error: You must have `pygmentize' installed to use this package

Its because it cant find the package location effectively ( the option to which shell command is not given properly). For that change

\immediate\write18{which -s #1 && touch \jobname.aex}

in minted.sty to

\immediate\write18{which #1 && touch \jobname.aex}

Now done!!!!!!!!!!

Everything is ok. Use minted in LaTeX file and compile with the --shell-escape option. Another thing to note that all the sty files are in proper place. I usually put them in the same folder as my .tex file folder itself as an easy method.