Properly add a List of Listings in LaTeX

Hi folks,
today I’d like to show you how to properly add a “List of Listings” using the listings package to your LaTeX document. Before we start you have to include the listings package:
\usepackage{listings}

Then you can (if you like to) change the title of the section. By default it is named “Listings”. Here I change it to “List of Listings”:
\renewcommand{\lstlistlistingname}{List of Listings}

These two commands were outside the begin{document}, so the following ones will be inside.

Now we have to add a listing
\begin{lstlisting}[caption={ANSI C Hello World},label={lst:c_helloworld}]
#include <stdio.h>

int main(void) {
printf("Hello World\n");
return 0;
}
\end{lstlisting}

And finally here is the code which properly adds the List of Listings (also to the ToC)
% List of Listings
\newpage
\phantomsection
\addcontentsline{toc}{section}{\lstlistlistingname} % Add it to the ToC
\lstlistoflistings % The list itself

Hope this helps šŸ˜‰

gr33z,
g0hl1n

Advertisements
This entry was posted in LaTeX and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s