| 1 |
% \iffalse meta-comment |
| 2 |
% Copyright 2020-2022 Nelson Lago <[email protected]> |
| 3 |
% |
| 4 |
% This work may be distributed and/or modified under the conditions of the |
| 5 |
% LaTeX Project Public License, either version 1.3c of this license or (at |
| 6 |
% your option) any later version. The latest version of this license can be |
| 7 |
% found at http://www.latex-project.org/lppl.txt and version 1.3 or later |
| 8 |
% is part of all distributions of LaTeX version 2005/12/01 or later. |
| 9 |
% |
| 10 |
% This work has the LPPL maintenance status `maintained'. |
| 11 |
% |
| 12 |
% The Current Maintainer of this work is Nelson Lago <[email protected]>. |
| 13 |
% |
| 14 |
% \fi |
| 15 |
% \iffalse |
| 16 |
% |
| 17 |
%<package>\NeedsTeXFormat{LaTeX2e}[2015/01/01] |
| 18 |
%<package>\ProvidesPackage{pbalance}[2022/07/28 v1.4.0 Poor man's balance] |
| 19 |
% |
| 20 |
%<*driver> |
| 21 |
|
| 22 |
\documentclass{ltxdoc} |
| 23 |
|
| 24 |
\usepackage[hyperref,svgnames,x11names,table]{xcolor} |
| 25 |
\usepackage{url} |
| 26 |
\urlstyle{sf} |
| 27 |
\usepackage{hyperref} |
| 28 |
\hypersetup{ |
| 29 |
colorlinks=true, |
| 30 |
citecolor=DarkGreen, |
| 31 |
linkcolor=NavyBlue, |
| 32 |
urlcolor=DarkRed, |
| 33 |
filecolor=green, |
| 34 |
anchorcolor=black, |
| 35 |
} |
| 36 |
|
| 37 |
\usepackage{microtype} |
| 38 |
|
| 39 |
\usepackage[draft]{pbalance} |
| 40 |
|
| 41 |
\usepackage{libertinus} |
| 42 |
\usepackage[scale=.85]{sourcecodepro} |
| 43 |
|
| 44 |
%%\EnableCrossrefs |
| 45 |
%%\CodelineIndex |
| 46 |
\RecordChanges |
| 47 |
|
| 48 |
\OnlyDescription |
| 49 |
|
| 50 |
\begin{document} |
| 51 |
\DocInput{pbalance.dtx} |
| 52 |
\end{document} |
| 53 |
|
| 54 |
%</driver> |
| 55 |
% |
| 56 |
% \fi |
| 57 |
% |
| 58 |
% \CheckSum{0} |
| 59 |
% |
| 60 |
% \changes{v1.0}{2020/09/14}{Initial version} |
| 61 |
% \changes{v1.0.1}{2020/12/16}{Improvements to documentation} |
| 62 |
% \changes{v1.1.0}{2021/04/26}{Do not crash in one-column mode} |
| 63 |
% \changes{v1.1.1}{2021/05/24}{Do not use balance package if |
| 64 |
% there are dbltop floats} |
| 65 |
% \changes{v1.2.0}{2022/06/20}{Improvements to latexmk file} |
| 66 |
% \changes{v1.2.0}{2022/06/20}{More logging and sanity checks} |
| 67 |
% \changes{v1.2.0}{2022/06/20}{Add ``safe'' option} |
| 68 |
% \changes{v1.2.0}{2022/06/20}{Do not use \texttt{balance} pkg |
| 69 |
% with \texttt{lineno}} |
| 70 |
% \changes{v1.2.0}{2022/06/20}{Compatibility with \texttt{lineno} pkg} |
| 71 |
% \changes{v1.2.1}{2022/06/21}{Better error handling} |
| 72 |
% \changes{v1.3.0}{2022/06/22}{Compatibility with \texttt{stfloats} pkg} |
| 73 |
% \changes{v1.4.0}{2022/07/28}{Fix bug when switching single/double cols} |
| 74 |
% \changes{v1.4.0}{2022/07/28}{Add alternatives and troubleshooting |
| 75 |
% sections to the docs} |
| 76 |
% \changes{v1.4.0}{2022/07/28}{Create \texttt{\textbackslash{}balancePageNum} |
| 77 |
% and \texttt{\textbackslash{}nopbalance} |
| 78 |
% commands} |
| 79 |
% \changes{v1.4.0}{2022/07/28}{Fix bug: detect float pages correctly} |
| 80 |
% \changes{v1.4.0}{2022/07/28}{Compatibility with memoir, footmisc, ftnright, |
| 81 |
% flafter, fnpos, midfloat, and cuted} |
| 82 |
% |
| 83 |
% \GetFileInfo{pbalance.sty} |
| 84 |
% |
| 85 |
% \title{The \textsf{pbalance} (poor man's balance) package\thanks{This |
| 86 |
% document corresponds to \textsf{pbalance}~\fileversion, dated~\filedate.}} |
| 87 |
% |
| 88 |
% \author{ |
| 89 |
% Nelson Lago\\ |
| 90 |
% \texttt{[email protected]}\\ |
| 91 |
% ~\\ |
| 92 |
% \url{https://gitlab.com/lago/pbalance} |
| 93 |
%} |
| 94 |
% |
| 95 |
% \maketitle |
| 96 |
% |
| 97 |
% \begin{abstract} |
| 98 |
% |
| 99 |
% This package tries to \emph{safely} make the columns on the last page |
| 100 |
% of a two-column document have approximately the same height. It should |
| 101 |
% ``just work'' without user intervention, which is particularly useful |
| 102 |
% for class authors, but also offers the user a command to adjust the |
| 103 |
% height of the columns. There are, however, three caveats: |
| 104 |
% |
| 105 |
% \begin{enumerate} |
| 106 |
% \item Results are adequate, but often not optimal; |
| 107 |
% \item In some very rare cases, the package may give up (the document |
| 108 |
% is generated correctly but the last page is not balanced); |
| 109 |
% \item The package demands additional \LaTeX{} passes. |
| 110 |
% \end{enumerate} |
| 111 |
% |
| 112 |
% \end{abstract} |
| 113 |
% |
| 114 |
% \section{Introduction} |
| 115 |
% |
| 116 |
% First things first: this package is a \textbf{hack}. It is also |
| 117 |
% \textbf{beta} quality. However, I believe it is \emph{safe}, |
| 118 |
% i.e., it should not generate incorrect output. |
| 119 |
% |
| 120 |
% In a two-column document, it is desirable for the columns in the last |
| 121 |
% page to have the same height (the columns should be ``balanced''). Two |
| 122 |
% packages (\texttt{balance} and \texttt{flushend}) try to provide this |
| 123 |
% in \LaTeX, but both may generate defective output in some circumstances. |
| 124 |
% This package tries to solve this problem. |
| 125 |
% |
| 126 |
% Balancing is often not possible: imagine a document with an odd number |
| 127 |
% of lines on the last page, or with ten lines of text together with an |
| 128 |
% image that takes the height of fifteen lines of text, or a document where |
| 129 |
% making columns the same height would leave a sectioning comand at the |
| 130 |
% bottom of the first column etc. Since such cases happen often, we consider |
| 131 |
% it enough for the columns to be ``not too unequal'', even when equal |
| 132 |
% heights might be possible (the algorithm cannot deliver better results |
| 133 |
% and, in fact, striving to make columns exactly the same height with it |
| 134 |
% may sometimes yield poor results, so we do not even try). |
| 135 |
% |
| 136 |
% \section{Usage} |
| 137 |
% |
| 138 |
% To use the package, add \verb|\usepackage{pbalance}| to the preamble |
| 139 |
% and things should ``just work''. However, the document will take more |
| 140 |
% \LaTeX{} passes to compile, and minute changes to it that would normally |
| 141 |
% need one additional \LaTeX{} pass will likely demand three. If you load |
| 142 |
% the package with the \verb|draft| option (or pass the \verb|draft| |
| 143 |
% option to the document class), balancing is disabled, alleviating the |
| 144 |
% need for extra passes during document preparation. You may also use |
| 145 |
% \verb|\shrinkLastPage{some-measurement}| to manually define how much |
| 146 |
% shorter the first column of the last page should be instead of letting |
| 147 |
% the package determine the amount and \verb|\balancePageNum{page-number}| |
| 148 |
% to force balancing a specific page (note that this is the \textit{physical} |
| 149 |
% page number). These options, however, will not eliminate the need |
| 150 |
% for extra passes. Finally, you may disable processing by calling |
| 151 |
% \verb|\nopbalance| (useful if a class preloaded the package, for example). |
| 152 |
% |
| 153 |
% \section{Troubleshooting and fine tuning} |
| 154 |
% |
| 155 |
% \texttt{pbalance} should never generate defective output or cause an |
| 156 |
% error; if it does, please try passing the \texttt{safe} option to the |
| 157 |
% package and submit a bug report with a MWE. Other than that, three |
| 158 |
% things may go wrong when using \texttt{pbalance}: |
| 159 |
% |
| 160 |
% \begin{enumerate} |
| 161 |
% \item The final result may be ``ugly''; |
| 162 |
% \item \texttt{pbalance} may give up and produce an unbalanced last page; |
| 163 |
% \item Some other package may prevent \texttt{pbalance} from working. |
| 164 |
% \end{enumerate} |
| 165 |
% |
| 166 |
% \subsection{The final result is ``ugly''} |
| 167 |
% |
| 168 |
% If you find the results unsatisfactory, there are two things you may try: |
| 169 |
% |
| 170 |
% \begin{itemize} |
| 171 |
% \item Change the layout: apply \verb|\looseness| to a paragraph, move |
| 172 |
% where floats are defined\footnote{Remember that, in two-column |
| 173 |
% mode, dblfloats should be defined before the place you expect |
| 174 |
% them to appear; see the docs for the \texttt{stfloats} package.} |
| 175 |
% or change their sizes, etc.; |
| 176 |
% \item Use \verb|\shrinkLastPage| and \verb|\balancePageNum| to manually |
| 177 |
% define the size of the left column and the page to balance. |
| 178 |
% \end{itemize} |
| 179 |
% |
| 180 |
% \subsection{\texttt{pbalance} gave up} |
| 181 |
% |
| 182 |
% This should happen very rarely, but \texttt{pbalance} may sometimes |
| 183 |
% generate the warning ``\texttt{Could not balance, doing nothing}'' |
| 184 |
% (the document is still generated correctly in this case), which means |
| 185 |
% the package algorithm failed to find a suitable solution to do its |
| 186 |
% thing\footnote{For example, imagine a document in which page 5 is |
| 187 |
% a float page that happens to be the last one. We will balance page |
| 188 |
% 4, which is the last text page. Balancing page 4 may move a float |
| 189 |
% from the left to the right column, but this may in turn exceed the |
| 190 |
% maximum number of floats in the right column. As a result, one or |
| 191 |
% more floats may be deferred to page 5. At the same time, a float of |
| 192 |
% a different kind from page 5 may be pulled back to the extra space |
| 193 |
% that just opened up in page 4. Depending on the size of this float, |
| 194 |
% the text may no longer fit in page 4, making some text spill onto |
| 195 |
% page 5. This means page 5 is now the last text page and should then |
| 196 |
% be balanced instead of page 4. Oops!\looseness=-1}. This problem is |
| 197 |
% amenable to the same mitigation strategies discussed in the previous |
| 198 |
% section. However, if there are many floats near the end of the document, |
| 199 |
% you should also consider making the \LaTeX{} float placement parameters |
| 200 |
% (\verb|\topfraction|, \verb|topnumber|, etc.) a little more liberal |
| 201 |
% than the default, for example:\looseness=-1 |
| 202 |
% |
| 203 |
% \begin{verbatim} |
| 204 |
%\setcounter{totalnumber}{5} % default 3 |
| 205 |
%\setcounter{topnumber}{3} % default 2 |
| 206 |
%\renewcommand{\topfraction}{.85} % default .7 |
| 207 |
%\renewcommand{\textfraction}{.15} % default .2 |
| 208 |
%\renewcommand{\floatpagefraction}{.75} % default .5, must be < \topfraction |
| 209 |
% \end{verbatim} |
| 210 |
% |
| 211 |
% \subsection{Patching failed due to another package} |
| 212 |
%^^A TODO: list all other packages in a comment |
| 213 |
% |
| 214 |
% If \texttt{pbalance} yields the warning ``\texttt{Patching failed, cannot |
| 215 |
% balance; try \mbox{loading} pbalance earlier or later}'', there is some |
| 216 |
% incompatibility with another package. Since \texttt{pbalance} modifies |
| 217 |
% the internal \LaTeX{} macros \verb|\@outputdblcol|, \verb|\@addtocurcol|, |
| 218 |
% \verb|\@makecol|, and \verb|\@addmarginpar|, there may be problems with |
| 219 |
% other packages that do the same; thankfully, not many packages do. For |
| 220 |
% some of them (such as \texttt{stfloats}), \texttt{pbalance} includes code |
| 221 |
% to circumvent any problems; for others, your best bet is to change the |
| 222 |
% order in which the packages are loaded (in general, \texttt{pbalance} |
| 223 |
% prefers to go last).\looseness=-1 |
| 224 |
% |
| 225 |
% \subsection{Some details you normally do not need to consider} |
| 226 |
% |
| 227 |
% The package actually balances the last \emph{text} page; if the last |
| 228 |
% page is a float page, it is ignored. It also \emph{should} work if you |
| 229 |
% switch to single-column mode mid-document (with \verb|\onecolumn|); it |
| 230 |
% will then balance the last two-column text page. The same should happen |
| 231 |
% in a document that is mainly typeset in one column and you switch to |
| 232 |
% two-column layout (with \verb|\twocolumn|), but \emph{only} if the page |
| 233 |
% to be balanced does \emph{not} contain the \verb|\twocolumn| command |
| 234 |
% (i.e., there are at least two consecutive two-column text pages). |
| 235 |
% |
| 236 |
% If the last page does not have floats, footnotes, or marginpars, the |
| 237 |
% package simply uses \verb|\balance|, from the \texttt{balance} package, |
| 238 |
% otherwise it uses its own algorithm. If that fails for some reason, you |
| 239 |
% may add the \verb|safe| option to the package, which makes the code |
| 240 |
% never use (or even load) the \texttt{balance} package. |
| 241 |
% |
| 242 |
% \section{Other options} |
| 243 |
% |
| 244 |
% There are other approaches to balancing that may be better suited to your |
| 245 |
% needs. Beyond the already mentioned \texttt{balance} and \texttt{flushend} |
| 246 |
% packages, I am aware of: |
| 247 |
% |
| 248 |
% \begin{itemize} |
| 249 |
% \item The \texttt{multicol} package, which allows the user to freely |
| 250 |
% switch between one- and multi-column layouts. The only drawback |
| 251 |
% of the package is that it cannot handle ordinary floats, only |
| 252 |
% dblfloats (\verb|\begin{figure*}|, \verb|\begin{table*}|); |
| 253 |
% \item The \texttt{ltxgrid} package, which allows the user to freely |
| 254 |
% switch between one- and two-column layouts but, differently from |
| 255 |
% \texttt{multicol}, works even in the presence of floats. With it, |
| 256 |
% switching from two columns to one balances the previous output |
| 257 |
% (its main disadvantage is that it may have compatibility problems |
| 258 |
% with other packages); |
| 259 |
% \item The \texttt{revtex} class, for which \texttt{ltxgrid} was |
| 260 |
% developed; |
| 261 |
% \item The \verb|\IEEEtriggeratref{}| macro from the \texttt{IEEEtran} |
| 262 |
% class, which allows you to attach arbitrary code (such as |
| 263 |
% \verb|\newpage|) to a specific entry in a bibliography listing; |
| 264 |
% \looseness=-1 |
| 265 |
% \item The code suggested at \url{https://tex.stackexchange.com/a/583228/}, |
| 266 |
% similar to the \texttt{IEEEtran} approach. |
| 267 |
% \end{itemize} |
| 268 |
% |
| 269 |
% \section{How does it work} |
| 270 |
% |
| 271 |
% This package works on two fronts: |
| 272 |
% |
| 273 |
% \begin{enumerate} |
| 274 |
% |
| 275 |
% \item It uses a \LaTeX{} pass to gather information: which is the last |
| 276 |
% two-column text page? Does it have floats? Footnotes? How much |
| 277 |
% free space in each column? |
| 278 |
% |
| 279 |
% \item It uses this information in a subsequent pass to balance that |
| 280 |
% page. If there are no floats or footnotes, it uses the balance |
| 281 |
% package, otherwise it uses the measurements collected to shrink |
| 282 |
% the first column (using \verb|\enlargethispage|) by a |
| 283 |
% ``reasonable'' amount. |
| 284 |
% |
| 285 |
% \end{enumerate} |
| 286 |
% |
| 287 |
% If measurements change between passes, either the document was edited |
| 288 |
% or \LaTeX{} is still adding crossrefs, citations, etc. In both cases, |
| 289 |
% the previously gathered data is bogus, so we start over. However, the |
| 290 |
% package cannot detect changes that happen between the ``collect data'' |
| 291 |
% pass and the ``balance for the first time'' pass: it has to assume |
| 292 |
% that any change at this point is caused by the fact that we started |
| 293 |
% balancing (it can, however, detect whether the last text page changed; |
| 294 |
% if this happens, we give up balancing). If a small change (such as an |
| 295 |
% updated crossref adding or removing a line somewhere) slips through, |
| 296 |
% that is acceptable: we are not aiming at balancing perfectly anyway. |
| 297 |
% However, if the change is big (for example, the addition of the |
| 298 |
% bibliography block after running bibtex/biber), results will suffer. |
| 299 |
% |
| 300 |
% To prevent this, it would be ideal to only start the process after all |
| 301 |
% passes needed to stabilize the document have run. In practice, however, |
| 302 |
% this would be too hard to detect (and demand even more passes). What |
| 303 |
% we do instead is simply wait for two consecutive passes to result in |
| 304 |
% columns of the same size in the last two-column page. When this happens, |
| 305 |
% we proceed to balance in the next pass and assume (quite reasonably) |
| 306 |
% that, as long as the document is not modified, the effect of concurrent |
| 307 |
% changes during that pass in the last page is small, so ignoring it |
| 308 |
% disturbs the balancing only slightly\footnote{Like I said on the |
| 309 |
% introduction: this package is a hack.}. |
| 310 |
% |
| 311 |
% \section{TODO / wish list} |
| 312 |
% |
| 313 |
% Some useful stuff we should consider implementing: |
| 314 |
% |
| 315 |
% \begin{itemize} |
| 316 |
% |
| 317 |
% \item make the mechanism able to work with any page, not just the last, |
| 318 |
% and with more than one page at once. This is useful for the last |
| 319 |
% page of each chapter of a two-column book, for example; |
| 320 |
% |
| 321 |
% \item on top of that: |
| 322 |
% \begin{enumerate} |
| 323 |
% \item create command to manually indicate the page numbers to balance |
| 324 |
% \item create command similar to \verb|\balance| from the |
| 325 |
% \texttt{balance} package |
| 326 |
% \item allow user to set manual balancing for each page independently |
| 327 |
% \end{enumerate} |
| 328 |
% |
| 329 |
% \end{itemize} |
| 330 |
% |
| 331 |
% \StopEventually{\PrintChanges} |
| 332 |
% |
| 333 |
% \section{The implementation} |
| 334 |
% |
| 335 |
% We define \verb|\@PBlastPage| using \texttt{zref-abspage}; It is the page |
| 336 |
% number for the last two-column page of text. This might \emph{not} be the |
| 337 |
% last page of the document: there may be one or more float pages after it |
| 338 |
% or the user may have issued \verb|\onecolumn|. Still, it \emph{is} the page |
| 339 |
% we want to balance. We do not act on it, however; instead, we save its value |
| 340 |
% to the aux file as \verb|\@PBprevLastPage| and proceed to balance on the next |
| 341 |
% \LaTeX{} pass. |
| 342 |
% |
| 343 |
% We also define two toggles: |
| 344 |
% |
| 345 |
% \begin{description} |
| 346 |
% \item[\texttt{@PBstabilized}] There have been a few passes |
| 347 |
% already and the document has apparently stabilized. |
| 348 |
% \item[\texttt{@PBimpossible}] We tried to balance on a |
| 349 |
% previous pass and failed. |
| 350 |
% \end{description} |
| 351 |
% |
| 352 |
% \subsection{Initialization, required packages etc.} |
| 353 |
% |
| 354 |
% Besides some other required packages, here we load the \texttt{balance} |
| 355 |
% package; if there are no floats or footnotes involved and if the document |
| 356 |
% does not load \texttt{lineno.sty}, we will simply use it. We need to load |
| 357 |
% it after we modify \verb|\@outputdblcol|, so we use \verb|\AtEndOfPackage|. |
| 358 |
% |
| 359 |
% We also define the \texttt{draft} option, which disables processing. |
| 360 |
% |
| 361 |
% \begin{macrocode} |
| 362 |
|
| 363 |
\RequirePackage{etoolbox} |
| 364 |
\RequirePackage{expl3} |
| 365 |
\RequirePackage{atbegshi} % Manipulate the page output routine |
| 366 |
\RequirePackage{atveryend} % Write to the aux file after processing ends |
| 367 |
\RequirePackage{zref-abspage} % Figure out the current page |
| 368 |
\RequirePackage{filehook} % Patch other packages that modify the output routine |
| 369 |
|
| 370 |
\newtoggle{@PBpatchFailed} |
| 371 |
|
| 372 |
% These may be already defined in the .aux file from a previous run |
| 373 |
\providetoggle{@PBstabilized} |
| 374 |
\providetoggle{@PBimpossible} |
| 375 |
|
| 376 |
\newtoggle{@PBnoBalancePackage} |
| 377 |
\DeclareOption{safe}{\toggletrue{@PBnoBalancePackage}} |
| 378 |
|
| 379 |
\newtoggle{@PBdraft} |
| 380 |
\DeclareOption{draft}{\toggletrue{@PBdraft}} |
| 381 |
|
| 382 |
\ProcessOptions\relax |
| 383 |
|
| 384 |
% lineno may be loaded after us, so we need to check for it later... |
| 385 |
\AtEndPreamble{\@ifpackageloaded{lineno}{\toggletrue{@PBnoBalancePackage}}{}} |
| 386 |
|
| 387 |
% ... But it does not harm to check here too, which may save us |
| 388 |
% the trouble of needlessly loading the balance package only to |
| 389 |
% never use it later on. If we get it wrong, that's ok. |
| 390 |
\@ifpackageloaded{lineno}{\toggletrue{@PBnoBalancePackage}}{} |
| 391 |
|
| 392 |
\AtEndOfPackage{ |
| 393 |
\ifboolexpr{togl {@PBdraft} or togl {@PBnoBalancePackage}} |
| 394 |
{} |
| 395 |
{\RequirePackage{balance}} |
| 396 |
} |
| 397 |
|
| 398 |
% See https://github.com/latex3/latex2e/issues/399#issuecomment-703081793 |
| 399 |
\gdef \@reinserts{% |
| 400 |
\ifvbox\@kludgeins\insert\@kludgeins |
| 401 |
{\unvbox\@kludgeins}\fi |
| 402 |
\ifvoid\footins\else\insert\footins{\unvbox\footins}\fi |
| 403 |
} |
| 404 |
|
| 405 |
% \end{macrocode} |
| 406 |
|
| 407 |
% \subsection{The balancing front} |
| 408 |
% |
| 409 |
% The basic balancing process is reasonably simple: right before each |
| 410 |
% new page, check whether it should be balanced. If so, add the adequate |
| 411 |
% code to the top of the page (either call \verb|\balance|, from the |
| 412 |
% \texttt{balance} package, or add \verb|\enlargethispage|). |
| 413 |
% |
| 414 |
% \begin{macrocode} |
| 415 |
|
| 416 |
% Right before the first page |
| 417 |
\AtBeginDocument{ |
| 418 |
\@PBifShouldBalanceNascentPage |
| 419 |
{\@PBStartBalancing} |
| 420 |
{} |
| 421 |
} |
| 422 |
|
| 423 |
% Right before all other pages |
| 424 |
\AtBeginShipout{ |
| 425 |
\@PBifShouldBalanceNascentPage |
| 426 |
{\@PBStartBalancing} |
| 427 |
{} |
| 428 |
} |
| 429 |
|
| 430 |
\newcommand\@PBifShouldBalanceNascentPage[2]{ |
| 431 |
\ifboolexpr |
| 432 |
{ |
| 433 |
togl {@PBdraft} |
| 434 |
or not togl {@PBstabilized} |
| 435 |
or test {\ifdefvoid{\@PBprevLastPage}} |
| 436 |
} |
| 437 |
{#2} |
| 438 |
{ |
| 439 |
% abspage refers to the finished page, not the nascent page |
| 440 |
\ifnumcomp{\@PBprevLastPage - 1}{=}{\value{abspage}} |
| 441 |
{#1} |
| 442 |
{#2} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
% Let's give the user a chance to manually define what to do |
| 447 |
\newcommand\shrinkLastPage[1]{\dimgdef\@PBslack{#1}} |
| 448 |
\newcommand\balancePageNum[1]{\gdef\@PBthepage{#1}} |
| 449 |
\newcommand\nopbalance{\toggletrue{@PBdraft}} |
| 450 |
|
| 451 |
\newcommand\@PBStartBalancing{ |
| 452 |
\ifdefvoid{\@PBslack} |
| 453 |
{\@PBautomaticBalance} |
| 454 |
{ |
| 455 |
\PackageInfo{pbalance}{Automatic mode disabled, doing manual adjustment} |
| 456 |
\@PBshrinkPage |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
\newcommand\@PBautomaticBalance{ |
| 461 |
\@PBifBalancePossible |
| 462 |
{ |
| 463 |
\@PBifBalancePkgPossible |
| 464 |
{\balance} % easy peasy, use the balance package |
| 465 |
{ |
| 466 |
\@PBifPoorBalancePossible |
| 467 |
{ |
| 468 |
% use the poor man's balance algorithm |
| 469 |
\@PBcalculateShrinkage |
| 470 |
\@PBshrinkPage |
| 471 |
} |
| 472 |
{\PackageInfo{pbalance}{Page "naturally" balanced, doing nothing}} |
| 473 |
} |
| 474 |
} |
| 475 |
{\PackageInfo{pbalance}{Float-only column at last page, doing nothing}} |
| 476 |
} |
| 477 |
|
| 478 |
% Calculate how much we should shrink the left column of the last |
| 479 |
% page based on the measurements we took on previous LaTeX passes. |
| 480 |
\newcommand\@PBcalculateShrinkage{ |
| 481 |
|
| 482 |
% Let's use some shorter names, please |
| 483 |
\dimdef\@PBtmpH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}Height}} |
| 484 |
\dimdef\@PBtmpUL{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}UsedLeft}} |
| 485 |
\dimdef\@PBtmpUR{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}UsedRight}} |
| 486 |
|
| 487 |
\dimdef\@PBtmpLH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}LeftHeight}} |
| 488 |
\dimdef\@PBtmpLFH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}LeftFloatsHeight}} |
| 489 |
\dimdef\@PBtmpRH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}RightHeight}} |
| 490 |
\dimdef\@PBtmpRFH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}RightFloatsHeight}} |
| 491 |
|
| 492 |
% Figure out the amount of unused space on the last page (both |
| 493 |
% cols); we will reduce the first column height by half that amount |
| 494 |
\dimgdef\@PBslack{\@PBtmpH *2 - \@PBtmpUL - \@PBtmpUR} |
| 495 |
\dimgdef\@PBslack{\@PBslack /2} |
| 496 |
|
| 497 |
% Actually, I lied. While half, as said above, sometimes yields |
| 498 |
% perfectly balanced columns, that is often not the case (the page |
| 499 |
% may have an odd number of lines, or textheight may not be a |
| 500 |
% multiple of baselineskip, or there may be glues etc.). Sometimes |
| 501 |
% the difference is very small, less than a line, which does not |
| 502 |
% look good at all, and sometimes the right column is slightly |
| 503 |
% higher than the left, which is also not ideal. Here, we force |
| 504 |
% the left column to always be somewhat taller than the left; the |
| 505 |
% result is often ``better''. |
| 506 |
\dimgdef\@PBslack{\@PBslack - 1.5\baselineskip} |
| 507 |
|
| 508 |
% If necessary, reduce @PBslack to prevent LaTeX from adding a page |
| 509 |
\@PBsafetyCheck |
| 510 |
} |
| 511 |
|
| 512 |
\def\@PBshrinkPage{ |
| 513 |
% Zero obviously means ``do nothing''. We could |
| 514 |
% ``enlarge'' by 0, but IDK, maybe that would |
| 515 |
% trigger another LaTeX pass or something. |
| 516 |
\ifdimcomp{\@PBslack}{=}{0pt} |
| 517 |
{} |
| 518 |
{ |
| 519 |
% Shrink the first column |
| 520 |
\enlargethispage{-\@PBslack} |
| 521 |
|
| 522 |
% Modify the behavior of \@outputdblcol |
| 523 |
% to raise footnotes in the second column. |
| 524 |
\global\let\@PBbalanceSecondColumn\@PBrealBalanceSecondColumn |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
% If there are footnotes on the right column, it is better to |
| 529 |
% ``push'' them up to be aligned with the end of the left column, |
| 530 |
% especially if there are footnotes on the left column too. We |
| 531 |
% do this by manipulating \@textbottom in \@outputdblcol. |
| 532 |
\pretocmd{\@outputdblcol}{\@PBbalanceSecondColumn} |
| 533 |
{} |
| 534 |
{ |
| 535 |
\toggletrue{@PBpatchFailed} |
| 536 |
\PackageWarningNoLine{pbalance}{Patch failed (@outputdblcol)} |
| 537 |
} |
| 538 |
|
| 539 |
\let\@PBbalanceSecondColumn\relax |
| 540 |
|
| 541 |
\def\@PBrealBalanceSecondColumn{ |
| 542 |
\if@firstcolumn |
| 543 |
% Affects the next column |
| 544 |
\global\let\@PBorigtextbottom\@textbottom |
| 545 |
\gdef\@textbottom{\vskip \@PBslack plus .0001fil minus 10000fill\relax \@PBorigtextbottom} |
| 546 |
\else |
| 547 |
% Back to normal before starting the next page |
| 548 |
\global\let\@textbottom\@PBorigtextbottom |
| 549 |
\global\let\@PBbalanceSecondColumn\relax |
| 550 |
\fi |
| 551 |
} |
| 552 |
|
| 553 |
% \end{macrocode} |
| 554 |
|
| 555 |
% \subsection{The measuring front} |
| 556 |
% For the mechanism above to work, we need to know: |
| 557 |
% |
| 558 |
% \begin{enumerate} |
| 559 |
% \item The page number of the last two-column text page; |
| 560 |
% \item Whether it has floats, footnotes etc.; |
| 561 |
% \item The available height in it; |
| 562 |
% \item The height of the columns in it. |
| 563 |
% \end{enumerate} |
| 564 |
% |
| 565 |
% Let's collect this info for every page. |
| 566 |
% |
| 567 |
% \subsubsection{Are there floats, footnotes, or marginpars in the last page?} |
| 568 |
% |
| 569 |
% \begin{macrocode} |
| 570 |
|
| 571 |
% To check whether there are floats etc. in the last page, |
| 572 |
% we will copy the fancyhdr package: |
| 573 |
% https://tex.stackexchange.com/questions/56673/is-there-a-way-to-determine-if-there-is-a-float-on-a-page |
| 574 |
% But we add the tests to a different place. |
| 575 |
\newcommand\@PBifmidfloat[2]{\ifx\@midlist\empty #2\else #1\fi} |
| 576 |
\newcommand\@PBiftopfloat[2]{\ifx\@toplist\empty #2\else #1\fi} |
| 577 |
\newcommand\@PBifbotfloat[2]{\ifx\@botlist\empty #2\else #1\fi} |
| 578 |
\newcommand\@PBiffootnote[2]{\ifvoid\footins #2\else #1\fi} |
| 579 |
\newcommand\@PBiffloatcol[2]{\if@fcolmade #1\else #2\fi} |
| 580 |
\newcommand\@PBifdbltopfloat[2]{\ifx\@dbltoplist\empty #2\else #1\fi} |
| 581 |
% dblbotlist only exists with package stfloats |
| 582 |
\newcommand\@PBifdblbotfloat[2]{% |
| 583 |
\ifdefvoid{\@dblbotlist} |
| 584 |
{#2} |
| 585 |
{\ifx\@dblbotlist\empty #2\else #1\fi}% |
| 586 |
} |
| 587 |
|
| 588 |
% If there are footnotes, floats or marginpars in |
| 589 |
% the page, we should not use the balance package |
| 590 |
\newtoggle{@PBtmpHasFootnotes} |
| 591 |
\newtoggle{@PBtmpHasFloats} |
| 592 |
\newtoggle{@PBtmpHasMarginpars} |
| 593 |
|
| 594 |
\def\@PBcollectPageInfo{ |
| 595 |
\ifboolexpr |
| 596 |
{ |
| 597 |
test {\@PBifmidfloat} or test {\@PBiftopfloat} or |
| 598 |
test {\@PBifbotfloat} or test {\@PBifdblbotfloat} or |
| 599 |
test {\@PBifdbltopfloat} |
| 600 |
} |
| 601 |
{\global\toggletrue{@PBtmpHasFloats}} |
| 602 |
{} |
| 603 |
|
| 604 |
\@PBiffootnote |
| 605 |
{\global\toggletrue{@PBtmpHasFootnotes}} |
| 606 |
{} |
| 607 |
} |
| 608 |
|
| 609 |
\pretocmd{\@makecol}{\@PBcollectPageInfo} |
| 610 |
{} |
| 611 |
{ |
| 612 |
\toggletrue{@PBpatchFailed} |
| 613 |
\PackageWarningNoLine{pbalance}{Patch failed (@makecol)} |
| 614 |
} |
| 615 |
|
| 616 |
% This only gets called if there is a marginpar, so that's easy |
| 617 |
\pretocmd{\@addmarginpar}{\global\toggletrue{@PBtmpHasMarginpars}} |
| 618 |
{} |
| 619 |
{ |
| 620 |
\toggletrue{@PBpatchFailed} |
| 621 |
\PackageWarningNoLine{pbalance}{Patch failed (@addmarginpar)} |
| 622 |
} |
| 623 |
|
| 624 |
% \end{macrocode} |
| 625 |
% |
| 626 |
% \subsubsection{What are the sizes of the floats in the left column?} |
| 627 |
% |
| 628 |
% \begin{macrocode} |
| 629 |
|
| 630 |
\ExplSyntaxOn |
| 631 |
\seq_gclear_new:N \@PBtmpLeftFloatHeights |
| 632 |
\seq_gclear_new:N \@PBtmpLeftFloatSpacesBelow |
| 633 |
\def\@PBcollectFloatHeights{ |
| 634 |
\if@firstcolumn |
| 635 |
\dimdef\@PBfloatHeight{\@textfloatsheight - \@PBtmpHeightBefore} |
| 636 |
\ifdimcomp{\@PBfloatHeight}{>}{0pt} |
| 637 |
{ |
| 638 |
\global\seq_put_right:NV \@PBtmpLeftFloatHeights \@PBfloatHeight |
| 639 |
|
| 640 |
\dimdef\@PBspaceBelow{\@colroom - \@pageht - \@PBfloatHeight} |
| 641 |
% within \@addtocurcol, \@pageht corresponds to all |
| 642 |
% currently used space in the column, including footnotes |
| 643 |
% (check \@specialoutput in the LaTeX kernel). Let's |
| 644 |
% exclude the footnotes, we only want the space below |
| 645 |
% the float. |
| 646 |
\ifvoid\footins\else |
| 647 |
\dimdef\@PBspaceBelow{\@PBspaceBelow + \ht\footins + \skip\footins + \dp\footins} |
| 648 |
\fi |
| 649 |
|
| 650 |
% No idea why, but this can happen; maybe because of the page depth? |
| 651 |
\ifdimcomp{\@PBspaceBelow}{<}{0pt} |
| 652 |
{\dimdef\@PBspaceBelow{0pt}} |
| 653 |
{} |
| 654 |
|
| 655 |
\global\seq_put_right:NV \@PBtmpLeftFloatSpacesBelow \@PBspaceBelow |
| 656 |
} |
| 657 |
{} |
| 658 |
\fi |
| 659 |
} |
| 660 |
\ExplSyntaxOff |
| 661 |
|
| 662 |
\pretocmd{\@addtocurcol}{\dimgdef{\@PBtmpHeightBefore}{\@textfloatsheight}} |
| 663 |
{} |
| 664 |
{ |
| 665 |
\toggletrue{@PBpatchFailed} |
| 666 |
\PackageWarningNoLine{pbalance}{Patch failed (@addtocurcol)} |
| 667 |
} |
| 668 |
|
| 669 |
\apptocmd{\@addtocurcol}{\@PBcollectFloatHeights} |
| 670 |
{} |
| 671 |
{ |
| 672 |
\toggletrue{@PBpatchFailed} |
| 673 |
\PackageWarningNoLine{pbalance}{Patch failed (@addtocurcol)} |
| 674 |
} |
| 675 |
|
| 676 |
% \end{macrocode} |
| 677 |
% |
| 678 |
% \subsubsection{What is the size of each column? Is there a float column / floatpage?} |
| 679 |
% |
| 680 |
% \begin{macrocode} |
| 681 |
|
| 682 |
% If there is a float column in the page, it makes no sense to try to |
| 683 |
% balance; if both columns are float columns, this page cannot be the |
| 684 |
% final text page, i.e., we want to balance some other page. |
| 685 |
\newtoggle{@PBtmpHasFloatcol} |
| 686 |
\newtoggle{@PBtmpFloatcolBoth} |
| 687 |
|
| 688 |
% It would be nice to modify @makecol to do something like |
| 689 |
% |
| 690 |
% if@firstcolumn |
| 691 |
% @PBtmpUsedLeft = currentColumnHeight |
| 692 |
% else |
| 693 |
% @PBtmpUsedRight = currentColumnHeight |
| 694 |
% |
| 695 |
% Unfortunately, as stated in texdoc source2e on the section about the |
| 696 |
% output routine (ltoutput.dtx) around source line 273, "any conditional |
| 697 |
% code for the two-column case within output may not get executed with |
| 698 |
% the correct value of if@firstcolumn." |
| 699 |
% |
| 700 |
% Another approach, then, would be to defer the measurement of the |
| 701 |
% columns to @outputdblcol, where there are @leftcolumn and @outputbox. |
| 702 |
% This works, unless the package lineno was loaded: this package modifies |
| 703 |
% @makecol, preventing us from obtaining the correct height for the |
| 704 |
% output boxes. |
| 705 |
% |
| 706 |
% What we do, then, is (1) collect the column height in @makecol and |
| 707 |
% (2) decide whether the collected height corresponds to the left or |
| 708 |
% right column in @outputdblcol. |
| 709 |
\newcommand\@PBcollectColumnUsedHeight{ |
| 710 |
\setbox\@tempboxa=\vbox{\unvcopy\@outputbox\unskip\unskip} |
| 711 |
\dimgdef\@PBtmpColumnUsedHeight{\ht\@tempboxa\relax} |
| 712 |
} |
| 713 |
|
| 714 |
\apptocmd{\@makecol}{\@PBcollectColumnUsedHeight} |
| 715 |
{} |
| 716 |
{ |
| 717 |
\toggletrue{@PBpatchFailed} |
| 718 |
\PackageWarningNoLine{pbalance}{Patch failed (@makecol)} |
| 719 |
} |
| 720 |
|
| 721 |
% Let's make sure our patched code actually runs. We only switch this |
| 722 |
% to true after we verify that the patches in @makecol and @outputdblcol |
| 723 |
% are actually executed when the page is built (therefore, after the |
| 724 |
% preamble has ended). |
| 725 |
\newtoggle{@PBpatchStillActive} |
| 726 |
|
| 727 |
\newcommand\@PBcollectColumnInfo{ |
| 728 |
\iftoggle{@PBpatchFailed} |
| 729 |
{} |
| 730 |
{ |
| 731 |
\ifdefvoid{\@PBtmpColumnUsedHeight} |
| 732 |
{} % the patch to @makecol is not active, so we do nothing here |
| 733 |
{ |
| 734 |
% the patch to @makecol is active and so is ours, we're good to go! |
| 735 |
\global\toggletrue{@PBpatchStillActive} |
| 736 |
\@PBreallyCollectColumnInfo |
| 737 |
} |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
\newcommand\@PBreallyCollectColumnInfo{ |
| 742 |
\if@firstcolumn |
| 743 |
|
| 744 |
\@PBiffloatcol |
| 745 |
{\global\toggletrue{@PBtmpHasFloatcol}} |
| 746 |
{} |
| 747 |
|
| 748 |
% Available vertical space excluding |
| 749 |
% dblfloats; the same for both columns |
| 750 |
\dimgdef\@PBtmpHeight{\@colht} |
| 751 |
|
| 752 |
% Available vertical space excluding top/bottom floats |
| 753 |
\dimgdef\@PBtmpLeftHeight{\@colroom} |
| 754 |
|
| 755 |
% Space used by \texttt{here} floats |
| 756 |
\dimgdef\@PBtmpLeftFloatsHeight{\@textfloatsheight} |
| 757 |
\dimgdef\@PBtmpUsedLeft{\@PBtmpColumnUsedHeight} |
| 758 |
|
| 759 |
\else |
| 760 |
|
| 761 |
\@PBiffloatcol |
| 762 |
{ |
| 763 |
\iftoggle{@PBtmpHasFloatcol} |
| 764 |
{\global\toggletrue{@PBtmpFloatcolBoth}} |
| 765 |
{\global\toggletrue{@PBtmpHasFloatcol}} |
| 766 |
} |
| 767 |
{} |
| 768 |
|
| 769 |
\dimgdef\@PBtmpRightHeight{\@colroom} |
| 770 |
\dimgdef\@PBtmpRightFloatsHeight{\@textfloatsheight} |
| 771 |
\dimgdef\@PBtmpUsedRight{\@PBtmpColumnUsedHeight} |
| 772 |
\fi |
| 773 |
} |
| 774 |
|
| 775 |
% Now we patch the default @outputdblcol LaTeX macro... |
| 776 |
\pretocmd{\@outputdblcol}{\@PBcollectColumnInfo} |
| 777 |
{} |
| 778 |
{ |
| 779 |
\toggletrue{@PBpatchFailed} |
| 780 |
\PackageWarningNoLine{pbalance}{Patch failed (@outputdblcol)} |
| 781 |
} |
| 782 |
|
| 783 |
% ... And the balance package version, but only if we actually loaded it |
| 784 |
\ifboolexpr{togl {@PBdraft} or togl {@PBnoBalancePackage}} |
| 785 |
{} |
| 786 |
{ |
| 787 |
\AtEndOfPackage{ |
| 788 |
\pretocmd{\@BAdblcol}{\@PBcollectColumnInfo} |
| 789 |
{} |
| 790 |
{ |
| 791 |
\toggletrue{@PBpatchFailed} |
| 792 |
\PackageWarningNoLine{pbalance}{Patch failed (@BAdblcol)} |
| 793 |
} |
| 794 |
% Revert \balance before next page if it exists (it might be a float page) |
| 795 |
\patchcmd{\@BAdblcol}{\endgroup}{\endgroup\nobalance} |
| 796 |
{} |
| 797 |
{ |
| 798 |
\toggletrue{@PBpatchFailed} |
| 799 |
\PackageWarningNoLine{pbalance}{Patch failed (@BAdblcol)} |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
% \end{macrocode} |
| 805 |
% |
| 806 |
% \subsection{Compatibility with other packages} |
| 807 |
% |
| 808 |
% We mostly modify stuff from the output routine to collect data, not to |
| 809 |
% change what is typeset (the only exception is @textbottom, which we |
| 810 |
% manipulate to align footnotes). This means that, if some other package |
| 811 |
% modifies the output routine as well, we will not break anything, which |
| 812 |
% is why this package is mostly safe to use. Unfortunately, such other |
| 813 |
% packages may prevent us from balancing. Here we try to play nice with |
| 814 |
% some of them. These workarounds may result in the same patch being |
| 815 |
% applied and run more than once; that is not ideal (we are wasting time) |
| 816 |
% but it should not cause problems. |
| 817 |
% |
| 818 |
% \subsubsection{stfloats} |
| 819 |
% |
| 820 |
% \begin{macrocode} |
| 821 |
|
| 822 |
% stfloats overwrites @addtocurcol; if it is already loaded, we already |
| 823 |
% patched the modified version, so we only need to run this if the package |
| 824 |
% is loaded later on. Therefore, we use AtEndOfPackageFile without *. |
| 825 |
\AtEndOfPackageFile{stfloats} |
| 826 |
{ |
| 827 |
|
| 828 |
\pretocmd{\@addtocurcol}{\dimgdef{\@PBtmpHeightBefore}{\@textfloatsheight}} |
| 829 |
{} |
| 830 |
{ |
| 831 |
\toggletrue{@PBpatchFailed} |
| 832 |
\PackageWarningNoLine{pbalance}{Patch failed (stfloats/@addtocurcol)} |
| 833 |
} |
| 834 |
|
| 835 |
\apptocmd{\@addtocurcol}{\@PBcollectFloatHeights} |
| 836 |
{} |
| 837 |
{ |
| 838 |
\toggletrue{@PBpatchFailed} |
| 839 |
\PackageWarningNoLine{pbalance}{Patch failed (stfloats/@addtocurcol)} |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
% We always need to patch these, so this code should run whether the |
| 844 |
% package is already loaded or gets loaded later on. Therefore, we |
| 845 |
% use AtEndOfPackageFile with *. |
| 846 |
\AtEndOfPackageFile*{stfloats} |
| 847 |
{ |
| 848 |
\pretocmd{\fn@makecol}{\@PBcollectPageInfo} |
| 849 |
{} |
| 850 |
{ |
| 851 |
\toggletrue{@PBpatchFailed} |
| 852 |
\PackageWarningNoLine{pbalance}{Patch failed (stfloats/fn@makecol)} |
| 853 |
} |
| 854 |
\pretocmd{\org@makecol}{\@PBcollectPageInfo} |
| 855 |
{} |
| 856 |
{ |
| 857 |
\toggletrue{@PBpatchFailed} |
| 858 |
\PackageWarningNoLine{pbalance}{Patch failed (stfloats/org@makecol)} |
| 859 |
} |
| 860 |
\apptocmd{\fn@makecol}{\@PBcollectColumnUsedHeight} |
| 861 |
{} |
| 862 |
{ |
| 863 |
\toggletrue{@PBpatchFailed} |
| 864 |
\PackageWarningNoLine{pbalance}{Patch failed (stfloats/fn@makecol)} |
| 865 |
} |
| 866 |
\apptocmd{\org@makecol}{\@PBcollectColumnUsedHeight} |
| 867 |
{} |
| 868 |
{ |
| 869 |
\toggletrue{@PBpatchFailed} |
| 870 |
\PackageWarningNoLine{pbalance}{Patch failed (stfloats/org@makecol)} |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
% \end{macrocode} |
| 875 |
% |
| 876 |
% \subsubsection{memoir} |
| 877 |
% |
| 878 |
% \begin{macrocode} |
| 879 |
|
| 880 |
% memoir defines the macros \mem@makecol and \mem@makecolbf; the commands |
| 881 |
% \feetabovefloat and \feetbelowfloat then redefine \@makecol to call |
| 882 |
% either one and executes \feetabovefloat. What we do, then, is patch |
| 883 |
% these two macros. Since memoir is a class, we assume it is always |
| 884 |
% loaded before us. Note that this means that, by default, our patch |
| 885 |
% is applied twice: one to \@makecol (because we always patch it) and |
| 886 |
% one to \mem@makecol, which is called by \@makecol. Only if the user |
| 887 |
% executes \feetabovefloat or \feetbelowfloat again does the patch in |
| 888 |
% \@makecol disappears. |
| 889 |
|
| 890 |
\ifdefvoid{\mem@makecol} |
| 891 |
{} |
| 892 |
{ |
| 893 |
\pretocmd{\mem@makecol}{\@PBcollectPageInfo} |
| 894 |
{} |
| 895 |
{ |
| 896 |
\toggletrue{@PBpatchFailed} |
| 897 |
\PackageWarningNoLine{pbalance}{Patch failed (memoir/mem@makecol)} |
| 898 |
} |
| 899 |
\apptocmd{\mem@makecol}{\@PBcollectColumnUsedHeight} |
| 900 |
{} |
| 901 |
{ |
| 902 |
\toggletrue{@PBpatchFailed} |
| 903 |
\PackageWarningNoLine{pbalance}{Patch failed (memoir/mem@makecol)} |
| 904 |
} |
| 905 |
\pretocmd{\mem@makecolbf}{\@PBcollectPageInfo} |
| 906 |
{} |
| 907 |
{ |
| 908 |
\toggletrue{@PBpatchFailed} |
| 909 |
\PackageWarningNoLine{pbalance}{Patch failed (memoir/mem@makecolbf)} |
| 910 |
} |
| 911 |
\apptocmd{\mem@makecolbf}{\@PBcollectColumnUsedHeight} |
| 912 |
{} |
| 913 |
{ |
| 914 |
\toggletrue{@PBpatchFailed} |
| 915 |
\PackageWarningNoLine{pbalance}{Patch failed (memoir/mem@makecolbf)} |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
% \end{macrocode} |
| 920 |
% |
| 921 |
% \subsubsection{footmisc} |
| 922 |
% |
| 923 |
% \begin{macrocode} |
| 924 |
|
| 925 |
% footmisc simply redefines @makecol. If the package is loaded before |
| 926 |
% us, everything works; we just need to apply the patches again if it |
| 927 |
% is loaded after us. Therefore, we use AtEndOfPackageFile without *. |
| 928 |
% Note that there are two active versions of this package (the newer |
| 929 |
% uses new features from the LaTeX kernel). While their inner workings |
| 930 |
% are different, our strategy here is the same. footmisc checks if |
| 931 |
% @makecol has been modified before and issues a warning is that is |
| 932 |
% true. We could prevent this from happening, but if some other package |
| 933 |
% modifies it too that could be misleading, so we leave the warning |
| 934 |
% alone. |
| 935 |
\AtEndOfPackageFile{footmisc} |
| 936 |
{ |
| 937 |
\pretocmd{\@makecol}{\@PBcollectPageInfo} |
| 938 |
{} |
| 939 |
{ |
| 940 |
\toggletrue{@PBpatchFailed} |
| 941 |
\PackageWarningNoLine{pbalance}{Patch failed (footmisc/@makecol)} |
| 942 |
} |
| 943 |
\apptocmd{\@makecol}{\@PBcollectColumnUsedHeight} |
| 944 |
{} |
| 945 |
{ |
| 946 |
\toggletrue{@PBpatchFailed} |
| 947 |
\PackageWarningNoLine{pbalance}{Patch failed (footmisc/@makecol)} |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
% \end{macrocode} |
| 952 |
% |
| 953 |
% \subsubsection{ftnright} |
| 954 |
% |
| 955 |
% \begin{macrocode} |
| 956 |
|
| 957 |
% ftnright simply redefines @makecol and @outputdblcol. If the package is |
| 958 |
% loaded before us, everything works; we just need to apply the patches |
| 959 |
% again if it is loaded after us. Therefore, we use AtEndOfPackageFile |
| 960 |
% without *. |
| 961 |
|
| 962 |
\AtEndOfPackageFile{ftnright} |
| 963 |
{ |
| 964 |
\pretocmd{\@makecol}{\@PBcollectPageInfo} |
| 965 |
{} |
| 966 |
{ |
| 967 |
\toggletrue{@PBpatchFailed} |
| 968 |
\PackageWarningNoLine{pbalance}{Patch failed (ftnright/@makecol)} |
| 969 |
} |
| 970 |
\apptocmd{\@makecol}{\@PBcollectColumnUsedHeight} |
| 971 |
{} |
| 972 |
{ |
| 973 |
\toggletrue{@PBpatchFailed} |
| 974 |
\PackageWarningNoLine{pbalance}{Patch failed (ftnright/@makecol)} |
| 975 |
} |
| 976 |
|
| 977 |
\pretocmd{\@outputdblcol}{\@PBbalanceSecondColumn} |
| 978 |
{} |
| 979 |
{ |
| 980 |
\toggletrue{@PBpatchFailed} |
| 981 |
\PackageWarningNoLine{pbalance}{Patch failed (ftnright/@outputdblcol)} |
| 982 |
} |
| 983 |
|
| 984 |
\pretocmd{\@outputdblcol}{\@PBcollectColumnInfo} |
| 985 |
{} |
| 986 |
{ |
| 987 |
\toggletrue{@PBpatchFailed} |
| 988 |
\PackageWarningNoLine{pbalance}{Patch failed (ftnright/@outputdblcol)} |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
% \end{macrocode} |
| 993 |
% |
| 994 |
% \subsubsection{dblfnote} |
| 995 |
% |
| 996 |
% \begin{macrocode} |
| 997 |
|
| 998 |
% dblfnote copies @makecol to dfn@latex@makecol and defines dfn@makecol. |
| 999 |
% It then alternates between the two with the \twocolumn and \onecolumn |
| 1000 |
% commands (in two-column mode, the default LaTeX version of the command |
| 1001 |
% is used). dblfnote does the copy at the end of the preamble, so we do |
| 1002 |
% not need to patch anything else, but it does assume that the document |
| 1003 |
% is one-column. This means it substitutes dfn@makecol even in a two- |
| 1004 |
% column document, which is clearly a bug. We could try to work around |
| 1005 |
% this with AtBeginDocument, but this might make us overwrite some other |
| 1006 |
% modification, so let's do nothing. |
| 1007 |
|
| 1008 |
% \end{macrocode} |
| 1009 |
% |
| 1010 |
% \subsubsection{flafter} |
| 1011 |
% |
| 1012 |
% \begin{macrocode} |
| 1013 |
|
| 1014 |
% flafter is probably irrelevant in two-column mode: I believe the default |
| 1015 |
% algorithm always places the float on the next column or page. Still, |
| 1016 |
% flafter overwrites @addtocurcol, so let's deal with it to be on the |
| 1017 |
% safe side. We only need to worry if flafter is loaded after us, so we |
| 1018 |
% use AtEndOfPackageFile without "*". |
| 1019 |
|
| 1020 |
\AtEndOfPackageFile{flafter} |
| 1021 |
{ |
| 1022 |
|
| 1023 |
\pretocmd{\@addtocurcol}{\dimgdef{\@PBtmpHeightBefore}{\@textfloatsheight}} |
| 1024 |
{} |
| 1025 |
{ |
| 1026 |
\toggletrue{@PBpatchFailed} |
| 1027 |
\PackageWarningNoLine{pbalance}{Patch failed (flafter/@addtocurcol)} |
| 1028 |
} |
| 1029 |
|
| 1030 |
\apptocmd{\@addtocurcol}{\@PBcollectFloatHeights} |
| 1031 |
{} |
| 1032 |
{ |
| 1033 |
\toggletrue{@PBpatchFailed} |
| 1034 |
\PackageWarningNoLine{pbalance}{Patch failed (flafter/@addtocurcol)} |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
% \end{macrocode} |
| 1039 |
% |
| 1040 |
% \subsubsection{fnpos} |
| 1041 |
% |
| 1042 |
% \begin{macrocode} |
| 1043 |
|
| 1044 |
% fnpos redefines \@makecol, so we need to patch it again. With option |
| 1045 |
% \makeFNbottom (the default), our modification to \@textbottom that |
| 1046 |
% aligns footnotes on the right column is overwritten; the user can |
| 1047 |
% solve this by calling \makeFNmid. We only need to worry if fnpos |
| 1048 |
% is loaded after us, so we use AtEndOfPackageFile without "*". |
| 1049 |
|
| 1050 |
\AtEndOfPackageFile{fnpos} |
| 1051 |
{ |
| 1052 |
\pretocmd{\@makecol}{\@PBcollectPageInfo} |
| 1053 |
{} |
| 1054 |
{ |
| 1055 |
\toggletrue{@PBpatchFailed} |
| 1056 |
\PackageWarningNoLine{pbalance}{Patch failed (fnpos/@makecol)} |
| 1057 |
} |
| 1058 |
\apptocmd{\@makecol}{\@PBcollectColumnUsedHeight} |
| 1059 |
{} |
| 1060 |
{ |
| 1061 |
\toggletrue{@PBpatchFailed} |
| 1062 |
\PackageWarningNoLine{pbalance}{Patch failed (fnpos/@makecol)} |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
% \end{macrocode} |
| 1068 |
% |
| 1069 |
% \subsubsection{midfloat and cuted} |
| 1070 |
% |
| 1071 |
% \begin{macrocode} |
| 1072 |
|
| 1073 |
% These packages are similar in spirit, but use different implementations. |
| 1074 |
% Both redefine \@outputdblcol, so we just need to patch that again if |
| 1075 |
% the package is loaded after us. |
| 1076 |
|
| 1077 |
\AtEndOfPackageFile{cuted} |
| 1078 |
{ |
| 1079 |
\pretocmd{\@outputdblcol}{\@PBbalanceSecondColumn} |
| 1080 |
{} |
| 1081 |
{ |
| 1082 |
\toggletrue{@PBpatchFailed} |
| 1083 |
\PackageWarningNoLine{pbalance}{Patch failed (cuted/@outputdblcol)} |
| 1084 |
} |
| 1085 |
|
| 1086 |
\pretocmd{\@outputdblcol}{\@PBcollectColumnInfo} |
| 1087 |
{} |
| 1088 |
{ |
| 1089 |
\toggletrue{@PBpatchFailed} |
| 1090 |
\PackageWarningNoLine{pbalance}{Patch failed (cuted/@outputdblcol)} |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
\AtEndOfPackageFile{midfloat} |
| 1095 |
{ |
| 1096 |
\pretocmd{\@outputdblcol}{\@PBbalanceSecondColumn} |
| 1097 |
{} |
| 1098 |
{ |
| 1099 |
\toggletrue{@PBpatchFailed} |
| 1100 |
\PackageWarningNoLine{pbalance}{Patch failed (midfloat/@outputdblcol)} |
| 1101 |
} |
| 1102 |
|
| 1103 |
\pretocmd{\@outputdblcol}{\@PBcollectColumnInfo} |
| 1104 |
{} |
| 1105 |
{ |
| 1106 |
\toggletrue{@PBpatchFailed} |
| 1107 |
\PackageWarningNoLine{pbalance}{Patch failed (midfloat/@outputdblcol)} |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
% \end{macrocode} |
| 1112 |
% |
| 1113 |
% \subsubsection{Analyze the obtained information} |
| 1114 |
% |
| 1115 |
% We have inserted the hooks we need to collect data during |
| 1116 |
% the processing of each page and column. Now we inspect the |
| 1117 |
% collected data after each page is finalized. |
| 1118 |
% |
| 1119 |
% \begin{macrocode} |
| 1120 |
|
| 1121 |
\ExplSyntaxOn |
| 1122 |
\AtBeginShipout{ |
| 1123 |
\iftoggle{@PBpatchFailed} |
| 1124 |
{} |
| 1125 |
{ |
| 1126 |
% It only makes sense to consider pages that actually have two columns. |
| 1127 |
\if@twocolumn |
| 1128 |
\iftoggle{@PBpatchStillActive} |
| 1129 |
{\@PBfinalizePageData} |
| 1130 |
{ |
| 1131 |
\global\toggletrue{@PBpatchFailed} |
| 1132 |
\PackageWarningNoLine{pbalance}{Patch~disabled~by~another~package} |
| 1133 |
} |
| 1134 |
\fi |
| 1135 |
|
| 1136 |
% Reset before processing next page |
| 1137 |
\global\togglefalse{@PBtmpHasFloats} |
| 1138 |
\global\togglefalse{@PBtmpHasFloatcol} |
| 1139 |
\global\togglefalse{@PBtmpHasFootnotes} |
| 1140 |
\global\togglefalse{@PBtmpHasMarginpars} |
| 1141 |
\global\togglefalse{@PBtmpFloatcolBoth} |
| 1142 |
|
| 1143 |
\seq_gclear_new:N \@PBtmpLeftFloatHeights |
| 1144 |
\seq_gclear_new:N \@PBtmpLeftFloatSpacesBelow |
| 1145 |
|
| 1146 |
\gundef{\@PBtmpHeight} |
| 1147 |
\gundef{\@PBtmpLeftHeight} |
| 1148 |
\gundef{\@PBtmpLeftFloatsHeight} |
| 1149 |
\gundef{\@PBtmpUsedLeft} |
| 1150 |
\gundef{\@PBtmpRightHeight} |
| 1151 |
\gundef{\@PBtmpRightFloatsHeight} |
| 1152 |
\gundef{\@PBtmpUsedRight} |
| 1153 |
} |
| 1154 |
} |
| 1155 |
\ExplSyntaxOff |
| 1156 |
|
| 1157 |
\newcommand\@PBfinalizePageData{ |
| 1158 |
% LaTeX3 does not do short-circuit evaluation, |
| 1159 |
% so we cannot use a single ifboolexpr here |
| 1160 |
\ifdefvoid{\@PBtmpUsedRight} |
| 1161 |
% This is probably a float page or the user disabled |
| 1162 |
% the package somehow and recompiled. Either way, |
| 1163 |
% this is not the last text page, ignore |
| 1164 |
{} |
| 1165 |
{ |
| 1166 |
\ifboolexpr |
| 1167 |
{ |
| 1168 |
% If both cols are float cols, this is not the last text page, ignore |
| 1169 |
togl {@PBtmpFloatcolBoth} |
| 1170 |
or |
| 1171 |
% A page with a float column on the left and an empty |
| 1172 |
% column on the right is not the last text page, ignore |
| 1173 |
( |
| 1174 |
togl {@PBtmpHasFloatcol} |
| 1175 |
and |
| 1176 |
test {\ifdimcomp{\@PBtmpUsedRight}{=}{\topskip}} |
| 1177 |
) |
| 1178 |
} |
| 1179 |
{} |
| 1180 |
{ |
| 1181 |
\ifdefvoid{\@PBthepage} |
| 1182 |
{ |
| 1183 |
% The user did not choose a page, so this page is |
| 1184 |
% a candidate to be the last two-column text page |
| 1185 |
\xdef\@PBlastPage{\the\value{abspage}} |
| 1186 |
\@PBcopyPageData{tmp}{candidate} |
| 1187 |
} |
| 1188 |
{ |
| 1189 |
\ifnumcomp{\@PBthepage}{=}{\the\value{abspage}} |
| 1190 |
{ |
| 1191 |
% This is the page the user indicated |
| 1192 |
% as the last two-column text page |
| 1193 |
\xdef\@PBlastPage{\the\value{abspage}} |
| 1194 |
\@PBcopyPageData{tmp}{candidate} |
| 1195 |
} |
| 1196 |
{} |
| 1197 |
} |
| 1198 |
} |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
% \end{macrocode} |
| 1203 |
% |
| 1204 |
% All pages processed, the last candidate page is actually the last two-column |
| 1205 |
% page. Instead of continuing to use the \texttt{@PBcandidate*} macros we already |
| 1206 |
% have, we will keep the information we just gathered associated with the |
| 1207 |
% page number. We do this because, in the future, we may want to add suport |
| 1208 |
% for balancing multiple pages (such as the last page of each chapter) and |
| 1209 |
% not only the last one. This also makes it easier to handle the situation |
| 1210 |
% where the document was modified and the last page has changed. |
| 1211 |
% |
| 1212 |
% \begin{macrocode} |
| 1213 |
|
| 1214 |
\AfterLastShipout{ |
| 1215 |
% If there are no two-column pages in the |
| 1216 |
% document, there is no candidate page. |
| 1217 |
\ifcsdef{@PBlastPage} |
| 1218 |
{\@PBcopyPageData{candidate}{pg\@Roman{\@PBlastPage}}} |
| 1219 |
{\PackageInfo{pbalance}{No two-column pages, doing nothing}} |
| 1220 |
} |
| 1221 |
|
| 1222 |
% \end{macrocode} |
| 1223 |
% |
| 1224 |
% \subsubsection{Saving measurements} |
| 1225 |
% The whole document has already been processed and data about the last |
| 1226 |
% two-column page has been collected in macros with names \texttt{@PBpgNUM*}. |
| 1227 |
% We now process this collected data and save whatever is relevant in the aux |
| 1228 |
% file for the next \LaTeX{} pass. |
| 1229 |
% |
| 1230 |
% In the first \LaTeX{} pass, we will save data to the aux file in macros |
| 1231 |
% with names \texttt{@PBunbalpgNUM*}. That is all we need for balancing. |
| 1232 |
% However, to detect whether the document has changed and, therefore, |
| 1233 |
% whether we should re-balance, we will also record the same data after |
| 1234 |
% balancing (during the second pass) in macros with names |
| 1235 |
% \texttt{@PBbalpgNUM*}. If, in the third or later \LaTeX{} passes, |
| 1236 |
% \texttt{@PBbalpgNUM*} differ from \texttt{@PBpgNUM*}, the document |
| 1237 |
% has been changed and we need to re-balance. |
| 1238 |
% |
| 1239 |
% So, we have: |
| 1240 |
% |
| 1241 |
% \begin{description} |
| 1242 |
% \item[\texttt{@PBpgNUM*}] data collected during the current pass; |
| 1243 |
% \item[\texttt{@PBunbalpgNUM*}] data collected in the first pass; |
| 1244 |
% \item[\texttt{@PBbalpgNUM*}] data collected in the second pass (after |
| 1245 |
% balancing); in subsequent passes, these should always be equal |
| 1246 |
% to \texttt{@PBpgNUM*}. |
| 1247 |
% \end{description} |
| 1248 |
% |
| 1249 |
% The last two are saved to / read from the aux file. |
| 1250 |
% |
| 1251 |
% \begin{macrocode} |
| 1252 |
|
| 1253 |
% Make sure the document has stabilized before using \@PBmanageBalancingPasses |
| 1254 |
\AfterLastShipout{ |
| 1255 |
% Reset \@PBimpossible if we are in draft mode, |
| 1256 |
% so the user can start over if they want to. |
| 1257 |
\iftoggle{@PBdraft}{\togglefalse{@PBimpossible}}{} |
| 1258 |
|
| 1259 |
\ifboolexpr{togl {@PBdraft} or togl {@PBimpossible} or togl {@PBpatchFailed}} |
| 1260 |
{ |
| 1261 |
\iftoggle{@PBimpossible} |
| 1262 |
{\PackageWarningNoLine{pbalance}{Could not balance, doing nothing}} |
| 1263 |
{ |
| 1264 |
\iftoggle{@PBpatchFailed} |
| 1265 |
{ |
| 1266 |
\PackageWarningNoLine{pbalance}{Patching failed, cannot balance; |
| 1267 |
try \MessageBreak loading |
| 1268 |
pbalance earlier or later} |
| 1269 |
} |
| 1270 |
{\PackageInfo{pbalance}{Draft mode, doing nothing}} |
| 1271 |
} |
| 1272 |
} |
| 1273 |
{ |
| 1274 |
\ifdefvoid{\@PBprevLastPage} |
| 1275 |
{\@PBsaveUnbalancedInfo\@PBnotifyRerun} % First pass |
| 1276 |
{ |
| 1277 |
\iftoggle{@PBstabilized} |
| 1278 |
{\@PBmanageBalancingPasses} |
| 1279 |
{ |
| 1280 |
\@PBifSomethingChanged[unbal] |
| 1281 |
% Document still changing; discard data from previous pass |
| 1282 |
{\@PBsaveUnbalancedInfo[update]\@PBnotifyRerun} |
| 1283 |
% No changes, so let's balance from now on (once @PBstabilized |
| 1284 |
% becomes true, it never reverts back to false, unless the aux |
| 1285 |
% file is deleted). |
| 1286 |
{\toggletrue{@PBstabilized}\@PBsaveUnbalancedInfo\@PBnotifyRerun} |
| 1287 |
} |
| 1288 |
} |
| 1289 |
\@PBsaveToggle{@PBstabilized} |
| 1290 |
} |
| 1291 |
|
| 1292 |
\@PBsaveToggle{@PBimpossible} |
| 1293 |
\immediate\write\@mainaux{\gdef\string\@PBprevLastPhysPage{\the\value{abspage}}} |
| 1294 |
} |
| 1295 |
|
| 1296 |
\newcommand\@PBmanageBalancingPasses{ |
| 1297 |
% This is the second pass or later after we started balancing |
| 1298 |
|
| 1299 |
\providetoggle{@PBpg\@Roman{\@PBlastPage}alreadyBalanced} |
| 1300 |
|
| 1301 |
\iftoggle{@PBpg\@Roman{\@PBlastPage}alreadyBalanced} |
| 1302 |
{ |
| 1303 |
% Third pass or later; document was already |
| 1304 |
% balanced, so there should be no more changes |
| 1305 |
\@PBifSomethingChanged |
| 1306 |
% The document has changed, so we need to measure |
| 1307 |
% stuff again; clear aux file and start over. |
| 1308 |
{\@PBnotifyRerun} |
| 1309 |
{ |
| 1310 |
% Nothing changed, just write the same info down |
| 1311 |
\@PBsaveUnbalancedInfo |
| 1312 |
\@PBsaveBalancedInfo |
| 1313 |
\PackageInfo{pbalance}{Done balancing page \@PBlastPage} |
| 1314 |
} |
| 1315 |
} |
| 1316 |
% Second pass, which means something did change: we have just |
| 1317 |
% balanced the columns for the first time. |
| 1318 |
{ |
| 1319 |
\xdef\@tempa{\the\value{abspage}} |
| 1320 |
\ifboolexpr |
| 1321 |
{ |
| 1322 |
test {\ifdefstrequal{\@PBprevLastPage}{\@PBlastPage}} |
| 1323 |
and |
| 1324 |
test {\ifdefstrequal{\@PBprevLastPhysPage}{\@tempa}} |
| 1325 |
} |
| 1326 |
% All is well; save the new info |
| 1327 |
{\@PBsaveUnbalancedInfo\@PBsaveBalancedInfo} |
| 1328 |
% Oh, no! As we attempted to balance, we actually changed |
| 1329 |
% the number of pages (maybe we added a float page, maybe |
| 1330 |
% we turned the last float page into a text page etc.). If |
| 1331 |
% we continue, the document might never converge (endless |
| 1332 |
% ``please rerun LaTeX'' messages). Let's give up balancing. |
| 1333 |
{\toggletrue{@PBimpossible}} |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
% \end{macrocode} |
| 1338 |
% |
| 1339 |
% \subsection{Auxiliary macros} |
| 1340 |
% |
| 1341 |
% \begin{macrocode} |
| 1342 |
|
| 1343 |
\newcommand\@PBifBalancePossible[2]{ |
| 1344 |
\iftoggle{@PBunbalpg\@Roman{\@PBprevLastPage}HasFloatcol} |
| 1345 |
{#2} |
| 1346 |
{#1} |
| 1347 |
} |
| 1348 |
|
| 1349 |
% The balance package may fail badly in the presence of footnotes: |
| 1350 |
% they may end up in the middle of the text. Marginpars may end up |
| 1351 |
% on the wrong side of the page. Floats are usually ok, but in some |
| 1352 |
% cases the columns may become very badly balanced. For ordinary |
| 1353 |
% text, it works perfectly. |
| 1354 |
\newcommand\@PBifBalancePkgPossible[2]{ |
| 1355 |
\ifboolexpr |
| 1356 |
{ |
| 1357 |
togl {@PBunbalpg\@Roman{\@PBprevLastPage}HasFloats} |
| 1358 |
or |
| 1359 |
togl {@PBunbalpg\@Roman{\@PBprevLastPage}HasFootnotes} |
| 1360 |
or |
| 1361 |
togl {@PBunbalpg\@Roman{\@PBprevLastPage}HasMarginpars} |
| 1362 |
or |
| 1363 |
togl {@PBnoBalancePackage} |
| 1364 |
} |
| 1365 |
{#2} |
| 1366 |
{#1} |
| 1367 |
} |
| 1368 |
|
| 1369 |
% \end{macrocode} |
| 1370 |
% |
| 1371 |
% We only want to balance if: |
| 1372 |
% |
| 1373 |
% \begin{enumerate} |
| 1374 |
% |
| 1375 |
% \item The left column is higher than the right one. If that is not the |
| 1376 |
% case, either things are already balanced or the right column is |
| 1377 |
% higher, which means \LaTeX{} was forced into a bad solution already. |
| 1378 |
% |
| 1379 |
% \item This difference is ``large enough'' (6 lines), which means two things: |
| 1380 |
% \begin{enumerate} |
| 1381 |
% |
| 1382 |
% \item There will be an actual improvement in the result. |
| 1383 |
% |
| 1384 |
% \item There is plenty of free space on the right column, so even if |
| 1385 |
% our changes happen to take more space than before, \LaTeX{} will |
| 1386 |
% hopefully not add a new page (but it might make the right |
| 1387 |
% column higher than the left, which is not so great). |
| 1388 |
% \end{enumerate} |
| 1389 |
% |
| 1390 |
% \end{enumerate} |
| 1391 |
% |
| 1392 |
% If there is material only on the left column, we want to balance even |
| 1393 |
% if the difference in height is not ``large enough'', as long as there |
| 1394 |
% are more than 4 lines to split among columns. This is actually quite |
| 1395 |
% unlikely: if there is so little material, the page probably has only |
| 1396 |
% normal text and we will use the balance package. |
| 1397 |
% |
| 1398 |
% \begin{macrocode} |
| 1399 |
|
| 1400 |
\newcommand\@PBifPoorBalancePossible[2]{ |
| 1401 |
|
| 1402 |
% Let's use some shorter names, please |
| 1403 |
\dimdef\@PBtmpH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}Height}} |
| 1404 |
\dimdef\@PBtmpUL{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}UsedLeft}} |
| 1405 |
\dimdef\@PBtmpUR{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}UsedRight}} |
| 1406 |
|
| 1407 |
\dimdef\@PBtmpLH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}LeftHeight}} |
| 1408 |
\dimdef\@PBtmpLFH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}LeftFloatsHeight}} |
| 1409 |
\dimdef\@PBtmpRH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}RightHeight}} |
| 1410 |
\dimdef\@PBtmpRFH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}RightFloatsHeight}} |
| 1411 |
|
| 1412 |
\ifboolexpr{ |
| 1413 |
% Plenty of space on right column, difference is worth it |
| 1414 |
test {\ifdimcomp{\@PBtmpUL}{>}{\@PBtmpUR + 6\baselineskip}} |
| 1415 |
or |
| 1416 |
( |
| 1417 |
test {\ifdimcomp{\@PBtmpUL}{>}{4\baselineskip}} |
| 1418 |
and |
| 1419 |
test {\ifdimcomp{\@PBtmpUR}{=}{\topskip}} % column is empty |
| 1420 |
) |
| 1421 |
}{#1}{#2} |
| 1422 |
} |
| 1423 |
|
| 1424 |
% \end{macrocode} |
| 1425 |
% |
| 1426 |
% If there are floats, things can get messy: imagine that we want to |
| 1427 |
% shrink by 50pt, but there is a 250pt float near the end of the left |
| 1428 |
% column that won't fit after we shrink, so it will move to the right. |
| 1429 |
% What if there is no room for such a large float there? Here, we try |
| 1430 |
% to prevent this situation and shrink by a smaller amount if there |
| 1431 |
% is a float that won't fit. Note that, if the float goes to the |
| 1432 |
% right column, there will be more room for text in the left column. |
| 1433 |
% Therefore, a float will not fit only if the floats on the right |
| 1434 |
% column take too much space. So, what we do here is, starting from |
| 1435 |
% the bottom, check whether the accumulated height of the floats |
| 1436 |
% from the left column fit in the right. If we find a float that |
| 1437 |
% will not fit, we know that we should limit shrinking to the space |
| 1438 |
% below that float. |
| 1439 |
% |
| 1440 |
% In the same vein, we do not want top/bottom floats from the left |
| 1441 |
% column that were deferred from a previous page to be pushed to |
| 1442 |
% the right column. To that end, we limit the shrinking amount to |
| 1443 |
% the space on the left column not taken by top/bottom floats. |
| 1444 |
|
| 1445 |
% \textbf{TODO:} Top and bottom floats may appear in two cases: (1) they |
| 1446 |
% were generated on a previous page and deferred or (2) the user |
| 1447 |
% chose placement options \texttt{t} or \texttt{b}, without |
| 1448 |
% \texttt{h}. Deferred floats are not a problem: we do not reduce |
| 1449 |
% the space available for top/bottom floats, so they should |
| 1450 |
% continue to fit. Floats on the left column with placement |
| 1451 |
% options \texttt{t} and \texttt{b} without \texttt{h} can be |
| 1452 |
% a problem: \LaTeX{} only considers putting a float on the |
| 1453 |
% top/bottom of the current column if, at the point of its |
| 1454 |
% definition, there is enough space for it. Since we are shrinking |
| 1455 |
% the column, it may not fit and be deferred - something the |
| 1456 |
% algorithm does not take into consideration. However, not all is |
| 1457 |
% lost: most likely, the user will only use such placement |
| 1458 |
% options when trying to manually adjust the page, which |
| 1459 |
% means they will be able to detect and work around eventual |
| 1460 |
% problems. |
| 1461 |
% |
| 1462 |
% \begin{macrocode} |
| 1463 |
|
| 1464 |
\ExplSyntaxOn |
| 1465 |
\def\@PBsafetyCheck{ |
| 1466 |
|
| 1467 |
% Let's use some shorter names, please |
| 1468 |
\dimdef\@PBtmpH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}Height}} |
| 1469 |
\dimdef\@PBtmpUL{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}UsedLeft}} |
| 1470 |
\dimdef\@PBtmpUR{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}UsedRight}} |
| 1471 |
|
| 1472 |
\dimdef\@PBtmpLH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}LeftHeight}} |
| 1473 |
\dimdef\@PBtmpLFH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}LeftFloatsHeight}} |
| 1474 |
\dimdef\@PBtmpRH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}RightHeight}} |
| 1475 |
\dimdef\@PBtmpRFH{\csuse{@PBunbalpg\@Roman{\@PBprevLastPage}RightFloatsHeight}} |
| 1476 |
|
| 1477 |
|
| 1478 |
% Make sure we do not push top/bottom floats out of the left col |
| 1479 |
\dimdef\@PBmaxSlack{\@PBtmpLH} |
| 1480 |
|
| 1481 |
\dimdef\@PBtmpdim{0pt} |
| 1482 |
|
| 1483 |
% Start from the bottom |
| 1484 |
\numdef{\@PBtmpcnt}{\seq_count:c {@PBunbalpg\@Roman{\@PBprevLastPage}LeftFloatHeights}} |
| 1485 |
\int_while_do:nNnn {\@PBtmpcnt} > {0} |
| 1486 |
{ |
| 1487 |
|
| 1488 |
\dimdef\@PBcurrentFloatHeight |
| 1489 |
{\seq_item:cn {@PBunbalpg\@Roman{\@PBprevLastPage}LeftFloatHeights} {\@PBtmpcnt}} |
| 1490 |
|
| 1491 |
\dimdef\@PBcurrentFloatSpaceBelow |
| 1492 |
{\seq_item:cn {@PBunbalpg\@Roman{\@PBprevLastPage}LeftFloatSpacesBelow} {\@PBtmpcnt}} |
| 1493 |
|
| 1494 |
\dimdef{\@PBtmpdim}{\@PBtmpdim + \@PBcurrentFloatHeight} |
| 1495 |
|
| 1496 |
\ifboolexpr |
| 1497 |
{ |
| 1498 |
% Does the float fit in the second column? |
| 1499 |
test {\ifdimcomp{\@PBtmpdim}{<}{\@PBtmpRH - \@PBtmpRFH}} |
| 1500 |
and |
| 1501 |
% Is there something above this float, i.e., if we move |
| 1502 |
% this float to the next column, will anything remain? |
| 1503 |
test |
| 1504 |
{ |
| 1505 |
\ifdimcomp |
| 1506 |
{\@PBcurrentFloatSpaceBelow + \@PBcurrentFloatHeight} |
| 1507 |
{<} |
| 1508 |
{\@PBtmpLH - 6\baselineskip} |
| 1509 |
} |
| 1510 |
} |
| 1511 |
{\numdef{\@PBtmpcnt}{\@PBtmpcnt - 1}} % this one is ok; check next |
| 1512 |
{ |
| 1513 |
% this one should not be moved, we found the limit |
| 1514 |
\ifdimcomp{\@PBmaxSlack}{>}{\@PBcurrentFloatSpaceBelow} |
| 1515 |
{\dimdef{\@PBmaxSlack}{\@PBcurrentFloatSpaceBelow}} |
| 1516 |
{} |
| 1517 |
\numdef{\@PBtmpcnt}{0} % exit the loop |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
\ifdimcomp{\@PBmaxSlack}{<}{\@PBslack} |
| 1522 |
{\dimgdef{\@PBslack}{\@PBmaxSlack}} |
| 1523 |
{} |
| 1524 |
} |
| 1525 |
\ExplSyntaxOff |
| 1526 |
|
| 1527 |
% Compare current measurements with previous ``bal'' or ``unbal'' measurements |
| 1528 |
\newcommand\@PBifSomethingChanged[3][bal]{ |
| 1529 |
\ifdefstrequal{\@PBprevLastPage}{\@PBlastPage} |
| 1530 |
{ |
| 1531 |
\@PBifPageDataMatches |
| 1532 |
{pg\@Roman{\@PBlastPage}} |
| 1533 |
{#1pg\@Roman{\@PBlastPage}} |
| 1534 |
{#3} |
| 1535 |
{#2} |
| 1536 |
} |
| 1537 |
{#2} |
| 1538 |
} |
| 1539 |
|
| 1540 |
\newcommand\@PBifPageDataMatches[4]{ |
| 1541 |
\ifboolexpr{ |
| 1542 |
test {\@PBifTogglesMatch{@PB#1HasFloats}{@PB#2HasFloats}} |
| 1543 |
and test {\@PBifTogglesMatch{@PB#1HasFloatcol}{@PB#2HasFloatcol}} |
| 1544 |
and test {\@PBifTogglesMatch{@PB#1HasFootnotes}{@PB#2HasFootnotes}} |
| 1545 |
and test {\@PBifTogglesMatch{@PB#1HasMarginpars}{@PB#2HasMarginpars}} |
| 1546 |
and test {\@PBifDimensionsMatch{@PB#1Height}{@PB#2Height}} |
| 1547 |
and test {\@PBifDimensionsMatch{@PB#1LeftHeight}{@PB#2LeftHeight}} |
| 1548 |
and test {\@PBifDimensionsMatch{@PB#1RightHeight}{@PB#2RightHeight}} |
| 1549 |
and test {\@PBifDimensionsMatch{@PB#1UsedLeft}{@PB#2UsedLeft}} |
| 1550 |
and test {\@PBifDimensionsMatch{@PB#1UsedRight}{@PB#2UsedRight}} |
| 1551 |
} |
| 1552 |
{#3} |
| 1553 |
{#4} |
| 1554 |
} |
| 1555 |
|
| 1556 |
% https://en.wikipedia.org/wiki/XNOR_gate |
| 1557 |
\newcommand\@PBifTogglesMatch[4]{ |
| 1558 |
\ifboolexpr{(togl{#1} or not togl{#2}) and (togl{#2} or not togl{#1})} |
| 1559 |
{#3} |
| 1560 |
{#4} |
| 1561 |
} |
| 1562 |
|
| 1563 |
\newcommand\@PBifDimensionsMatch[4]{ |
| 1564 |
\ifdimcomp{\csuse{#1}}{=}{\csuse{#2}} |
| 1565 |
{#3} |
| 1566 |
{#4} |
| 1567 |
} |
| 1568 |
|
| 1569 |
\newcommand\@PBsaveUnbalancedInfo[1][]{ |
| 1570 |
|
| 1571 |
% If there are no two-column pages in the |
| 1572 |
% document, there is nothing to save. |
| 1573 |
\ifcsdef{@PBlastPage} |
| 1574 |
{ |
| 1575 |
\immediate\write\@mainaux{\gdef\string\@PBprevLastPage{\@PBlastPage}} |
| 1576 |
|
| 1577 |
\ifboolexpr{test {\ifdefvoid{\@PBprevLastPage}} or test {\ifstrequal{#1}{update}}} |
| 1578 |
% Columns are currently unbalanced, either because this is |
| 1579 |
% the first pass (we do not have any previous information) |
| 1580 |
% or because the document has not stabilized yet, so any |
| 1581 |
% previous information is unreliable. Save the information |
| 1582 |
% we just collected as ``unbalanced''. |
| 1583 |
{\@PBsavePageDataAs{pg\@Roman{\@PBlastPage}}{unbalpg\@Roman{\@PBlastPage}}} |
| 1584 |
% Not the first pass, so columns are possibly balanced; |
| 1585 |
% to save the unbalanced information, repeat what was |
| 1586 |
% gathered during the first pass. |
| 1587 |
{\@PBsavePageData{unbalpg\@Roman{\@PBprevLastPage}}} |
| 1588 |
} |
| 1589 |
{} |
| 1590 |
} |
| 1591 |
|
| 1592 |
\def\@PBsaveBalancedInfo{ |
| 1593 |
\global\toggletrue{@PBpg\@Roman{\@PBlastPage}alreadyBalanced} |
| 1594 |
\@PBsaveToggle{@PBpg\@Roman{\@PBlastPage}alreadyBalanced} |
| 1595 |
|
| 1596 |
\@PBsavePageDataAs{pg\@Roman{\@PBlastPage}}{balpg\@Roman{\@PBlastPage}} |
| 1597 |
} |
| 1598 |
|
| 1599 |
\def\@PBnotifyRerun{ |
| 1600 |
\AtVeryEndDocument{ |
| 1601 |
% If there are no two-column pages in the document, |
| 1602 |
% it makes no sense to emit this warning. |
| 1603 |
\ifcsdef{@PBlastPage} |
| 1604 |
{ |
| 1605 |
\PackageWarningNoLine{pbalance} |
| 1606 |
{Last two-column page cols not balanced. Rerun LaTeX} |
| 1607 |
} |
| 1608 |
{ |
| 1609 |
\PackageWarningNoLine{pbalance} |
| 1610 |
{No two-column pages, doing nothing} |
| 1611 |
} |
| 1612 |
} |
| 1613 |
} |
| 1614 |
|
| 1615 |
\ExplSyntaxOn |
| 1616 |
\newcommand\@PBcopyPageData[2]{ |
| 1617 |
|
| 1618 |
\global\providetoggle{@PB#2HasFloats} |
| 1619 |
\global\providetoggle{@PB#2HasFloatcol} |
| 1620 |
\global\providetoggle{@PB#2HasFootnotes} |
| 1621 |
\global\providetoggle{@PB#2HasMarginpars} |
| 1622 |
|
| 1623 |
\iftoggle{@PB#1HasFloats} |
| 1624 |
{\global\toggletrue{@PB#2HasFloats}} |
| 1625 |
{\global\togglefalse{@PB#2HasFloats}} |
| 1626 |
|
| 1627 |
\iftoggle{@PB#1HasFloatcol} |
| 1628 |
{\global\toggletrue{@PB#2HasFloatcol}} |
| 1629 |
{\global\togglefalse{@PB#2HasFloatcol}} |
| 1630 |
|
| 1631 |
\iftoggle{@PB#1HasFootnotes} |
| 1632 |
{\global\toggletrue{@PB#2HasFootnotes}} |
| 1633 |
{\global\togglefalse{@PB#2HasFootnotes}} |
| 1634 |
|
| 1635 |
\iftoggle{@PB#1HasMarginpars} |
| 1636 |
{\global\toggletrue{@PB#2HasMarginpars}} |
| 1637 |
{\global\togglefalse{@PB#2HasMarginpars}} |
| 1638 |
|
| 1639 |
\csdimgdef{@PB#2Height}{\csuse{@PB#1Height}} |
| 1640 |
\csdimgdef{@PB#2LeftHeight}{\csuse{@PB#1LeftHeight}} |
| 1641 |
\csdimgdef{@PB#2LeftFloatsHeight}{\csuse{@PB#1LeftFloatsHeight}} |
| 1642 |
\csdimgdef{@PB#2RightHeight}{\csuse{@PB#1RightHeight}} |
| 1643 |
\csdimgdef{@PB#2RightFloatsHeight}{\csuse{@PB#1RightFloatsHeight}} |
| 1644 |
\csdimgdef{@PB#2UsedLeft}{\csuse{@PB#1UsedLeft}} |
| 1645 |
\csdimgdef{@PB#2UsedRight}{\csuse{@PB#1UsedRight}} |
| 1646 |
|
| 1647 |
\seq_gclear_new:c {@PB#2LeftFloatHeights} |
| 1648 |
\seq_gclear_new:c {@PB#2LeftFloatSpacesBelow} |
| 1649 |
\seq_gset_eq:cc {@PB#2LeftFloatHeights} {@PB#1LeftFloatHeights} |
| 1650 |
\seq_gset_eq:cc {@PB#2LeftFloatSpacesBelow} {@PB#1LeftFloatSpacesBelow} |
| 1651 |
} |
| 1652 |
\ExplSyntaxOff |
| 1653 |
|
| 1654 |
\newcommand\@PBsavePageDataAs[2]{ |
| 1655 |
\@PBsaveDimAs{@PB#1Height}{@PB#2Height} |
| 1656 |
\@PBsaveDimAs{@PB#1LeftHeight}{@PB#2LeftHeight} |
| 1657 |
\@PBsaveDimAs{@PB#1LeftFloatsHeight}{@PB#2LeftFloatsHeight} |
| 1658 |
\@PBsaveDimAs{@PB#1RightHeight}{@PB#2RightHeight} |
| 1659 |
\@PBsaveDimAs{@PB#1RightFloatsHeight}{@PB#2RightFloatsHeight} |
| 1660 |
\@PBsaveDimAs{@PB#1UsedLeft}{@PB#2UsedLeft} |
| 1661 |
\@PBsaveDimAs{@PB#1UsedRight}{@PB#2UsedRight} |
| 1662 |
|
| 1663 |
\@PBsaveToggleAs{@PB#1HasFloats}{@PB#2HasFloats} |
| 1664 |
\@PBsaveToggleAs{@PB#1HasFloatcol}{@PB#2HasFloatcol} |
| 1665 |
\@PBsaveToggleAs{@PB#1HasFootnotes}{@PB#2HasFootnotes} |
| 1666 |
\@PBsaveToggleAs{@PB#1HasMarginpars}{@PB#2HasMarginpars} |
| 1667 |
|
| 1668 |
\@PBsaveSeqAs{@PB#1LeftFloatHeights}{@PB#2LeftFloatHeights} |
| 1669 |
\@PBsaveSeqAs{@PB#1LeftFloatSpacesBelow}{@PB#2LeftFloatSpacesBelow} |
| 1670 |
} |
| 1671 |
|
| 1672 |
\newcommand\@PBsavePageData[1]{ |
| 1673 |
\@PBsavePageDataAs{#1}{#1} |
| 1674 |
} |
| 1675 |
|
| 1676 |
\newcommand\@PBsaveToggleAs[2]{ |
| 1677 |
\immediate\write\@mainaux{\providetoggle{#2}} |
| 1678 |
\iftoggle{#1} |
| 1679 |
{\immediate\write\@mainaux{\global\toggletrue{#2}}} |
| 1680 |
{\immediate\write\@mainaux{\global\togglefalse{#2}}} |
| 1681 |
} |
| 1682 |
|
| 1683 |
\newcommand\@PBsaveToggle[1]{ |
| 1684 |
\@PBsaveToggleAs{#1}{#1} |
| 1685 |
} |
| 1686 |
|
| 1687 |
\newcommand\@PBsaveDimAs[2]{ |
| 1688 |
\immediate\write\@mainaux{\csdimgdef{#2}{\csuse{#1}}} |
| 1689 |
} |
| 1690 |
|
| 1691 |
\newcommand\@PBsaveDim[1]{ |
| 1692 |
\@PBsaveDimAs{#1}{#1} |
| 1693 |
} |
| 1694 |
|
| 1695 |
\ExplSyntaxOn |
| 1696 |
\newcommand\@PBsaveSeqAs[2]{ |
| 1697 |
\immediate\write\@mainaux{\string\ExplSyntaxOn} |
| 1698 |
\immediate\write\@mainaux{\string\seq_gclear_new:c {#2}} |
| 1699 |
\seq_map_inline:cn {#1} |
| 1700 |
{\immediate\write\@mainaux{\string\global\string\seq_put_right:cn {#2}{##1}}} |
| 1701 |
\immediate\write\@mainaux{\string\ExplSyntaxOff} |
| 1702 |
} |
| 1703 |
\ExplSyntaxOff |
| 1704 |
|
| 1705 |
\newcommand\@PBsaveSeq[1]{ |
| 1706 |
\@PBsaveSeqAs{#1}{#1} |
| 1707 |
} |
| 1708 |
|
| 1709 |
% \end{macrocode} |
| 1710 |
% \Finale |