Is it possible to define a previouspage
environment, so that whatever is inside it is output on the previous page?
Motivation: In a two-column document, the only way to get a full-width float to be placed on a given page is to put the float on the previous page. At best this is awkward, because it separates the source code of the float from where it logically should be in the tex code. Also, I have an application where my tex file is machine generated, so that doing this is impossible — the program generating the tex code isn't capable of looking at the pdf output and figuring out what landed on what page.
Possible implementation: I was thinking that this might be possible to implement by the following method. Use everyshi
to write some kind of hook code on every page. On page 17, the code might say something like \input{page17}
. When the previouspage
environment is invoked on page 18, it generates a label, and after the document is compiled for the first time, the aux file will say that this label lies on page 18. When latex is run the second time, we read the aux file, we determine that the previouspage environment landed on p. 18, and therefore we write the stuff inside the previouspage environment to page17.tex. On the third compilation, this code is read back in from page17.tex.
A complication in this implementation is that we are hoping the pagination converges to what we want. To get it close to the final pagination on the initial pass, we need to make sure that the floats actually do show up in the document, although probably one page too late. There is no guarantee that the whole process will converge to the desired result after three iterations, or indeed after any number of iterations.
My tex fu is probably insufficient to do anything as fancy as implementing all of the above in pure latex, so if I were to do this myself, I would probably write an external script in some other programming language that would do some of the work. Or maybe something like this already exists…?
Related:
Best Answer
EDIT -- After posting the code below, I worked on it some more and got rid of some of the ugliness. Rather than continuing to update this answer, I made a project on github: https://github.com/bcrowell/timetravel
Below is the code for a proof-of-concept implementation of this idea.
The good news:
It accomplishes what I wanted to do for this application. In the sample below, we have a two-column document. A floating full-page-width table occurs in the source code at page 2, and is typeset at the top of page 2.
It should normally converge to a definite result after compiling the document three times. Compiling a fourth or subsequent times should not cause floats to move to different pages.
The bad news:
It's implemented as a separate ruby script that preprocesses the tex source code.
It won't work for the very first page of the document.
The float is inserted at the beginning of the first paragraph that lands on the desired page. To accomplish this, I had to use
everyhook
to place a hook at the beginning of every paragraph. This would cause an error if the first paragraph on the target page wasn't in outer paragraph mode. To work around this, any material that isn't normal paragraphs has to have\prevpagedisable
above it and\prevpageenable
below it.Bad-news item #3 is quite ugly, and that's the most important reason that I would consider this no more than a proof of concept. A usenet post by Donald Aseneau suggests that there is no reliable way for latex code to detect whether it's in outer paragraph mode. The original idea I had when I wrote the question was to get the necessary hook using
everyshi
oreso-pic
, but that doesn't work, because the material typeset by those packages is not in outer paragraph mode.Sample LaTeX file:
Style file:
Ruby code:
Makefile: