Curious float layout from listings package



Hi all,

I've encountered an annoying layout oddity while using the listings package with floating listings, and none of my tweaking so far seems to fix it. I'd be grateful if anyone can suggest anything else to try...

In essence, I have created a new environment for floating listings, thus:

\lstdefinestyle{floatbox}{
basicstyle=\ttfamily\footnotesize,
float=ptbh,
frame=topline}

\lstnewenvironment{myfloat}[2]{\lstset{style=floatbox,label=#1,caption=#2}}{}

In one part of my document, I have (fairly close together) several short listings:


== Start of code ==================================================

I have some text in a paragraph, referring to listing~\ref{listing:first}.
\begin{myfloat}{listing:first}{My first listing}
About half
A dozen
Lines go here
\end{myfloat}
And then I have some more text, which is equally exciting, referring to listing~\ref{listing:second}.
\begin{myfloat}{listing:second}{My second listing}
Another half a dozen
Lines or so
Go here
\end{myfloat}
And then, unsurprisingly, there's some more text, filling out the remainder of a paragraph to illustrate the problem. It would be nice if all of this text stayed together.

== End of code ====================================================


Unfortunately, these render something like the following:


== Start of output ================================================

I have some text in a paragraph, referring to listing 1. And then I have some more text, which is equally exciting, referring to listing 2.


(Page break)


Listing 1: My first listing
---------------------------
About half
A dozen
Lines go here


And then, unsurprisingly, there's some more text, filling out


Listing 2: My second listing
----------------------------
Another half a dozen
Lines or so
Go here

the remainder of the paragraph to illustrate the problem. It would be nice if all of this text stayed together.

== End of output ==================================================


That is, a single line of the body text is unattractively stranded between two fairly short listings at the top of the second page, complete with spacing either side. I get a similar effect in another place in my document when the line in between the two listings contains the reference to and definition of the second listing.

The only explanation I can think of is that the first listing is being placed (t)op in each case, while the second listing is being placed (h)ere, and the latter happens to work out just below the former. However, it would be nice to avoid the unsightly single-line effect. Can anyone think of a way to do this, short of manually going through the document and replacing the predefined environment with a custom one lacking the (h)ere option every time this occurs? (The environment generally gives me good results, so I'm somewhat reluctant to remove the (h)ere option globally.)

And insights or suggestions would be much appreciated.

Thanks,
Chris
.



Relevant Pages

  • Re: display adapters listed twice
    ... listing, disable the second listing, enable both listings. ... It is showing 64 megs of video memory when it should be 32. ... This two display adapter listing might be normal. ...
    (microsoft.public.windowsxp.help_and_support)
  • My items not showing in searches twice
    ... Last evening I created a listing that immediately showed in 'My eBay' items ... and then created a second listing. ... still not showing some fours later when searched for. ...
    (uk.people.consumers.ebay)
  • Re: working storage values
    ... Paragraph numbering is really useful when you need to ... listing. ... that, as I pointed out before, make Cobol a dying language. ...
    (comp.lang.cobol)
  • Re: Compare two .dat files
    ... >> in the boxes of punched cards that was the source code deck and in the ... >> printed lineflow listings. ... > to find the previous paragraph name without picking up a comment. ... knowing or caring where it might be in a printed listing. ...
    (comp.lang.cobol)
  • Re: Laying out a roster in columns
    ... Create each entry as a single paragraph, ... "Keep Lines Together" on all except the last paragraph. ... two styles: "Roster" and "Roster Last". ... > then hard-return at the end of the listing. ...
    (microsoft.public.mac.office.word)