Graphics, Figures & Tableslstlisting & caption & pagebreak

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
mainbrain
Posts: 2
Joined: Sat Jan 09, 2010 4:19 am

lstlisting & caption & pagebreak

Post by mainbrain »

The caption on a listing breaks on the next page sometimes. Is there a way to glue listing and caption together?

Here is an example listing:

Code: Select all

\begin{lstlisting}[caption={Manipulation und Substitution durch bufferorientierte Implementierung.}]{}
 int main() 
 {
        printf("hello, world");
        return 0;
 }
\end{lstlisting}
The following initialization has been patched together from various forums:

Code: Select all

 \usepackage{listings}
 \usepackage{courier}
 \lstset{
         basicstyle=\tiny, % Standardschrift
         numberstyle=\tiny,          % Stil der Zeilennummern
         numbersep=5pt,              % Abstand der Nummern zum Text
         tabsize=2,                  % Groesse von Tabs
         extendedchars=true,         %
         breaklines=true,            % Zeilen werden Umgebrochen
         keywordstyle=\color{red},
         frame=b,         
         stringstyle=\color{white}\ttfamily, % Farbe der String
         showspaces=false,           % Leerzeichen anzeigen ?
         showtabs=false,             % Tabs anzeigen ?
         captionpos=b,
         xleftmargin=17pt,
         framexleftmargin=17pt,
         framexrightmargin=5pt,
         framexbottommargin=4pt,
         backgroundcolor=\color{lightgray},
         showstringspaces=false      % Leerzeichen in Strings anzeigen ?        
 }
 \lstloadlanguages{% Check Dokumentation for further languages ...
         C++
 }

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}
Attachments
listingproblem.png
listingproblem.png (13.18 KiB) Viewed 4783 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

mainbrain
Posts: 2
Joined: Sat Jan 09, 2010 4:19 am

Re: lstlisting & caption & pagebreak

Post by mainbrain »

I tried removing the caption package related lines. Also I tried to play with all caption related options but to no avail :(

Another forum said to use the float option but that also didn't work.

Hopefully this is the correct forum. A caption under a listing should be similar to that under an image or table I think.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: lstlisting & caption & pagebreak

Post by gmedina »

Please provide complete, compilable code (not just snippets) allowing us to quickly reproduce the undesired behaviour and provide effective help.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply