3

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
  • declare it as a float, see documentation of listings
    – user2478
    Commented Sep 6, 2012 at 12:10

1 Answer 1

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]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .