LaTeX Source Code Formatting Using Listings Package

The LaTeX listings package is possibly the most widely used source code formatting package. I am a LaTeX newbie and tend to evaluate potential packages primarily based of their popularity –Google plays a vital role here. The listings package was somewhat an exception since I resorted to using it not only because it is popular, but also because it fits in perfectly within my typesetting workflow.

My approach to writing, like most things I do in life, is centered on decomposing individual chapters into smaller manageable compoenents. The figure below illustrates the level of fragmentation I typically follow…

chapter03/
├── 3-pilot-study.tex
├── 3-1-pilot-objective.tex
├── 3-2-pilot-system.tex
├── 3-3-evaluation.tex
├── 3-4-conclusion.tex
├── code
│      ├── container-object.metadata
│      ├── digital-content.metadata
│      └── virtual-object.metadata
├── figures
│      ├── pilot-implementation-screenshot.eps
└── tables
│      └── pilot-collection-profile.tex

The listings package fits into this workflow because it is possible to use external files containing code snippets that would otherwise have to be included into a particular section.

LaTeX Coding Listing using Listings Package
LaTeX Coding Listing using Listings Package

For instance, to get the output shown in the figure above, all that would have to be done is to specify the package name in the preamble, and then use the lstinputlisting command, as shown below.

\documentclass[12pt, lot, lof, singlespace]{phiri-thesis}<br />:<br />\usepackage{listings}<br />:<br />\begin{document}<br />:<br />:<br />\lstinputlisting[float,frame=lines,caption=A Digital Content Metadata File,<br />label=lst:pilot-study:pilot-system:content, language=XML]{<br />chapter03/code/digital-content.metadata}<br /><br />\lstinputlisting[float,frame=lines, caption=A Virtual Object Metadata File,<br />label=lst:pilot-study:pilot-system:virtual,language=XML]{<br />chapter03/code/virtual-object.metadata<br />}<br />:<br />:<br />\end{document}

Bibliography

[1] http://www.ctan.org/tex-archive/macros/latex/contrib/listings