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?
1 Answer
listings
provides the means to create a caption using the caption={[<short/LoL caption>]<long caption>}
key-value interface:
\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]