Brett Klamer

List of Listings for R Code in LaTeX

knitr officially supports the listings package, but I didn’t really care for the output it created. Here’s what I did to add a list of listings while still keeping things simple.

Add the following to the latex header

\usepackage{scrhack} % For float package and koma compatibility. Must load before "float".
\newfloat{listing}{H}{loc}[chapter] % reference

Then add

\listof{listing}{List of Listings}

Inside the document where you want the list to show up, usually after \listoftables and \listoffigures. Now you need to wrap any R code you think is important enough inside the new listing environment (don’t forget the caption and label).

x <- rnorm(10)
\caption{This is the listing Caption}