1 % \iffalse meta-comment
5 % The LaTeX3 Project and any individual authors listed elsewhere
8 % This file is part of the Standard LaTeX `Tools Bundle'.
9 % -------------------------------------------------------
11 % It may be distributed and/or modified under the
12 % conditions of the LaTeX Project Public License, either version 1.3c
13 % of this license or (at your option) any later version.
14 % The latest version of this license is in
15 % http://www.latex-project.org/lppl.txt
16 % and version 1.3c or later is part of all distributions of LaTeX
17 % version 2005/12/01 or later.
19 % The list of all files belonging to the LaTeX `Tools Bundle' is
20 % given in the file `manifest.txt'.
24 %% File: afterpage.dtx Copyright (C) 1994 1994 1995 David Carlisle
26 %<package>\NeedsTeXFormat{LaTeX2e}
27 %<package>\ProvidesPackage{afterpage}
28 %<package> [2014/10/28 v1.08 After-Page Package (DPC)]
31 \documentclass{ltxdoc}
32 \usepackage{afterpage}
33 \GetFileInfo{afterpage.sty}
35 \title{The \textsf{afterpage} package\thanks{This file
36 has version number \fileversion, last
38 \author{David Carlisle}
40 \MaintainedByLaTeXTeam{tools}
42 \DocInput{afterpage.dtx}
47 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 % \changes{v1.00}{1993/01/25}{Initial version}
50 % \changes{v1.05}{1993/07/14}{First public version}
51 % \changes{v1.06}{1994/02/01}{Update for LaTeX2e}
52 % \changes{v1.07}{1994/05/23}{Documentation Changes}
53 % \changes{v1.08}{1995/10/27}
54 % {Use \cs{par} not \cs{endgraf}, for tools/1579}
55 % \changes{v1.08}{1995/10/27}
56 % {Try and do something sensible with nested \cs{clearpage},
58 % \changes{v1.08}{1995/10/27}
59 % {Try and do something sensible with footnotes, for tools/1884}
62 % This package implements a command, |\afterpage|, that causes the
63 % commands specified in its argument to be expanded after the current
64 % page is output.\footnote{This is really a pre-release, to see whether
65 % people like the idea of a command like this. This implementation is
66 % \emph{not} particularly robust. This implementation does not work in
67 % two column mode, and can get `confused' by \LaTeX's floating
71 % \item Sometimes \LaTeX's float positioning mechanism gets overloaded,
72 % and all floating |figure|s and |table|s drift to the end of the
73 % document. One may flush out all the unprocessed floats by issuing a
74 % |\clearpage| command, but this has the effect of making the current
75 % page end prematurely. Now you can issue |\afterpage{\clearpage}| and
76 % the current page will be filled up with text as usual, but then a
77 % |\clearpage| command will flush out all the floats before the next
79 % \item An earlier mechanism to help with float placement was the
80 % optional argument |[H]| (meaning {\bf HERE!}) which was originally
81 % added to the standard floating environments by |here.sty|, and is now
82 % provided by |float.sty|. However some |[H]| users have commented that
83 % they did not really mean `Here!' They actually wanted `Somewhere
84 % close'. This can now be achieved by\\
85 % |\afterpage{\clearpage\begin{figure}[H] |\ldots
87 % This ensures that the figure is at the top of the next page. (The
88 % |\clearpage| stops any other figures drifting past the |[H]| figure.)
89 % \item Floating longtables. |longtable.sty| provides the |longtable|
90 % environment, a multi-page version of |tabular|. Many |longtable| users
91 % have told me that it is difficult to set the text surrounding the long
92 % table, and that they wanted a `floating' version. As, presumably,
93 % |longtable|s are long, they are probably too large to hold in memory,
94 % and float in the way that the |table| environment is floated, however
95 % if the table is in a separate file, say |ltfile.tex|, you can now
97 % |\afterpage{\clearpage\input{ltfile}}|\\
98 % |\afterpage{\clearpage\input{ltfile}\clearpage}|.\\
99 % The first form lets text appear on the same page as the end of the
100 % longtable, the second ensures that the surrounding text starts again
110 % \begin{macro}{\afterpage}
111 % The token register used to save the old output routine.
114 \global\AP@output\expandafter{\the\output}
117 % A box register used to save any part of the next page which has
118 % already been processed.
123 % A box register used to save any footnote texts that are `tied' to
124 % the text that gets saved in |\AP@partial|.
129 % The following macro attempts to get safely into vertical mode, and
130 % then invokes a special output routine to grab the current page into
135 % Now begins a test to see what state we are in. |\AP@noindent| will
136 % be defined so as to return to this state (well, almost!) after
137 % afterpage has finished.
141 % Vertical mode. This is the simplest case, do nothing.
143 \let\AP@noindent\empty
147 % `Back out' into vertical mode, removing the indentation box as we go.
148 % If in fact there was no indentation box, the output routine was
149 % invoked by |\noindent| (what bad luck!) so we have to remember to
150 % re-insert the |\noindent| before the paragraph is seen again.
151 % |\everypar| tokens have already been inserted, so don't insert them
156 {{\everypar{}\ifvoid\z@\noindent\else\indent\fi}}%
160 % The remaining (even worse) possibility that the output routine
161 % was triggered by the start of displaymath within a paragraph.
163 % Come out of displaymath with |$$|, then adjust the spacing (getting
164 % into vmode at the same time). |\AP@noindent| will restart display math
165 % later. |\everydisplay| tokens have already been inserted (they apply
166 % to the math list that will be started by |\AP@noindent|, even though
167 % they were triggered by the display math that was closed by the lines
168 % below!). Save the values |\prevgraf| and |\predisplaysize| for use in
169 % the re-started math list.
171 \abovedisplayshortskip\z@\abovedisplayskip\z@
172 \belowdisplayshortskip\z@\belowdisplayskip\z@
174 \predisplaysize\the\predisplaysize
175 \prevgraf\the\prevgraf\relax}%
176 $$\vskip-\baselineskip\vskip-\parskip
179 % Do not insert |\everydisplay| tokens again.
181 \toks@{\the\everydisplay}\everydisplay{}%
183 % Start displaymath mode with no spurious paragraph line above it.
184 % Restore |\prevgraf| and |\predisplaysize|. Use |\aftergroup| to
185 % restore the correct setting for |\everydisplay| after this display
188 {\everypar{}\noindent}$$\AP@disp\aftergroup\noexpand\AP@ed}%
191 % Now switch the output routine and remove everything from the current
192 % page into the box |\AP@partial|.
195 \nointerlineskip\null
197 \global\setbox\AP@partial\vbox{%
199 \global\setbox\@ne\lastbox}%
201 % If the text that is saved in |\AP@partial| had footnotes, we'd
202 % better grab them as well otherwise they may come out on a page
203 % with the `afterpage' text, before the page that has the
204 % footnote mark! (Added at v1.08.)
206 \global\setbox\AP@footins\box\footins}%
208 % Having defined the output routine, trigger it\ldots
214 % |\AP@| stores all the commands that must be executed after the page
220 % Restore the |\everydisplay| register. |\ignorespaces| prevents a space
221 % or newline after |$$| creating rogue a indentation or paragraph.
223 \def\AP@ed{\everydisplay\expandafter{\the\toks@}\ignorespaces}
226 % Remove the current vertical list, insert the commands |\AP@|
227 % at the top of the page, and then re-insert the saved text.
231 \global\expandafter\let\expandafter\AP@\expandafter\relax\AP@
234 % The text originally at the top of this page is now stored in the box
235 % |\AP@partial|, including |\topskip| glue. Now we want to unbox
236 % |\AP@partial|, placing the baseline of the first row |\baselineskip|
237 % below the baseline of the last line coming from the afterpage text.
238 % If we assumed nothing has too much height or depth (and |\topskip| is
239 % rigid), it would be fairly trivial to position the contents of
240 % |\AP@partial| so that the baseline of the first row was
241 % |\baselineskip| below the last row just added.
243 % In this version, I thought it might be fun to try to exactly achieve
244 % the |\baselineskip|--or--|\lineskip| calculation that \TeX\ normally
245 % does internally. The call to |\addboxcontents| does the right thing
248 \addboxcontents\AP@partial
250 % Now re-insert any footnote text. This may not be quite the right
251 % place, as the text that has just been unboxed may break over a page
252 % in its new position. Also it may not be the right number if the text
253 % from |\afterpage| itself contains footnotes. Too bad!
255 \ifvoid\AP@footins\else
256 \insert\footins{\unvbox\AP@footins}\fi
258 % Now repair things if we started off in horizontal mode.
263 % If |\AP@| is not |\relax| then the current page already has some
264 % `afterpage' commands, so just add the new commands to the end of the
265 % list. Otherwise save the commands in |\AP@|. (within a local group),
266 % and switch the output routine. (The new output routine just calls the
267 % old one if it is invoked by a \LaTeX{} float.
269 \long\def\afterpage#1{%
274 \ifnum\outputpenalty>-\@Mi
275 \global\output\expandafter{\the\AP@output}%
279 \expandafter\gdef\expandafter\AP@\expandafter{\AP@{#1\par}}%
283 % If we have got to the end of the document or clearpage
284 % just put the stuff out without any trickery.
286 \let\AP@clearpage\clearpage
291 \global\output\expandafter{\the\AP@output}%
294 % At this point (since v1.08) Need to clear |\AP@| \emph{before}
295 % using its expansion, as otherwise hit an infinite loop. Sigh.
297 \global\expandafter\let\expandafter\AP@\expandafter\relax
298 \expandafter\expandafter\AP@
300 \let\AP@enddocument\enddocument
303 \global\output\expandafter{\the\AP@output}%
305 \global\expandafter\let\expandafter\AP@\expandafter\relax
306 \expandafter\expandafter\AP@
312 % \begin{macro}{\addboxcontents}
313 % Given a vbox |#1|, add to the current vertical list such that the end
314 % result is equivalent to the list that \TeX\ would have built had the
315 % contents of |#1| (apart from any initial glue) been added individually
316 % to the current list.
318 % So essentially, the problem is that of unboxing |#1|, but replacing
319 % the glue at the top of |#1| with (something equivalent to) the
320 % |\baselineskip| or |\lineskip| glue that \TeX\ would normally have
321 % placed before the first box in |#1|. Also |\prevdepth| must be set at
324 \def\addboxcontents#1{{%
326 % Perhaps I shouldn't use grouping here, as I probably don't really want
327 % to save |#1|. If it is removed, |\splittopskip| and |\splitmaxdepth|
328 % would need to be restored by hand.
330 % First replace any glue at the top by |\vskip 0pt|.
333 \splitmaxdepth\maxdimen
334 \setbox#1\vbox{\break\unvbox#1}%
335 \setbox\z@\vsplit#1to\z@
337 % Put the breakpoint back.
339 \setbox#1\vbox{\break\unvbox#1}%
341 % Set |\skip@| to be height of |#1| (without top glue)
345 % Now make the first baseline of the first row be |\vsize| from the top.
346 % (This assumes that the first row has height less than |\vsize|.)
349 \setbox\z@\vsplit#1to\z@
351 % Subtract the new height of |#1| from |\skip@|, and add back on
352 % |\splittopskip|, so |\skip@| is now the height of the first row of
353 % |#1| This may still be 0pt if (eg) a mark or whatsit is between the
354 % top glue and the first box. Save (this height${}-{}$|\splittopskip|)
357 \advance\skip@-\ht#1%
359 \advance\skip@\splittopskip
361 % Now fake \TeX's |\baselineskip| calculation.
363 \advance\skip@\prevdepth
364 \advance\skip@-\baselineskip
365 \advance\skip\tw@\ifdim-\skip@<\lineskiplimit\lineskip\else-\skip@\fi
367 % Finally add the glue.
371 % Now unbox the box, setting |\prevdepth| by hand, as |\unvbox| (unlike
372 % |\box|) does not automatically set it.
374 \global\dimen@i\dp#1%