Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

\usepackage{floats}
\usepackage{listings}

\newfloat{program}{tbphH}{lop}[section]
\floatname{program}{Program}

\newcommand{\codefrom}[2][Matlab]
{
\begin{program}[p]
    \lstinputlisting[language=#1]{#2}
    \caption{#2}
    \label{#2}
\end{program}
}

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  
+1 since most of us will need this at some point –  dank Oct 27 '10 at 9:42
add comment

2 Answers

up vote 8 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
add comment

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.

\documentclass{article}

\usepackage{listings}

\begin{document}

\lstset{language=Pascal}

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

And we continue the listing. 

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

\end{document}
share|improve this answer
add comment

Your Answer

 
discard

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.