1

I find recently the afterpage package which brings the \afterpage{<stuff>} command which puts the <stuff> part in the next page.

But I was searching a reversed command, something like \beforepage{}. And there is no matter if I compile two times my project to get this feature. I search about it but it seems that nobody talk about it. In this Google Groups tread, Jochen Küpper says “\afterpage comes to mind, but you would need something like "\beforepage" that doesn't exist”.

So, is there really impossible to get a \beforepage{} command?

1

The following provides an \beforepage command that prints the argument of \beforepage to the top of the previous page. The idea is to save the argument sent to \beforepage in the aux file together with the info of what page number we want the content on. I chose to do this by defining a macro on the form \later@pagenumber (e.g. \csname later@1\endcsname) then if the command \csname later@1\endcsname exists, then it will be executed at the top of page 1. I have to admit that I'm not completely comfortable with \write and \protected@write, so this might be somewhat "imperfect". Wowever, it works, and as I often say:

If it seems stupid, but it works, it's probably stupid -- but it does work.

\documentclass{article}
\usepackage{everyshi}
\makeatletter

\def\beforepage#1{%
% For some (probably good) reason just writing \string\csname later@...
% would not include the space. However, the following
% worked to get that space to define the \csname inside
% the .aux file.
    \edef\later@{ later@}%
% Write the previous page number to the .aux file. This could be generalized to take
% the page number as an argument! Then you can make an \AtPage{pagenumber}!
    \write\@auxout{%
        \string\expandafter\string\gdef\string\csname\later@\the\numexpr\c@page-1\endcsname{\unexpanded{#1}}
    }%
}
% When a shipout happens, the current page number is too low (because the page number hasn't been updated yet).
% So we have to increase it by one.
\EveryShipout{\ifcsname later@\the\numexpr\c@page+1\endcsname\csname later@\the\numexpr\c@page+1\endcsname\fi}
% At the first page, no shipout has happened so we have to manually trigger that one.
\AtBeginDocument{\ifcsname later@1\endcsname\csname later@1\endcsname\fi}
\makeatother
\begin{document}
    1st page\newpage
    2nd page\beforepage{Top of 1st page\newline}\newpage
    3rd page\beforepage{Top of 2nd page\newline}\newpage
    4th page\beforepage{Top of 3rd page\newline}
\end{document}
  • Hmm… it seams only work when theire is two pages. And also in this case “Yo” is printed on all pages. – fauve Jun 25 '18 at 6:37
  • Whoops. But something along these lines should work. At work now, I’ll experiment with this later :) – Andreas Storvik Strauman Jun 25 '18 at 6:49
  • you are using \edef of \thepage in the page, and using \immediate\write neither of which is safe to do if you want to get the current page number. Use \protected@write which is specifically designed to do a non-immediate write, preserving the page number. – David Carlisle Jun 25 '18 at 6:52
  • You are probably right, since you are after all @DavidCarlisle himself. However, the aux file does not seem to have a problem remembering the page numbers. – Andreas Storvik Strauman Jun 25 '18 at 7:17
  • @fauve it is not on "all pages". Only an extra time on the first page, due to latest@1 instead of latest@0. See edited :) – Andreas Storvik Strauman Jun 25 '18 at 7:22
4

\afterpage is used far more than it deserves but \beforepage can not be implemented without changing the normal model of tex processing.

Tex (mostly because of its 1970s design) never holds the document in memory, it just holds enough to decide the current page break but once a page is shipped out (and you see [1] [2].... in the log) then it is really cleared from TeX memory and can not be accessed at all, so you can not affect earlier pages on this run.

It is of course possible to affect earlier pages on a following run just as a \section{...} command added to a document will cause an entry to be written to the toc file which will be input at the start of the next run and affect the table of contents.

So any implementation would need to be a multi-run system using external files, and like table of contents specific to a particular set of features.

Given memory available now is rather more than was available when latex was designed in theory you could typeset multiple pages, say an entire chapter into a single vertical list and then manage all the page breaking and float placement "by hand" but that would mean rewriting a large part of latex.

  • The OP does say multiple runs are OK, so presumably something along those lines would work. Is your main point that such a solution cannot provide a \emph{general} solution? – cfr Jun 24 '18 at 23:31
  • @cfr well yes the table of contents example, you know what you are saving and you use a specific command to recall the information from a file. A vague "before page" specification wouldn't let me even guess what to save or how to bring it back. – David Carlisle Jun 24 '18 at 23:34
  • I think you could clarify this a bit. Initially, I just thought you hadn't really read the question :(. – cfr Jun 24 '18 at 23:37
  • @cfr perhaps, basically the answer is already a long version of "no, it isn't possible" that is you can do several things but none of them are at all similar to \afterpage – David Carlisle Jun 24 '18 at 23:49
  • 1
    @fauve if you edited the question to clarify what "it" is, then perhaps I could change my answer but current;y I do not see what I could change it to? Clearly something is possible with TOC as \tableofcontents uses it to make a table of contents, but that is unrelated to page breaking and apart from the fact that both generate forward references, it does not seem that related to your question. – David Carlisle Jun 25 '18 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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