The afterpage package

Similar documents
The showkeys package

32 TUGboat, Volume 36 (2015), No. 1

The showkeys package

The trace package. Frank Mittelbach 2014/10/28

The supertabular environment

Visual Debugging in TEX Part 1: The Story

Omit passages optionally under L A TEX. Uwe Lück 2005/04/28

Version v2.01, 2000/06/29

Visual Debugging in T E X

The xtheorem package

Welcome to the floatflt package!

Trees in TEX David Eppstein; February 6, 1985

The file ltxdoc.dtx for use with L A TEX 2ε. It contains the code for ltxdoc.cls

The l3galley package Galley code

Square cells: an array cooking lesson

The asciilist package

The keyval package. David Carlisle 2014/10/28

The fullminipage package

The mcaption package

The widetable package

LuaTEX says goodbye to Pascal

The lips LaTeX package Chicago Manual text ellipses (Frankenstein s lips)

The xtab package. Contents. List of Tables. 1 Introduction

The textcase package

Termcal.sty printing a class calendar

The dashrule package

The ted package. Manuel Pégourié-Gonnard v1.06 (2008/03/07) 1 Introduction 1. 2 Usage 1. 3 Implementation 3

The calc package Infix notation arithmetic in L A TEX

The cals package. 1 Introduction. 2 Usage. Oleg Parashchenko November 23, 2016

CONTEXT. Missing (For Generic Use) group: CONTEXT Support Macros. version: date: 1997 July 25. author: Hans Hagen

The xtab package. Peter Wilson herries dot press (at) earthlink dot net 2008/07/26

FiNK the L A TEX2ε File Name Keeper

Documentation for xcomment.sty

footnotehyper Abstract footnotehyper footnotehyper footnotehyper 1 License

The threadcol package

The bigfoot Bundle for Critical Editions

qstest.sty QuinScape Unit Test Package version

The crush package. Jesse A. Tov This document corresponds to crush v0.2, dated 2011/07/29.

The verbatimbox Package

The manyfoot package

% \lineskip is 1pt for all font sizes. \normallineskip 1pt \def\baselinestretch{1.37}

The cjwoutl Package. Contents. Introduction. Colin J. Wynne 1996/04/26. 1 Making an Outline Outline Labels Cross References...

A package for rotated objects in L A TEX

Indian T E X Users Group

Indian T E X Users Group

The calculation environment

252 TUGboat, Volume 39 (2018), No. 3

The totpages package

mylatexformat Use mylatexformat to make a format based on the preamble of any LaTeX file 2011/02/12 version 3.4

The graphicx package

Manuel Pégourié-Gonnard v1.2a 2012/01/23

volumes.sty: Support for Printing of only parts of a LaTeX document, with complete indices etc.

The nccfloats package

% \lineskip is 1pt for all font sizes. \normallineskip 1pt \def\baselinestretch{1.37}

L A TEX examples. a b c a b c b b a c b c c b a. This table was typeset with the following commands.

Getting Started with L A TEX

1.7 Limit of a Function

A Sample L A TEX Document

The rotfloat package

A couple of things involving environments

The NotesPages Package Filling documents, so the total number of pages is a multiple of a given number.

The export package. Jean-Pierre F. Drucbert 2000/10/16

Typesetting lettrines in L A TEX 2ε documents Daniel Flipo

CPSC 320 Sample Solution, Playing with Graphs!

Notes from the Boards Set # 5 Page

Fancy paragraph designs with the fancypar package

Q&A Session for Connect with Remedy - CMDB Best Practices Coffee Break

The filecontents package

The hanging package. Contents. 1 Introduction

The svn package. Richard Lewis 25th September 2007

Fancy paragraph designs with the fancypar package

The cybercic package

TEX beauties and oddities

The lips LaTeX package Chicago Manual text ellipses (Frankenstein s lips)

Subversion was not there a minute ago. Then I went through a couple of menus and eventually it showed up. Why is it there sometimes and sometimes not?

LATEX Primer. 1 Introduction (Read Me)

Documentation for colortab: Shading and coloring TEX tables, v. 1.0

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

The calc package Infix notation arithmetic in L A TEX

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

fnlineno.sty Numbering Footnote Lines

The showlabels package

It s possible to get your inbox to zero and keep it there, even if you get hundreds of s a day.

Text Input and Conditionals

These are notes for the third lecture; if statements and loops.

NCCTOOLS. Release 3.5

The file ltxdoc.dtx for use with L A TEX 2ε. It contains the code for ltxdoc.cls

Mastering the Visual LISP Integrated Development Environment

How to Get Your Inbox to Zero Every Day

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

The compiler is spewing error messages.

The footbib package. Eric Domenjoud 2007/02/20. 1 General overview 1. 3 Known and potential problems 7

Lab 5 - Repetition. September 26, 2018

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Wick contractions in L A TEX with simplewick.sty

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Producing proceedings articles with L A TEX2ε

Integrated Math, Part C Chapter 1 SUPPLEMENTARY AND COMPLIMENTARY ANGLES

Transcription:

