I'm using the listings package to put a box around some source code in a document and also to provide some syntax hilighting. Is there an easy way that I could create a table of contents which just listed the code listings? I have already used the figure listing for actual figures and I would rather not mix figures and code listings. Is there perhaps a way I could build two distinct figures collections?
[Tex/LaTex] Table of code listings
listingstable of contents
Related Solutions
The framed
or mdframed
packages allow you to put text in frames. Here's a way to do what you want with mdframed
.
\documentclass{article}
\usepackage{caption}
\usepackage{listings}
\lstset{extendedchars=true,
tabsize=3,
frame=none,
showspaces=false,
boxpos=c,
float=h,
escapechar=\%
}
\usepackage[style=1]{mdframed}
\newcounter{boxctr}
\DeclareCaptionType{boxx}
\usepackage{needspace}
\newenvironment{Boxx}[2]{%
\captionsetup{type=boxx}
\begin{mdframed}[%
linewidth=1pt,
roundcorner=10pt,%
leftmargin=.05\textwidth,
rightmargin=.05\textwidth,
skipabove=.7\baselineskip]%
\refstepcounter{boxctr}%
\label{#1}%
\gdef\boxxcaption{\caption{#2}}
\needspace{2\baselineskip}
}{%
\end{mdframed}%
\boxxcaption%
}
\usepackage{lipsum}
\begin{document}
This is a reference to Box~\ref{box:one}!
\vspace{1.5cm}
\lipsum[1-4]
\begin{Boxx}{box:one}{This is a caption}
\begin{enumerate}
\item One
\item Two
\item Three
\end{enumerate}
\end{Boxx}
\lipsum[1]
\begin{Boxx}{box:two}{This is also a caption}
\begin{lstlisting}
Also works with listings
\foo is escaped!
\end{lstlisting}
\end{Boxx}
\lipsum[2]
\end{document}
Boxes take two extra arguments: a label and a caption, just like the environment defined in the question.
They use the caption
package's captioning magic. So this should make doing lists of boxes easier. The label should stick with the bottom of the box now. This might lead to some ugly whitespace, but other than making the boxes float, this is probably as good as it gets.
One way to do this is to split the listings for each line range, and place an ornament in between them. There are many options as to what to use as a separator. To illustrate, I have used an ornament from this question in between the listings and another from this question on how to insert a border at the end of the listings.
Here is the code:
\documentclass{article}
\usepackage{filecontents}
\usepackage{listings}
%------------------------ Used for ornament between listings
% Ornament from https://tex.stackexchange.com/questions/11320/end-of-paragraph-with-ornament
\usepackage{pifont,fourier-orns}% These are needed only for the ornament
\newcommand\crulefill[1][1ex]{\leavevmode\leaders\hrule depth \dimexpr-#1+0.4pt height #1\hfill\kern0pt}
\newcommand\ornline[2][1ex]{\trivlist\item\crulefill[#1]#2\crulefill[#1]\endtrivlist}
%------------------------ Used for ornament at end of listings
%https://tex.stackexchange.com/questions/30973/how-do-i-insert-a-border-below-text/30979#30979
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{xparse}% http://ctan.org/pkg/xparse
\NewDocumentCommand{\myrule}{O{1pt} O{3pt} O{black}}{%
\par\nobreak % don't break a page here
\kern\the\prevdepth % don't take into account the depth of the preceding line
\kern#2 % space before the rule
{\color{#3}\hrule height #1 width\hsize} % the rule
\kern#2 % space after the rule
\nointerlineskip % no additional space after the rule
}
%------------------------
\begin{filecontents*}{foo.java}
public int nextInt(int n) {
if (n<=0)
throw new IllegalArgumentException("n must be positive");
if ((n & -n) == n) // i.e., n is a power of 2
return (int)((n * (long)next(31)) >> 31);
int bits, val;
do {
bits = next(31);
val = bits % n;
} while(bits - val + (n-1) < 0);
return val;
}
\end{filecontents*}
\lstdefinestyle{MyListStyle} {numbers=left, language=Java}
\begin{document}
\lstinputlisting[style=MyListStyle,linerange={2-6},firstnumber=2]{foo.java}
\ornline[0.6ex]{\decoone}
\lstinputlisting[style=MyListStyle,linerange={9-11},firstnumber=9]{foo.java}
\myrule[5pt][5pt][orange]
\end{document}
Best Answer
One could use the
\lstlistoflistings
macro that is part of thelistings
package.