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}
63 % This package implements a command, |\afterpage|, that causes the
64 % commands specified in its argument to be expanded after the current
65 % page is output.\footnote{This is really a pre-release, to see whether
66 % people like the idea of a command like this. This implementation is
67 % \emph{not} particularly robust. This implementation does not work in
68 % two column mode, and can get `confused' by \LaTeX's floating
72 % \item Sometimes \LaTeX's float positioning mechanism gets overloaded,
73 % and all floating |figure|s and |table|s drift to the end of the
74 % document. One may flush out all the unprocessed floats by issuing a
75 % |\clearpage| command, but this has the effect of making the current
76 % page end prematurely. Now you can issue |\afterpage{\clearpage}| and
77 % the current page will be filled up with text as usual, but then a
78 % |\clearpage| command will flush out all the floats before the next
80 % \item An earlier mechanism to help with float placement was the
81 % optional argument |[H]| (meaning {\bf HERE!}) which was originally
82 % added to the standard floating environments by |here.sty|, and is now
83 % provided by |float.sty|. However some |[H]| users have commented that
84 % they did not really mean `Here!' They actually wanted `Somewhere
85 % close'. This can now be achieved by\\
86 % |\afterpage{\clearpage\begin{figure}[H] |\ldots
88 % This ensures that the figure is at the top of the next page. (The
89 % |\clearpage| stops any other figures drifting past the |[H]| figure.)
90 % \item Floating longtables. |longtable.sty| provides the |longtable|
91 % environment, a multi-page version of |tabular|. Many |longtable| users
92 % have told me that it is difficult to set the text surrounding the long
93 % table, and that they wanted a `floating' version. As, presumably,
94 % |longtable|s are long, they are probably too large to hold in memory,
95 % and float in the way that the |table| environment is floated, however
96 % if the table is in a separate file, say |ltfile.tex|, you can now
98 % |\afterpage{\clearpage\input{ltfile}}|\\
99 % |\afterpage{\clearpage\input{ltfile}\clearpage}|.\\
100 % The first form lets text appear on the same page as the end of the
101 % longtable, the second ensures that the surrounding text starts again
111 % \begin{macro}{\afterpage}
112 % The token register used to save the old output routine.
115 \global\AP@output\expandafter{\the\output}
118 % A box register used to save any part of the next page which has
119 % already been processed.
124 % A box register used to save any footnote texts that are `tied' to
125 % the text that gets saved in |\AP@partial|.
130 % The following macro attempts to get safely into vertical mode, and
131 % then invokes a special output routine to grab the current page into
136 % Now begins a test to see what state we are in. |\AP@noindent| will
137 % be defined so as to return to this state (well, almost!) after
138 % afterpage has finished.
142 % Vertical mode. This is the simplest case, do nothing.
144 \let\AP@noindent\empty
148 % `Back out' into vertical mode, removing the indentation box as we go.
149 % If in fact there was no indentation box, the output routine was
150 % invoked by |\noindent| (what bad luck!) so we have to remember to
151 % re-insert the |\noindent| before the paragraph is seen again.
152 % |\everypar| tokens have already been inserted, so don't insert them
157 {{\everypar{}\ifvoid\z@\noindent\else\indent\fi}}%
161 % The remaining (even worse) possibility that the output routine
162 % was triggered by the start of displaymath within a paragraph.
164 % Come out of displaymath with |$$|, then adjust the spacing (getting
165 % into vmode at the same time). |\AP@noindent| will restart display math
166 % later. |\everydisplay| tokens have already been inserted (they apply
167 % to the math list that will be started by |\AP@noindent|, even though
168 % they were triggered by the display math that was closed by the lines
169 % below!). Save the values |\prevgraf| and |\predisplaysize| for use in
170 % the re-started math list.
172 \abovedisplayshortskip\z@\abovedisplayskip\z@
173 \belowdisplayshortskip\z@\belowdisplayskip\z@
175 \predisplaysize\the\predisplaysize
176 \prevgraf\the\prevgraf\relax}%
177 $$\vskip-\baselineskip\vskip-\parskip
180 % Do not insert |\everydisplay| tokens again.
182 \toks@{\the\everydisplay}\everydisplay{}%
184 % Start displaymath mode with no spurious paragraph line above it.
185 % Restore |\prevgraf| and |\predisplaysize|. Use |\aftergroup| to
186 % restore the correct setting for |\everydisplay| after this display
189 {\everypar{}\noindent}$$\AP@disp\aftergroup\noexpand\AP@ed}%
192 % Now switch the output routine and remove everything from the current
193 % page into the box |\AP@partial|.
196 \nointerlineskip\null
198 \global\setbox\AP@partial\vbox{%
200 \global\setbox\@ne\lastbox}%
202 % If the text that is saved in |\AP@partial| had footnotes, we'd
203 % better grab them as well otherwise they may come out on a page
204 % with the `afterpage' text, before the page that has the
205 % footnote mark! (Added at v1.08.)
207 \global\setbox\AP@footins\box\footins}%
209 % Having defined the output routine, trigger it\ldots
215 % |\AP@| stores all the commands that must be executed after the page
221 % Restore the |\everydisplay| register. |\ignorespaces| prevents a space
222 % or newline after |$$| creating rogue a indentation or paragraph.
224 \def\AP@ed{\everydisplay\expandafter{\the\toks@}\ignorespaces}
227 % Remove the current vertical list, insert the commands |\AP@|
228 % at the top of the page, and then re-insert the saved text.
232 \global\expandafter\let\expandafter\AP@\expandafter\relax\AP@
235 % The text originally at the top of this page is now stored in the box
236 % |\AP@partial|, including |\topskip| glue. Now we want to unbox
237 % |\AP@partial|, placing the baseline of the first row |\baselineskip|
238 % below the baseline of the last line coming from the afterpage text.
239 % If we assumed nothing has too much height or depth (and |\topskip| is
240 % rigid), it would be fairly trivial to position the contents of
241 % |\AP@partial| so that the baseline of the first row was
242 % |\baselineskip| below the last row just added.
244 % In this version, I thought it might be fun to try to exactly achieve
245 % the |\baselineskip|--or--|\lineskip| calculation that \TeX\ normally
246 % does internally. The call to |\addboxcontents| does the right thing
249 \addboxcontents\AP@partial
251 % Now re-insert any footnote text. This may not be quite the right
252 % place, as the text that has just been unboxed may break over a page
253 % in its new position. Also it may not be the right number if the text
254 % from |\afterpage| itself contains footnotes. Too bad!
256 \ifvoid\AP@footins\else
257 \insert\footins{\unvbox\AP@footins}\fi
259 % Now repair things if we started off in horizontal mode.
264 % If |\AP@| is not |\relax| then the current page already has some
265 % `afterpage' commands, so just add the new commands to the end of the
266 % list. Otherwise save the commands in |\AP@|. (within a local group),
267 % and switch the output routine. (The new output routine just calls the
268 % old one if it is invoked by a \LaTeX{} float.
270 \long\def\afterpage#1{%
275 \ifnum\outputpenalty>-\@Mi
276 \global\output\expandafter{\the\AP@output}%
280 \expandafter\gdef\expandafter\AP@\expandafter{\AP@{#1\par}}%
284 % If we have got to the end of the document or clearpage
285 % just put the stuff out without any trickery.
287 \let\AP@clearpage\clearpage
292 \global\output\expandafter{\the\AP@output}%
295 % At this point (since v1.08) Need to clear |\AP@| \emph{before}
296 % using its expansion, as otherwise hit an infinite loop. Sigh.
298 \global\expandafter\let\expandafter\AP@\expandafter\relax
299 \expandafter\expandafter\AP@
301 \let\AP@enddocument\enddocument
304 \global\output\expandafter{\the\AP@output}%
306 \global\expandafter\let\expandafter\AP@\expandafter\relax
307 \expandafter\expandafter\AP@
313 % \begin{macro}{\addboxcontents}
314 % Given a vbox |#1|, add to the current vertical list such that the end
315 % result is equivalent to the list that \TeX\ would have built had the
316 % contents of |#1| (apart from any initial glue) been added individually
317 % to the current list.
319 % So essentially, the problem is that of unboxing |#1|, but replacing
320 % the glue at the top of |#1| with (something equivalent to) the
321 % |\baselineskip| or |\lineskip| glue that \TeX\ would normally have
322 % placed before the first box in |#1|. Also |\prevdepth| must be set at
325 \def\addboxcontents#1{{%
327 % Perhaps I shouldn't use grouping here, as I probably don't really want
328 % to save |#1|. If it is removed, |\splittopskip| and |\splitmaxdepth|
329 % would need to be restored by hand.
331 % First replace any glue at the top by |\vskip 0pt|.
334 \splitmaxdepth\maxdimen
335 \setbox#1\vbox{\break\unvbox#1}%
336 \setbox\z@\vsplit#1to\z@
338 % Put the breakpoint back.
340 \setbox#1\vbox{\break\unvbox#1}%
342 % Set |\skip@| to be height of |#1| (without top glue)
346 % Now make the first baseline of the first row be |\vsize| from the top.
347 % (This assumes that the first row has height less than |\vsize|.)
350 \setbox\z@\vsplit#1to\z@
352 % Subtract the new height of |#1| from |\skip@|, and add back on
353 % |\splittopskip|, so |\skip@| is now the height of the first row of
354 % |#1| This may still be 0pt if (eg) a mark or whatsit is between the
355 % top glue and the first box. Save (this height${}-{}$|\splittopskip|)
358 \advance\skip@-\ht#1%
360 \advance\skip@\splittopskip
362 % Now fake \TeX's |\baselineskip| calculation.
364 \advance\skip@\prevdepth
365 \advance\skip@-\baselineskip
366 \advance\skip\tw@\ifdim-\skip@<\lineskiplimit\lineskip\else-\skip@\fi
368 % Finally add the glue.
372 % Now unbox the box, setting |\prevdepth| by hand, as |\unvbox| (unlike
373 % |\box|) does not automatically set it.
375 \global\dimen@i\dp#1%