# What's the best way to typeset C++ codes in latex?

I'm participating in a programming contest and we are allowed to prepare a team reference document which must not exceed 25 pages. Typically we will prepare many code templates and this year I'm trying to use latex to typeset these C++ codes and I want to know what's the best way to do that.

Some requirements:

1. Since there is a page length limit I want to typeset my codes as compact as possible.
2. I want to have the C++ codes syntax highlighted.
3. If it is possible to generate a compact table of content in front, it would be best.
-
I'd recommend the listings package. And yes you should be able to obtain a TOC, by escaping to LaTeX in the code -- this should be done so that the LaTeX is treated as comments to C++. –  Peter Grill Nov 7 '12 at 19:08
You can also consider the minted package, although it requires the external python program pygmentize. I personally prefer the minted output, but listings output is easier to customize. –  guillem Nov 8 '12 at 7:15

Recently I have finished my thesis, and I added an appendix with some CINT (C/C++) code. I used listings package, adding those lines on my preamble:

\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstset{
backgroundcolor=\color{lbcolor},
tabsize=4,
%   rulecolor=,
language=[GNU]C++,
basicstyle=\scriptsize,
upquote=true,
aboveskip={1.5\baselineskip},
columns=fixed,
showstringspaces=false,
extendedchars=false,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=single,
numbers=left,
showtabs=false,
showspaces=false,
showstringspaces=false,
identifierstyle=\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
stringstyle=\color[rgb]{0.627,0.126,0.941},
numberstyle=\color[rgb]{0.205, 0.142, 0.73},
%        \lstdefinestyle{C++}{language=C++,style=numbers}’.
}
\lstset{
backgroundcolor=\color{lbcolor},
tabsize=4,
language=C++,
captionpos=b,
tabsize=3,
frame=lines,
numbers=left,
numberstyle=\tiny,
numbersep=5pt,
breaklines=true,
showstringspaces=false,
basicstyle=\footnotesize,
%  identifierstyle=\color{magenta},
keywordstyle=\color[rgb]{0,0,1},
stringstyle=\color{red}
}

The output I get looks like that:

-

Here's how I'd do it with my PythonTeX package, which uses the Python Pygments library for highlighting. The PythonTeX package provides a listings environment, using newfloat. I've created a command \codeline that allows you to insert references to specific lines of code in the list of listings as well. \codeline must only be used in comments, so that it will be processed (automatically and correctly) during code highlighting. Also, for it to work properly, you must give each listing a caption before actually entering the code (otherwise, the order in the list of listings is off).

\documentclass{article}

\setpythontexlistingenv{listing}

\newcounter{sublisting}[listing]
\newcommand{\codeline}[1]{%
{\protect\numberline{\hspace{0.5in}\thelisting.\arabic{FancyVerbLine}}\hspace{0.5in}#1}%
}

\begin{document}

\listoflistings

\begin{listing}[h!]
\caption{Hello, world}
\begin{pygments}[numbers=left]{c++}
# include <iostream>

int main()
{
std::cout << "Hello, world!\n"; //\codeline{Important code line!}
}
\end{pygments}
\end{listing}

\begin{listing}[h!]
\caption{Hello, world, again!}
\begin{pygments}[numbers=left]{c++}
# include <iostream>

int main() //\codeline{Another important line}
{
std::cout << "Hello, world!\n";
}
\end{pygments}
\end{listing}

\end{document}
-