What I need to be able to do is apply a header and footer when I use the listings environment. I have a few requirements:
- The header and footer should be outside the listing itself, i.e. they are not part of the listing content.
- The amount of space between the header and the listing should be the same as the amount of space between the footer and the listing.
- The amount of space between the listing and the header/footer should not be rubber, i.e. TeX should not be able adjust the header/footer closer to or farther from the listing.
- The header and footer may not be the same for every listing.
- Page breaks should not be possible between the header/footer and the listing.
- Page breaks should be possible within the listing itself.
As far as I can tell I can get most of the way there if use minipage:
\usepackage{listings}
\newcommand{\codeHeaderFooter}{}
\lstnewenvironment{code}[2][]{
\lstset{language=c++,aboveskip=12pt,belowskip=12pt,#1}
\renewcommand{\codeHeaderFooter}{#2}
\begin{minipage}[c]{\linewidth}
\codeHeaderFooter{}
} {
\end{minipage}
\begin{flushright}
\codeHeaderFooter{}
\end{flushright}
}
\begin{code}{header/footer}
std::cout << "Hello, World!" << std::endl;
\end{code}
The problem is that minipage doesn't allow page breaks, so the listing cannot span multiple pages. Other solutions I've tried (i.e. a combination of \nopagebreak[4] and \vspace, and \raggedbottom) fail to either (a) prevent page breaks between the header/footer and the listing, or (b) prevent TeX from adjusting the space between the header/footer and the listing. Floats are not an option.
Any ideas about how I can satisfy all of the requirement above?