Adding a new environment for code listings in LaTeX

Documentation of code in LaTeX is easy: Just use the package listings. To be able to refer to those code snippets, you can define a new environment as follows:

The name of the environment is {listing} and the name used for the caption is [Listing]. It was not necessary to add the name as the default is to take the name of the environment with the first character capitalized. However, I need the last setting to define the title of the list of listings (the default value here is “List of listings”).

This results, with some options for the listing itself, to this:

With “\listoflistings” after the table of contents entry (\tableofcontents), you get a nice listing of the code snippets with the title as defined before above.

If you are wondering how to get the code listing in this format, here are the settings for the listing:

A last remark: the document used as an example is based on KOMA.


Name *

Email *