Using the floats and listings packages, I have defined a float for code listings and a shortcut for using it as follows:




Now, if a code file is too long, it will just continue down outside the page. Instead, I would like to automatically split the float at the end of the page, and continue on the next page.

Is there any way to accomplish this without sacrificing the float structure? If not, is there another way to get numbering etc as easily as with a float?


2 Answers 2


I spent some time trying to do something similar the other day. It seems that there is no way to page break floats in LaTeX. For example this discussion which as far as I understand it basically says that the philosophy about floats is that they are placed somewhere where they will fit and if they don't fit they will simple keep on floating further down the text. Thus if a float is higher than a page height they will just keep on floating. I so wish there was a way though...

As for doing numbering the way floats do I suppose you can wrap your own counter using \newcounter, \addtocounter{counter}{value} and \value{counter}.

  • But how do I set the numbering to be within sections? Do I have to redefine the \section{} command to reset the counters, or is there some other, simpler way? Commented Nov 2, 2010 at 21:12
  • This is what I ended up doing. For anyone else coming to look at this, \refstepcounter from the hyperref package is really useful for the counters. Commented Nov 12, 2010 at 20:04

I don't know about automatic pagebreaking, but the listings package has the capability to break a listing up in several pieces, continuing the line numbering etc. It also works for floats, but what I don't know is how you can suppress the numbering. In this example each float gets its own number.





\begin{lstlisting}[float,name=Test,numbers=left,caption={our listing}]
for i:=maxint to 0 do
{ do nothing }

And we continue the listing. 

\begin{lstlisting}[float,name=Test,numbers=right,caption={our listing continued}]
Write('Case insensitive ');
WritE('Pascal keywords.');


You must log in to answer this question.

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