%%% ====================================================================
%%%  @TeX-file{
%%%     author          = "Walter D. Neumann",
%%%     version         = "1.2",
%%%     date            = "22 July 1992",
%%%     time            = "15:57:20 EDT",
%%%     filename        = "newinsert.tex",
%%%     address         = "Department of Mathematics
%%%                        Ohio State University
%%%                        Columbus, OH 43201
%%%                        USA",
%%%     email           = "neumann@mps.ohio-state.edu (Internet)",
%%%     codetable       = "ISO/ASCII",
%%%     keywords        = "plain TeX, AMS-TeX, inserts, figures",
%%%     supported       = "yes",
%%%     abstract        = "Bug fixes to plain TeX macros \midinsert and
%%%                        \topinsert",
%%%  }
%%% ====================================================================

% Include with "\input newinsert.tex" to fix following problems
% with plain TeX's handling of \midinsert and \topinsert:
%
% 1. An insert that falls at the top of the page is too high: its
%    top is at top-of-page rather than top-of-ink.
% 2. Plain uses \bigskip for space around inserts and assumes \bigskip
%    is 12pt plus stretch, so \midinsert misbehaves if \bigskipamount
%    is changed. This space should be an independent quantity.
% 3. Consecutive midinserts that did not float are spaced twice
%    as far apart as consecutive topinserts or floated midinserts.
% 4. Midinserts can get out of order (see Exercise 15.5 of TeXbook).
% 5. A midinsert can incorrectly convert to a topinsert if TeX is
%    reading ahead (see top of page 116 of TeXbook).
% 6. \removelastskip fails after a floated insert. This is relevant for
%    an insertion before any construction (such as a proclamation in
%    plain TeX) that does an explicit or implicit \removelastskip.
% 7. The routine to split an insertion in the program TeX fails if
%    the insertion would fit on the current page after subtracting
%    shrink.  To avoid this, INSERTIONS SHOULD NOT CONTAIN SHRINK.
%
% Remaining problem: With the default values of 10pt and 12pt for
% \topskip and \baselineskip, TeX puts 2pt less space between a
% topinsert or floated midinsert and following text than between a
% middle insertion and following text. To avoid this without rewriting
% the plain output routine use:
%
% \topskip=\baselineskip
%
% PARAMETERS:
% Plain.tex puts \bigskipamount of space before and after inserts. As
% described above, this should be an independent parameter with no shrink.
% We provide \insertskipamount for this purpose (default 6pt plus 2pt;
% this makes \insertskip behave like an unshrinkable \medskip).
% In addition, \inserthardskipamount of glue is added at the top of EVERY
% insert; it remains even at top of page (default 6pt; it should be at
% least \topskipamount-(text height)).
%   These defaults give good balance for figure insertions of the form:
%       \midinsert
%       [Commands to include a graphics file]
%       \insertskip \centerline{Figure Title}
%       \endinsert.
% Note use of \insertskip instead of \medskip to avoid shrink.
%
\chardef\newinsCatAt\the\catcode `\@
\catcode `\@=11
%
%%%%%%%%%%%% Corrected insert macros for plain.tex %%%%%%%%%%%%%%%%%
%
%  New skipamounts:
%
\newskip\insertskipamount\newskip\inserthardskipamount
\insertskipamount 6pt plus2pt       % Redefine as desired, but with no
\inserthardskipamount 6pt           %    shrink (see above).
\def\insertskip{\vskip\insertskipamount}
%
%  Save and restore \lastskip:
%
\newskip\LastSkip
\def\SaveLastSkip{\LastSkip\lastskip}
\def\RestoreLastSkip{\nobreak\vskip-\LastSkip\vskip\LastSkip}
%
%  Larry Siebenmann's test for split topinserts:
%
\newcount\SplitTest%        will be set to -1 if a topinsert has split
\def\SetSplitTest{\SplitTest\insertpenalties
  \insert\topins{\floatingpenalty1}%
  \advance\SplitTest-\insertpenalties}
%
%  From here on we modify definitions in plain.tex.
%
% Redefine \midinsert to convert to \topinsert if a topinsert has been
% split, to prevent midinserts getting out of order (cf. TeXbook Exercise
% 15.5). As in plain.tex, a \midinsert still converts to a \topinsert
% (which then splits) if the insert is too big for current page.
%   Was:    \def\midinsert{\@midtrue\@ins}
\def\midinsert{\par
 \SaveLastSkip\penalty-150\SetSplitTest\RestoreLastSkip
 \ifnum\SplitTest=-1
  \@midfalse\else\@midtrue\fi\@ins}
% Redefine \@ins to add \inserthardskipamount of glue above.
%   Was:  \def\@ins{\par\begingroup\setbox\z@\vbox\bgroup}
\def\@ins{\par\begingroup\setbox\z@\vbox\bgroup%
  \vglue\inserthardskipamount}
% Changes to \endinsert of plain.tex 3.0:
% - Use \insertskipamount instead of \bigskipamount throughout.
% - Use larger of previous skip and insertskip before middle insert.
% - Add \nointerlineskip to avoid unwanted extra 1pt skip.
% - Save and restore lastskip when an insert floats.
\def\endinsert{\egroup % finish the \vbox
  \if@mid \dimen@\ht\z@ \advance\dimen@\dp\z@
    \advance\dimen@\insertskipamount%            was 12pt (wn)
    \advance\dimen@\pagetotal\advance\dimen@-\pageshrink
    \ifdim\dimen@>\pagegoal\@midfalse\p@gefalse\fi\fi
% Next 3 lines replace:  \if@mid \bigskip\box\z@\bigbreak (wn)
  \if@mid%
    \ifdim\lastskip<\insertskipamount\removelastskip\insertskip\fi
    \nointerlineskip\box\z@\penalty-200\insertskip
  \else%
    \SaveLastSkip%                                  added (wn)
    \insert\topins{\penalty100 % floating insertion
    \splittopskip\z@skip
    \splitmaxdepth\maxdimen \floatingpenalty\z@
    \ifp@ge \dimen@\dp\z@
    \vbox to\vsize{\unvbox\z@\kern-\dimen@}% depth is zero
    \else \box\z@\nobreak\insertskip\fi}% was \bigskip\fi (wn)
    \RestoreLastSkip%                               added (wn)
   \fi\endgroup}
%%%%%%%%%%%%%%%%%% Done correcting insert macros %%%%%%%%%%%%%%%%%%%
%
\catcode `\@=\newinsCatAt
\endinput
%% end newinsert.tex
%% Revision history:
%% Version 1.2, 21 July 1992: midinserts still occasionally converted
%%   wrongly to topinsert if Tex is scanning ahead.  Added \penalty-150
%%   to fix this.
%    Changed \medskipamount to 6pt plus2pt to avoid shrink in inserts.
%% Version 1.1, 02 April 1990: used L.Siebenmann's split test