Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. – Werner Jul 11 '12 at 20:24

migrated from stackoverflow.com Jul 11 '12 at 17:43

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.