summaryrefslogtreecommitdiff
path: root/Master
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2022-06-22 20:52:52 +0000
committerKarl Berry <karl@freefriends.org>2022-06-22 20:52:52 +0000
commite30bffce416c228b04a7418a047408d3c0ae08d5 (patch)
tree9d427681546fdd04ecd51f2a95c5f4a5f2229fc8 /Master
parent8371bb42081e6a6b466faf47b7d97ec9d5f68af9 (diff)
pbalance (22jun22)
git-svn-id: svn://tug.org/texlive/trunk@63692 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master')
-rw-r--r--Master/texmf-dist/doc/latex/pbalance/LICENSE2
-rw-r--r--Master/texmf-dist/doc/latex/pbalance/README.md2
-rw-r--r--Master/texmf-dist/doc/latex/pbalance/pbalance.pdfbin242951 -> 39562 bytes
-rw-r--r--Master/texmf-dist/source/latex/pbalance/pbalance.dtx322
-rw-r--r--Master/texmf-dist/source/latex/pbalance/pbalance.ins4
-rw-r--r--Master/texmf-dist/tex/latex/pbalance/pbalance.sty237
6 files changed, 471 insertions, 96 deletions
diff --git a/Master/texmf-dist/doc/latex/pbalance/LICENSE b/Master/texmf-dist/doc/latex/pbalance/LICENSE
index a953e0b7097..8447540ad06 100644
--- a/Master/texmf-dist/doc/latex/pbalance/LICENSE
+++ b/Master/texmf-dist/doc/latex/pbalance/LICENSE
@@ -1,4 +1,4 @@
-Copyright 2020-2021 Nelson Lago <lago@ime.usp.br>
+Copyright 2020-2022 Nelson Lago <lago@ime.usp.br>
This work may be distributed and/or modified under the conditions of the
LaTeX Project Public License, either version 1.3c of this license or (at
diff --git a/Master/texmf-dist/doc/latex/pbalance/README.md b/Master/texmf-dist/doc/latex/pbalance/README.md
index 93f1e7c969a..2d1137b196d 100644
--- a/Master/texmf-dist/doc/latex/pbalance/README.md
+++ b/Master/texmf-dist/doc/latex/pbalance/README.md
@@ -13,7 +13,7 @@ the columns. There are, however, three caveats:
Code etc: <https://gitlab.com/lago/pbalance>
-Copyright 2020-2021 Nelson Lago <lago@ime.usp.br>
+Copyright 2020-2022 Nelson Lago <lago@ime.usp.br>
This work may be distributed and/or modified under the conditions of the
LaTeX Project Public License, either version 1.3c of this license or (at
diff --git a/Master/texmf-dist/doc/latex/pbalance/pbalance.pdf b/Master/texmf-dist/doc/latex/pbalance/pbalance.pdf
index 43fc1c40f86..d8ddbb38f0d 100644
--- a/Master/texmf-dist/doc/latex/pbalance/pbalance.pdf
+++ b/Master/texmf-dist/doc/latex/pbalance/pbalance.pdf
Binary files differ
diff --git a/Master/texmf-dist/source/latex/pbalance/pbalance.dtx b/Master/texmf-dist/source/latex/pbalance/pbalance.dtx
index 9f984e0e114..b5712f00c61 100644
--- a/Master/texmf-dist/source/latex/pbalance/pbalance.dtx
+++ b/Master/texmf-dist/source/latex/pbalance/pbalance.dtx
@@ -1,5 +1,5 @@
% \iffalse meta-comment
-% Copyright 2020-2021 Nelson Lago <lago@ime.usp.br>
+% Copyright 2020-2022 Nelson Lago <lago@ime.usp.br>
%
% This work may be distributed and/or modified under the conditions of the
% LaTeX Project Public License, either version 1.3c of this license or (at
@@ -15,7 +15,7 @@
% \iffalse
%
%<package>\NeedsTeXFormat{LaTeX2e}[2015/01/01]
-%<package>\ProvidesPackage{pbalance}[2021/05/24 v1.1.1 Poor man's balance]
+%<package>\ProvidesPackage{pbalance}[2022/06/22 v1.3.0 Poor man's balance]
%
%<*driver>
@@ -34,6 +34,8 @@
anchorcolor=black,
}
+\usepackage{microtype}
+
\usepackage[draft]{pbalance}
\usepackage{libertinus}
@@ -60,6 +62,14 @@
% \changes{v1.1.0}{2021/04/26}{Do not crash in one-column mode}
% \changes{v1.1.1}{2021/05/24}{Do not use balance package if
% there are dbltop floats}
+% \changes{v1.2.0}{2022/06/20}{Improvements to latexmk file}
+% \changes{v1.2.0}{2022/06/20}{More logging and sanity checks}
+% \changes{v1.2.0}{2022/06/20}{Add ``safe'' option}
+% \changes{v1.2.0}{2022/06/20}{Do not use \texttt{balance} pkg
+% with \texttt{lineno}}
+% \changes{v1.2.0}{2022/06/20}{Compatibility with \texttt{lineno} pkg}
+% \changes{v1.2.1}{2022/06/21}{Better error handling}
+% \changes{v1.3.0}{2022/06/22}{Compatibility with \texttt{stfloats} pkg}
%
% \GetFileInfo{pbalance.sty}
%
@@ -126,11 +136,14 @@
%
% If the last page does not have floats, footnotes, or marginpars, the
% package simply uses \verb|\balance|, from the \texttt{balance} package,
-% otherwise it uses its own algorithm. You may instead use
+% otherwise it uses its own algorithm. If that fails for some reason, you
+% may add the \verb|safe| option to the package, which makes the code
+% never use (or even load) the \texttt{balance} package. You may also use
% \verb|\shrinkLastPage{some-measurement}| to manually define how much
-% shorter the first column of the last page should be. This, however, will
-% not eliminate the need for extra passes (the package needs to find out
-% which page is the last).
+% shorter the first column of the last page should be instead of letting
+% the package determine the amount. This, however, will not eliminate the
+% need for extra passes (the package needs to find out which \mbox{page is
+% the last).}\looseness=1
%
% The package actually balances the last \emph{text} page; if the last
% page is a float page, it is ignored. It also \emph{should} work if you
@@ -157,7 +170,7 @@
% to give up on balancing (the document is still generated correctly
% in this case). To minimize the chances of this outcome, it is
% probably a good idea to make the parameters a little more liberal
-% than the default, for example:
+% than the default, for example:\looseness=-1
%
% \begin{verbatim}
%\setcounter{totalnumber}{5} % default 3
@@ -215,9 +228,6 @@
%
% \begin{itemize}
%
-% \item add options to (1) allow using the balance package on pages with
-% floats and (2) completely disable the balance package;
-%
% \item make the mechanism able to work with any page, not just the last,
% and with more than one page at once. This is useful for the last
% page of each chapter of a two-column book, for example;
@@ -256,9 +266,9 @@
% \subsection{Initialization, required packages etc.}
%
% Besides some other required packages, here we load the \texttt{balance}
-% package; if there are no floats or footnotes involved, we will simply use
-% it. We need to load it after we modify \verb|\@outputdblcol|, so we use
-% \verb|\AtEndOfPackage|.
+% package; if there are no floats or footnotes involved and if the document
+% does not load \texttt{lineno.sty}, we will simply use it. We need to load
+% it after we modify \verb|\@outputdblcol|, so we use \verb|\AtEndOfPackage|.
%
% We also define the \texttt{draft} option, which disables processing.
%
@@ -269,23 +279,43 @@
\RequirePackage{atbegshi} % Manipulate the page output routine
\RequirePackage{atveryend} % Write to the aux file after processing ends
\RequirePackage{zref-abspage} % Figure out the current page
+\RequirePackage{filehook} % Patch other packages that modify the output routine
-\AtEndOfPackage{\RequirePackage{balance}}
-
-% See https://github.com/latex3/latex2e/issues/399#issuecomment-703081793
-\gdef \@reinserts{%
- \ifvbox\@kludgeins\insert\@kludgeins
- {\unvbox\@kludgeins}\fi
- \ifvoid\footins\else\insert\footins{\unvbox\footins}\fi
-}
+\newtoggle{@PBpatchFailed}
+% These may be already defined in the .aux file from a previous run
\providetoggle{@PBstabilized}
\providetoggle{@PBimpossible}
+\newtoggle{@PBnoBalancePackage}
+\DeclareOption{safe}{\toggletrue{@PBnoBalancePackage}}
+
\newtoggle{@PBdraft}
\DeclareOption{draft}{\toggletrue{@PBdraft}}
+
\ProcessOptions\relax
+% lineno may be loaded after us, so we need to check for it later...
+\AtEndPreamble{\@ifpackageloaded{lineno}{\toggletrue{@PBnoBalancePackage}}{}}
+
+% ... But it does not harm to check here too, which may save us
+% the trouble of needlessly loading the balance package only to
+% never use it later on. If we get it wrong, that's ok.
+\@ifpackageloaded{lineno}{\toggletrue{@PBnoBalancePackage}}{}
+
+\AtEndOfPackage{
+ \ifboolexpr{togl {@PBdraft} or togl {@PBnoBalancePackage}}
+ {}
+ {\RequirePackage{balance}}
+}
+
+% See https://github.com/latex3/latex2e/issues/399#issuecomment-703081793
+\gdef \@reinserts{%
+ \ifvbox\@kludgeins\insert\@kludgeins
+ {\unvbox\@kludgeins}\fi
+ \ifvoid\footins\else\insert\footins{\unvbox\footins}\fi
+}
+
% \end{macrocode}
% \subsection{The balancing front}
@@ -333,7 +363,10 @@
\newcommand\@PBStartBalancing{
\ifdefvoid{\@PBslack}
{\@PBautomaticBalance}
- {\@PBshrinkPage}
+ {
+ \PackageInfo{pbalance}{Automatic mode disabled, doing manual adjustment}
+ \@PBshrinkPage
+ }
}
\newcommand\@PBautomaticBalance{
@@ -348,10 +381,10 @@
\@PBcalculateShrinkage
\@PBshrinkPage
}
- {}
+ {\PackageInfo{pbalance}{Page "naturally" balanced, doing nothing}}
}
}
- {}
+ {\PackageInfo{pbalance}{Float-only column at last page, doing nothing}}
}
% Calculate how much we should shrink the left column of the last
@@ -370,8 +403,8 @@
% Figure out the amount of unused space on the last page (both
% cols); we will reduce the first column height by half that amount
- \dimgdef\@PBslack{2\dimexpr\@PBtmpH\relax - \@PBtmpUL - \@PBtmpUR}
- \dimgdef\@PBslack{.5\dimexpr\@PBslack\relax}
+ \dimgdef\@PBslack{\@PBtmpH *2 - \@PBtmpUL - \@PBtmpUR}
+ \dimgdef\@PBslack{\@PBslack /2}
% Actually, I lied. While half, as said above, sometimes yields
% perfectly balanced columns, that is often not the case (the page
@@ -408,7 +441,12 @@
% ``push'' them up to be aligned with the end of the left column,
% especially if there are footnotes on the left column too. We
% do this by manipulating \@textbottom in \@outputdblcol.
-\pretocmd{\@outputdblcol}{\@PBbalanceSecondColumn}{}{}
+\pretocmd{\@outputdblcol}{\@PBbalanceSecondColumn}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
\let\@PBbalanceSecondColumn\relax
@@ -466,7 +504,6 @@
\newtoggle{@PBtmpHasMarginpars}
\def\@PBcollectPageInfo{
-
\ifboolexpr
{
test {\@PBifmidfloat} or test {\@PBiftopfloat} or
@@ -481,10 +518,20 @@
{}
}
-\pretocmd{\@makecol}{\@PBcollectPageInfo}{}{}
+\pretocmd{\@makecol}{\@PBcollectPageInfo}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
% This only gets called if there is a marginpar, so that's easy
-\pretocmd{\@addmarginpar}{\global\toggletrue{@PBtmpHasMarginpars}}{}{}
+\pretocmd{\@addmarginpar}{\global\toggletrue{@PBtmpHasMarginpars}}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
% \end{macrocode}
%
@@ -524,8 +571,19 @@
}
\ExplSyntaxOff
-\pretocmd{\@addtocurcol}{\dimgdef{\@PBtmpHeightBefore}{\@textfloatsheight}}{}{}
-\apptocmd{\@addtocurcol}{\@PBcollectFloatHeights}{}{}
+\pretocmd{\@addtocurcol}{\dimgdef{\@PBtmpHeightBefore}{\@textfloatsheight}}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+
+\apptocmd{\@addtocurcol}{\@PBcollectFloatHeights}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
% \end{macrocode}
%
@@ -539,8 +597,58 @@
\newtoggle{@PBtmpHasFloatcol}
\newtoggle{@PBtmpIsFloatPage}
+% It would be nice to modify @makecol to do something like
+%
+% if@firstcolumn
+% @PBtmpUsedLeft = currentColumnHeight
+% else
+% @PBtmpUsedRight = currentColumnHeight
+%
+% Unfortunately, as stated in texdoc source2e on the section about the
+% output routine (ltoutput.dtx) around source line 273, "any conditional
+% code for the two-column case within output may not get executed with
+% the correct value of if@firstcolumn."
+%
+% Another approach, then, would be to defer the measurement of the
+% columns to @outputdblcol, where there are @leftcolumn and @outputbox.
+% This works, unless the package lineno was loaded: this package modifies
+% @makecol, preventing us from obtaining the correct height for the
+% output boxes.
+%
+% What we do, then, is (1) collect the column height in @makecol and
+% (2) decide whether the collected height corresponds to the left or
+% right column in @outputdblcol.
+\newcommand\@PBcollectColumnUsedHeight{
+ \setbox\@tempboxa=\vbox{\unvcopy\@outputbox\unskip\unskip}
+ \dimgdef\@PBtmpColumnUsedHeight{\ht\@tempboxa\relax}
+}
+
+\apptocmd{\@makecol}{\@PBcollectColumnUsedHeight}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+
+\newtoggle{@PBpatchStillActive} % Let's make sure our patched code actually runs
+
\newcommand\@PBcollectColumnInfo{
+ \global\toggletrue{@PBpatchStillActive}
+
+ \iftoggle{@PBpatchFailed}
+ {}
+ {
+ \ifdefvoid{\@PBtmpColumnUsedHeight}
+ {
+ \global\toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch disabled by another
+ package, won't balance}
+ }
+ {\@PBreallyCollectColumnInfo}
+ }
+}
+\newcommand\@PBreallyCollectColumnInfo{
\if@firstcolumn
\@PBiffloatcol
@@ -556,6 +664,7 @@
% Space used by \texttt{here} floats
\dimgdef\@PBtmpLeftFloatsHeight{\@textfloatsheight}
+ \dimgdef\@PBtmpUsedLeft{\@PBtmpColumnUsedHeight}
\else
@@ -569,26 +678,106 @@
\dimgdef\@PBtmpRightHeight{\@colroom}
\dimgdef\@PBtmpRightFloatsHeight{\@textfloatsheight}
-
- \setbox\@tempboxa=\copy\@leftcolumn
- \setbox\@tempboxa=\vbox{\unvbox\@tempboxa\unskip\unskip}
- \dimgdef\@PBtmpUsedLeft{\ht\@tempboxa\relax}
-
- \setbox\@tempboxa=\copy\@outputbox
- \setbox\@tempboxa=\vbox{\unvbox\@tempboxa\unskip\unskip}
- \dimgdef\@PBtmpUsedRight{\ht\@tempboxa\relax}
+ \dimgdef\@PBtmpUsedRight{\@PBtmpColumnUsedHeight}
\fi
}
-% The default LaTeX macro
-\pretocmd{\@outputdblcol}{\@PBcollectColumnInfo}{}{}
+% Now we patch the default @outputdblcol LaTeX macro...
+\pretocmd{\@outputdblcol}{\@PBcollectColumnInfo}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+
+% ... And the balance package version, but only if we actually loaded it
+\ifboolexpr{togl {@PBdraft} or togl {@PBnoBalancePackage}}
+ {}
+ {
+ \AtEndOfPackage{
+ \pretocmd{\@BAdblcol}{\@PBcollectColumnInfo}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ % Revert \balance before next page if it exists (it might be a float page)
+ \patchcmd{\@BAdblcol}{\endgroup}{\endgroup\nobalance}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ }
+ }
-% The version from the balance package
-\AtEndOfPackage{
- \pretocmd{\@BAdblcol}{\@PBcollectColumnInfo}{}{}
- % Revert \balance before next page if it exists (it might be a float page)
- \patchcmd{\@BAdblcol}{\endgroup}{\endgroup\nobalance}{}{}
-}
+% \end{macrocode}
+%
+% \subsection{Compatibility with other packages}
+%
+% We mostly modify stuff from the output routine to collect data, not to
+% change what is typeset (the only exception is @textbottom, which we
+% manipulate to align footnotes). This means that, if some other package
+% modifies the output routine as well, we will not break anything, which
+% is why this package is mostly safe to use. Unfortunately, such other
+% packages may prevent us from balancing. Here we try to play nice with
+% some of them.
+%
+% \subsubsection{stfloats}
+%
+% \begin{macrocode}
+
+% stfloats overwrites @addtocurcol; if it is already loaded, we already
+% patched the modified version, so we only need to run this if the package
+% is loaded later on. Therefore, we use AtEndOfPackageFile without *.
+\AtEndOfPackageFile{stfloats}
+ {
+
+ \pretocmd{\@addtocurcol}{\dimgdef{\@PBtmpHeightBefore}{\@textfloatsheight}}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+
+ \apptocmd{\@addtocurcol}{\@PBcollectFloatHeights}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ }
+
+% We always need to patch these, so this code should run whether the
+% package is already loaded or gets loaded later on. Therefore, we
+% use AtEndOfPackageFile with *.
+\AtEndOfPackageFile*{stfloats}
+ {
+ \pretocmd{\fn@makecol}{\@PBcollectPageInfo}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ \pretocmd{\org@makecol}{\@PBcollectPageInfo}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ \apptocmd{\fn@makecol}{\@PBcollectColumnUsedHeight}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ \apptocmd{\org@makecol}{\@PBcollectColumnUsedHeight}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ }
% \end{macrocode}
%
@@ -600,8 +789,22 @@
%
% \begin{macrocode}
-\ExplSyntaxOn
\AtBeginShipout{
+ \iftoggle{@PBpatchFailed}
+ {}
+ {
+ \iftoggle{@PBpatchStillActive}
+ {\@PBfinalizePageData}
+ {
+ \global\toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch disabled by another
+ package, won't balance}
+ }
+ }
+}
+
+\ExplSyntaxOn
+\newcommand\@PBfinalizePageData{
% It only makes sense to consider pages that actually have two
% columns. LaTeX3 does not do short-circuit evaluation, so we
% cannot add this test to the ifboolexpr below.
@@ -656,7 +859,7 @@
% document, there is no candidate page.
\ifcsdef{@PBlastPage}
{\@PBcopyPageData{candidate}{pg\@Roman{\@PBlastPage}}}
- {}
+ {\PackageInfo{pbalance}{No two-column pages, doing nothing}}
}
% \end{macrocode}
@@ -696,8 +899,16 @@
% so the user can start over if they want to.
\iftoggle{@PBdraft}{\togglefalse{@PBimpossible}}{}
- \ifboolexpr{togl {@PBdraft} or togl {@PBimpossible}}
- {}
+ \ifboolexpr{togl {@PBdraft} or togl {@PBimpossible} or togl {@PBpatchFailed}}
+ {
+ \iftoggle{@PBimpossible}
+ {\PackageWarningNoLine{pbalance}{Could not balance, doing nothing}}
+ {
+ \iftoggle{@PBpatchFailed}
+ {\PackageWarningNoLine{pbalance}{Patching failed, doing nothing}}
+ {\PackageInfo{pbalance}{Draft mode, doing nothing}}
+ }
+ }
{
\ifdefvoid{\@PBprevLastPage}
{\@PBsaveUnbalancedInfo\@PBnotifyRerun} % First pass
@@ -783,6 +994,8 @@
togl {@PBunbalpg\@Roman{\@PBprevLastPage}HasFootnotes}
or
togl {@PBunbalpg\@Roman{\@PBprevLastPage}HasMarginpars}
+ or
+ togl {@PBnoBalancePackage}
}
{#2}
{#1}
@@ -1027,7 +1240,10 @@
\PackageWarningNoLine{pbalance}
{Last two-column page cols not balanced. Rerun LaTeX}
}
- {}
+ {
+ \PackageWarningNoLine{pbalance}
+ {No two-column pages, doing nothing}
+ }
}
}
diff --git a/Master/texmf-dist/source/latex/pbalance/pbalance.ins b/Master/texmf-dist/source/latex/pbalance/pbalance.ins
index 4dbd564c672..980cf5050fd 100644
--- a/Master/texmf-dist/source/latex/pbalance/pbalance.ins
+++ b/Master/texmf-dist/source/latex/pbalance/pbalance.ins
@@ -1,4 +1,4 @@
-%% Copyright 2020-2021 Nelson Lago <lago@ime.usp.br>
+%% Copyright 2020-2022 Nelson Lago <lago@ime.usp.br>
%%
%% This work may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License, either version 1.3c of this license or (at
@@ -16,7 +16,7 @@
\usedir{tex/latex/pbalance}
\preamble
-Copyright 2020-2021 Nelson Lago <lago@ime.usp.br>
+Copyright 2020-2022 Nelson Lago <lago@ime.usp.br>
This work may be distributed and/or modified under the conditions of the
LaTeX Project Public License, either version 1.3c of this license or (at
diff --git a/Master/texmf-dist/tex/latex/pbalance/pbalance.sty b/Master/texmf-dist/tex/latex/pbalance/pbalance.sty
index 1b54a4f76c6..6cab784cb9d 100644
--- a/Master/texmf-dist/tex/latex/pbalance/pbalance.sty
+++ b/Master/texmf-dist/tex/latex/pbalance/pbalance.sty
@@ -5,7 +5,7 @@
%% The original source files were:
%%
%% pbalance.dtx (with options: `package')
-%% Copyright 2020-2021 Nelson Lago <lago@ime.usp.br>
+%% Copyright 2020-2022 Nelson Lago <lago@ime.usp.br>
%%
%% This work may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License, either version 1.3c of this license or (at
@@ -18,29 +18,44 @@
%% The Current Maintainer of this work is Nelson Lago <lago@ime.usp.br>.
%%
\NeedsTeXFormat{LaTeX2e}[2015/01/01]
-\ProvidesPackage{pbalance}[2021/05/24 v1.1.1 Poor man's balance]
+\ProvidesPackage{pbalance}[2022/06/22 v1.3.0 Poor man's balance]
\RequirePackage{etoolbox}
\RequirePackage{expl3}
\RequirePackage{atbegshi} % Manipulate the page output routine
\RequirePackage{atveryend} % Write to the aux file after processing ends
\RequirePackage{zref-abspage} % Figure out the current page
+\RequirePackage{filehook} % Patch other packages that modify the output routine
-\AtEndOfPackage{\RequirePackage{balance}}
-
-\gdef \@reinserts{%
- \ifvbox\@kludgeins\insert\@kludgeins
- {\unvbox\@kludgeins}\fi
- \ifvoid\footins\else\insert\footins{\unvbox\footins}\fi
-}
+\newtoggle{@PBpatchFailed}
\providetoggle{@PBstabilized}
\providetoggle{@PBimpossible}
+\newtoggle{@PBnoBalancePackage}
+\DeclareOption{safe}{\toggletrue{@PBnoBalancePackage}}
+
\newtoggle{@PBdraft}
\DeclareOption{draft}{\toggletrue{@PBdraft}}
+
\ProcessOptions\relax
+\AtEndPreamble{\@ifpackageloaded{lineno}{\toggletrue{@PBnoBalancePackage}}{}}
+
+\@ifpackageloaded{lineno}{\toggletrue{@PBnoBalancePackage}}{}
+
+\AtEndOfPackage{
+ \ifboolexpr{togl {@PBdraft} or togl {@PBnoBalancePackage}}
+ {}
+ {\RequirePackage{balance}}
+}
+
+\gdef \@reinserts{%
+ \ifvbox\@kludgeins\insert\@kludgeins
+ {\unvbox\@kludgeins}\fi
+ \ifvoid\footins\else\insert\footins{\unvbox\footins}\fi
+}
+
\AtBeginDocument{
@@ -76,7 +91,10 @@
\newcommand\@PBStartBalancing{
\ifdefvoid{\@PBslack}
{\@PBautomaticBalance}
- {\@PBshrinkPage}
+ {
+ \PackageInfo{pbalance}{Automatic mode disabled, doing manual adjustment}
+ \@PBshrinkPage
+ }
}
\newcommand\@PBautomaticBalance{
@@ -91,10 +109,10 @@
\@PBcalculateShrinkage
\@PBshrinkPage
}
- {}
+ {\PackageInfo{pbalance}{Page "naturally" balanced, doing nothing}}
}
}
- {}
+ {\PackageInfo{pbalance}{Float-only column at last page, doing nothing}}
}
\newcommand\@PBcalculateShrinkage{
@@ -111,8 +129,8 @@
% Figure out the amount of unused space on the last page (both
% cols); we will reduce the first column height by half that amount
- \dimgdef\@PBslack{2\dimexpr\@PBtmpH\relax - \@PBtmpUL - \@PBtmpUR}
- \dimgdef\@PBslack{.5\dimexpr\@PBslack\relax}
+ \dimgdef\@PBslack{\@PBtmpH *2 - \@PBtmpUL - \@PBtmpUR}
+ \dimgdef\@PBslack{\@PBslack /2}
% Actually, I lied. While half, as said above, sometimes yields
% perfectly balanced columns, that is often not the case (the page
@@ -145,7 +163,12 @@
}
}
-\pretocmd{\@outputdblcol}{\@PBbalanceSecondColumn}{}{}
+\pretocmd{\@outputdblcol}{\@PBbalanceSecondColumn}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
\let\@PBbalanceSecondColumn\relax
@@ -180,7 +203,6 @@
\newtoggle{@PBtmpHasMarginpars}
\def\@PBcollectPageInfo{
-
\ifboolexpr
{
test {\@PBifmidfloat} or test {\@PBiftopfloat} or
@@ -195,9 +217,19 @@
{}
}
-\pretocmd{\@makecol}{\@PBcollectPageInfo}{}{}
+\pretocmd{\@makecol}{\@PBcollectPageInfo}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
-\pretocmd{\@addmarginpar}{\global\toggletrue{@PBtmpHasMarginpars}}{}{}
+\pretocmd{\@addmarginpar}{\global\toggletrue{@PBtmpHasMarginpars}}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
\ExplSyntaxOn
@@ -232,15 +264,55 @@
}
\ExplSyntaxOff
-\pretocmd{\@addtocurcol}{\dimgdef{\@PBtmpHeightBefore}{\@textfloatsheight}}{}{}
-\apptocmd{\@addtocurcol}{\@PBcollectFloatHeights}{}{}
+\pretocmd{\@addtocurcol}{\dimgdef{\@PBtmpHeightBefore}{\@textfloatsheight}}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+
+\apptocmd{\@addtocurcol}{\@PBcollectFloatHeights}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
\newtoggle{@PBtmpHasFloatcol}
\newtoggle{@PBtmpIsFloatPage}
+\newcommand\@PBcollectColumnUsedHeight{
+ \setbox\@tempboxa=\vbox{\unvcopy\@outputbox\unskip\unskip}
+ \dimgdef\@PBtmpColumnUsedHeight{\ht\@tempboxa\relax}
+}
+
+\apptocmd{\@makecol}{\@PBcollectColumnUsedHeight}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+
+\newtoggle{@PBpatchStillActive} % Let's make sure our patched code actually runs
+
\newcommand\@PBcollectColumnInfo{
+ \global\toggletrue{@PBpatchStillActive}
+
+ \iftoggle{@PBpatchFailed}
+ {}
+ {
+ \ifdefvoid{\@PBtmpColumnUsedHeight}
+ {
+ \global\toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch disabled by another
+ package, won't balance}
+ }
+ {\@PBreallyCollectColumnInfo}
+ }
+}
+\newcommand\@PBreallyCollectColumnInfo{
\if@firstcolumn
\@PBiffloatcol
@@ -256,6 +328,7 @@
% Space used by \texttt{here} floats
\dimgdef\@PBtmpLeftFloatsHeight{\@textfloatsheight}
+ \dimgdef\@PBtmpUsedLeft{\@PBtmpColumnUsedHeight}
\else
@@ -269,28 +342,101 @@
\dimgdef\@PBtmpRightHeight{\@colroom}
\dimgdef\@PBtmpRightFloatsHeight{\@textfloatsheight}
-
- \setbox\@tempboxa=\copy\@leftcolumn
- \setbox\@tempboxa=\vbox{\unvbox\@tempboxa\unskip\unskip}
- \dimgdef\@PBtmpUsedLeft{\ht\@tempboxa\relax}
-
- \setbox\@tempboxa=\copy\@outputbox
- \setbox\@tempboxa=\vbox{\unvbox\@tempboxa\unskip\unskip}
- \dimgdef\@PBtmpUsedRight{\ht\@tempboxa\relax}
+ \dimgdef\@PBtmpUsedRight{\@PBtmpColumnUsedHeight}
\fi
}
-\pretocmd{\@outputdblcol}{\@PBcollectColumnInfo}{}{}
+\pretocmd{\@outputdblcol}{\@PBcollectColumnInfo}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
-\AtEndOfPackage{
- \pretocmd{\@BAdblcol}{\@PBcollectColumnInfo}{}{}
- % Revert \balance before next page if it exists (it might be a float page)
- \patchcmd{\@BAdblcol}{\endgroup}{\endgroup\nobalance}{}{}
-}
+\ifboolexpr{togl {@PBdraft} or togl {@PBnoBalancePackage}}
+ {}
+ {
+ \AtEndOfPackage{
+ \pretocmd{\@BAdblcol}{\@PBcollectColumnInfo}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ % Revert \balance before next page if it exists (it might be a float page)
+ \patchcmd{\@BAdblcol}{\endgroup}{\endgroup\nobalance}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ }
+ }
+
+
+\AtEndOfPackageFile{stfloats}
+ {
+
+ \pretocmd{\@addtocurcol}{\dimgdef{\@PBtmpHeightBefore}{\@textfloatsheight}}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+
+ \apptocmd{\@addtocurcol}{\@PBcollectFloatHeights}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ }
+
+\AtEndOfPackageFile*{stfloats}
+ {
+ \pretocmd{\fn@makecol}{\@PBcollectPageInfo}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ \pretocmd{\org@makecol}{\@PBcollectPageInfo}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ \apptocmd{\fn@makecol}{\@PBcollectColumnUsedHeight}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ \apptocmd{\org@makecol}{\@PBcollectColumnUsedHeight}
+ {}
+ {
+ \toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch failed, won't balance}
+ }
+ }
-\ExplSyntaxOn
\AtBeginShipout{
+ \iftoggle{@PBpatchFailed}
+ {}
+ {
+ \iftoggle{@PBpatchStillActive}
+ {\@PBfinalizePageData}
+ {
+ \global\toggletrue{@PBpatchFailed}
+ \PackageWarningNoLine{pbalance}{Patch disabled by another
+ package, won't balance}
+ }
+ }
+}
+
+\ExplSyntaxOn
+\newcommand\@PBfinalizePageData{
% It only makes sense to consider pages that actually have two
% columns. LaTeX3 does not do short-circuit evaluation, so we
% cannot add this test to the ifboolexpr below.
@@ -334,7 +480,7 @@
% document, there is no candidate page.
\ifcsdef{@PBlastPage}
{\@PBcopyPageData{candidate}{pg\@Roman{\@PBlastPage}}}
- {}
+ {\PackageInfo{pbalance}{No two-column pages, doing nothing}}
}
@@ -343,8 +489,16 @@
% so the user can start over if they want to.
\iftoggle{@PBdraft}{\togglefalse{@PBimpossible}}{}
- \ifboolexpr{togl {@PBdraft} or togl {@PBimpossible}}
- {}
+ \ifboolexpr{togl {@PBdraft} or togl {@PBimpossible} or togl {@PBpatchFailed}}
+ {
+ \iftoggle{@PBimpossible}
+ {\PackageWarningNoLine{pbalance}{Could not balance, doing nothing}}
+ {
+ \iftoggle{@PBpatchFailed}
+ {\PackageWarningNoLine{pbalance}{Patching failed, doing nothing}}
+ {\PackageInfo{pbalance}{Draft mode, doing nothing}}
+ }
+ }
{
\ifdefvoid{\@PBprevLastPage}
{\@PBsaveUnbalancedInfo\@PBnotifyRerun} % First pass
@@ -420,6 +574,8 @@
togl {@PBunbalpg\@Roman{\@PBprevLastPage}HasFootnotes}
or
togl {@PBunbalpg\@Roman{\@PBprevLastPage}HasMarginpars}
+ or
+ togl {@PBnoBalancePackage}
}
{#2}
{#1}
@@ -593,7 +749,10 @@
\PackageWarningNoLine{pbalance}
{Last two-column page cols not balanced. Rerun LaTeX}
}
- {}
+ {
+ \PackageWarningNoLine{pbalance}
+ {No two-column pages, doing nothing}
+ }
}
}