# Picture breaking listing

When putting long code using lstlisting on a page it breaks, which is fine. When placing a picture in a floating figure environment, it gets between the two parts of the listing, which obviously is not as it is supposed to be. I’m sorry for not being able to explain my problem better; therefore, here is a picture:

A MWE with a different part of the document, though with the same result:

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}

\usepackage{graphicx}
\usepackage{listings}

\begin{document}
\section*{B.3}
\begin{lstlisting}[language=Java, caption=B.3a)]
float s = 1.8f;
int tx = 20;
int ty = 50;
int r = 50;

public void setup() {
size(500, 500);
background(255, 255, 0); // aus draw zu setup

} // setup

public void draw() {
scale(s);
translate(tx, ty);

stroke(0, 0, 255);
strokeWeight(4);
fill(0, 255, 0);
triangle(5, 20, 55, 3, 105, 20);
fill(255, 0, 0);
rect(25, 20, 60, 60);

} // draw
\end{lstlisting}

\begin{figure}[htb]
\caption{B.3a)}
\includegraphics[scale=0.7]{Screenshots/B3a.PNG}
\end{figure}

\begin{lstlisting}[language=Java, caption=B.3b)]
float s = 1.8f;
int tx = 20;
int ty = 50;
int r = 50;

public void setup() {
size(500,500);
background(255,255,0); //aus draw zu setup

} //setup

public void draw() {
translate(tx,ty);
scale(s);

stroke(0,0,255);
strokeWeight(4);
fill(0,255,0);
triangle(5,20,55,3,105,20);
fill(255,0,0);
rect(25,20,60,60);

} //draw
\end{lstlisting}
\end{document}


I already tried to change the parameters for the figures (using h, htbp or !h) but that didn’t help.

So my question: What do I have to change to have the listing in one block – page breaks are okay – without images interfering?

-
Have a look at tex.stackexchange.com/questions/82467/… – lockstep Nov 13 '12 at 18:15
@lockstep I tried that and put it after every listing environment but it didn’t help, unfortunately. – PattaFeuFeu Nov 13 '12 at 18:19
in addition to @lockstep's answer note you should almost always include p otherwise it is much harder for latex to place the float and its likely to go to end of document. If you also omit t of course the problem goes away as the float is prevented from being at the top of any page. – David Carlisle Nov 13 '12 at 20:13

I don't have B3a.PNG on my harddisk, but using \rule{1cm}{10cm} as a placeholder, the problem can be solved by putting a) the figure environment after the second lstlisting environment b) David Carlisle's "magic code" (\global\csname @topnum\endcsname 0) between lstlisting and figure.

\documentclass[11pt]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
% \usepackage{graphicx}
\usepackage{listings}

\begin{document}

\section*{B.3}

\begin{lstlisting}[language=Java, caption=B.3a)]
float s = 1.8f;
int tx = 20;
int ty = 50;
int r = 50;

public void setup() {
size(500, 500);
background(255, 255, 0); // aus draw zu setup

} // setup

public void draw() {
scale(s);
translate(tx, ty);

stroke(0, 0, 255);
strokeWeight(4);
fill(0, 255, 0);
triangle(5, 20, 55, 3, 105, 20);
fill(255, 0, 0);
rect(25, 20, 60, 60);

} // draw
\end{lstlisting}

\begin{lstlisting}[language=Java, caption=B.3b)]
float s = 1.8f;
int tx = 20;
int ty = 50;
int r = 50;

public void setup() {
size(500,500);
background(255,255,0); //aus draw zu setup

} //setup

public void draw() {
translate(tx,ty);
scale(s);

stroke(0,0,255);
strokeWeight(4);
fill(0,255,0);
triangle(5,20,55,3,105,20);
fill(255,0,0);
rect(25,20,60,60);

} //draw
\end{lstlisting}

\global\csname @topnum\endcsname 0

\begin{figure}[htb]
\caption{B.3a)}
\rule{1cm}{10cm}% placeholder for graphic
\end{figure}

\end{document}

-