Take the 2-minute tour ×
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.

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

1 Answer 1

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

enter image description here

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

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

 
discard

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.