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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using the tips found in here to show a code listing with a caption. Sometimes the caption is put at the end of the page and the listing on the next page. How can I keep them together?

share|improve this question
declare it as a float, see documentation of listings – Herbert Sep 6 '12 at 12:10
thanks, easier than I thought – Seagull Sep 6 '12 at 13:41

listings provides the means to create a caption using the caption={[<short/LoL caption>]<long caption>} key-value interface:

enter image description here

\usepackage{listings}% http://ctan.org/pkg/listings
\begin{lstlisting}[caption={[My listing]My awesome listing}]
for i:=maxint to 0 do
  { do nothing }

For a "captionless" caption, use the title key-value. However, these all parse the listing as-is, allowing for page-breaking where needed and causing a possible break in the (top) caption and subsequent listing. Boxing the entire contents avoids this using the float key is encouraged, since a captioned item should be allowed to move and can be adequately referenced:

\begin{lstlisting}[caption={[My listing]My awesome listing},float]
share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.