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:
1 2 |
\usepackage{caption} \DeclareCaptionType[placement={!ht}]{listing}[Listing][Code Listings] |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
\lstset{ basicstyle=\fontfamily{pcr}\fontseries{m}\selectfont\footnotesize, commentstyle=\color{gray}\itshape, keywordstyle=\color{blue}\bfseries, stringstyle=\color[rgb]{0.5,0,0.5}\itshape, showstringspaces=false, numbers=left, numberstyle=\color[rgb{0,0.5,0.5}\fontfamily{pcr}\fontseries{m}\selectfont\tiny, numberblanklines=false, showlines=false, breaklines=true, %stepnumber=2, tabsize=6, %extendedchars=true, %float=h, frame=tb, linewidth= 0.9\textwidth, lineskip=1pt, frame=single, framesep=\fboxsep, framerule=\fboxrule, rulecolor=\color{blue}, belowskip=0pt, xleftmargin=\dimexpr\fboxsep+\fboxrule\relax, xrightmargin=\dimexpr\fboxsep+\fboxrule\relax, tabsize=2, columns=flexible, xleftmargin=.25in, backgroundcolor=\color{yellow!5} } |
A last remark: the document used as an example is based on KOMA.