The afterpage package David Carlisle 2014/10/28 This file is maintained by the L A TEX Project team. Bug reports can be opened (category tools) at https://latex-project.org/bugs.html. This package implements a command, \afterpage, that causes the commands specified in its argument to be expanded after the current page is output. 1 1. Sometimes L A TEX s float positioning mechanism gets overloaded, and all floating figures and tables drift to the end of the document. One may flush out all the unprocessed floats by issuing a \clearpage command, but this has the effect of making the current page end prematurely. Now you can issue \afterpage{\clearpage} and the current page will be filled up with text as usual, but then a \clearpage command will flush out all the floats before the next text page begins. 2. An earlier mechanism to help with float placement was the optional argument [H] (meaning HERE!) which was originally added to the standard floating environments by here.sty, and is now provided by float.sty. However some [H] users have commented that they did not really mean Here! They actually wanted Somewhere close. This can now be achieved by \afterpage{\clearpage\begin{figure}[h]... \end{figure}} This ensures that the figure is at the top of the next page. (The \clearpage stops any other figures drifting past the [H] figure.) 3. Floating longtables. longtable.sty provides the longtable environment, a multi-page version of tabular. Many longtable users have told me that it is difficult to set the text surrounding the long table, and that they wanted a floating version. As, presumably, longtables are long, they are probably too large to hold in memory, and float in the way that the table environment is floated, however if the table is in a separate file, say ltfile.tex, you can now use one of: \afterpage{\clearpage\input{ltfile}} \afterpage{\clearpage\input{ltfile}\clearpage}. The first form lets text appear on the same page as the end of the longtable, the second ensures that the surrounding text starts again on a new page. This file has version number v1.08, last revised 2014/10/28. 1 This is really a pre-release, to see whether people like the idea of a command like this. This implementation is not particularly robust. This implementation does not work in two column mode, and can get confused by LATEX s floating environments. 1

