summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/sttools/flushend.dtx
diff options
context:
space:
mode:
Diffstat (limited to 'macros/latex/contrib/sttools/flushend.dtx')
-rw-r--r--macros/latex/contrib/sttools/flushend.dtx1480
1 files changed, 582 insertions, 898 deletions
diff --git a/macros/latex/contrib/sttools/flushend.dtx b/macros/latex/contrib/sttools/flushend.dtx
index 20d20f396e..4270459384 100644
--- a/macros/latex/contrib/sttools/flushend.dtx
+++ b/macros/latex/contrib/sttools/flushend.dtx
@@ -1,6 +1,6 @@
% \iffalse meta-comment/*% \end{macrocode}*/
%
-% Copyright (C) 2020 by Sigitas Tolu\v{s}is <sigitas@vtex.lt>
+% Copyright (C) 2021 by Sigitas Tolu\v{s}is <sigitas@vtex.lt>
% ---------------------------------------------------------------------------
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
@@ -47,8 +47,8 @@
\ProvidesFile{flushend.dtx}
%</driver>
%<*package>
-%% Copyright (C) 1997-2020 by Sigitas Tolu\v{s}is <sigitas@vtex.lt>
-%% VTeX Ltd., Mokslinink\k{u} 2a, Vilnius, Lithuania
+%% Copyright (C) 1997-2021 by Sigitas Tolu\v{s}is <sigitas@vtex.lt>
+%% UAB VTeX, Mokslininkų 2A, LT-08412 Vilnius, Lithuania
%% --------------------------------------------------------------------------
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
@@ -64,16 +64,19 @@
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{flushend}
%<*package>
- [2020/10/16 v3.5 Balancing columns in twocolumn mode]
+ [2021/10/04 v4.0 Balancing columns in twocolumn mode]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
-\usepackage{flushend}[2020/10/16]
+\usepackage{flushend}[2021/10/04]
+\usepackage{geometry}
+\geometry{a4paper}
+\setlength\marginparwidth{5pc}
\EnableCrossrefs
\CodelineIndex
%%\RecordChanges
-%\OnlyDescription
+\OnlyDescription
\begin{document}
\DocInput{flushend.dtx}
%%\PrintChanges
@@ -117,6 +120,7 @@
% \changes{v3.3}{2017/03/27}{bugfix for get@@footnoterule@@box; options for some checks on/off}
% \changes{v3.4}{2020/10/14}{bugfix development and test}
% \changes{v3.5}{2020/10/16}{bugfix version: modern with noautobase}
+% \changes{v4.0}{2021/10/04}{another rewrite with new balance algorithm and options}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
@@ -132,8 +136,11 @@
%
% The package is used to balance columns on any page in twocolumn
% mode. By default it is switched on at the end of document. It is
-% expected to work with footnotes, top floats and column break
-% before one line section title.
+% expected to work with leftcolumn footnotes (option checkfootnote)
+% and rightcolumn top floats (option checkfloat).
+%
+% To compare with the previous version the package is completely rewritten
+% with new balancing algorithm and new options, removed dependency on other packages.
%
% \section{Usage}
%
@@ -142,145 +149,167 @@
% \cs{flushcolsend} at that page.
%
% \medskip
-% \DescribeMacro{\flushend}
+% \leftline{\textbf{\textit{Available commands}}:}
+%
+% \begin{description}
%
+% \item[\cs{flushend}]
% This macro switches on column balancing on the last page. It is used
% by default.
%
-% \medskip
-% \DescribeMacro{\raggedend}
-%
+% \item[\cs{raggedend}]
% This macro switches off column balancing on the last page.
%
-% \medskip
-% \DescribeMacro{\flushcolsend}
-%
+% \item[\cs{flushcolsend}]
% This macro switches on column balancing on the current page.
%
-% \medskip
-% \DescribeMacro{\raggedcolsend}
-%
+% \item[\cs{raggedcolsend}]
% This macro switches off column balancing on the current page.
%
-% \medskip
-% \DescribeMacro{\atColsBreak}
-% \marg{tokens}
-%
+% \item[\cs{atColsBreak}\meta{tokens}]
% Adds \meta{tokens} in place of the original column break (made without balancing).
%
% \medskip
% \textit{Example}: |\atColsBreak{\vskip-2pt}|
%
-% \medskip
-% \DescribeMacro{\atColsEnd}
-% \marg{tokens}
-%
+% \item[\cs{atColsEnd}\meta{tokens}]
% Adds \meta{tokens} at the end of right column.
%
-% \medskip
-% \textit{Example}: |\atColsEnd{\vfil}|.
-%
-% \medskip
-% \DescribeMacro{\showcolsendrule}
-%
+% \item[\cs{showcolsendrule}]
% It can be used just for debugging: adds rule to the bottom of columns.
%
+% \end{description}
+%
% \subsection{Package options}
%
-% \medskip
-% \DescribeMacro{ancient}
-% It is used to switch on old version of balancing algorithm.
-% This version is not so accurate or "clever" but quite safe.
+% \begin{description}
+%
+% \item[\meta{|spread|}]
+% \texttt{[default]} It enables columns spreading trying to balance.
%
-% \medskip
-% \DescribeMacro{modern}
-% [default] It is used to switch on new version of balancing algorithm.
-% This version has some heuristic guesses for footnotes, floats
-% and section like head skip. So it is more dangerous in the case
-% for text misplace or disappear.
+% \item[\meta{|nospread|}]
+% It disables columns spreading trying to balance.
%
-% \medskip
-% \DescribeMacro{autobase}
-% [default] It is used to guess |\baselineskip| value on original columns
-% break. On failure just use |\atColsBreak{...}|.
+% \item[\meta{|shrink|}]
+% \texttt{[default]} It enables columns shrinking trying to balance.
%
-% \medskip
-% \DescribeMacro{noautobase}
-% It is used to switch off the previous option behaviour.
+% \item[\meta{|noshrink|}]
+% It disables columns shrinking trying to balance.
%
% \medskip
-% \DescribeMacro{spread}
-% [default] It adds extra glue to the rightcolumn trying to balance.
+% \textit{Similiar options exist for single column separatly}:
%
-% \medskip
-% \DescribeMacro{nospread}
-% It skips adding extra glue to the last column.
+% |lspread|, |nolspread|, |lshrink|, |nolshrink| -- for left column
%
-% \medskip
-% \DescribeMacro{removelastbox}
-% [default] It tries to remove an empty box (some more material) from the last column bottom.
+% |rspread|, |norspread|, |rshrink|, |norshrink| -- for right column
%
-% \medskip
-% \DescribeMacro{keeplastbox}
-% It skips removing anything from the last column. As example it is usefull and can be used
-% then document ends by reference item and last line of text should be with indend.
+% \item[\meta{|checkfootnote|}]
+% \texttt{[default]} Adds guess and checking for possible footnote on left column.
%
-% \medskip
-% \DescribeMacro{checkfootnote}
-% [default] Adds guess for footnote on left column then modern balancing algorithm is in use.
+% \item[\meta{|nocheckfootnote|}]
+% Skips guess for possible footnote and ignores any existing one.
%
-% \medskip
-% \DescribeMacro{nocheckfootnote}
-% Skips guess for footnote then modern balansing algorithm is in use.
+% \item[\meta{|checkfloat|}]
+% Adds guess for top float on right column. Any box with height bigger then 5|\topskip|
+% is considered as float. There are no user interface to change this limit except
+% internal macro definition: |\gdef\top@@floatbox@min@default{5\topskip}|
%
-% \medskip
-% \DescribeMacro{checkfloat}
-% [default] Adds guess for top float on right column then modern balancing algorithm is in use.
+% As guess is too brutal, not accurate, and somehow dangerous in case for text misplacement
+% it is switched off by default.
%
-% \medskip
-% \DescribeMacro{nocheckfloat}
-% Skips guess for top float on right column then modern balancing algorithm is in use.
+% \item[\meta{|nocheckfloat|}]
+% \texttt{[default]} Skips guess for top float on right column.
%
-% \medskip
-% \DescribeMacro{debug}
+% \item[\meta{|debug|}]
% Adds rules to the bottom of columns (just for debugging) and leftcolumn break place.
% Adds some additional log info.
%
-% \medskip
-% \DescribeMacro{nodebug}
-% [default] Skips putting debuging lines.
+% \item[\meta{|nodebug|}]
+% \texttt{[default]} Skips putting debuging lines and additional info.
%
-% \StopEventually{}
+% \end{description}
%
% \section{Implementation}
%
+% Improved balancing (at least it is expected so) is based on using possible spread and shrink
+% in both columns checking constructed columns badness.
+% Hard coded limit for badness is |101|. Available only as internal macro: |\def\flushend@@column@badness{101}|.
+% |\pagediscards| material dimensions are used to restore original vertical skip at original columns break.
+%
+% \bigskip
+% \leftline{\textbf{\textit{Expected usecase}} \texttt{(with options [checkfootnote,checkfloat])}:}
+%
+% \begin{verbatim}
+%
+% -------------------------- --------------------------
+% |xxxxxxxxxx ++++++++++| |xxxxxxxxxx ++++++++++|
+% |xxxxxxxxxx + +| |xxxxxxxxxx + +|
+% |xxxxxxxxxx + +| |xxxxxxxxxx + +|
+% |xxxxxxxxxx ++++++++++| |xxxxxxxxxx ++++++++++|
+% |xxxxxxxxxx figure | |xxxxxxxxxx figure |
+% | | | |
+% |section xxxxxxxxxx| |section yyyyyyyyyy|
+% | xxxxxxxxxx| -----> | yyyyyyyyyy|
+% |xxxxxxxxxx xxxxxxxxxx| |xxxxxxxxxx xxxxxxxxxx|
+% |xxxxxxxxxx xxxxxxxxxx| |xxxxxxxxxx xxxxxxxxxx|
+% |yyyyyyyyyy xxxxxxxxxx| | xxxxxxxxxx|
+% |yyyyyyyyyy | |--- xxxxxxxxxx|
+% | | |footnote xxxxxxxxxx|
+% |--- | | |
+% |footnote | | |
+% -------------------------- --------------------------
+%
+% \end{verbatim}
+% \StopEventually{}
+%
% \iffalse
%<*package>
% \fi
%
%/*% \begin{macrocode}*/
% \begin{macrocode}
-\newif\if@auto@baselineskip \@auto@baselineskiptrue
-\newif\if@ancient@balance@version \@ancient@balance@versionfalse
+
+\newif\if@footnote@check \@footnote@checktrue
\newif\if@right@column@spread \@right@column@spreadtrue
-\newif\if@remove@lastbox@at@balancing \@remove@lastbox@at@balancingtrue
+\newif\if@right@column@shrink \@right@column@shrinktrue
+\newif\if@left@column@spread \@left@column@spreadtrue
+\newif\if@left@column@shrink \@left@column@shrinktrue
\newif\if@balance@debug \@balance@debugfalse
-\DeclareOption{autobase}{\global\@auto@baselineskiptrue}
-\DeclareOption{noautobase}{\global\@auto@baselineskipfalse}
-\DeclareOption{ancient}{\global\@ancient@balance@versiontrue}
-\DeclareOption{modern}{\global\@ancient@balance@versionfalse}
-\DeclareOption{spread}{\global\@right@column@spreadtrue}
-\DeclareOption{nospread}{\global\@right@column@spreadfalse}
-\DeclareOption{removelastbox}{\global\@remove@lastbox@at@balancingtrue}
-\DeclareOption{keeplastbox}{\global\@remove@lastbox@at@balancingfalse}
+
+\DeclareOption{spread}{%
+ \global\@left@column@spreadtrue
+ \global\@right@column@spreadtrue
+ }
+\DeclareOption{nospread}{%
+ \global\@left@column@spreadfalse
+ \global\@right@column@spreadfalse
+ }
+\DeclareOption{lspread}{\global\@left@column@spreadtrue}
+\DeclareOption{nolspread}{\global\@left@column@spreadfalse}
+\DeclareOption{rspread}{\global\@right@column@spreadtrue}
+\DeclareOption{norspread}{\global\@right@column@spreadfalse}
+\DeclareOption{shrink}{%
+ \global\@left@column@shrinktrue
+ \global\@right@column@shrinktrue
+ }
+\DeclareOption{noshrink}{%
+ \global\@left@column@shrinkfalse
+ \global\@right@column@shrinkfalse
+ }
+\DeclareOption{lshrink}{\global\@left@column@shrinktrue}
+\DeclareOption{nolshrink}{\global\@left@column@shrinkfalse}
+\DeclareOption{rshrink}{\global\@right@column@shrinktrue}
+\DeclareOption{norshrink}{\global\@right@column@shrinkfalse}
\DeclareOption{checkfootnote}{%
\AtEndOfPackage{%
\let\check@@footnoterule@@box\initial@check@@footnoterule@@box
+ \@footnote@checktrue
}%
}
\DeclareOption{nocheckfootnote}{%
\AtEndOfPackage{%
\let\check@@footnoterule@@box\@gobblethree
+ \@footnote@checkfalse
}%
}
\DeclareOption{checkfloat}{%
@@ -295,240 +324,151 @@
}
\DeclareOption{debug}{\global\@balance@debugtrue}
\DeclareOption{nodebug}{\global\@balance@debugfalse}
+
\ProcessOptions
-% \end{macrocode}
-%
-% \begin{macrocode}
+
\newskip\flushend@@lastskip@a
\newskip\flushend@@lastskip@b
-\newskip\flushend@@lastskip@c
-\newcount\flushend@@penalty@a
+\newcount\flushend@@lastnode
\newcount\var@@loop@iter
-\newdimen\flushend@@lastkern@a
\newdimen\var@@temp@spread
\newdimen\var@@temp@a
\newdimen\var@@temp@loop
-\def\top@@skip@@limit{.7\topskip}
\newdimen\flushend@@page@rule \flushend@@page@rule\z@
\def\showcolsendrule{\global\flushend@@page@rule=.4\p@}
-% \end{macrocode}
-%
-% \begin{macro}{\top@@floatbox@min}\marg{float size}
-% This value is used to catch possible float on right column top.
-% \begin{macrocode}
-\gdef\top@@floatbox@min@default{4\topskip}
-\let\top@@floatbox@min\top@@floatbox@min@default
-% \end{macrocode}
-% \end{macro}
-%
-% \begin{macrocode}
+\gdef\top@@floatbox@min@default{5\topskip}
+\let\top@@floatbox@min\maxdimen
+\def\flushend@@column@badness{101}
+\newbox\flushend@@varbox@lastpage
\newbox\flushend@@varbox@a
\newbox\flushend@@varbox@c
\newbox\flushend@@tempbox@a
\newbox\flushend@@tempbox@c
+\newbox\flushend@@floatbox
\@ifundefined{@viper}{\newbox\@viper}{}
\@ifundefined{hold@viper}{\newbox\hold@viper}{}
-% \end{macrocode}
-%
-% \begin{macrocode}
\newtoks\atColsBreak \atColsBreak={}
\newtoks\atColsEnd \atColsEnd={}
-% \end{macrocode}
-%
-% \begin{macrocode}
\@ifundefined{fmt@box@adds}{\def\fmt@box@adds#1{#1}}{}
\@ifundefined{fmt@vbox@adds}{\def\fmt@vbox@adds[#1]#2{#2}}{}
-% \end{macrocode}
-%
-% \begin{macro}{\remove@lastbox@at@balancing}
-% It is used to remove empty box at the right column end.
-% \begin{macrocode}
+\def\wlog@balance@debug#1{\if@balance@debug \wlog{#1}\fi}
+\def\log@box@info#1{<box\the#1>(\the\ht#1+\the\dp#1)x\the\wd#1}
+
\def\unskip@three@kern@penalty{%
\unskip\unkern\unpenalty
\unskip\unkern\unpenalty
\unskip\unkern\unpenalty
}
-\gdef\remove@lastbox@at@balancing{%
- \unskip@three@kern@penalty
- \if@remove@lastbox@at@balancing
- \else
- \expandafter\null
- \fi
- \bgroup
+
+\gdef\remove@empty@lastbox@at@balancing#1{%
+ %%
+ %% Removes lastbox without dimensions.
+ %% Removes latest skip, kern, penalty.
+ %% Cuts the lastbox's depth bigger then strutbox's depth.
+ %%
+ \var@@temp@a=\z@
+ \setbox\flushend@@tempbox@a\vbox{%
+ \unvcopy#1%
+ \unskip@three@kern@penalty
+ \global\flushend@@lastnode=\lastnodetype
+ }%
+ \setbox#1\vbox{%
+ \unvcopy\flushend@@tempbox@a
\setbox\z@\lastbox
- \ifdim\wd\z@>\z@
- \box\z@
+ \ifdim\dimexpr\ht\z@+\dp\z@+\wd\z@\relax>\z@
+ %% not empty lastbox - store depth of the lastbox
+ \global\var@@temp@a=\dp\z@
\else
- \aftergroup\unskip@three@kern@penalty
+ %% empty lastbox
+ \setbox\z@\box\flushend@@tempbox@a
+ \expandafter\unskip@three@kern@penalty
\fi
- \egroup
+ }%
+ \ifvoid\flushend@@tempbox@a\else
+ %% not empty lastbox - restore original
+ \setbox#1\box\flushend@@tempbox@a
+ \fi
+ \ifdim\var@@temp@a>\z@
+ %% cut the depth of the lastbox with the negative skip
+ \ifdim\var@@temp@a>\dp\strutbox
+ %% it is something more than single line depth
+ \else
+ \setbox#1\vbox{\unvbox#1\vskip-\var@@temp@a}%
+ \fi
+ \fi
}
+
\def\set@split@topskip@to#1{%
- \splittopskip#1\relax \vfuzz\maxdimen \vbadness\maxdimen
- }
-% \end{macrocode}
-% \end{macro}
-%
-% Macros used in debug mode.
-%
-% \begin{macrocode}
-\def\show@@box#1{%
- \bgroup
- \showboxbreadth=20000\showboxdepth=20000%
- \showbox#1\relax
- \egroup
- }
-\def\wlog@balance@debug#1{\if@balance@debug \wlog{#1}\fi}
-\def\log@box@info#1{<box\the#1>(\the\ht#1+\the\dp#1)x\the\wd#1}
-\let\show@@box@next\@gobble
-% \end{macrocode}
-%
-% To get more or less proper result we need to know about footnotes at
-% the left column end, top floats at the right column top, skips and
-% so on. This part of the package is responsible for such analysis.
-%
-% \begin{macrocode}
-\gdef\analyze@lastbox@box#1{%
- \setbox\flushend@@varbox@a\vbox{%
- \unvbox#1%
- \global\flushend@@lastskip@a\lastskip
- \unskip
- \global\var@@temp@a\lastkern
- \unkern
- \global\flushend@@penalty@a\lastpenalty
- \unpenalty
- \ifdim\lastskip>\z@
- \global\flushend@@lastskip@a\lastskip
- \fi
- \unskip
- \ifdim\lastkern>\z@
- \global\var@@temp@a\lastkern
- \fi
- \unkern
- \ifnum\lastpenalty>\z@
- \global\flushend@@penalty@a\lastpenalty
- \fi
- \unpenalty
- \global\setbox\flushend@@tempbox@a\lastbox
- }%
- \wlog@balance@debug{^^J::analyze@lastbox@box::\log@box@info{#1}%
- ^^J ::unvbox: \log@box@info{\flushend@@varbox@a}%
- ^^J::lastbox: \log@box@info{\flushend@@tempbox@a}%
- ^^J::lastskip: \the\flushend@@lastskip@a;
- lastkern: \the\var@@temp@a;
- lastpenalty: \the\flushend@@penalty@a
- }%
+ \splittopskip=#1\relax
+ \vfuzz=\maxdimen
+ \vbadness=\maxdimen
}
-% \end{macrocode}
-%
-% \begin{macro}{\analyze@leftcolumn@box}\marg{box}
-%
-% Fills:
-% \begin{itemize}
-% \item baselineskip skip from the last boxes to |\flushend@@lastskip@c|
-% \item leftcolumn height without the footnote to |\main@box@height|
-% \item footnote skip to |\main@box@skip|
-% \end{itemize}
-%
-% \begin{macrocode}
+
\gdef\analyze@leftcolumn@box#1{%
+ %%
+ %% Try to find footnotes on the leftcolumn
+ %%
\set@split@topskip@to\z@
\flushend@@lastskip@b\ht#1%
- \wlog@balance@debug{^^Jmainbox: \log@box@info{#1}}%
+ \advance\flushend@@lastskip@b\dp#1%
\setbox\flushend@@varbox@a\vbox{%
\unvbox#1%
\global\flushend@@lastskip@a\lastskip
- \unskip
- \global\flushend@@lastkern@a\lastkern
- \unkern
- \global\flushend@@penalty@a\lastpenalty
- \unpenalty
+ \unskip\unkern\unpenalty
\global\setbox\flushend@@tempbox@a\lastbox
}%
\advance\flushend@@lastskip@b -\ht\flushend@@varbox@a
\advance\flushend@@lastskip@b -\ht\flushend@@tempbox@a
- \wlog@balance@debug{.-box: \log@box@info{\flushend@@varbox@a}%
- ^^J.-lastbox: \log@box@info{\flushend@@tempbox@a}%
- ^^J.-lastskip: \the\flushend@@lastskip@a;
- lastkern: \the\flushend@@lastkern@a;
- lastpenalty: \the\flushend@@penalty@a
- ^^J.-diff: \the\flushend@@lastskip@b
- }%
- \check@@footnoterule@@box\flushend@@tempbox@a\flushend@@lastskip@b\flushend@@varbox@a
- \check@@baselineskip@@skip
- \flushend@@lastskip@a
- \flushend@@lastskip@b
- \flushend@@lastskip@c
- \ifx\check@@footnoterule@@box\check@@baselineskip@@skip
- \else
- \ifvoid\flushend@@tempbox@a
- \ifdim\ht\flushend@@varbox@a>\topskip
- \flushend@@lastskip@b\ht\flushend@@varbox@a
- \var@@temp@a\ht\flushend@@varbox@a
- \loop
- \setbox\flushend@@tempbox@c\vsplit\flushend@@varbox@a to\var@@temp@a
- \ifvoid\flushend@@varbox@a
- \setbox\flushend@@varbox@a\vbox{\unvbox\flushend@@tempbox@c}%
- \advance \var@@temp@a -3\p@
- \else
- \var@@temp@a=-1\p@
- \fi
- \ifdim \var@@temp@a>\z@
- \repeat
- \setbox\flushend@@varbox@a\vbox{\unvbox\flushend@@varbox@a}%
- \setbox\flushend@@tempbox@c\vbox{\unvbox\flushend@@tempbox@c}%
- \advance\flushend@@lastskip@b -\ht\flushend@@varbox@a
- \advance\flushend@@lastskip@b -\ht\flushend@@tempbox@c
- \wlog@balance@debug{..--box: \log@box@info{\flushend@@tempbox@c}%
- ^^J..--lastbox: \log@box@info{\flushend@@varbox@a}%
- ^^J..--diff: \the\flushend@@lastskip@b
- }%
- \check@@footnoterule@@box\flushend@@varbox@a\flushend@@lastskip@b
- \flushend@@tempbox@c
- \check@@baselineskip@@skip\flushend@@lastskip@a\flushend@@lastskip@b
- \flushend@@lastskip@c
- \ifx\check@@footnoterule@@box\check@@baselineskip@@skip
- \else
- \ifdim\ht\flushend@@varbox@a>.5\topskip
- \analyze@lastbox@box\flushend@@varbox@a
- \fi
+ \ifvoid\flushend@@tempbox@a
+ \ifdim\ht\flushend@@varbox@a>\topskip
+ \flushend@@lastskip@b\ht\flushend@@varbox@a
+ \var@@temp@a\ht\flushend@@varbox@a
+ \loop
+ \setbox\flushend@@tempbox@c\vsplit\flushend@@varbox@a to\var@@temp@a
+ \ifvoid\flushend@@varbox@a
\setbox\flushend@@varbox@a\vbox{\unvbox\flushend@@tempbox@c}%
+ \advance\var@@temp@a -3\p@
+ \else
+ \var@@temp@a=-1\p@
\fi
- \else
- \wlog@balance@debug{lastbox: \log@box@info{\flushend@@varbox@a}}%
- \fi
+ \ifdim \var@@temp@a>\z@
+ \repeat
+ \setbox\flushend@@varbox@a\vbox{\unvbox\flushend@@varbox@a}%
+ \setbox\flushend@@tempbox@c\vbox{\unvbox\flushend@@tempbox@c}%
+ \advance\flushend@@lastskip@b -\ht\flushend@@varbox@a
+ \advance\flushend@@lastskip@b -\ht\flushend@@tempbox@c
+ \check@@footnoterule@@box\flushend@@varbox@a\flushend@@lastskip@b
+ \flushend@@tempbox@c
+ \setbox\flushend@@varbox@a\vbox{\unvbox\flushend@@tempbox@c}%
+ \else
+ \global\@footnote@checkfalse
\fi
+ \else
+ \check@@footnoterule@@box\flushend@@tempbox@a\flushend@@lastskip@b\flushend@@varbox@a
+ \fi
+ \if@footnote@check
\ifdim\var@@temp@loop=\ht\flushend@@varbox@a\relax
- \global\advance\var@@loop@iter\@ne
+ \global\advance\var@@loop@iter\@ne
\fi
\var@@temp@loop=\ht\flushend@@varbox@a\relax
\ifnum\var@@loop@iter<3\relax
- \wlog@balance@debug{analyze left col: iteration \number\var@@loop@iter; ht \the\var@@temp@loop}%
+ %% next iteration
\else
- \wlog@balance@debug{analyze left col loop limit!}%
- \ht\flushend@@varbox@a\z@
+ %% loop limit, break loop
+ \ht\flushend@@varbox@a=\z@
\fi
\ifdim\ht\flushend@@varbox@a>\topskip
\expandafter\analyze@leftcolumn@box\expandafter\flushend@@varbox@a
\fi
\fi
}
-% \end{macrocode}
-% \end{macro}
-%
-% \begin{macro}{\get@@footnoterule@@box}
-%
-% Gets dimensions of the box made by |footnoterule| and writes to:
-% |\footnoterule@@box@@ht|, |\footnoterule@@box@@dp|, |\footnoterule@@box@@wd|.
-%
-% \begin{macrocode}
+
\def\get@@footnoterule@@box{%
\set@split@topskip@to\z@
\setbox\flushend@@varbox@a\vbox{\strut
\vskip\z@
\footnoterule
- \unskip\unkern\unpenalty
- \unskip\unkern\unpenalty
+ \unskip@three@kern@penalty
}%
\setbox\flushend@@tempbox@c\vsplit\flushend@@varbox@a to\p@
\xdef\footnoterule@@box@@ht{\the\ht\flushend@@varbox@a}%
@@ -537,19 +477,9 @@
\wlog@balance@debug{:footnoterule: \log@box@info{\flushend@@varbox@a}}%
\setbox\flushend@@tempbox@c\vbox{\unvbox\flushend@@varbox@a}%
}
-% \end{macrocode}
-% \end{macro}
-%
-% \begin{macro}{\check@@footnoterule@@box}\marg{box1}\marg{skip}\marg{box2}
-%
-% Checks if \marg{box1} dimensions equals to |footnoterule| box dimensions.
-% If so and if \marg{skip} is not |\topskip| we guess it is footnote.
-% In that case we define |leftcolumn| height without |footnote| as
-% \marg{box2} height and save it as |\main@box@height|. Skip from text to
-% footnote define as |\main@box@skip|.
-%
-% \begin{macrocode}
+
\long\def\@gobblethree#1#2#3{}
+
\def\check@@footnoterule@@box#1#2#3{%
\ifdim\ht#1=\footnoterule@@box@@ht
\ifdim\dp#1=\footnoterule@@box@@dp
@@ -557,456 +487,400 @@
\ifdim#2=\topskip
\else
\xdef\main@box@height{\the\ht#3}%
+ \xdef\main@box@depth{\the\dp#3}%
\xdef\main@box@skip{\the#2}%
\wlog@balance@debug{^^J:footnoterule found:
main@box <ht=\main@box@height,
skip=\main@box@skip>
}%
\global\let\check@@footnoterule@@box\@gobblethree
- \global\let\check@@baselineskip@@skip\reset@check@@baselineskip@@skip
+ \global\@footnote@checkfalse
\fi
\fi
\fi
\fi
}
+
\let\initial@check@@footnoterule@@box\check@@footnoterule@@box
-% \end{macrocode}
-% \end{macro}
-%
-% \begin{macro}{\check@@baselineskip@@skip}\marg{dimension1}\marg{dimension2}\marg{var}
-%
-% If both dimensions: \marg{dimension1} and \marg{dimension2} are positive we
-% define by \marg{var} value of \marg{dimension1} as last skip between text lines
-% and deactivate |\check@@baselineskip@@skip| itself.
-%
-% \begin{macrocode}
-\def\check@@baselineskip@@skip#1#2#3{%
- \ifdim#1>\z@
- \ifdim#2>\z@
- \global#3=\the#1%
- \global\let\check@@baselineskip@@skip\@gobblethree
- \fi
- \fi
- }
-\let\initial@check@@baselineskip@@skip\check@@baselineskip@@skip
-% \end{macrocode}
-%
-% In the case we found footnote it is necessary to look for the last skip
-% between text lines again. For that we use
-% |\reset@check@@baselineskip@@skip|. It activates |\check@@baselineskip@@skip|.
-%
-% \begin{macrocode}
-\def\reset@check@@baselineskip@@skip#1#2#3{%
- \global#3=\z@
- \global\let\check@@baselineskip@@skip\initial@check@@baselineskip@@skip
- }
-% \end{macrocode}
-% \end{macro}
-%
-% \begin{macro}{\analyze@rightcolumn@box}\marg{box}
-%
-% Some guess of possible float or section title at the top of right column.\break
-% Fills:
-% \begin{itemize}
-% \item float height to |\top@@floatbox@ht|
-% \item float skip to |\top@@floatbox@skip|
-% \item head skip to |\top@@section@skip|
-% \end{itemize}
-%
-% \begin{macrocode}
+
\def\analyze@rightcolumn@box#1{%
+ %%
+ %% Looking for the top floats on the right column.
+ %%
+ %% Top box is considered as float if box's height
+ %% is larger then \top@@floatbox@min.
+ %% So, it is a dangerous feature to be used carefully.
+ %%
\bgroup
\xdef\top@@floatbox@ht{\z@}%
- \xdef\top@@floatbox@skip{\z@}%
- \xdef\top@@section@skip{\z@}%
\set@split@topskip@to\z@
\setbox\flushend@@varbox@a\vbox{\strut
\vskip\z@
\unvcopy#1%
}%
\setbox\flushend@@tempbox@c\vsplit\flushend@@varbox@a to\p@
- \wlog@balance@debug{^^J(R)mainbox: \log@box@info{#1}}%
\flushend@@lastskip@a\ht\flushend@@varbox@a
\advance\flushend@@lastskip@a\dp\flushend@@varbox@a
\ifdim\flushend@@lastskip@a>\z@
- \analyze@nonempty@rightcolumn@box
- \fi
- \egroup
- }
-\def\analyze@nonempty@rightcolumn@box{%
- \bgroup
- \var@@temp@a3\p@
- \loop
- \flushend@@lastskip@b\ht\flushend@@varbox@a
- \advance\flushend@@lastskip@b\dp\flushend@@varbox@a
- \setbox\flushend@@tempbox@c\vsplit\flushend@@varbox@a to\var@@temp@a
- \ifvoid\flushend@@tempbox@c
- \advance \var@@temp@a 3\p@
- \ifdim\var@@temp@a>\flushend@@lastskip@b
- \var@@temp@a=-1\p@
- \fi
- \else
- \setbox\flushend@@tempbox@c\vbox{\unvbox\flushend@@tempbox@c}%
- \ifdim\ht\flushend@@tempbox@c>\top@@floatbox@min
- \advance\flushend@@lastskip@b -\ht\flushend@@tempbox@c
- \advance\flushend@@lastskip@b -\dp\flushend@@tempbox@c
- \advance\flushend@@lastskip@b -\ht\flushend@@varbox@a
- \advance\flushend@@lastskip@b -\dp\flushend@@varbox@a
- \xdef\top@@floatbox@skip{\the\flushend@@lastskip@b}%
- \flushend@@lastskip@b\flushend@@lastskip@a
- \advance\flushend@@lastskip@b -\ht\flushend@@varbox@a
- \advance\flushend@@lastskip@b -\top@@floatbox@skip
- \xdef\top@@floatbox@ht{\the\flushend@@lastskip@b}%
- \var@@temp@a=3\p@
- \else
- \advance\flushend@@lastskip@b -\ht\flushend@@tempbox@c
- \advance\flushend@@lastskip@b -\dp\flushend@@tempbox@c
- \advance\flushend@@lastskip@b -\ht\flushend@@varbox@a
- \advance\flushend@@lastskip@b -\dp\flushend@@varbox@a
- \ifdim\ht\flushend@@tempbox@c>\topskip
- \else
- \xdef\top@@section@skip{\the\flushend@@lastskip@b}%
- \fi
- \var@@temp@a=-1\p@
- \fi
- \fi
- \ifdim \var@@temp@a>\z@
- \repeat
- \ifvoid\flushend@@tempbox@c
- \else
- \ifdim\top@@section@skip>\z@
- \else
- \ifdim\ht\flushend@@tempbox@c>\z@
- \setbox\flushend@@varbox@a\vbox{\unvbox\flushend@@tempbox@c}%
- \fi
+ %% analyze non empty rightcolumn box
+ \var@@temp@a=3\p@
+ \loop
+ \setbox\flushend@@tempbox@c\vsplit\flushend@@varbox@a to\var@@temp@a
\flushend@@lastskip@b\ht\flushend@@varbox@a
- \var@@temp@a=3\p@
- \loop
- \setbox\flushend@@tempbox@c\vsplit\flushend@@varbox@a to\var@@temp@a
- \ifvoid\flushend@@tempbox@c
- \advance \var@@temp@a 3\p@
- \ifdim\var@@temp@a>\flushend@@lastskip@b
- \var@@temp@a=-1\p@
- \fi
- \else
- \ifdim\ht\flushend@@tempbox@c<\z@
+ \advance\flushend@@lastskip@b\dp\flushend@@varbox@a
+ \ifvoid\flushend@@tempbox@c
+ \advance\var@@temp@a by3\p@
+ \ifdim\var@@temp@a>\flushend@@lastskip@b
+ %% reach column end, break loop
+ \var@@temp@a=-1\p@
+ \fi
+ \else
+ \setbox\flushend@@tempbox@c\vbox{\unvbox\flushend@@tempbox@c}%
+ \ifdim\ht\flushend@@tempbox@c>\top@@floatbox@min
+ %% found float; set height; look for the next
+ \setbox\flushend@@tempbox@a\vbox{\splitdiscards}%
+ \ifvoid\flushend@@tempbox@a
\else
- \var@@temp@a=-1\p@
+ \advance\flushend@@lastskip@b \ht\flushend@@tempbox@a
+ \advance\flushend@@lastskip@b \dp\flushend@@tempbox@a
\fi
- \fi
- \ifdim \var@@temp@a>\z@
- \repeat
- \loop
- \setbox\flushend@@varbox@a\vbox{%
- \unvbox\flushend@@tempbox@c
- \unskip\unkern\unpenalty
- \unskip\unkern\unpenalty
- \unskip\unkern\unpenalty
- \setbox\flushend@@tempbox@c\lastbox
- }%
- \flushend@@lastskip@b\ht\flushend@@varbox@a
- \setbox\flushend@@varbox@a\vbox{%
- \unvbox\flushend@@varbox@a
- \unskip\unkern\unpenalty
- \unskip\unkern\unpenalty
- \unskip\unkern\unpenalty
- }%
- \advance\flushend@@lastskip@b -\ht\flushend@@varbox@a
- \ifdim\flushend@@lastskip@b>\topskip
- \xdef\top@@section@skip{\the\flushend@@lastskip@b}%
- \fi
- \ifdim\var@@temp@loop=\ht\flushend@@varbox@a\relax
- \advance\var@@loop@iter\@ne
- \fi
- \var@@temp@loop=\ht\flushend@@varbox@a\relax
- \ifnum\var@@loop@iter<3\relax
- \wlog@balance@debug{analyze right col: iteration \number\var@@loop@iter; ht \the\var@@temp@loop}%
+ \advance\flushend@@lastskip@b -\flushend@@lastskip@a
+ \flushend@@lastskip@b=-\flushend@@lastskip@b
+ \xdef\top@@floatbox@ht{\the\flushend@@lastskip@b}%
+ \var@@temp@a=3\p@
\else
- \wlog@balance@debug{analyze right col loop limit!}%
- \ht\flushend@@varbox@a=\z@
+ %% break loop
+ \var@@temp@a=-1\p@
\fi
- \ifdim\ht\flushend@@varbox@a>2\topskip
- \setbox\flushend@@tempbox@c\vbox{\unvbox\flushend@@varbox@a}%
- \repeat
- \fi
+ \fi
+ \ifdim \var@@temp@a>\z@
+ \repeat
\fi
\egroup
}
-% \end{macrocode}
-% \end{macro}
-%
-% \begin{macro}{\combine@columns@for@balance}
-%
-% Collects both columns to one box removing left column footnotes
-% and right column top float and adding right skip in between them.
-%
-% \begin{macrocode}
-\def\combine@columns@for@balance{%
- \setbox\@outputbox\vbox{\strut
- \vskip\z@
- \unvbox\@outputbox
- }%
- \set@split@topskip@to\z@
- \setbox\flushend@@tempbox@c\vsplit\@outputbox to\p@
- \if@auto@baselineskip
- %% Trying guess baselineskip value on column break
- \ifdim\top@@floatbox@skip>\z@
- \setbox\flushend@@tempbox@c\vsplit\@outputbox to\top@@floatbox@ht
- \setbox\flushend@@tempbox@c\vbox{\unvbox\flushend@@tempbox@c}%
- \fi
- \setbox\@tempboxa\vbox{%
- \unvbox\flushend@@varbox@a
- \unskip\unkern\unpenalty
- \hrule\@height\flushend@@page@rule width\columnwidth
- \vskip-\flushend@@page@rule
- \hrule\@height\z@ width\columnwidth
- \vskip\flushend@@lastskip@c
- \the\atColsBreak
- \ifdim\top@@section@skip>\top@@skip@@limit
- \vskip\top@@section@skip
- \fi
- \unvbox\@outputbox
- \remove@lastbox@at@balancing
- \the\atColsEnd
- }%
- \ifdim\top@@floatbox@skip>\z@
- \setbox\@outputbox\vbox{\unvbox\flushend@@tempbox@c}%
- \fi
- \else
- %% Ignore baselineskip value on column break
- \setbox\@tempboxa\vbox{%
- \unvbox\flushend@@varbox@a
- \unskip\unkern\unpenalty
- \hrule\@height\flushend@@page@rule width\columnwidth
- \vskip-\flushend@@page@rule
- \hrule\@height\z@ width\columnwidth
- \the\atColsBreak
- \unvbox\@outputbox
- \remove@lastbox@at@balancing
- \the\atColsEnd
- }%
- \fi
- }
-% \end{macrocode}
-% \end{macro}
-%
-% \begin{macro}{\set@outputbox@with@footnote}
-%
-% Calculates break position and formats balanced columns in the |\@outputbox|
-% with footnotes.
-%
-% \begin{macrocode}
-\def\set@outputbox@with@footnote{%
- %% footnote in left column
- \@tempdimb\ht\@tempboxa
- \ifdim\top@@floatbox@skip>\z@
- \advance\@tempdimb \top@@floatbox@skip
+
+\def\set@outputbox@with@footnote@and@float{%
+ %%
+ %% The main unit formatting balanced columns.
+ %%
+ %% \flushend@@tempbox@a - left column
+ %% \flushend@@tempbox@c - left column with natural dimensions
+ %% \flushend@@varbox@a - right column
+ %% \flushend@@varbox@c - right column with natural dimensions
+ %% \flushend@@floatbox - rightcolumn top float
+ %% \@leftcolumn - void or leftcolumn footnote
+ %%
+ \@tempdimb\ht\flushend@@varbox@lastpage
+ \ifdim\top@@floatbox@ht>\z@
+ \advance\@tempdimb \textfloatsep
\advance\@tempdimb \top@@floatbox@ht
\fi
- \advance\@tempdimb \main@box@skip
- \advance\@tempdimb \ht\@leftcolumn
- \@tempdimb .5\@tempdimb
- \advance\@tempdimb -\main@box@skip
- \advance\@tempdimb -\ht\@leftcolumn
+ \ifdim\main@box@skip>\z@
+ \advance\@tempdimb \main@box@skip
+ \advance\@tempdimb \ht\@leftcolumn
+ \fi
+ \@tempdimb=.5\@tempdimb
+ \ifdim\@tempdimb>\@colht
+ \@tempdimb=\@colht
+ \fi
+ \ifdim\main@box@skip>\z@
+ \advance\@tempdimb -\main@box@skip
+ \advance\@tempdimb -\ht\@leftcolumn
+ \fi
\set@split@topskip@to\topskip
\loop
- \setbox\flushend@@varbox@a\copy\@tempboxa
+ \xdef\saved@tempdimb{\the\@tempdimb}%
+ \setbox\flushend@@varbox@a\copy\flushend@@varbox@lastpage
\setbox\flushend@@tempbox@a\vsplit\flushend@@varbox@a to\@tempdimb
- \setbox\flushend@@tempbox@c\vbox{\unvcopy\flushend@@tempbox@a}%
- \ifdim\top@@floatbox@skip>\z@
- \setbox\flushend@@varbox@c\vbox{%
- \unvcopy\@outputbox
- \vskip\top@@floatbox@skip
+ \ifdim\main@box@skip>\z@
+ \setbox\flushend@@tempbox@c\vbox{%
+ \unvbox\flushend@@tempbox@a
+ \vskip\main@box@skip
+ \vskip\z@ plus\p@
+ \unvcopy\@leftcolumn
+ }%
+ \else
+ \setbox\flushend@@tempbox@c\vbox{\unvbox\flushend@@tempbox@a}%
+ \fi
+ \setbox\flushend@@tempbox@a\vbox to\@tempdimb{\unvcopy\flushend@@tempbox@c}%
+ \xdef\var@@leftcolumn@badness{\the\badness}%
+ \ifdim\top@@floatbox@ht>\z@
+ \setbox\flushend@@varbox@c\vbox{%
+ \unvcopy\flushend@@floatbox
+ \vskip\textfloatsep
+ \vskip\z@ plus\p@ minus\p@
\unvbox\flushend@@varbox@a
}%
\else
\setbox\flushend@@varbox@c\vbox{\unvbox\flushend@@varbox@a}%
\fi
+ \setbox\flushend@@varbox@a\vbox to\@tempdimb{\unvcopy\flushend@@varbox@c}%
+ \xdef\var@@rightcolumn@badness{\the\badness}%
\var@@temp@spread=\ht\flushend@@tempbox@c
\advance\var@@temp@spread by\dp\flushend@@tempbox@c
- \advance\var@@temp@spread \main@box@skip
- \advance\var@@temp@spread \ht\@leftcolumn
\advance\var@@temp@spread by-\ht\flushend@@varbox@c
\advance\var@@temp@spread by-\dp\flushend@@varbox@c
- \wlog@balance@debug{Left x: \the\ht\flushend@@tempbox@a
- +\the\dp\flushend@@tempbox@a=\the\ht\flushend@@varbox@a
- +\the\dp\flushend@@varbox@a::\the\@tempdimb
- ^^JLeft 0: \the\ht\flushend@@tempbox@c
- +\the\dp\flushend@@tempbox@c::\the\@tempdimb
- ^^JRight x: \the\ht\flushend@@varbox@c
- +\the\dp\flushend@@varbox@c=\the\ht\flushend@@varbox@a
- +\the\dp\flushend@@varbox@a::\the\@tempdimb
- ^^JExtra height:\the\var@@temp@spread\space when \the\@tempdimb
- }%
+ \wlog@balance@debug{-----
+ ^^JLeft: \the\ht\flushend@@tempbox@c
+ +\the\dp\flushend@@tempbox@c\space / \var@@leftcolumn@badness
+ ^^JRight: \the\ht\flushend@@varbox@c
+ +\the\dp\flushend@@varbox@c\space / \var@@rightcolumn@badness
+ ^^JSplit: \the\@tempdimb
+ ^^JExtra height: \the\var@@temp@spread
+ }%
+ \ifdim\main@box@skip>\z@
+ \wlog@balance@debug{Footnote: \the\dimexpr\main@box@skip + \the\ht\@leftcolumn\relax}%
+ \fi
+ \ifdim\top@@floatbox@ht>\z@
+ \wlog@balance@debug{Float: \the\dimexpr\the\ht\flushend@@floatbox + \textfloatsep\relax}%
+ \fi
+ \ifnum\var@@leftcolumn@badness<\flushend@@column@badness
+ \ifnum\var@@rightcolumn@badness<\flushend@@column@badness
+ \ifdim\main@box@skip>\z@
+ \advance\@tempdimb -\main@box@skip
+ \advance\@tempdimb -\ht\@leftcolumn
+ \fi
+ \var@@temp@spread=\z@
+ \fi
+ \fi
+ \ifdim\var@@temp@spread<\z@
+ \if@left@column@spread
+ \if@right@column@shrink
+ \ifdim\dimexpr\var@@temp@spread+\pageshrink>\z@
+ \var@@temp@a=\dimexpr\ht\flushend@@tempbox@c-0.5\var@@temp@spread\relax
+ \else
+ \var@@temp@a=-\dimexpr\var@@temp@spread+.5\pageshrink\relax
+ \fi
+ \setbox\flushend@@varbox@a\vbox to\var@@temp@a{\unvcopy\flushend@@tempbox@c}%
+ \ifnum\badness<\flushend@@column@badness
+ \@tempdimb=\var@@temp@a
+ \ifdim\main@box@skip>\z@
+ \advance\@tempdimb -\main@box@skip
+ \advance\@tempdimb -\ht\@leftcolumn
+ \fi
+ \var@@temp@spread=\z@
+ \fi
+ \wlog{[leftcolumn badness]: \number\badness\space on \the\var@@temp@a}%
+ \setbox\flushend@@varbox@a\vbox to\var@@temp@a{\unvcopy\flushend@@varbox@c}%
+ \wlog{[rightcolumn badness]: \number\badness\space on \the\var@@temp@a}%
+ \else
+ \setbox\flushend@@varbox@a\vbox to\ht\flushend@@varbox@c{%
+ \unvcopy\flushend@@tempbox@c
+ }%
+ \ifnum\badness<\flushend@@column@badness
+ \@tempdimb=\ht\flushend@@varbox@c
+ \ifdim\main@box@skip>\z@
+ \advance\@tempdimb -\main@box@skip
+ \advance\@tempdimb -\ht\@leftcolumn
+ \fi
+ \var@@temp@spread=\z@
+ \fi
+ \wlog{[leftcolumn badness]: \number\badness on \the\ht\flushend@@varbox@c}%
+ \setbox\flushend@@varbox@a\vbox to\ht\flushend@@varbox@c{%
+ \unvcopy\flushend@@varbox@c
+ }%
+ \wlog{[rightcolumn badness]: \number\badness on \the\ht\flushend@@varbox@c}%
+ \fi
+ \else
+ \if@right@column@shrink
+ \ifdim\dimexpr\var@@temp@spread+\pageshrink<\z@
+ \else
+ \@tempdimb=\ht\flushend@@tempbox@c
+ \ifdim\main@box@skip>\z@
+ \advance\@tempdimb -\main@box@skip
+ \advance\@tempdimb -\ht\@leftcolumn
+ \fi
+ \var@@temp@spread=\z@
+ \fi
+ \fi
+ \fi
+ \else
+ \ifdim\var@@temp@spread>\z@
+ \ifdim\dimexpr\@tempdimb+\p@\relax>\@colht
+ \@tempdimb=\@colht
+ \ifdim\main@box@skip>\z@
+ \advance\@tempdimb -\main@box@skip
+ \advance\@tempdimb -\ht\@leftcolumn
+ \fi
+ \var@@temp@spread=\z@
+ \fi
+ \fi
+ \fi
\ifdim\var@@temp@spread<\z@
\advance\@tempdimb 1\p@
\repeat
- \flushend@@lastskip@a\the\ht\flushend@@tempbox@c
- \advance\flushend@@lastskip@a \main@box@skip
- \advance\flushend@@lastskip@a \ht\@leftcolumn
- \flushend@@lastskip@b\the\@tempdimb
- \setbox\flushend@@tempbox@a\vsplit\@tempboxa to\@tempdimb
- \setbox\@leftcolumn\vbox to\@tempdima{%
- \vbox to \flushend@@lastskip@a{%
- \unvbox\flushend@@tempbox@a
- \vskip\main@box@skip
- \unvbox\@leftcolumn
- \vss
- }%
- \hrule\@height\flushend@@page@rule width\textwidth
- \vskip-\flushend@@page@rule
- \hrule\@height\z@ width\columnwidth
- \vss
- }%
- \global\ht\@leftcolumn=\the\@tempdima
+ %% final construction of the lastpage
\wlog{- LAST -%
^^JExtra skip:\the\var@@temp@spread
- ^^JLeft:\the\ht\@leftcolumn/\the\dp\@leftcolumn
- ^^JRight:\the\ht\@tempboxa/\the\dp\@tempboxa
+ ^^JLeft:\the\ht\flushend@@tempbox@c/\the\dp\flushend@@tempbox@c
+ ^^JRight:\the\ht\flushend@@varbox@c/\the\dp\flushend@@varbox@c
+ ^^JSplit: \saved@tempdimb
^^JOutput:\the\@tempdimb
+ ^^JPageshrink: \the\pageshrink
+ ^^JPagestretch: \the\pagestretch
+ ^^J@colht:\the\@colht
}%
- \ifdim\top@@floatbox@skip>\z@
- \setbox\@outputbox\vbox to\@tempdima{%
- \if@right@column@spread
- \vbox spread\var@@temp@spread{%
- \unvbox\@outputbox
- \vskip\top@@floatbox@skip
- \unvbox\@tempboxa
- }%
- \else
- \vbox spread\var@@temp@spread{%
- \unvbox\@outputbox
- \vskip\top@@floatbox@skip
- \unvbox\@tempboxa
+ \setbox\flushend@@tempbox@c\vsplit\flushend@@varbox@lastpage to\saved@tempdimb
+ \ifdim\main@box@skip>\z@
+ \@tempdimb=\dimexpr\@tempdimb+\main@box@skip+\ht\@leftcolumn\relax
+ \setbox\@leftcolumn\vbox to\@tempdimb{%
+ \flushend@@lastskip@a=\dimexpr
+ \@tempdimb
+ -\ht\flushend@@tempbox@c
+ -\main@box@skip
+ -\ht\@leftcolumn
+ \relax
+ \unvbox\flushend@@tempbox@c
+ \vskip\main@box@skip
+ \vskip\z@ plus\p@
+ \unvbox\@leftcolumn
+ \prevdepth\z@
+ \hrule\@height\flushend@@page@rule width\textwidth
+ \vskip-\flushend@@page@rule
+ \ifdim\flushend@@lastskip@a>\z@
+ \if@left@column@spread
+ \else
\vss
- }%
+ \fi
+ \else
+ \ifdim\flushend@@lastskip@a<\z@
+ \if@left@column@shrink
+ \else
+ \vss
+ \fi
+ \fi
\fi
- \vss
}%
\else
- \setbox\@outputbox\vbox to\@tempdima{%
- \if@right@column@spread
- \vbox spread\var@@temp@spread{%
- \unvbox\@tempboxa
- }%
+ \setbox\@leftcolumn\vbox to\@tempdimb{%
+ \flushend@@lastskip@a=\dimexpr
+ \@tempdimb-\ht\flushend@@tempbox@c
+ \relax
+ \var@@temp@spread=\dp\flushend@@tempbox@c
+ \unvbox\flushend@@tempbox@c
+ \ifdim\var@@temp@spread>\dp\strutbox
\else
- \vbox spread\var@@temp@spread{%
- \unvbox\@tempboxa
+ \vskip-\var@@temp@spread
+ \fi
+ \prevdepth\z@
+ \hrule\@height\flushend@@page@rule width\textwidth
+ \vskip-\flushend@@page@rule
+ \ifdim\flushend@@lastskip@a>\z@
+ \if@left@column@spread
+ \else
\vss
- }%
+ \fi
+ \else
+ \ifdim\flushend@@lastskip@a<\z@
+ \if@left@column@shrink
+ \else
+ \vss
+ \fi
+ \fi
\fi
- \vss
}%
\fi
- }
-% \end{macrocode}
-% \end{macro}
-%
-% \begin{macro}{\set@outputbox@without@footnote}
-%
-% Calculates break position and formats balanced columns in the |\@outputbox|
-% without footnotes.
-%
-% \begin{macrocode}
-\def\set@outputbox@without@footnote{%
- %% no footnote in left column
- \@tempdimb \ht\@tempboxa
- \ifdim\top@@floatbox@skip>\z@
- \advance\@tempdimb \top@@floatbox@skip
- \advance\@tempdimb \top@@floatbox@ht
- \fi
- \@tempdimb .5\@tempdimb
- \set@split@topskip@to\topskip
- \loop
- \setbox\flushend@@varbox@a\copy\@tempboxa
- \setbox\flushend@@tempbox@a\vsplit\flushend@@varbox@a to\@tempdimb
- \setbox\flushend@@tempbox@c\vbox{\unvcopy\flushend@@tempbox@a}%
- \ifdim\top@@floatbox@skip>\z@
- \setbox\flushend@@varbox@c\vbox{%
- \unvcopy\@outputbox
- \vskip\top@@floatbox@skip
- \unvbox\flushend@@varbox@a
- }%
- \else
- \setbox\flushend@@varbox@c\vbox{\unvbox\flushend@@varbox@a}%
- \fi
- \var@@temp@spread=\ht\flushend@@tempbox@c
- \advance\var@@temp@spread by\dp\flushend@@tempbox@c
- \advance\var@@temp@spread by-\ht\flushend@@varbox@c
- \advance\var@@temp@spread by-\dp\flushend@@varbox@c
- \wlog@balance@debug{Left x: \the\ht\flushend@@tempbox@a
- +\the\dp\flushend@@tempbox@a=\the\ht\flushend@@varbox@a
- +\the\dp\flushend@@varbox@a::\the\@tempdimb
- ^^JLeft 0: \the\ht\flushend@@tempbox@c
- +\the\dp\flushend@@tempbox@c::\the\@tempdimb
- ^^JRight x: \the\ht\flushend@@varbox@c
- +\the\dp\flushend@@varbox@c=\the\ht\flushend@@varbox@a
- +\the\dp\flushend@@varbox@a::\the\@tempdimb
- ^^JExtra height:\the\var@@temp@spread\space when \the\@tempdimb
- }%
- \ifdim\var@@temp@spread<\z@ \advance\@tempdimb 1\p@ \repeat
- \wlog{- LAST -%
- ^^JExtra skip:\the\var@@temp@spread
- ^^JLeft:\the\ht\flushend@@tempbox@c/\the\dp\flushend@@tempbox@c
- ^^JRight:\the\ht\flushend@@varbox@c/\the\dp\flushend@@varbox@c
- ^^JOutput:\the\@tempdimb
- }%
- \setbox\flushend@@tempbox@c\vsplit\@tempboxa to\@tempdimb
- \setbox\flushend@@varbox@c\vbox{\unvbox\flushend@@tempbox@c}%
- \setbox\@leftcolumn\vbox to\@tempdima{%
- \vbox to\@tempdimb{\unvbox\flushend@@varbox@c\vss}%
- \hrule\@height\flushend@@page@rule width\textwidth
- \vskip-\flushend@@page@rule
- \hrule\@height\z@ width\textwidth
- \vss
- }%
+ \wlog{FLUSHEND [output]: \the\@tempdimb
+ ^^JFLUSHEND [leftcolumn badness]: \number\badness
+ }%
\global\ht\@leftcolumn=\the\@tempdima
- \ifdim\top@@floatbox@skip>\z@
- \setbox\@outputbox\vbox to\@tempdima{%
- \if@right@column@spread
- \vbox spread\var@@temp@spread{%
- \unvbox\@outputbox
- \vskip\top@@floatbox@skip
- \unvbox\@tempboxa
- }%
- \else
- \vbox spread\var@@temp@spread{%
- \unvbox\@outputbox
- \vskip\top@@floatbox@skip
- \unvbox\@tempboxa
+ \ifdim\top@@floatbox@ht>\z@
+ \setbox\@outputbox\vbox to\@tempdimb{%
+ \flushend@@lastskip@a=\dimexpr
+ \@tempdimb
+ -\ht\flushend@@floatbox
+ -\textfloatsep
+ -\ht\flushend@@varbox@lastpage
+ \relax
+ \unvbox\flushend@@floatbox
+ \vskip\textfloatsep
+ \vskip\z@ plus\p@ minus\p@
+ \unvbox\flushend@@varbox@lastpage
+ \ifdim\flushend@@lastskip@a>\z@
+ \if@right@column@spread
+ \else
\vss
- }%
+ \fi
+ \else
+ \ifdim\flushend@@lastskip@a<\z@
+ \if@right@column@shrink
+ \else
+ \vss
+ \fi
+ \fi
\fi
- \vss
}%
\else
- \setbox\@outputbox\vbox to\@tempdima{%
- \if@right@column@spread
- \vbox spread\var@@temp@spread{%
- \unvbox\@tempboxa
- }%
- \else
- \vbox spread\var@@temp@spread{%
- \unvbox\@tempboxa
+ \setbox\@outputbox\vbox to\@tempdimb{%
+ \flushend@@lastskip@a=\dimexpr
+ \@tempdimb-\ht\flushend@@varbox@lastpage
+ \relax
+ \unvbox\flushend@@varbox@lastpage
+ \ifdim\flushend@@lastskip@a>\z@
+ \if@right@column@spread
+ \else
\vss
- }%
+ \fi
+ \else
+ \ifdim\flushend@@lastskip@a<\z@
+ \if@right@column@shrink
+ \else
+ \vss
+ \fi
+ \fi
\fi
- \vss
}%
\fi
+ \wlog{FUSHEND [rightcolumn badness]: \number\badness}%
+ \global\ht\@outputbox=\the\@tempdima
}
-% \end{macrocode}
-% \end{macro}
-% \begin{macro}{\last@outputdblcol@modern}
-%
-% Columns balancing of the twocolumn mode page (modern algorithm).
-% It is maybe not so safe for text moving or missing at all
-% as it uses some heuristic technique to find footnotes, last skip between lines
-% at left column and floats or section like heads at right column top
-% for replacing them (except heads of course) to other places in text.
-%
-% \begin{macrocode}
-\def\last@outputdblcol@modern{%
+
+\newdimen\oldbreak@skip
+
+\def\combine@columns@for@balance{%
+ %%
+ %% Combines lastpage columns for balancing.
+ %%
+ \setbox\flushend@@tempbox@a\vbox{\pagediscards}%
+ \oldbreak@skip=\ht\flushend@@tempbox@a
+ \advance\oldbreak@skip by\dp\flushend@@tempbox@a
+ \setbox\@outputbox\vbox{\strut
+ \vskip\z@
+ \unvbox\@outputbox
+ }%
+ \set@split@topskip@to\z@
+ %% Removes topskip
+ \setbox\flushend@@tempbox@c\vsplit\@outputbox to\p@
+ \ifdim\top@@floatbox@ht>\z@
+ %% removes top float from right column (if option enabled)
+ \setbox\flushend@@floatbox\vsplit\@outputbox to\top@@floatbox@ht
+ \setbox\flushend@@floatbox\vbox{\unvbox\flushend@@floatbox}%
+ \fi
+ \setbox\flushend@@varbox@lastpage\vbox{%
+ \remove@empty@lastbox@at@balancing\flushend@@varbox@a
+ \unvbox\flushend@@varbox@a
+ \hrule\@height\flushend@@page@rule width\columnwidth
+ \vskip-\flushend@@page@rule
+ \hrule\@height\z@ width\columnwidth
+ \penalty\z@
+ \ifdim\var@@temp@a>\z@
+ \ifdim\var@@temp@a>\dp\strutbox
+ \else
+ \vskip\var@@temp@a
+ \fi
+ \fi
+ \vskip\oldbreak@skip
+ \the\atColsBreak
+ \remove@empty@lastbox@at@balancing\@outputbox
+ \unvbox\@outputbox
+ \the\atColsEnd
+ }%
+ }
+
+\def\last@outputdblcol{%
+ %%
+ %% Redefined \@outputdblcol with columns balancing.
+ %%
\if@firstcolumn
\global \@firstcolumnfalse
\global\setbox\@leftcolumn\copy\@outputbox
@@ -1032,216 +906,57 @@
\advance\@tempdima by\dp\@leftcolumn
\set@split@topskip@to\topskip
\var@@temp@spread=\wd\@outputbox
- \get@@footnoterule@@box
- \xdef\main@box@height{\the\ht\@leftcolumn}%
+ \xdef\top@@floatbox@ht{\z@}%
\xdef\main@box@skip{\z@}%
- \reset@check@@baselineskip@@skip\relax\relax\flushend@@lastskip@c
- \setbox\flushend@@varbox@a\vbox{\unvcopy\@leftcolumn}%
- \var@@loop@iter\z@ \var@@temp@loop\z@
- \analyze@leftcolumn@box\flushend@@varbox@a
- \setbox\flushend@@varbox@a\vbox{\unvcopy\@outputbox}%
- \var@@loop@iter\z@ \var@@temp@loop\z@
- \analyze@rightcolumn@box\flushend@@varbox@a
- \ifdim\main@box@skip>\z@
- %% footnote in left column
- \set@split@topskip@to\z@
- \setbox\flushend@@varbox@a\vsplit\@leftcolumn to\main@box@height
- \else
- %% no footnote in left column
- \setbox\flushend@@varbox@a\box\@leftcolumn
- \fi
\ifdim\var@@temp@spread>\z@
%% rightcolumn non empty
- \combine@columns@for@balance
- \else
- %% rightcolumn empty
- \setbox\@tempboxa\vbox{%
- \unvbox\flushend@@varbox@a
- \remove@lastbox@at@balancing
- \the\atColsEnd
- }%
- \fi
- \ifdim\main@box@skip>\z@
- %% footnote in left column
- \set@outputbox@with@footnote
- \else
- %% no footnote in left column
- \set@outputbox@without@footnote
- \fi
- \setbox\@outputbox \vbox {%
- \hb@xt@\textwidth {%
- \hb@xt@\columnwidth {%
- \fmt@box@adds{\box\@leftcolumn}\hss}%
- \hfil
- \vrule \@width\columnseprule
- \hfil
- \hb@xt@\columnwidth {%
- \fmt@box@adds{\box\@outputbox}\hss}%
- }%
- }%
- \global\let\@outputdblcol\saved@orig@@outputdblcol
- \global\atColsEnd{}%
- \ifvoid\hold@viper
- \else
- \setbox\@outputbox\vbox{\box\hold@viper\box\@outputbox}%
- \fi
- \@combinedblfloats
- \csname @setmarks\endcsname
- \@outputpage
- \begingroup
- \@dblfloatplacement
- \@startdblcolumn
- \@whilesw\if@fcolmade\fi
- {\@outputpage\@startdblcolumn}%
- \ifvoid\@viper
- \else
- \global\setbox\@viper\vbox{%
- \vskip-\stripsep
- \unvbox\@viper
- }%
- \csname @viperoutput\endcsname
- \fi
- \endgroup
- \fi
- }
-% \end{macrocode}
-% \end{macro}
-% \begin{macro}{\last@outputdblcol@old}
-%
-% Columns balancing of the twocolumn mode page (ancient algorithm).
-% It is maybe not so good in some cases but maybe more safe for text
-% moving or missing at all.
-%
-% \begin{macrocode}
-\def\last@outputdblcol@ancient{%
- \if@firstcolumn
- \global\@firstcolumnfalse
- \global\setbox\@leftcolumn\box\@outputbox
- \else
- \global\@firstcolumntrue
- \@tempdima\ht\@leftcolumn
- \set@split@topskip@to\topskip
- \if@auto@baselineskip
- \setbox\@tempboxa\vbox{%
- \unvcopy\@leftcolumn
- \global\flushend@@lastskip@c\lastskip
- \global\flushend@@lastskip@b\lastskip
- \loop
- \global\flushend@@lastskip@a\flushend@@lastskip@b
- \unskip\unpenalty
- \global\flushend@@lastskip@b\lastskip
- \global\advance\flushend@@lastskip@c by\lastskip
- \ifdim\flushend@@lastskip@b=-\flushend@@lastskip@a
- \global\advance\flushend@@lastskip@c by-\lastskip
+ \setbox\flushend@@varbox@a\vbox{\unvcopy\@outputbox}%
+ \ifdim\dimexpr\@colht-\ht\flushend@@varbox@a>\z@\relax
+ \if@footnote@check
+ \get@@footnoterule@@box
+ \xdef\main@box@height{\the\ht\@leftcolumn}%
+ \setbox\flushend@@varbox@a\vbox{\unvcopy\@leftcolumn}%
+ \var@@loop@iter\z@ \var@@temp@loop\z@
+ \analyze@leftcolumn@box\flushend@@varbox@a
+ \fi
+ \ifdim\top@@floatbox@min<\maxdimen
+ \setbox\flushend@@varbox@a\vbox{\unvcopy\@outputbox}%
+ \var@@loop@iter\z@ \var@@temp@loop\z@
+ \analyze@rightcolumn@box\flushend@@varbox@a
+ \fi
+ \ifdim\main@box@skip>\z@
+ %% footnote in left column
+ \set@split@topskip@to\z@
+ \setbox\flushend@@varbox@a\vsplit\@leftcolumn to\main@box@height
\else
- \repeat
- \ifdim\flushend@@lastskip@b=-\flushend@@lastskip@a
- \ifdim\flushend@@lastskip@b=\z@
- \setbox\flushend@@varbox@a\lastbox
- \global\advance\flushend@@lastskip@c by\ht\flushend@@varbox@a
- \global\advance\flushend@@lastskip@c by\lastskip
- \unskip\unpenalty
- \setbox\flushend@@varbox@a\lastbox
- \global\advance\flushend@@lastskip@c by\dp\flushend@@varbox@a
- \else
- \global\flushend@@lastskip@c\lastskip
- \global\advance\flushend@@lastskip@c by\topskip
- \fi
+ %% no footnote in left column
+ \setbox\flushend@@varbox@a\box\@leftcolumn
\fi
- \global\advance\flushend@@lastskip@c by-\topskip
- }%
- \setbox\@tempboxa\vbox{%
- \unvbox\@leftcolumn
- \unskip\unpenalty
- \vskip\flushend@@lastskip@c
- \hrule\@height\flushend@@page@rule width\columnwidth
- \vskip-\flushend@@page@rule
- \hrule\@height\z@ width\textwidth
- \the\atColsBreak
- \unvbox\@outputbox
- \remove@lastbox@at@balancing
- \the\atColsEnd
- }%
- \else
- \setbox\@tempboxa\vbox{%
- \unvbox\@leftcolumn
- \setbox\z@\lastbox
- \unskip
- \hrule\@height\flushend@@page@rule width\columnwidth
- \vskip-\flushend@@page@rule
- \hrule\@height\z@ width\textwidth
- \the\atColsBreak
- \unvbox\@outputbox
- \remove@lastbox@at@balancing
- \the\atColsEnd
- }%
- \fi
- \@tempdimb .5\ht\@tempboxa
- \loop
- \setbox\flushend@@tempbox@a\copy\@tempboxa
- \setbox\flushend@@tempbox@c\vbox to\@tempdimb{%
- \vsplit\flushend@@tempbox@a to\@tempdimb
- \vss
- \vsplit\flushend@@tempbox@a to\@tempdimb
- }%
- \wlog{Extra height:\the\ht\flushend@@tempbox@a\space when \the\@tempdimb}%
- \ifvoid\flushend@@tempbox@a
+ \combine@columns@for@balance
+ \else
+ \xdef\main@box@skip{\maxdimen}%
+ \fi
\else
- \advance\@tempdimb 1\p@
- \repeat
- \loop
- \setbox\flushend@@tempbox@a\copy\@tempboxa
- \setbox\flushend@@tempbox@c\vbox to\@tempdimb{%
- \vsplit\flushend@@tempbox@a to\@tempdimb
- \vss
- }%
- \wlog{(2)Left:\the\ht\flushend@@tempbox@c\space
- Right:\the\ht\flushend@@tempbox@a\space
- Output:\the\@tempdimb
- }%
- \ifdim\ht\flushend@@tempbox@c<\ht\flushend@@tempbox@a
- \@tempdimb=\the\ht\flushend@@tempbox@a
- \repeat
- \wlog{- LAST -^^JExtra skip: \the\z@
- ^^JLeft:\the\ht\flushend@@tempbox@c
- ^^JRight:\the\ht\flushend@@tempbox@a
- ^^JOutput:\the\@tempdimb
- }%
- \setbox\flushend@@tempbox@c\vbox to\@tempdimb{%
- \vsplit\@tempboxa to\@tempdimb
- \vss
- }%
- \setbox\@leftcolumn\vbox to\@tempdima{%
- \fmt@vbox@adds[\columnwidth]{%
- \vbox to\@tempdimb{%
- \unvbox
- \flushend@@tempbox@c
+ %% rightcolumn empty
+ \setbox\flushend@@varbox@a\box\@leftcolumn
+ \setbox\flushend@@varbox@lastpage\vbox{%
+ \remove@empty@lastbox@at@balancing\flushend@@varbox@a
+ \unvbox\flushend@@varbox@a
+ \the\atColsEnd
}%
- }%
- \hrule\@height\flushend@@page@rule
- \vskip-\flushend@@page@rule
- \hrule\@height\z@ width\textwidth
- \vss
- }%
- \setbox\@outputbox\vbox to\@tempdima{%
- \fmt@vbox@adds[\columnwidth]{%
- \vbox to\@tempdimb{%
- \unvbox\@tempboxa
- \vskip\z@
- }%
- }%
- \hrule\@height\flushend@@page@rule
- \vskip-\flushend@@page@rule
- \hrule\@height\z@ width\textwidth
- \vss
- }%
+ \fi
+ \ifdim\main@box@skip<\maxdimen
+ \set@outputbox@with@footnote@and@float
+ \fi
\setbox\@outputbox\vbox{%
\hb@xt@\textwidth{%
- \hb@xt@\columnwidth{\box\@leftcolumn\hss}%
+ \hb@xt@\columnwidth{%
+ \fmt@box@adds{\box\@leftcolumn}\hss}%
\hfil
\vrule \@width\columnseprule
\hfil
- \hb@xt@\columnwidth{\box\@outputbox\hss}%
+ \hb@xt@\columnwidth{%
+ \fmt@box@adds{\box\@outputbox}\hss}%
}%
}%
\global\let\@outputdblcol\saved@orig@@outputdblcol
@@ -1251,6 +966,7 @@
\setbox\@outputbox\vbox{\box\hold@viper\box\@outputbox}%
\fi
\@combinedblfloats
+ \csname @setmarks\endcsname
\@outputpage
\begingroup
\@dblfloatplacement
@@ -1268,61 +984,29 @@
\endgroup
\fi
}
-% \end{macrocode}
-% \end{macro}
-%
-% Adds default balancing of the last page at the end of document.
-%
-% \begin{macrocode}
-\usepackage{etoolbox}
-\AtBeginDocument{%
+
+\def\initialize@flushend{%
+ %%
+ %% Activates flushend package functionality.
+ %%
\global\let\saved@orig@@outputdblcol\@outputdblcol
- \if@ancient@balance@version
- \global\let\last@outputdblcol\last@outputdblcol@ancient
- \else
- \global\let\last@outputdblcol\last@outputdblcol@modern
- \fi
\global\let\balanced@@outputdblcol\last@outputdblcol
- \preto\enddocument{\let\@outputdblcol\balanced@@outputdblcol}%
- \appto\footnoterule{\vskip\z@}%
+ \edef\enddocument{%
+ \unexpanded{\let\@outputdblcol\balanced@@outputdblcol}%
+ \unexpanded\expandafter{\enddocument}%
+ }%
+ \g@addto@macro\footnoterule{\vskip\z@}%
+ \savingvdiscards=\@ne
}
-% \end{macrocode}
-%
-% User interface commands for the balancing pages.
-%
-% \cs{flushcolsend} is used to switch on column balancing on the current page.
-%
-% \begin{macrocode}
+
\def\flushcolsend{\global\let\@outputdblcol\last@outputdblcol}
-% \end{macrocode}
-%
-% \cs{raggedcolsend} is used to switch off column balancing on the current page.
-%
-% \begin{macrocode}
\def\raggedcolsend{\global\let\@outputdblcol\saved@orig@@outputdblcol}
-% \end{macrocode}
-%
-% \cs{flushend} is used to switch on column balancing on the last page.
-% It is used by default.
-%
-% \begin{macrocode}
\def\flushend{\global\let\balanced@@outputdblcol\last@outputdblcol}
-% \end{macrocode}
-%
-% \cs{raggedend} is used to switch off column balancing on the last page.
-%
-% \begin{macrocode}
\def\raggedend{\global\let\balanced@@outputdblcol\saved@orig@@outputdblcol}
-% \end{macrocode}
-%
-% In the case to see some debug information |debug| option is used and it is
-% possible to see how package calculates dimensions of balanced columns by
-% additional rules on balanced page and some log records in log file.
-%
-% \begin{macrocode}
\if@balance@debug
\showcolsendrule
\fi
+\AtBeginDocument{\initialize@flushend}
% \end{macrocode}
%
% \iffalse