I recommend to use the newer mdframed
package instead of the framed
package. However, it also doesn't seem to have such a feature, but it might be done by patching \md@putbox@first
and \md@putbox@middle
.
Here something quick and dirty:
\documentclass{article}
\usepackage{mdframed}
\usepackage{lipsum}
\usepackage{xcolor}
\makeatletter
\let\orig@md@putbox@first\md@putbox@first
\def\md@putbox@first{%
\orig@md@putbox@first
\nopagebreak
\vspace{-\baselineskip}%
\vbox to 0pt{\hbox to \linewidth{\hss(continue on next page)}\vss}%
}
\let\orig@md@putbox@middle\md@putbox@middle
\def\md@putbox@middle{%
\orig@md@putbox@middle
\nopagebreak
\vspace{-\baselineskip}%
\vbox to 0pt{\hbox to \linewidth{\hss(continue on next page)}\vss}%
}
\makeatother
\begin{document}
\lipsum[1]
\bigskip
\begin{mdframed}[backgroundcolor=gray]
\lipsum[1-30]
\end{mdframed}
\end{document}
The lstlisting
environment (whether used directly or via \lstinputlisting
) together with the float
specifier does not break across pages. The float
key boxes the entire listing into a single unit or block that allows it to float. It does not break the listing into separate floats the with move as a sequential unit. This is similar to the restriction imposed by the minipage
environment. Even though the listings
package documentation mentions breaking (and a host of breaking options), they all refer to line breaking, but not page breaking.
So, in order for the listing to break across pages, you could remove the float=<specifier>
key-value. However, this does not accommodate an automatic (continued) caption for subsequent page-broken listings. This may not be that important (or as big-a-deal) if the listing spans only two pages, since a reader would easily be able to follow the broken listing. However, a listing spanning multiple pages might not be such a good idea.
Alternatively, if you want you could manually break the listing into (at least) two blocks so that neither block is larger than a single page. For this you can use a combination of the firstline
and lastline
option:
% First part of listing
\lstinputlisting[...,lastline=<x>]{<filename>}
% Second part of listing
\lstinputlisting[...,firstline=<x+1>,firstnumber=<x+1>]{<filename>}
where <x>
is the last line of the first part of the lstlisting
, and <x+1>
the first line of the second part of the lstlisting
. If you are using line numbers in your listing, you may need to specify the line number for the second part, as this may default to starting from 1
. Hence, the addition of firstnumber=<x+1>
as part of the lstlisting
key-value sequence. You may also be required to modify the listing caption and specify that the listing continues from some previous one rather than having a number that does not match the former. If your listing has to be split across more than two pages, the same configuration as above holds, with the use of linerange=<first>-<last>,firstnumber=<first>
for the middle listings.
There is no need to place the lstinputlisting
environment in a float (like figure
), since it the package provides it's own form of floating listing (via the key-value float=<specifier>
).
Lastly, an alternative that should work but is not easy to accomplish, is to actually shorten your code so that the listing can fit on a single page.
On a side-note, wrapping the center
environment around a floating environment (like figure
) won't help you either and cause problems. In any case, the outer-most environment should be the float (table
, or figure
, or...), with the formatting environments contained within.
Best Answer
You can use
\ContinuedFloat
like this:\ContinuedFloat
takes care of numbers by itself.