I have a document consisting only of floats, and I would like to order them so as to use the least number of pages. In my case, each float contains song lyrics, but they could easily be poems or code snippets or images. I don't care how they're ordered, but I would like LaTeX to automatically reorder them so that each page is as close to full as possible.
Is there a way to do this with plain LaTeX? If not, I assume I'd have to write some code which would calculate the size of each float and then implement my own ordering algorithm - any hints about how I would go about doing that?
A dummy example is below. In this example, box 2 won't fit on the first page, but box 4 will. Ideally, the result should only take two pages rather than three.
\documentclass{article}
\usepackage{float}
\floatstyle{boxed}
\newfloat{testbox}{p}{ext}
\begin{document}
\begin{testbox}
This is box one
\vspace{4in}
The end of box one%
\end{testbox}
\begin{testbox}
This is box two
\vspace{4in}
The end of box two%
\end{testbox}
\begin{testbox}
This is box three
\vspace{2in}
The end of box three%
\end{testbox}
\begin{testbox}
This is box four
\vspace{1in}
The end of box four%
\end{testbox}
\end{document}
Possible ordering algorithm (as requested by Yiannis):
Calculate the size of each box
Loop through all of the boxes:
Select the largest box
Put it on the first page which has space, starting a new page if necessary


ordering algorithm, its exceptions and rules the macros are the easy part. – Yiannis Lazarides Dec 20 '11 at 19:07