TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
+1 since most of us will need this at some point – dank Oct 27 '10 at 9:42
You might be interested in the matlab-prettifier package; see this answer. – Jubobs Apr 28 '14 at 15:24
up vote 9 down vote accepted

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}.

share|improve this answer
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? – Tomas Lycken Nov 2 '10 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. – Tomas Lycken Nov 12 '10 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.');

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.