1 *package \afterpage The token register used to save the old output routine. 2 \newtoks\ap@output 3 \global\ap@output\expandafter{\the\output} A box register used to save any part of the next page which has already been processed. 4 \newbox\ap@partial A box register used to save any footnote texts that are tied to the text that gets saved in \AP@partial. 5 \newbox\ap@footins The following macro attempts to get safely into vertical mode, and then invokes a special output routine to grab the current page into \AP@partial. 6 \def\ap@savetop{% Now begins a test to see what state we are in. \AP@noindent will be defined so as to return to this state (well, almost!) after afterpage has finished. 7 \ifvmode Vertical mode. This is the simplest case, do nothing. 8 \let\ap@noindent\empty 9 \else\ifhmode Horizontal mode. Back out into vertical mode, removing the indentation box as we go. If in fact there was no indentation box, the output routine was invoked by \noindent (what bad luck!) so we have to remember to re-insert the \noindent before the paragraph is seen again. \everypar tokens have already been inserted, so don t insert them again. 10 \setbox\z@\lastbox 11 \edef\ap@noindent 12 {{\everypar{}\ifvoid\z@\noindent\else\indent\fi}}% 13 \par 14 \else The remaining (even worse) possibility that the output routine was triggered by the start of displaymath within a paragraph. Come out of displaymath with $$, then adjust the spacing (getting into vmode at the same time). \AP@noindent will restart display math later. \everydisplay tokens have already been inserted (they apply to the math list that will be started by \AP@noindent, even though they were triggered by the display math that was closed by the lines below!). Save the values \prevgraf and \predisplaysize for use in the re-started math list. 15 \abovedisplayshortskip\z@\abovedisplayskip\z@ 16 \belowdisplayshortskip\z@\belowdisplayskip\z@ 17 \xdef\ap@disp{% 18 \predisplaysize\the\predisplaysize 19 \prevgraf\the\prevgraf\relax}% 20 $$\vskip-\baselineskip\vskip-\parskip 21 \edef\ap@noindent{% Do not insert \everydisplay tokens again. 22 \toks@{\the\everydisplay}\everydisplay{}% 2

Start displaymath mode with no spurious paragraph line above it. Restore \prevgraf and \predisplaysize. Use \aftergroup to restore the correct setting for \everydisplay after this display has finished. 23 {\everypar{}\noindent}$$\ap@disp\aftergroup\noexpand\ap@ed}% 24 \fi\fi Now switch the output routine and remove everything from the current page into the box \AP@partial. 25 \begingroup 26 \nointerlineskip\null 27 \output{% 28 \global\setbox\ap@partial\vbox{% 29 \unvbox\@cclv 30 \global\setbox\@ne\lastbox}% If the text that is saved in \AP@partial had footnotes, we d better grab them as well otherwise they may come out on a page with the afterpage text, before the page that has the footnote mark! (Added at v1.08.) 31 \global\setbox\ap@footins\box\footins}% Having defined the output routine, trigger it... 32 \eject 33 \endgroup} \AP@ stores all the commands that must be executed after the page break. 34 \let\ap@\relax Restore the \everydisplay register. \ignorespaces prevents a space or newline after $$ creating rogue a indentation or paragraph. 35 \def\ap@ed{\everydisplay\expandafter{\the\toks@}\ignorespaces} Remove the current vertical list, insert the commands \AP@ at the top of the page, and then re-insert the saved text. 36 \def\ap@@{% 37 \AP@savetop 38 \global\expandafter\let\expandafter\ap@\expandafter\relax\ap@ 39 \par The text originally at the top of this page is now stored in the box \AP@partial, including \topskip glue. Now we want to unbox \AP@partial, placing the baseline of the first row \baselineskip below the baseline of the last line coming from the afterpage text. If we assumed nothing has too much height or depth (and \topskip is rigid), it would be fairly trivial to position the contents of \AP@partial so that the baseline of the first row was \baselineskip below the last row just added. In this version, I thought it might be fun to try to exactly achieve the \baselineskip or \lineskip calculation that TEX normally does internally. The call to \addboxcontents does the right thing (I hope). 40 \addboxcontents\ap@partial Now re-insert any footnote text. This may not be quite the right place, as the text that has just been unboxed may break over a page in its new position. Also it may not be the right number if the text from \afterpage itself contains footnotes. Too bad! 41 \ifvoid\ap@footins\else 42 \insert\footins{\unvbox\ap@footins}\fi 3

Now repair things if we started off in horizontal mode. 43 \AP@noindent} If \AP@ is not \relax then the current page already has some afterpage commands, so just add the new commands to the end of the list. Otherwise save the commands in \AP@. (within a local group), and switch the output routine. (The new output routine just calls the old one if it is invoked by a L A TEX float. 44 \long\def\afterpage#1{% 45 \ifx\ap@\relax 46 \gdef\ap@{{#1\par}}% 47 \global\output{% 48 \the\ap@output 49 \ifnum\outputpenalty>-\@mi 50 \global\output\expandafter{\the\ap@output}% 51 \aftergroup\ap@@ 52 \fi}% 53 \else 54 \expandafter\gdef\expandafter\ap@\expandafter{\ap@{#1\par}}% 55 \fi} If we have got to the end of the document or clearpage just put the stuff out without any trickery. 56 \let\ap@clearpage\clearpage 57 \def\clearpage{% 58 \ifx\ap@\relax 59 \AP@clearpage 60 \else 61 \global\output\expandafter{\the\ap@output}% 62 \AP@clearpage At this point (since v1.08) Need to clear \AP@ before using its expansion, as otherwise hit an infinite loop. Sigh. 63 \global\expandafter\let\expandafter\ap@\expandafter\relax 64 \expandafter\expandafter\ap@ 65 \fi} 66 \let\ap@enddocument\enddocument 67 \def\enddocument{% 68 \ifx\ap@\relax\else 69 \global\output\expandafter{\the\ap@output}% 70 \AP@clearpage 71 \global\expandafter\let\expandafter\ap@\expandafter\relax 72 \expandafter\expandafter\ap@ 73 \fi 74 \AP@enddocument} \addboxcontents Given a vbox #1, add to the current vertical list such that the end result is equivalent to the list that TEX would have built had the contents of #1 (apart from any initial glue) been added individually to the current list. So essentially, the problem is that of unboxing #1, but replacing the glue at the top of #1 with (something equivalent to) the \baselineskip or \lineskip glue that TEX would normally have placed before the first box in #1. Also \prevdepth must be set at the end. 75 \def\addboxcontents#1{{% 4

Perhaps I shouldn t use grouping here, as I probably don t really want to save #1. If it is removed, \splittopskip and \splitmaxdepth would need to be restored by hand. First replace any glue at the top by \vskip 0pt. 76 \splittopskip\z@ 77 \splitmaxdepth\maxdimen 78 \setbox#1\vbox{\break\unvbox#1}% 79 \setbox\z@\vsplit#1to\z@ Put the breakpoint back. 80 \setbox#1\vbox{\break\unvbox#1}% Set \skip@ to be height of #1 (without top glue) 81 \skip@\ht#1% Now make the first baseline of the first row be \vsize from the top. (This assumes that the first row has height less than \vsize.) 82 \splittopskip\vsize 83 \setbox\z@\vsplit#1to\z@ Subtract the new height of #1 from \skip@, and add back on \splittopskip, so \skip@ is now the height of the first row of #1 This may still be 0pt if (eg) a mark or whatsit is between the top glue and the first box. Save (this height \splittopskip) in \skip\tw@. 84 \advance\skip@-\ht#1% 85 \skip\tw@\skip@ 86 \advance\skip@\splittopskip Now fake TEX s \baselineskip calculation. 87 \advance\skip@\prevdepth 88 \advance\skip@-\baselineskip 89 \advance\skip\tw@\ifdim-\skip@<\lineskiplimit\lineskip\else-\skip@\fi Finally add the glue. 90 \vskip\skip\tw@ Now unbox the box, setting \prevdepth by hand, as \unvbox (unlike \box) does not automatically set it. 91 \global\dimen@i\dp#1% 92 \unvbox#1}% 93 \prevdepth\dimen@i} 94 /package 5