# Minimal working example on how to use Listings in LaTeX

In case you want to show code snippets in your LaTeX document, you can use the “listings” package. The following is a (minimal) working example on how to use the listings package.
$$\documentclass{scrartcl} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} % Step 1: Load package \usepackage{listings} % Step 2: Set required options % You can set them also inline for each given lstlisting \lstset{ language=Java, numbers=left, stepnumber=1, tabsize=2, frame=single } \title{Minimal Working Example} \subtitle{Using Listings} \author{Robin} \begin{document} \maketitle \section{A Chapter} In Listing \ref{lst:example}, you find a simple “Hello World” program in Java. % Step 3: Create listing and set local options % (This can be also an external input file using % \lstinputlisting{HelloWorld.java}) \begin{lstlisting}[label={lst:example}, caption={Java Hello World Example}] public class HelloWorld { public static void main(String[] args){ System.out.println("Hello World"); } } \end{lstlisting} % Step 4: Create list of listings (optional) \lstlistoflistings \end{document}$$
Now you have to build the PDF file using the following steps (this example works in Debian jessie and the texlive suite). The filename is “listings.tex”.
[bash]
$> pdflatex listings.tex$> pdflatex listings.tex
[/bash]
The pdflatex command has to be executed twice, as in the first run, the references in the text are recognized and ordered, whereas in the second run the references are finalized. Then, you end up with a file “listings.pdf” which can be found here: Listings Example.
There are many options for setting up the listings especially for using colors, typesetting, defining new languages etc.
See http://www.ctan.org/pkg/listings for a complete list of options and examples.