Debian Codegraph @

texmf-dist/source/latex/tools/multicol.dtx

raw
1 % \iffalse meta-comment
2 %
3 %
4 % This file is part of the Standard LaTeX `Tools Bundle'.
5 % -------------------------------------------------------
6 %
7 % It may be distributed and/or modified under the
8 % conditions of the LaTeX Project Public License, either version 1.3c
9 % of this license or (at your option) any later version.
10 % The latest version of this license is in
11 % https://www.latex-project.org/lppl.txt
12 % and version 1.3c or later is part of all distributions of LaTeX
13 % version 2005/12/01 or later.
14 %
15 % The list of all files belonging to the LaTeX `Tools Bundle' is
16 % given in the file `manifest.txt'.
17 %
18 % \fi
19 %
20 % \iffalse This is a METACOMMENT
21 %
22 %% Package `multicol' to use with LaTeX2e
23 %% Copyright 1989-2025 Frank Mittelbach
24 %%
25 %% In addition to the terms of LPPL any distributed version
26 %% (unchanged or modified) of multicol has to keep the statement
27 %% about the moral obligation for using multicol. In case of major
28 %% changes where this would not be appropriate the author of the
29 %% changed version should contact the copyright holder.
30 %%
31 %%
32 %% Moral obligation for using multicol:
33 %% ------------------------------------
34 %%
35 %% Users of multicol who wish to include or use multicol or a modified
36 %% version in a proprietary and commercially market product are asked
37 %% under certain conditions (see below) for the payment of a license
38 %% fee. The size of this fee is to be determined, in each instance,
39 %% by the commercial user, depending on his/her judgment of the value of
40 %% multicol for his/her product.
41 %%
42 %%
43 %% The conditions for this are as follows:
44 %%
45 %% The producer of a proprietary and commercially market product
46 %% that involves typesetting using multicol is asked to determine
47 %% the value of a license fee for using multicol if
48 %%
49 %% - the product is a document and the producer has decided to
50 %% include multicol to typeset (parts of) the document or has
51 %% directed the author of the document to include multicol (for
52 %% example, by providing a class file to be used by the author)
53 %%
54 %% - the product is a LaTeX class or package that includes multicol
55 %%
56 %%
57 %% There is no moral obligation in case
58 %%
59 %% - the product is a document but producer has not directed
60 %% the author to include multicol (in that case the moral obligation
61 %% lies with the author of the document)
62 %%
63 %% - the product does not involve typesetting, e.g., consists, for
64 %% example, of distributing multicol and its documentation.
65 %%
66 %% - the product is not proprietary, i.e., is made available as free
67 %% software itself (which doesn't prohibit its commercial marketing)
68 %%
69 %% - multicol is used for non-commercial purposes
70 %%
71 %%
72 %% Determining a license fee might result in a license fee of zero
73 %% (i.e., no payment) in case a producer has determined that the use
74 %% of multicol has no enhancing effect on the product. This is a
75 %% plausible scenario, i.e., in the above two cases the producer is
76 %% only asked to evaluate the value of multicol for the product
77 %% not for the payment of a license fee per se (which might or might
78 %% not follow from this evaluation).
79 %%
80 %% The license fee, if any, can be paid either to the LaTeX fund
81 %% (see ltx3info.txt in the base LaTeX distribution) or to the author of
82 %% the program who can be contacted at
83 %%
84 %% [email protected]
85 %%
86 %<*dtx>
87 \ProvidesFile{multicol.dtx}
88 %</dtx>
89 %<package>
90 % For any rollback before 2017 load the 2017 version and hope for the best:
91 %<package>\DeclareRelease{}{1994-06-01}{multicol-2017-04-11.sty}
92 %
93 %<package>\DeclareRelease{}{2017-04-11}{multicol-2017-04-11.sty}
94 %<package>\DeclareRelease{v1.8}{2019-10-01}{multicol-2019-10-01.sty}
95 %<package>\DeclareRelease{v1.9}{2021-11-15}{multicol-2024-05-23.sty}
96 %<package>\DeclareCurrentRelease{}{2021-11-15}
97 %<package>
98 %<package>\ProvidesPackage{multicol}
99 %<driver> \ProvidesFile{multicol.drv}
100 % \fi
101 % \ProvidesFile{multicol.dtx}
102 [2025/10/21 v2.0b multicolumn formatting (FMi)]
103 %
104 %
105 %
106 %
107 % \changes{v1.9c}{2021/11/30}{Added rollback to v1.8}
108 % \changes{v1.5n}{1997/06/05}{Applied improvement of documentation,
109 % kindly done by Robin Fairbairns.}
110 % \changes{v1.4h}{1992/06/04}{Added mark tracing with
111 % tracingmulticols$\ge2$}
112 % \changes{v1.4a}{1992/02/11}{Added support for multicol in inner mode}
113 % \changes{v1.0d}{1989/05/17}{All lines shortened to 72 or less.}
114 % \changes{v1.0e}{1989/06/21}{Redefinition of description env. to use
115 % \cs{descriptionmargin}\quotechar=5pt
116 % in documentation.}
117 % \changes{v1.0f}{1989/07/11}{Changed \cs{z@} to 0pt in redefinition of
118 % description.}
119 % \changes{v1.1a}{1989/09/20}{\cs{multicolssep} changed to \cs{multicolsep}.}
120 % \changes{v1.9b}{2021/10/22}{Swap names \cs{mult@gfirstbox}
121 % and \cs{mult@firstbox} (gh/701)}
122 %
123 % \def\description{\list{}{\labelwidth 0pt \leftmargin\descriptionmargin
124 % \itemindent-\leftmargin \let\makelabel\descriptionlabel}}
125 % \newdimen\descriptionmargin \descriptionmargin=5pt
126 %
127 % \DoNotIndex{\@M,\@Mi,\@bsphack,\@cclv,\@colht,\@currlist,\@deferlist}
128 % \DoNotIndex{\@elt,\@esphack,\@floatplacement}
129 % \DoNotIndex{\@ifundefined,\@ifnextchar,\@makecol}
130 % \DoNotIndex{\@ne,\@outputpage,\@scolelt,\@spaces,\@tempb,\@tempcnta}
131 % \DoNotIndex{\@width}
132 % \DoNotIndex{\addvspace,\advance,\allowbreak}
133 % \DoNotIndex{\baselineskip,\begingroup,\box,\columnsep,\copy,\count}
134 % \DoNotIndex{\count@,\def,\dimen@,\divide,\docdate}
135 % \DoNotIndex{\edef,\eject,\egroup,\else,\endgroup,\endinput}
136 % \DoNotIndex{\fi,\fileversion,\filedate}
137 % \DoNotIndex{\gdef,\global}
138 % \DoNotIndex{\hbox,\hfil,\hrule,\ht,\hss}
139 % \DoNotIndex{\ifdim,\ifnum,\ifvoid,\ignorespaces,\insert,\immediate}
140 % \DoNotIndex{\let,\loop}
141 % \DoNotIndex{\@maxdepth,\message,\multiply}
142 % \DoNotIndex{\newbox,\newcount,\newdimen,\newskip,\number,\newpage}
143 % \DoNotIndex{\outputpenalty,\p@,\penalty}
144 % \DoNotIndex{\relax,\repeat,\setbox,\skip,\space,\splitmaxdepth}
145 % \DoNotIndex{\splittopskip,\string,\sixt@@n}
146 % \DoNotIndex{\the,\thepage,\thr@@,\topskip,\tw@,\typeout}
147 % \DoNotIndex{\unvbox,\vbox,\vfill,\vsplit,\voidb@x,\vrule}
148 % \DoNotIndex{\write,\wd}
149 % \DoNotIndex{\z@}
150 %
151 % \MakeShortVerb{\|}
152 % \newcommand{\mc}{\textsf{multicols}}
153 % \newcommand{\TUB}{{\sl TUGboat\/}}
154 % \newcommand{\TB}{{\sl\TeX book\/}}
155 %
156 %
157 % \setcounter{StandardModuleDepth}{2}
158 % \setcounter{collectmore}{3}
159 %
160 % \GetFileInfo{multicol.dtx}
161 % \title{An environment for multicolumn output\thanks{This file
162 % has version number \fileversion, last
163 % revised \filedate.}%
164 % \thanks{Note: This package is released under terms which affect
165 % its use in commercial applications. Please see the details at
166 % the top of the source file.}}
167 % \author{Frank Mittelbach\\
168 % {\rm Email:} see top of the source file}
169 % \date{Printed \today}
170 %
171 % \MaintainedByLaTeXTeam{tools}
172 % \maketitle
173 %
174 % \begin{abstract}
175 % This article describes the use and the implementation of the \mc{}
176 % environment. This environment allows switching between
177 % one and multicolumn format on the same page. Footnotes are handled
178 % correctly (for the most part), but will be placed at the bottom of
179 % the page and not under each column. \LaTeX{}'s float mechanism,
180 % however, is partly disabled in this implementation. At the
181 % moment only page-wide floats (i.e., star-forms) can be used within
182 % the scope of the environment.
183 % \end{abstract}
184 %
185 % \begin{multicols}{3}[\section*{Preface to versions 1.9 + 2.0}]
186 %
187 % Version 1.9 added tagging support and also a number of smaller
188 % enhancements, such as an optional argument to
189 % \cs{columnbreak} to allow for conditional breaks instead of
190 % forced ones. The min column depth was also made customizable
191 % (previously it was hardwired to the depth of \enquote{p}) to support
192 % special cases and in particular languages that do not have
193 % characters with any noticeable depth such as, for example, Japanese.
194 %
195 % Verson 2.0 then simplified and improved the mark handling, by
196 % fully supporting the new mark mechanism of \LaTeX.
197 %
198 % \end{multicols}
199 %
200 %
201 %
202 % \begin{multicols}{3}[\section*{Preface to version 1.8}]
203 % The 1.8 release improves on the balancing approach. If due to a
204 % limited number of break points (e.g., due to large objects) the
205 % balanced columns exceed the available vertical space, then
206 % balancing is canceled and a normal page is produced first. Some
207 % overflow is allowed (controlled by the parameter
208 % |\maxbalancingoverflow| which defaults to 12pt). This ensures
209 % that we only cut a normal page if we get enough material carried
210 % over to next page.
211 %
212 % Also added was support for |\enlargethispage|. This means it is
213 % now possible to request a page to be artificially enlarged or
214 % shortened. Note that if you enlarge pages by more than one line
215 % you may have to increase the \textsf{collectmore} counter value to
216 % ensure that enough material is being picked up.
217 %
218 % This command was used on the second page of this manual to shorten
219 % it by one line, in order to get rid of a number of widow lines on
220 % the following pages.
221 %
222 % There are also some small enhancements to the balancing algorithm
223 % including a ways to require a minimum number of rows in the result.
224 %
225 % Finally, version 1.8 adds the command |\docolaction| to help with
226 % more complicated actions that depend on the current column. This
227 % command expects 3 arguments: code that is executed if we are in
228 % the ``first'' column, code to execute if we end up in any
229 % ``middle'' column (if there are more than two) and finally code to
230 % execute if we are in the ``last'' column. Thus
231 %\begin{verbatim}
232 % \docolaction{first}
233 % {middle}{last}
234 %\end{verbatim}
235 % would typeset a different word depending the type of column this
236 % code is executed. Using it like this is probably pointless, but
237 % you can imagine applications like writing something into the
238 % nearest margin, etc.
239 %
240 % As this feature needs at least two \LaTeX{} runs to produce correct
241 % results and as it adds to the processing complexity it is only
242 % made available if one add the option \texttt{colaction} when
243 % loading the package.
244 %
245 % \end{multicols}
246 %
247 %
248 % \begin{multicols}{3}[\section*{Preface to version 1.7
249 % (right to left support)}]
250 % The 1.7 release adds support for languages that are typeset
251 % right-to-left. For those languages the order of the columns on the page
252 % also need to be reversed---something that wasn't supported before. The next
253 % paragraph demonstrates the result (as it is typeset as if we are writing
254 % in a left-to-right language--- so read the rightmost column
255 % first). The change is initialized via
256 % |\RLmulticolcolumns| and returning to left-right (default)
257 % is done via |\LRmulticolcolumns|.
258 % \end{multicols}
259 %
260 % \RLmulticolcolumns
261 %
262 % \begin{multicols}{3}
263 % Right-to-left typesetting will only reverse the column orders. Any
264 % other support needed will have to be provided by other means,
265 % e.g., using appropriate fonts and reversing the writing directions
266 % within the columns. As footnotes are typeset in full measure the
267 % footnote rule needs to be redefined as if they are below a single
268 % column, i.e., using |\textwidth| not |\columnwidth|. For example:
269 %\begin{verbatim}
270 %\renewcommand \footnoterule{%
271 % \kern-3pt\hbox to\textwidth
272 % {\hskip .6\textwidth
273 % \hrulefill }%
274 % \kern2.6pt}
275 %\end{verbatim}
276 % \end{multicols}
277 %
278 % \LRmulticolcolumns
279 %
280 % \begin{multicols}{3}[\section*{Preface to version 1.5 + 1.6}]
281 % \hbadness=10000
282 % The 1.5 release contains two major changes: \mc{} will now
283 % support up to 10 columns and two more tuning possibilities have
284 % been added to the balancing routine. The balancing routine now
285 % checks the badness
286 % of the resulting columns and rejects solutions that are larger
287 % than a certain threshold.
288 % At the same time \mc{} has been upgraded to run under \LaTeXe{}.
289 %
290 % Later changes to 1.5 include |\columnbreak| and |multicols*|.
291 %
292 % For version 1.6 micro-spacing around the boxes produced by \mc{}
293 % has been improved to allow for baseline-grid typesetting.
294 % \end{multicols}
295 %
296 %
297 % \setcounter{collectmore}{2}
298 % \begin{multicols}{3}[\section{Introduction}]
299 % \hbadness=10000
300 % Switching between two-column and one-column layout is possible in
301 % \LaTeX{}, but every use of |\twocolumn| or |\onecolumn|
302 % starts a new page. Moreover, the last page of two-column output
303 % isn't balanced and this often results in an empty, or nearly
304 % empty, right column. When I started to write macros for
305 % \textsf{doc.sty} (see ``The \texttt{doc}--Option'', \TUB\
306 % volume 10~\#2, pp.~245--273) I thought that it would be nice to
307 % place the index
308 % on the same page as the bibliography. And balancing the last page
309 % would not only look better, it also would save space; provided of
310 % course that it is also possible to start the next article on the
311 % same page. Rewriting the index environment was comparatively easy,
312 % but the next goal, designing an environment which takes care of
313 % footnotes, floats, etc., was a harder task. It took me a whole
314 % weekend\footnote{I started with the algorithm given in the \TeX
315 % book on page 417. Without this help a weekend would not have been
316 % enough. (This remark was made in the documentation of the initial
317 % release, since then several hundreds more hours went into
318 % improving the original code.)} to get together the few lines of
319 % code below and there is
320 % still a good chance that I missed something after all.
321 %
322 % Try it and, hopefully, enjoy it; and {\em please\/} direct bug
323 % reports and suggestions back to Mainz.
324 % \end{multicols}
325 %
326 %
327 % \setcounter{collectmore}{0}
328 % \begin{multicols}{3}[\section{The User Interface}]
329 % \hbadness=10000
330 % To use the environment one simply says\\*[2mm]
331 % \hspace*{2mm}|\begin{multicols}{|\meta{number}|}|
332 % \hspace*{12mm}\meta{multicolumn text}\\
333 % \hspace*{2mm}|\end{multicols}|\\[2mm]
334 % where \meta{number} is the required number of columns and
335 %^^A\meta{multicolumn text}
336 % $\langle${\it multi\-column text\/}$\rangle$ may contain arbitrary
337 % \LaTeX{} commands, except that floats and marginpars are not
338 % allowed in the current implementation\footnote{This is dictated by
339 % lack of time. To implement floats one has to reimplement the
340 % whole \LaTeX{} output routine.}.
341 %
342 % \enlargethispage{-1\baselineskip}
343 %
344 % \DescribeMacro\premulticols
345 % As its first action, the \textsf{multicols} environment measures the
346 % current page to determine whether there is enough room for some
347 % portion of multicolumn output. This is controlled by the
348 % \meta{dimen} variable |\premulticols| which can be changed by
349 % the user with ordinary \LaTeX{} commands.
350 % \DescribeMacro\multicolsep
351 % If the space is less than |\premulticols|, a new page is
352 % started. Otherwise, a |\vskip| of |\multicolsep| is
353 % added.\footnote{Actually the added space may be less because we use
354 % \cs{addvspace} (see the \LaTeX{} manual for further
355 % information about this command).}
356 %
357 % \DescribeMacro\postmulticols
358 % When the end of the \mc{} environment is encountered, an
359 % analogous mechanism is employed, but now we test whether there is a
360 % space larger than |\postmulticols| available. Again we add
361 % |\multicolsep| or start a new page.
362 %
363 % It is often convenient to spread some text over all columns, just
364 % before the multicolumn output, without any page break in between. To
365 % achieve this the \mc{} environment has an optional second
366 % argument which can be used for this purpose. For example, the text
367 % you are now reading was started with
368 % \begin{verbatim}
369 % \begin{multicols}{3}
370 % [\section{The User
371 % Interface}] ...
372 %\end{verbatim}
373 % If such text is unusually long (or short) the value of
374 % |\premulticols| might need adjusting to prevent a bad page
375 % break. We therefore provide a third argument which can be used to
376 % overwrite the default value of |\premulticols| just for this
377 % occasion. So if you want to combine some longer single column text
378 % with a multicols environment you could write
379 % \begin{verbatim}
380 % \begin{multicols}{3}
381 % [\section{Index}
382 % This index contains ...]
383 % [6cm]
384 % ...
385 %\end{verbatim}
386 %
387 %
388 % \DescribeMacro\columnsep
389 % The space between columns is controlled by the length parameter
390 % |\columnsep|. The width for the individual columns is automatically
391 % calculated from this parameter and the current |\linewidth|. In this
392 % article a value of \textsf{\the\columnsep} was used.
393 %
394 %
395 % \DescribeMacro\columnseprule
396 % Separation of columns with vertical rules is achieved by setting the
397 % parameter |\columnseprule| to some positive value. In this
398 % article a value of \textsf{.4pt} was used.
399 %
400 % \DescribeMacro\columnseprulecolor
401 % The color of the rules separating the columns can be specified through
402 % |\columnseprulecolor|. The default value is |\normalcolor|.
403 %
404 % \DescribeMacro\multicolbaselineskip
405 % Since narrow columns tend to need adjustments in interline spacing
406 % we also provide a \meta{skip} parameter called
407 % |\multicolbaselineskip| which is added to the
408 % |\baselineskip| parameter inside the \mc{}
409 % environment. Please use this parameter with care or leave it alone;
410 % it is intended only for package file designers since even small
411 % changes might produce totally unexpected changes to your document.
412 %
413 %
414 % \subsection{Balancing columns}
415 %
416 %
417 % Besides the previously mentioned parameters, some others are
418 % provided to influence the layout of the columns generated.
419 %
420 % Paragraphing in \TeX{} is controlled by several parameters. One of
421 % the most important is called |\tolerance|: this controls the
422 % allowed `looseness' (i.e.\ the amount of blank space between words).
423 % Its default value is 200 (the \LaTeX{} |\fussy|) which is too
424 % small for narrow columns.
425
426 % Setting it to \texttt{10000} (a.k.a.\ $\infty$) means arbitrary bad
427 % lines are possible. With that setting \LaTeX{} will make most lines
428 % perfect but intermix them with really bad lines. This was the
429 % setting originally used by \cs{sloppy} (nowadays it is a bit more
430 % cautious and used \texttt{9999} which makes a huge
431 % difference).\footnote{Look at the next paragraph, it was set with
432 % the \cs{tolerance}\texttt{=10000}.}
433 %
434 % \begin{sloppypar}\tolerance=10000
435 % \DescribeMacro\multicoltolerance \DescribeMacro\multicolpretolerance
436 % We therefore use a |\multicoltolerance| parameter for the
437 % |\tolerance| value inside the \mc{} environment. Its default value
438 % is 9999 which is less than infinity but `bad' enough for most
439 % paragraphs in a multicolumn environment. Changing its value should
440 % be done outside the \mc{} environment. Since |\tolerance| is set
441 % to |\multicoltolerance| at the beginning of every \textsf{multicols}
442 % environment one can locally overwrite this default by assigning
443 % \cs{tolerance}\texttt{=}\meta{desired value}. There also exists a
444 % |\multicolpretolerance| parameter holding the value for
445 % |\pretolerance| within a \mc{} environment. Both parameters are
446 % usually used only by package or class designers.
447 % \end{sloppypar}
448 %
449 % Generation of multicolumn output can be divided into two parts. In
450 % the first part we are collecting material for a page, shipping it
451 % out, collecting material for the next page, and so on. As a second
452 % step, balancing will be done when the end of the \mc{} environment
453 % is reached.
454 % In the first step \TeX{} might consider more material whilst
455 % finding the final column content than it actually uses when shipping out the
456 % page. This might cause a problem if a footnote is encountered in
457 % the part of the input considered, but not used, on the current page.
458 % In this case the footnote might show up on the current page, while the
459 % footnotemark corresponding to this footnote might be set on the next
460 % one.\footnote{The reason behind this behavior is the asynchronous
461 % character of the \TeX{} {\it page\_builder}.
462 % However, this
463 % could be avoided by defining very complicated output
464 % routines which don't use \TeX{} primitives like
465 % \cs{insert} but do everything by hand.
466 % This is clearly beyond the scope of a weekend problem.}
467 % Therefore the \mc{} environment gives a warning
468 % message\footnote{This message will be generated even if there are no
469 % footnotes in this part of the text.} whenever it is unable to use
470 % all the material considered so far.
471 %
472 % If you don't use footnotes too often the chances of something
473 % actually going wrong are very slim, but if this happens you can help
474 % \TeX{} by using a |\pagebreak| command in the final document.
475 % Another way to influence the behavior of \TeX{} in this respect is
476 % given by the counter variable `\textsf{collectmore}'. If you use the
477 % |\setcounter| declaration to set this counter to \meta{number},
478 % \TeX{} will consider \meta{number} more (or less) lines before
479 % making its final decision. So a value of $-1$ may solve all your
480 % problems at the cost of slightly less optimal columns.
481 %
482 %
483 % In the second step (balancing columns) we have other bells and
484 % whistles. First of all you can say |\raggedcolumns| if you
485 % don't want the bottom lines to be aligned.
486 % The default is |\flushcolumns|, so \TeX{} will normally try to
487 % make both
488 % the top and bottom baselines of all columns align.
489 %
490 % If there is only a small amount of material available for balancing
491 % then you may end up with very few lines per column. In an extreme
492 % case there may be only one line which looks distinctly odd. In that
493 % case it might be better to have more material distributed to the
494 % earlier columns even if that means that later columns are empty or
495 % partially empty. This is controlled through the counter
496 % `\textsf{minrows}' (default 1). If set to a higher value then the
497 % balancing will have at least that many rows in the first column (and
498 % also all further columns until it runs outs of material).
499 %
500 % Additionally you can set another counter, the `\textsf{unbalance}'
501 % counter, to some positive \meta{number}. This will make all but the
502 % right-most column \meta{number} of lines longer than they would
503 % normally have been. `Lines' in this context refer to normal text
504 % lines (i.e.\ one |\baselineskip| apart); thus, if your columns
505 % contain displays,
506 % for example, you may need a higher \meta{number}
507 % to shift something from one column into another. A negative value can make sense if
508 % you have set \textsf{minrows} and want to locally adjust that.
509 %
510 % Unlike `\textsf{collectmore},' the `\textsf{unbalance}' counter is reset
511 % to zero at the end of the environment so it only applies to one
512 % \mc{} environment.
513 %
514 % The two methods may be combined but I suggest using these features
515 % only when fine tuning important publications.
516 %
517 % Two more general tuning possibilities were added with version~1.5.
518 % \TeX{} allows to measure the badness of a column in terms of an
519 % integer value, where 0 means optimal and any higher value means a
520 % certain amount of extra white space. 10000 is considered to be
521 % infinitely bad (\TeX{} does not distinguish any further). In addition
522 % the special value 100000 means overfull (i.e., the column contains
523 % more text than could possibly fit into it).
524 %
525 % The new release now measures every generated column and ignores
526 % solutions where at least one column has a badness being larger than
527 % the value of the counter \textsf{columnbadness}. The default value for
528 % this counter is 10000, thus \TeX{} will accept all solutions except
529 % those being overfull.
530 % By setting the counter to a smaller value you can force the algorithm
531 % to search for solutions that do not have columns with a lot of white
532 % space.
533 %
534 % However, if the setting is too low, the algorithm may not find any
535 % acceptable solution at all and will then finally choose the extreme
536 % solution of placing all text into the first column.
537 %
538 % Often, when columns are balanced, it is impossible to find a solution
539 % that distributes the text evenly over all columns. If that is the case
540 % the last column usually has less text than the others. In the earlier
541 % releases this text was stretched to produce a column with the same
542 % height as all others, sometimes resulting in really ugly looking
543 % columns.
544 %
545 % In the new release this stretching is only done if the badness of
546 % the final column is not larger than the value of the counter
547 % \textsf{finalcolumnbadness}. The default setting is 9999, thus preventing
548 % the stretching for all columns that \TeX{} would consider infinitely
549 % bad. In that case the final column is allowed to run short which gives
550 % a much better result.
551 %
552 % And there are two more parameters of some experimental nature, one
553 % called |\multicolovershoot| the other |\multicolundershoot|. They
554 % control the amount of space a column within the \mc{} environment is
555 % allowed to be ``too full'' or ``too short'' without affecting the
556 % column badness. They are set to 0pt and 2pt, respectively.
557 %
558 % Finally, when doing the balancing at the end, columns may become
559 % higher than the remaining available space. In that case the
560 % algorithm aborts and instead generates a normal page. However, if the
561 % amount is not too large, e.g., a line or so, then it might be better to
562 % keep everything on the same page instead of starting a new page with
563 % just one line after balancing. So the parameter
564 % |\maxbalancingoverflow| governs this process: only when the excess
565 % gets larger than its value balancing is aborted.
566 %
567 % \subsection{Not balancing the columns}
568 %
569 % Although this package was written to solve the problem of balancing
570 % columns, I got repeated requests to provide a version where all
571 % white space is automatically placed in the last column or
572 % columns. Since version v1.5q this now exists: if you use
573 % \texttt{multicols*} instead of the usual environment the columns on
574 % the last page are not balanced. Of course, this environment only
575 % works on top-level, e.g., inside a box one has to balance to
576 % determine a column height in absence of a fixed value.
577 %
578 %
579 % \subsection{Manually breaking columns}
580 %
581 % Another request often voiced was: ``How do I tell \LaTeX{} that it
582 % should break the first column after this particular line?''. The
583 % |\pagebreak| command (which works with the two-column option of
584 % \LaTeX{}) is of no use here since it would end the collection phase
585 % of \mc{} and thus all columns on that page. So with version 1.5u
586 % the |\columnbreak| command was added. If used within a paragraph it
587 % marks the end of the current line as the desired breakpoint. You can
588 % observe its effect on
589 % the previous page where three lines of text have been artificially
590 % forced into the second column (resulting in some white
591 % space between paragraphs in the first column).
592 %
593 % From version 1.9 onwards \cs{columnbreak} accepts an optional
594 % argument (just like \cs{pagebreak}) in which you can specify the
595 % desirability to break the column at that point: supported values are
596 % \texttt{0} (slightly desirable) to \texttt{4} (forced).
597 % This version also adds \cs{newcolumn}
598 % which forces a column break but runs the column short (comparable to
599 % \cs{newpage}).
600 %
601 % \subsection{Floats inside a \mc{} environment}
602 %
603 % Within the \mc{} environment the usual star float commands are
604 % available but their function is somewhat different as in the
605 % two-column mode of standard \LaTeX. Stared floats, e.g.,
606 % \texttt{figure*}, denote page wide floats that are handled in a similar
607 % fashion as normal floats outside the \mc{} environment. However,
608 % they will never show up on the page where they are encountered. In
609 % other words, one can influence their placement by specifying a
610 % combination of \texttt{t}, \texttt{b}, and/or \texttt{p} in their optional
611 % argument, but \texttt{h} doesn't work because the first possible place
612 % is the top of the next page. One should also note, that this means
613 % that their placement behavior is determined by the values of
614 % |\topfraction|, etc.\ rather than by |\dbl...|.
615 %
616 %
617 % \subsection{Support for right-to-left typesetting}
618 %
619 % In right-to-left typesetting the order of the columns on the page
620 % also need to be reversed, i.e., the first column has to appear on the far
621 % right and the last column on the left. This is supported through the
622 % commands |\RLmulticolcolumns| (switching to right-to-left typesetting)
623 % and |\LRmulticolcolumns| (switching to left-to-right typesetting) the
624 % latter being the default.
625 %
626 % \subsection{Warnings}
627 %
628 % Under certain circumstances the use of the \mc{} environment may
629 % result in some
630 % warnings from \TeX{} or \LaTeX{}. Here is a list
631 % of the important ones and the possible cause:
632 % \begin{description}
633 %
634 % \item[] {\hspace*{-\labelsep}\tt Underfull \string\hbox\space
635 % (badness ...)}
636 %
637 % As the columns are often very narrow \TeX{} wasn't able to find a
638 % good way to break the paragraph. Underfull denotes a loose line but
639 % as long as the badness value is below $10000$ the result is probably
640 % acceptable.
641 %
642 % \item[]
643 % {\hspace*{-\labelsep}\tt Underfull \string\vbox\space ... while
644 % \string\output\space is active}
645 %
646 % If a column contains a character with an unusual depth, for
647 % example a `(', in the bottom line then this message may show up. It
648 % usually has no significance as long as the value is not more than a
649 % few points.
650 %
651 % \item[] {\hspace*{-\labelsep}\tt LaTeX Warning: I moved some lines
652 % to the next page}
653 %
654 % As mentioned above, \mc{} sometimes screws up the footnote
655 % numbering. As a precaution, whenever there is a footnote on a
656 % page where \mc{} had to leave a remainder for the following
657 % page this warning appears. Check the footnote numbering on this
658 % page. If it turns out that it is wrong, you have to manually break
659 % the page using |\newpage| or |\pagebreak[..]|.
660 %
661 % \item[] {\hspace*{-\labelsep}\tt Floats and marginpars not allowed
662 % inside `multicols' environment!}
663 %
664 % This message appears if you try to use the |\marginpar| command or
665 % an unstarred version of the \textsf{figure} or \textsf{table} environment.
666 % Such floats will disappear!
667 %
668 % \item[] {\hspace*{-\labelsep}\tt Very deep columns!
669 % Grid alignment might be broken}
670 %
671 % This message can only appear if the option \texttt{grid} was
672 % chosen. In that case it will show up if a column has a very large
673 % depth so that \mc{} is unable to back up to its baseline. This
674 % is only relevant if one tries to produce a document where all
675 % text lines are aligned at an invisible grid, something that
676 % requires careful adjustment of many parameters and macros, e.g.,
677 % heading definitions.
678 %
679 % \end{description}
680 %
681 %
682 % \subsection{Tracing the output}
683 %
684 % To understand the reasoning behind the decisions \TeX{} makes when
685 % processing a \mc{} environment, a tracing mechanism is provided.
686 % If you set the counter `\texttt{tracingmulticols}'
687 % to a positive \meta{number} you then
688 % will get some tracing information on the terminal and in the
689 % transcript file:
690 % \begin{description}
691 % \item[$\meta{number}=1$.] \TeX{} will now tell you, whenever it
692 % enters or leaves a \mc{} environment, the number of columns it
693 % is working on and its decision about starting a new page before
694 % or after the environment.
695 % \item[$\meta{number}=2$.]
696 % In this case you also get information from the balancing routine:
697 % the heights tried for the left and right-most columns,
698 % information about shrinking if the |\raggedcolumns|
699 % declaration is in force and the value of the `\textsf{unbalance}'
700 % counter if positive.
701 % \item[$\meta{number}= 3$.] Setting \meta{number}\pagebreak[2] to
702 % this value will additionally trace the mark handling
703 % algorithm. It will show what marks are found, what marks are
704 % considered, etc. To fully understand this information you will
705 % probably have to read carefully through the implementation.
706 % \item[$\meta{number}\geq 4$.] Setting \meta{number}\pagebreak[2] to
707 % such a high value will additionally place an |\hrule| into your
708 % output, separating the part of text which had already been
709 % considered on the previous page from the rest. Clearly this
710 % setting should {\em not\/} be used for the final output. It will
711 % also activate even more debugging code for mark handling.
712 % \end{description}
713 %
714 %
715 % \end{multicols}
716 %
717 % \begin{multicols}{3}[\section{Prefaces to older versions}
718 % \subsection{Preface to version 1.4}]
719 % \hbadness=10000
720 % Beside fixing some bugs as mentioned in the \textsf{multicol.bug} file
721 % this new release enhances the \mc{} environment by allowing for
722 % balancing in arbitrary contexts. It is now, for example, possible
723 % to balance text within a \mc{} or a \textsf{minipage} as shown in
724 % \ref{tab:newcmds} where a \textsf{multicols} environment within a
725 % \textsf{quote} environment was used. It is now even possible to nest
726 % \mc{} environments.
727 %
728 % The only restriction to such inner \mc{} environments (nested, or
729 % within \TeX's internal vertical mode) is that such variants will
730 % produce a box with the balanced material in it, so that they can
731 % not be broken across pages or columns.
732 %
733 % Additionally I rewrote the algorithm for balancing so that it will
734 % now produce slightly better results.
735 %
736 % I updated the source documentation but like to apologize in
737 % advance for some `left over' parts that slipped through the
738 % revision.
739 %
740 % A note to people who like to improve the balancing algorithm of
741 % \mc{}: The balancing routine is now placed into a single macro
742 % which is called |\balance@columns|. This means that one can easily
743 % try different balancing routines by rewriting this macro. The
744 % interface for it is explained in table \ref{tab:balance}. There are
745 % several improvements possible, one can think of integrating the
746 % |\badness| function of \TeX3, define a faster algorithm for finding
747 % the right column height, etc. If somebody thinks he/she has an
748 % enhancement I would be pleased to learn about it. But please obey
749 % the copyright notice and don't change \textsf{multicol.dtx} directly!
750 % \begin{table*}
751 % \begin{quote}
752 % \begin{multicols}{2}
753 % \raggedcolumns
754 % The macro |\balance@columns| that contains the code for balancing
755 % gathered material is a macro without parameters. It assumes that
756 % the material for balancing is stored in the box |\mult@box| which
757 % is a |\vbox|. It also ``knows'' about all parameters set up by the
758 % \mc{} environment, like |\col@number|, etc. It can also assume
759 % that |\@colroom| is the still available space on the current page.
760 %
761 % When it finishes it must return the individual columns in boxes
762 % suitable for further processing with |\page@sofar|. This means
763 % that the left column should be stored in box register
764 % |\mult@firstbox|, the next
765 % in register |\mult@firstbox|${}+2$, \ldots,
766 % only the last one as an exception in
767 % register |\mult@grightbox|.
768 % \end{multicols}
769 % \end{quote}
770 % \vspace*{-3ex}
771 % \vspace*{-0ex}
772 % \caption{Interface description for \cs{balance@columns}}
773 % \label{tab:balance}
774 % \end{table*}
775 % \end{multicols}
776 %
777 % \begin{multicols}{3}[\subsection{Preface to version 1.2}]
778 % \hbadness=10000
779 % After the article about the \mc{} environment was published in
780 % \TUB\ 10\#3, I got numerous requests for these macros. However, I
781 % also got a changed version of my style file, together with a
782 % letter asking me if I would include the changes to get better
783 % paragraphing results in the case of narrow lines. The main
784 % differences to my original style option were additional parameters
785 % (like |\multicoladjdemerits| to be used for |\adjdemerits|, etc.)
786 % which would influence the line breaking algorithm.
787 %
788 % But actually resetting such parameters to zero or even worse to a
789 % negative value won't give better line breaks inside the \mc{}
790 % environment. \TeX{}s line breaking algorithm will only look at
791 % those possible line breaks which can be reached without a badness
792 % higher than the current value of |\tolerance| (or |\pretolerance|
793 % in the first pass). If this isn't possible, then, as a last
794 % resort, \TeX{} will produce overfull boxes. All those (and only
795 % those) possible break points will be considered and finally the
796 % sequence which results in the fewest demerits will be chosen. This
797 % means that a value of $-1000$ for |\adjdemerits| instructs \TeX{}
798 % to prefer visibly incompatible lines instead of producing better
799 % line breaks.
800 %
801 % However, with \TeX{} 3.0 it is possible to get decent line breaks
802 % even in small columns by setting |\emergencystretch| to an
803 % appropriate value. I implemented a version which is capable of
804 % running both in the old and the new \TeX{} (actually it will
805 % simply ignore the new feature if it is not available). The
806 % calculation of |\emergencystretch| is probably incorrect. I
807 % made a few tests but of course one has to have much more experience
808 % with the new possibilities to achieve the maximum quality.
809 %
810 % Version 1.1a had a nice `feature': the penalty for using the
811 % forbidden floats was their ultimate removal from \LaTeX{}s
812 % |\@freelist| so that after a few |\marginpar|s inside the \mc{}
813 % environment floats where disabled forever. (Thanks to Chris
814 % Rowley for pointing this out.) I removed this misbehavior and at
815 % the same time decided to allow at least floats spanning all
816 % columns, e.g., generated by the |figure*| environment. You can
817 % see the new functionality in table~\ref{tab:newcmds} which was
818 % inserted at this very point.
819 % \begin{table*}
820 % \small
821 % \setlength{\multicolsep}{0pt}
822 % \begin{quote}
823 % \begin{multicols}{2}
824 % |\setemergencystretch|: This is a hook for people who like
825 % to play around. It is supposed to set the
826 % |\emergencystretch| \meta{dimen} register provided in the
827 % new \TeX{} 3.0. The first argument is the number of columns and
828 % the second one is the current |\hsize|. At the moment the
829 % default definition is $4\texttt{pt} \times |#1|$, i.e.\ the
830 % |\hsize| isn't used at all. But maybe there are better
831 % formulae.
832 %
833 % \setlength{\emergencystretch}{20pt} |\set@floatcmds|: This is
834 % the hook for the experts who like to implement a full float
835 % mechanism for the \mc{} environment. The |@| in the name
836 % should signal that this might not be easy.
837 % \end{multicols}
838 % \end{quote}
839 % \vspace*{-1ex}
840 % \vspace*{-0ex}
841 % \caption[]{The new commands of \textsf{multicol.sty} version 1.2.
842 % Both commands might be removed if good solutions to these
843 % open problems are found. I hope that these commands will
844 % prevent that nearly identical style files derived from
845 % this one are floating around.}
846 % \label{tab:newcmds}
847 % \end{table*}
848 % However single column floats are still forbidden and I don't think
849 % I will have time to tackle this problem in the near future. As an
850 % advice for all who want to try: wait for \TeX{} 3.0. It has a few
851 % features which will make life much easier in multi-column
852 % surroundings. Nevertheless we are working here at the edge of
853 % \TeX{}s capabilities, really perfect solutions would need a
854 % different approach than it was done in \TeX{}s page builder.
855 %
856 % The text below is nearly unchanged, I only added documentation at
857 % places where new code was added.
858 % \end{multicols}
859 %
860 % \changes{v1.5l}{1996/01/13}{Try hard to explain unresolved reference
861 % that happens if \cs{OnlyDescription} is used}
862 %
863 % \MaybeStop{\PrintIndex \PrintChanges
864 % \ifx\Finale\relax
865 % \typeout{**********************************}
866 % \typeout{* Info: Typesetting this document with
867 % \protect\OnlyDescription\space will}
868 % \typeout{* Info: result in one unresolved
869 % reference to `sec:v14'.}
870 % \typeout{* Info: --- tough, it's just not there in this case!}
871 % \typeout{**********************************}
872 % \fi
873 % }
874 %
875 %
876 % \begin{multicols}{2}[\section{The Implementation}
877 % We are now switching to two-column output to show the
878 % abilities of this environment (and bad layout decisions).
879 % \subsection{The documentation driver file}
880 % ][10\baselineskip]
881 %
882 % \hbadness=10000
883 %
884 %
885 % The next bit of code contains the documentation driver file for
886 % \TeX{}, i.e., the file that will produce the documentation you are
887 % currently reading. It will be extracted from this file by the \texttt{docstrip} program.
888 % Since this is the first code in this file one can produce the
889 % documentation
890 % simply by running \LaTeX{} on the \texttt{.dtx} file.
891 % \begin{macrocode}
892 %<*driver>
893 \documentclass{ltxdoc}
894 % \end{macrocode}
895 % We use the \texttt{balancingshow} option when loading \mc{} so
896 % that full tracing is produced. This has to be done before the
897 % \texttt{doc} package is loaded, since \texttt{doc} otherwise
898 % requires \mc{} without any options.
899 % \begin{macrocode}
900 \usepackage{multicol}[1999/05/25]
901 \usepackage{doc}
902 % \end{macrocode}
903 % First we set up the page layout suitable for
904 % this article.
905 % \begin{macrocode}
906 \setlength{\textwidth}{39pc}
907 \setlength{\textheight}{54pc}
908 \setlength{\parindent}{1em}
909 \setlength{\parskip}{0pt plus 1pt}
910 \setlength{\oddsidemargin}{0pc}
911 \setlength{\marginparwidth}{0pc}
912 \setlength{\topmargin}{-2.5pc}
913 \setlength{\headsep}{20pt}
914 \setlength{\columnsep}{1.5pc}
915 % \end{macrocode}
916 % We want a rule between columns.
917 % \begin{macrocode}
918 \setlength\columnseprule{.4pt}
919 % \end{macrocode}
920 % We also want to ensure that a new \mc{} environment finds enough
921 % space at the bottom of the page.
922 % \begin{macrocode}
923 \setlength\premulticols{6\baselineskip}
924 % \end{macrocode}
925 % When balancing columns we disregard solutions that
926 % are too bad. Also, if the last column is too bad
927 % we typeset it without stretch.
928 % \begin{macrocode}
929 \setcounter{columnbadness}{7000}
930 \setcounter{finalcolumnbadness}{7000}
931 % \end{macrocode}
932 % The index is supposed to come out in four columns.
933 % And we don't show macro names in the margin.
934 % \begin{macrocode}
935 \setcounter{IndexColumns}{4}
936 % \end{macrocode}
937 %
938 % The following redefinitions have to be moved until after the
939 % preamble because version 3 of \pkg{doc} resets them after the
940 % preamble (this is tmp, because \pkg{hypdoc} is not yet
941 % integrated, but as we all know, tmp solutions have a tendency to
942 % survive for a long time\ldots).
943 % \begin{macrocode}
944 \AddToHook{begindocument}{%
945 \let\DescribeMacro\SpecialUsageIndex
946 \let\DescribeEnv\SpecialEnvIndex
947 \renewcommand\PrintMacroName[1]{}%
948 }
949 % \end{macrocode}
950 %
951 % \begin{macrocode}
952 \CodelineIndex
953 %\DisableCrossrefs % Partial index
954 \RecordChanges % Change log
955 % \end{macrocode}
956 % Line numbers are very small for this article.
957 % \begin{macrocode}
958 \renewcommand{\theCodelineNo}
959 {\scriptsize\rm\arabic{CodelineNo}}
960 \settowidth\MacroIndent{\scriptsize\rm 00\ }
961
962 \begin{document}
963 \typeout
964 {****************************************
965 ^^J* Expect some Under- and overfull boxes.
966 ^^J****************************************}
967 \DocInput{multicol.dtx}
968 \end{document}
969 %</driver>
970 % \end{macrocode}
971 %
972 %
973 % \end{multicols}
974 %
975 % \begin{multicols}{2}[\subsection{Identification and
976 % option processing}]
977 %
978 %
979 % We start by identifying the package. Since it makes use of features
980 % only available in \LaTeXe{} we ensure that this format is available.
981 % (Now this is done earlier in the file.)
982 % \begin{macrocode}
983 %<*package>
984 % \NeedsTeXFormat{LaTeX2e}
985 % \ProvidesPackage{multicol}[..../../..
986 % v... multicolumn formatting]
987 % \end{macrocode}
988 %
989 %
990 %^^A \subsection{Option processing}
991 %
992 % Next we declare options supported by \mc{}. Two-column mode
993 % and \mc{} do not work together so we warn about possible
994 % problems. However, since you can revert to |\onecolumn|
995 % in which case \mc{} does work, we don't make this an error.
996 % \begin{macrocode}
997 \DeclareOption{twocolumn}
998 {\PackageWarning{multicol}{May not work
999 with the twocolumn option}}
1000 % \end{macrocode}
1001 % Tracing is done using a counter. However
1002 % it is also possible to invoke the tracing
1003 % using the options declared below.
1004 % \begin{macrocode}
1005 \newcount\c@tracingmulticols
1006 \DeclareOption{errorshow}
1007 {\c@tracingmulticols\z@}
1008 \DeclareOption{infoshow}
1009 {\c@tracingmulticols\@ne}
1010 \DeclareOption{balancingshow}
1011 {\c@tracingmulticols\tw@}
1012 % \end{macrocode}
1013 %
1014 % \changes{v2.0a}{2024/11/10}{Add \cs{DebugMarksOn} to the show options}
1015 % \begin{macrocode}
1016 \DeclareOption{markshow}
1017 {\c@tracingmulticols\thr@@
1018 \DebugMarksOn
1019 }
1020 \DeclareOption{debugshow}
1021 {\c@tracingmulticols5\relax
1022 \DebugMarksOn
1023 }
1024 % \end{macrocode}
1025 % The next option is intended for typesetting on a |\baselineskip|
1026 % grid. Right now it doesn't do anything other than warning if it
1027 % thinks that the grid got lost.
1028 % \changes{v1.6a}{2003/03/15}{New option grid}
1029 % \changes{v1.8x}{2019/10/15}{Use \cs{@maxdepth} not \cs{maxdepth} (gh/190)}
1030 % \begin{macrocode}
1031 \let\mc@gridwarn\maxdimen
1032 \DeclareOption{grid}
1033 {\def\mc@gridwarn{\@maxdepth}}
1034 % \end{macrocode}
1035 %
1036 % \changes{v1.8e}{2014/04/23}{Support \cs{docolaction}}
1037 % Next option enables the |\docolaction| command. As this changes
1038 % the \texttt{.aux} file content this is not automatically enabled.
1039 % \begin{macrocode}
1040 \DeclareOption{colaction}{%
1041 \def\mc@col@status@write{%
1042 \protected@write\@auxout{}%
1043 {\string\mc@col@status
1044 {\ifmc@firstcol 1\else 2\fi}}%
1045 \mc@firstcolfalse}%
1046 \def\mc@lastcol@status@write{%
1047 \protected@write\@auxout{}%
1048 {\string\mc@col@status{3}}}%
1049 }
1050 \let\mc@col@status@write\relax
1051 \let\mc@lastcol@status@write\relax
1052 % \end{macrocode}
1053 %
1054 % \begin{macrocode}
1055 \ProcessOptions
1056 % \end{macrocode}
1057 %
1058 %
1059 % \end{multicols}
1060 %
1061 % \begin{multicols}{2}[\subsection{Starting and
1062 % Ending the \mc{} Environment}]
1063 %
1064 %
1065 %
1066 %
1067 % \begin{macro}{\multicols}
1068 % As mentioned before, the \mc{} environment has one mandatory
1069 % argument (the number of columns) and up to two optional ones. We
1070 % start by reading the number of columns into the |\col@number|
1071 % register.
1072 % \begin{macrocode}
1073 \def\multicols#1{\col@number#1\relax
1074 % \end{macrocode}
1075 % If the user forgot the argument, \TeX{} will complain about a
1076 % missing number at this point. The error recovery mechanism will
1077 % then use zero, which isn't a good choice in this case. So we
1078 % should now test whether everything is okay. The minimum is two
1079 % columns at the moment.
1080 % \changes{v1.3b}{1990/10/09}{Minimum of two columns}
1081 % \begin{macrocode}
1082 \ifnum\col@number<\tw@
1083 \PackageWarning{multicol}%
1084 {Using `\number\col@number'
1085 columns doesn't seem a good idea.^^J
1086 I therefore use two columns instead}%
1087 \col@number\tw@ \fi
1088 % \end{macrocode}
1089 % We have only enough box registers for twenty columns, so we need to
1090 % check that the user hasn't asked for more.
1091 % \changes{v1.4k}{1992/06/27}{Maximum of 5 columns (temp)}
1092 % \changes{v1.5a}{1992/11/04}{Allow 10 columns again}
1093 % \changes{v1.8y}{2019/12/09}{Allow for 20 columns (gh/237)}
1094 % \begin{macrocode}
1095 \ifnum\col@number>20
1096 \PackageError{multicol}%
1097 {Too many columns}%
1098 {Current implementation doesn't
1099 support more than 20 columns.%
1100 \MessageBreak
1101 I therefore use 20 columns instead}%
1102 \col@number20 \fi
1103 % \end{macrocode}
1104 % Within the environment we need a special version of the
1105 % kernel |\@footnotetext| command since the original sets the
1106 % the |\hsize| to |\columnwidth| which is not correct in the
1107 % multicol environment. Here |\columnwidth| refers to the width of
1108 % the individual column and the footnote should be in |\textwidth|.
1109 % Since |\@footnotetext| has a different definition inside a
1110 % minipage environment we do not redefine it directly. Instead we
1111 % locally set |\columnwidth| to |\textwidth| and call the original
1112 % (current) definition stored in |\orig@footnotetext|.
1113 % \changes{v1.5p}{1997/12/14}{Redefinition of \cs{@footnotetext}
1114 % only within env pr/2689.}
1115 % \changes{v1.5r}{1998/08/17}{Use \cs{@footnotetext} but with
1116 % local change to \cs{columnwidth}.}
1117 % \changes{v1.5w}{1999/10/21}{Make \cs{@footnotetext} long to allow
1118 % multi-paragraph footnotes.}
1119 % If the \mc{} environment is nested inside another \mc{}
1120 % environment then the redefinition has already happened. So be
1121 % better test for this situation. Otherwise, we will get a \TeX{}
1122 % stack overflow as this would generate a self-referencing definition.
1123 % \changes{v1.6e}{2004/02/14}{Avoid self-referencing definition of
1124 % \cs{@footnotetext} (pr/3618)}
1125 % \begin{macrocode}
1126 \ifx\@footnotetext\mult@footnotetext
1127 \else
1128 \let\orig@footnotetext\@footnotetext
1129 \let\@footnotetext\mult@footnotetext
1130 \fi
1131 % \end{macrocode}
1132 % Now we can safely look for the optional arguments.
1133 % \begin{macrocode}
1134 \@ifnextchar[\mult@cols{\mult@cols[]}}
1135 % \end{macrocode}
1136 % \end{macro}
1137 %
1138 %
1139 %
1140 % \begin{macro}{\mult@footnotetext}
1141 %
1142 % \begin{macrocode}
1143 \long\def\mult@footnotetext#1{\begingroup
1144 \columnwidth\textwidth
1145 \orig@footnotetext{#1}\endgroup}
1146 % \end{macrocode}
1147 % \end{macro}
1148 %
1149 % \begin{macro}{\mult@cols}
1150 % The |\mult@cols| macro grabs the first optional argument
1151 % (if any) and looks for the second one.
1152 % \begin{macrocode}
1153 \def\mult@cols[#1]{\@ifnextchar[%
1154 % \end{macrocode}
1155 % This argument should be a \meta{dimen} denoting the minimum free
1156 % space needed on the current page to start the environment. If the
1157 % user didn't supply one, we use |\premulticols| as a
1158 % default.
1159 % \begin{macrocode}
1160 {\mult@@cols{#1}}%
1161 {\mult@@cols{#1}[\premulticols]}}
1162 % \end{macrocode}
1163 % \end{macro}
1164 %
1165 %
1166 % \begin{macro}{\mult@@cols}
1167 % After removing all arguments from the input we are able
1168 % to start with |\mult@@cols|.
1169 % \begin{macrocode}
1170 \def\mult@@cols#1[#2]{%
1171 % \end{macrocode}
1172 % First thing we do is to decide whether or not this is an
1173 % unbounded multicols environment, i.e. one that may split across
1174 % pages, or one that has to be typeset into a box. If we are in
1175 % \TeX's ``inner'' mode (e.g., inside a box already) then we have a
1176 % boxed version of multicols therefore we set the |@boxedmulticols|
1177 % switch to true. The \mc{} should start in vertical mode. If we
1178 % are not already there we now force it with |\par| since otherwise
1179 % the test for ``inner'' mode wouldn't show if we are in a box.
1180 % \changes{v1.4f}{1992/04/28}{\cs{par} added to allow for correct inner test}
1181 % \begin{macrocode}
1182 \par
1183 \ifinner \@boxedmulticolstrue
1184 % \end{macrocode}
1185 % Otherwise we check |\doublecol@number|. This counter is zero
1186 % outside a multicols environment but positive inside (this happens
1187 % a little later on). In the second case we need to process the
1188 % current multicols also in ``boxed mode'' and so change the switch
1189 % accordingly.
1190 % \begin{macrocode}
1191 \else
1192 \ifnum \doublecol@number>\z@
1193 \@boxedmulticolstrue
1194 \fi
1195 \fi
1196 % \end{macrocode}
1197 % Then we look to see if statistics are requested:
1198 % \begin{macrocode}
1199 \mult@info\z@
1200 {Starting environment with
1201 \the\col@number\space columns%
1202 % \end{macrocode}
1203 % In boxed mode we add some more info.
1204 % \changes{v1.4f}{1992/04/28}{\cs{on@line} added to tracing info}
1205 % \begin{macrocode}
1206 \if@boxedmulticols\MessageBreak
1207 (boxed mode)\fi
1208 }%
1209 % \end{macrocode}
1210 % Then we measure the current page to see whether a useful portion
1211 % of the multicolumn environment can be typeset. This routine
1212 % might start a new page.
1213 % \changes{v1.4a}{1992/02/14}{Forgotten braces added}
1214 % \begin{macrocode}
1215 \enough@room{#2}%
1216 % \end{macrocode}
1217 % Now we output the first argument and produce vertical space
1218 % above the columns. (Note that this argument corresponds to the
1219 % first optional argument of the \textsf{multicols} environment.)
1220 % For many releases this argument was typeset in a group to get
1221 % a similar effect as |\twocolumn[..]| where the argument is
1222 % also implicitly surrounded by braces. However, this conflicts
1223 % with local changes done by things like sectioning commands (which
1224 % account for the majority of commands used in that argument)
1225 % messing up vertical spacing etc.\ later in the document so that
1226 % from version v1.5q on this argument is again typeset at the outer
1227 % level.
1228 % \changes{v1.4e}{1992/03/16}{Typeset optional arg inside group}
1229 % \changes{v1.5q}{1998/01/19}{And removed the group again six years later}
1230 % \begin{macrocode}
1231 #1\par\addvspace\multicolsep
1232 % \end{macrocode}
1233 % When the last line of a paragraph had a positive depth then this
1234 % depth normally taken into account by the baselineskip calculation
1235 % for the next line. However, the columns produced by a following
1236 % \texttt{multicol} are rigid and thus the distance from the
1237 % baseline of a previous text line to the first line in a
1238 % \texttt{multicol} would differ depending on the depth of the
1239 % previous line. To account for this we add a negative space unless
1240 % the depth is \texttt{-1000pt} which signals something special to
1241 % \TeX and is not supposed to be a real depth.
1242 % \changes{v1.5z1}{2003/02/17}{Add a kern to cancel potential depth of
1243 % previous line}
1244 % \begin{macrocode}
1245 \ifdim \prevdepth = -\@m\p@
1246 \else
1247 % \end{macrocode}
1248 % The actual generation of this corrective space is a little bit
1249 % more complicated as it doesn't make sense to always back up to
1250 % the previous baseline (in case an object with a very large depth
1251 % was placed there, e.g., a centered tabular). So we only back up
1252 % to the extend that we are within the |\baselineskip| grid. We
1253 % know that the box produced by \mc{} has |\topskip| at its top so
1254 % that also needs to be taken into account.
1255 % \changes{v1.6a}{2003/03/15}{Adjust spacing}
1256 % \begin{macrocode}
1257 \@tempcnta\prevdepth
1258 \@tempcntb\baselineskip
1259 \divide\@tempcnta\@tempcntb
1260 \advance\@tempcnta\@ne
1261 \dimen@\prevdepth
1262 \advance\dimen@ -\@tempcnta\baselineskip
1263 \advance\dimen@ \topskip
1264 \kern-\dimen@
1265 \fi
1266 % \end{macrocode}
1267 % We start a new grouping level to hide all subsequent changes
1268 % (done in |\prepare@multicols| for example).
1269 % \begin{macrocode}
1270 \begingroup
1271 \prepare@multicols
1272 % \end{macrocode}
1273 % If we are in boxed mode we now open a box to typeset all material
1274 % from the multicols body into it, otherwise we simply go ahead.
1275 % \changes{v1.4g}{1992/05/07}{\cs{global} was probably wrong but at least
1276 % unnecessary}
1277 % \begin{macrocode}
1278 \if@boxedmulticols
1279 \setbox\mult@box\vbox\bgroup
1280 % \end{macrocode}
1281 %
1282 % \changes{v1.8c}{2013/11/03}{Add \cs{color@setgroup} to prevent color leak}
1283 % \begin{macrocode}
1284 \color@setgroup
1285 % \end{macrocode}
1286 % \changes{v1.5?}{1994/?/?}{Penalty moved to later point}
1287 % We may have to reset some parameters at this point,
1288 % perhaps |\@parboxrestore|
1289 % would be the right action but I leave it for the moment.
1290 % \changes{v1.4l}{1992/08/17}{\cs{@totalleftmargin} now in
1291 % \cs{prepare@multicols}}
1292 % \begin{macrocode}
1293 \fi
1294 % \end{macrocode}
1295 % We finish by suppressing initial spaces.
1296 % \begin{macrocode}
1297 \ignorespaces}
1298 % \end{macrocode}
1299 % \end{macro}
1300 %
1301 % \begin{macro}{\if@boxedmulticols}
1302 % Here is the switch and the box for ``boxed'' multicols code.
1303 % \begin{macrocode}
1304 \newif\if@boxedmulticols
1305 \@boxedmulticolsfalse
1306 \newbox\mult@box
1307 % \end{macrocode}
1308 % \end{macro}
1309 %
1310 % \begin{macro}{\enough@room}
1311 % \changes{v1.0c}{1989/05/12}{Penalty 0 added to empty the contribution
1312 % list.}
1313 % The |\enough@room| macro used
1314 % above isn't perfect but works reasonably well in this context. We
1315 % measure the free space on the current page by subtracting
1316 % |\pagetotal| from |\pagegoal|. This isn't entirely
1317 % correct since it doesn't take the `shrinking' (i.e.\
1318 % |\pageshrink|) into account. The `recent contribution list'
1319 % might be nonempty so we start with |\par| and an explicit
1320 % |\penalty|.\footnote{See the documentation of
1321 % \cs{endmulticols} for further details.}
1322 % Actually, we use |\addpenalty| to ensure that a following
1323 % |\addvspace| will `see' the vertical space that might be
1324 % present.
1325 % The use of |\addpenalty| will have the effect that all items from
1326 % the recent contributions will be moved to the main vertical list
1327 % and the |\pagetotal| value will be updated correctly. However,
1328 % the penalty will be placed in front of any dangling glue item
1329 % with the result that the main vertical list may already be
1330 % overfull even if \TeX{} is not invoking the output routine.
1331 % \changes{v1.3b}{1990/10/09}{Do \cs{penalty} with \cs{addpenalty}}
1332 % \changes{v1.4e}{1992/03/16}{But ignore \cs{@nobreak} in \cs{addpenalty}}
1333 % \begin{macrocode}
1334 \def\enough@room#1{%
1335 % \end{macrocode}
1336 % Measuring makes only sense when we are not in ``boxed mode'' so
1337 % the routine does nothing if the switch is true.
1338 % \begin{macrocode}
1339 \if@boxedmulticols\else
1340 \par
1341 % \end{macrocode}
1342 % \label{mac:enoughroom}
1343 % To empty the contribution list the first release contained a
1344 % penalty zero but this had the result that |\addvspace| couldn't
1345 % detect preceding glue. So this was changed to |\addpenalty|. But
1346 % this turned out to be not enough as |\addpenalty| will not add a
1347 % penalty when |@nobreak| is true. Therefore we force this switch
1348 % locally to false. As a result there may be a break between
1349 % preceding text and the start of a multicols environment, but this
1350 % seems acceptable since there is the optional argument for exactly
1351 % this reason.
1352 % \begin{macrocode}
1353 \bgroup\@nobreakfalse\addpenalty\z@\egroup
1354 \page@free \pagegoal
1355 \advance \page@free -\pagetotal
1356 % \end{macrocode}
1357 % To be able to output the value we need to assign it to a register
1358 % first since it might be a register (default) in which case we
1359 % need to use |\the| or it might be a plain value in which case
1360 % |\the| would be wrong.
1361 % \changes{v1.5e}{1994/05/26}{Assign arg to skip register to be able
1362 % to output value}
1363 % \begin{macrocode}
1364 \@tempskipa#1\relax
1365 % \end{macrocode}
1366 % Now we test whether tracing information is required:
1367 % \begin{macrocode}
1368 \mult@info\z@
1369 {Current page:\MessageBreak
1370 height=%
1371 \the\pagegoal: used \the\pagetotal
1372 \space -> free=\the\page@free
1373 \MessageBreak
1374 needed \the\@tempskipa
1375 \space(for #1)}%
1376 % \end{macrocode}
1377 % Our last action is to force a page break if there isn't enough
1378 % room left.
1379 % \begin{macrocode}
1380 \ifdim \page@free <#1\newpage \fi
1381 \fi}
1382 % \end{macrocode}
1383 % \end{macro}
1384 %
1385 % \begin{macro}{\prepare@multicols}
1386 % When preparing for multicolumn output several things must
1387 % be done.
1388 % \begin{macrocode}
1389 \def\prepare@multicols{%
1390 % \end{macrocode}
1391 % We start saving the current |\@totalleftmargin| and then
1392 % resetting the |\parshape| in case we are inside some list
1393 % environment. The correct indentation for the \mc{} environment in
1394 % such a case will be produced by moving the result to the right by
1395 % |\multicol@leftmargin| later on. If we would use the value of
1396 % |\@totalleftmargin| directly then lists inside the \mc{}
1397 % environment could cause a shift of the output.
1398 % \changes{v1.4l}{1992/08/17}{saved \cs{@totalleftmargin}}
1399 % \begin{macrocode}
1400 \multicol@leftmargin\@totalleftmargin
1401 \@totalleftmargin\z@
1402 \parshape\z@
1403 % \end{macrocode}
1404 % We also set the register |\doublecol@number| for later use. This
1405 % register should contain $2\times |\col@number|$. This is also an
1406 % indicator that we are within a \mc{} environment as mentioned
1407 % above.
1408 % \changes{v1.5a}{1992/11/04}{Add offset to \cs{doublecolnumber}}
1409 % \begin{macrocode}
1410 \doublecol@number\col@number
1411 \multiply\doublecol@number\tw@
1412 \advance\doublecol@number\mult@rightbox
1413 % \end{macrocode}
1414 %
1415 % \begin{macrocode}
1416 \mc@prepare@mark@regions
1417 % \end{macrocode}
1418 %
1419 % \begin{macrocode}
1420 \if@boxedmulticols
1421 \else
1422 % \end{macrocode}
1423 % We add an empty box to the main vertical list to ensure that we
1424 % catch any insertions (held over or inserted at the top of the
1425 % page). Otherwise it might happen that the |\eject| is discarded
1426 % without calling the output routine. Inside the output routine we
1427 % remove this box again. Again this code applies only if we are on
1428 % the main vertical list and not within a box.
1429 % However, it is not enough to turn off interline spacing, we also
1430 % have to clear |\topskip| before adding this box, since |\topskip|
1431 % is always inserted before the first box on a page which would
1432 % leave us with an extra space of |\topskip| if \mc{} start on a
1433 % fresh sheet.
1434 % \changes{v1.3c}{1991/03/03}{\cs{null} inserted and removed in output}
1435 % \changes{v1.4a}{1992/02/11}{Conditional code for boxed mode added.}
1436 % \changes{v1.4o}{1992/11/22}{\cs{topskip} locally zeroed.}
1437 % \begin{macrocode}
1438 \nointerlineskip {\topskip\z@\null}%
1439 \output{%
1440 \global\setbox\partial@page\vbox
1441 {%
1442 % \end{macrocode}
1443 % Now we have to make sure that we catch one special situation which
1444 % may result in loss of text! If the user has a huge amount of
1445 % vertical material within the first optional argument that is larger
1446 % then |\premulticols| and we are near the bottom of the page then it
1447 % can happen that not the |\eject| is triggering this special output
1448 % routine but rather the overfull main vertical list. In that case
1449 % we get another breakpoint through the |\eject| penalty. As a result
1450 % this special output routine would be called twice and the contents
1451 % of |\partial@page|, i.e.\ the material before the \mc{}
1452 % environment gets lost. There are several solutions to avoid this
1453 % problem, but for now we will simply detect this and inform the user
1454 % that he/she has to enlarge the |\premulticols| by using a suitable
1455 % value for the second argument.
1456 % \changes{v1.4a}{1992/02/11}{Checking for text losses.}
1457 % \begin{macrocode}
1458 %<*check>
1459 \ifvoid\partial@page\else
1460 \PackageError{multicol}%
1461 {Error saving partial page}%
1462 {The part of the page before
1463 the multicols environment was
1464 nearly full with^^Jthe result
1465 that starting the environment
1466 will produce an overfull
1467 page. Some^^Jtext may be lost!
1468 Please increase \premulticols
1469 either generally or for this%
1470 ^^Jenvironment by specifying a
1471 suitable value in the second
1472 optional argument to^^Jthe
1473 multicols environment.}
1474 \unvbox\partial@page
1475 \box\last@line
1476 \fi
1477 %</check>
1478 \unvbox\@cclv
1479 \global\setbox\last@line\lastbox
1480 }%
1481 }\eject
1482 % \end{macrocode}
1483 % The next thing to do is to assign a new value to |\vsize|.
1484 % \LaTeX{} maintains the free room on the page (i.e.\ the page
1485 % height without the space for already contributed floats) in the
1486 % register |\@colroom|. We must subtract the height of
1487 % |\partial@page| to put the actual free room into this
1488 % variable.
1489 % \begin{macrocode}
1490 \advance\@colroom-\ht\partial@page
1491 % \end{macrocode}
1492 % Then we have to calculate the |\vsize| value to use during column
1493 % assembly. |\set@mult@vsize| takes an argument which allows to
1494 % make the setting local (|\relax|) or global (|\global|). The
1495 % latter variant is used inside the output routine below. At this
1496 % point here we have to make a local change to |\vsize| because we
1497 % want to get the original value for |\vsize| restored in case
1498 % this \mc{} environment ends on the same page where it has started.
1499 % \changes{v1.4p}{1992/11/26}{Use different \cs{vsize} setting}
1500 % \changes{v1.4p}{1992/11/26}{Code moved to \cs{set@mult@vsize}}
1501 % \begin{macrocode}
1502 \set@mult@vsize\relax
1503 % \end{macrocode}
1504 % Now we switch to a new |\output| routine which will be used
1505 % to put the gathered column material together.
1506 % \begin{macrocode}
1507 \output{\multi@column@out}%
1508 % \end{macrocode}
1509 % Finally we handle the footnote insertions. We have to multiply
1510 % the magnification factor and the extra skip by the number of
1511 % columns since each footnote reduces the space for every column
1512 % (remember that we have page-wide footnotes). If, on the other
1513 % hand, footnotes are typeset at the very end of the document, our
1514 % scheme still works since |\count\footins| is zero then, so it
1515 % will not change.
1516 % To allow even further customization the setting of the |\footins|
1517 % parameters is done in a separate macro.
1518 % \changes{v1.5?}{1994/?/?}{Use \cs{init@mult@footins}}
1519 % \begin{macrocode}
1520 \init@mult@footins
1521 % \end{macrocode}
1522 % For the same reason (page-wide footnotes), the \meta{dimen}
1523 % register controlling the maximum space used for footnotes isn't
1524 % changed. Having done this, we must reinsert all the footnotes
1525 % which are already present (i.e.\ those encountered when the
1526 % material saved in |\partial@page| was first processed). This
1527 % will reduce the free space (i.e.\ |\pagetotal|) by the
1528 % appropriate amount since we have changed the magnification
1529 % factor, etc.\ above.
1530 % \begin{macrocode}
1531 \reinsert@footnotes
1532 % \end{macrocode}
1533 % Inside \mc{} a |\clearpage| is fairly useless as we aren't supporting
1534 % floats. In fact, it can cause harm as it doesn't know about the
1535 % |\partial@page| and may therefore result in making columns too long.
1536 % So we change that to behave like |\newpage| but also check if there
1537 % are any deferred floats. If so, perhaps the user tried to place them through
1538 % that |\clearpage| (but that needs to be done before starting the \mc{}
1539 % environment.
1540 % \changes{v1.8q}{2017/04/11}{Make \cs{clearpage} behave like \cs{newpage} (pr/4511)}
1541 % \begin{macrocode}
1542 \def\clearpage{%
1543 \ifx\@deferlist\@empty\else
1544 \PackageError{multicol}%
1545 {Deferred floats not cleared}%
1546 {A \string\clearpage\space inside
1547 multicols acts like
1548 \string\newpage\space and doesn't
1549 clear floats.\MessageBreak
1550 Move it before the multicols
1551 environment if you need it.}%
1552 \fi
1553 \newpage}%
1554 % \end{macrocode}
1555 % All the code above was only necessary for the unrestricted \mc{}
1556 % version, i.e.\ the one that allows page breaks. If we are within
1557 % a box there is no point in setting up special output routines or
1558 % |\vsize|, etc.
1559 % \begin{macrocode}
1560 \fi
1561 % \end{macrocode}
1562 % But now we are coming to code that is necessary in all cases. We
1563 % assign new values to |\vbadness|, |\hbadness| and |\tolerance|
1564 % since it's rather hard for \TeX{} to produce `good' paragraphs
1565 % within narrow columns.
1566 % \changes{v1.2a}{1990/02/05}{\cs{vbadness} 10001 now.}
1567 % \begin{macrocode}
1568 \vbadness\@Mi \hbadness5000
1569 \tolerance\multicoltolerance
1570 % \end{macrocode}
1571 % Since nearly always the first pass will fail we ignore it
1572 % completely telling \TeX{} to hyphenate directly. In fact, we now
1573 % use another register to keep the value for the multicol
1574 % pre-tolerance, so that a designer may allow to use
1575 % |\pretolerance|.
1576 % \changes{v1.2a}{1990/02/05}{\cs{pretolerance} -1 because it nearly never
1577 % succeeds.}
1578 % \changes{v1.4e}{1992/03/20}{Using}
1579 % \begin{macrocode}
1580 \pretolerance\multicolpretolerance
1581 % \end{macrocode}
1582 % For use with the new \TeX{} we set |\emergencystretch| to
1583 % $|\col@number| \times 4pt$. However this is only a guess
1584 % so at the moment this is done in a macro
1585 % |\setemergencystretch| which gets the current |\hsize|
1586 % and the number of columns as arguments. Therefore users are able
1587 % to figure out their own formula.
1588 % \changes{v1.2a}{1990/02/05}{\cs{setemergencystretch} added.}
1589 % \begin{macrocode}
1590 \setemergencystretch\col@number\hsize
1591 % \end{macrocode}
1592 % Another hook to allow people adding their own extensions without
1593 % making a new package is |\set@floatcmds| which handles any
1594 % redefinitions of \LaTeX{}s internal float commands to work with
1595 % the \mc{} environment. At the moment it is only used to redefine
1596 % |\@dblfloat| and |\end@dblfloat|.
1597 % \changes{v1.2a}{1990/02/05}{\cs{set@floatcmds} added.}
1598 % \begin{macrocode}
1599 \set@floatcmds
1600 % \end{macrocode}
1601 % Additionally, we advance |\baselineskip| by
1602 % |\multicolbaselineskip| to allow corrections for narrow
1603 % columns.
1604 % \begin{macrocode}
1605 \advance\baselineskip\multicolbaselineskip
1606 % \end{macrocode}
1607 % \changes{v1.0e}{1989/06/21}{\cs{textwidth} changed to \cs{linewidth}.}
1608 % \changes{v1.0e}{1989/06/21}{So this file will
1609 % work with the `twocolumn' command.}
1610 % The |\hsize| of the columns is given by the formula:
1611 % \[
1612 % { |\linewidth| - (|\col@number|-1)
1613 % \times
1614 % |\columnsep|
1615 % \over
1616 % |\col@number|}
1617 % \]
1618 % The formula above has changed from release to release. We now
1619 % start with the current value of |\linewidth| so that the column
1620 % width is properly calculated when we are inside a minipage or a
1621 % list or some other environment.
1622 % This will be achieved with:
1623 % \begin{macrocode}
1624 \hsize\linewidth \advance\hsize\columnsep
1625 \advance\hsize-\col@number\columnsep
1626 \divide\hsize\col@number
1627 % \end{macrocode}
1628 % We also set |\linewidth| and |\columnwidth| to |\hsize| In the past
1629 % |\columnwidth| was left unchanged. This is inconsistent,
1630 % but |\columnwidth| is used only by floats (which
1631 % aren't allowed in their current implementation) and by the
1632 % |\footnote| macro. Since we want page-wide
1633 % footnotes\footnote{I'm not sure that I really want page-wide
1634 % footnotes. But balancing of the last page can
1635 % only be achieved with this approach or with a
1636 % multi-path algorithm which is complicated and
1637 % slow. But it's a challenge to everybody to
1638 % prove me wrong! Another possibility is to
1639 % reimplement a small part of the {\it
1640 % fire\_up\/} procedure in \TeX{} (the program).
1641 % I think that this is the best solution if you
1642 % are interested in complex page makeup, but it
1643 % has the disadvantage that the resulting
1644 % program cannot be called \TeX{} thereafter.}
1645 % this simple trick saved us from rewriting the |\footnote|
1646 % macros. However, some applications referred to |\columnwidth| as
1647 % the ``width of the current column'' to typeset displays
1648 % (the \texttt{amsmath} package, for example) and to allow the use
1649 % of such applications together with \texttt{multicol} this is now
1650 % changed.
1651 %
1652 % Before we change |\linewidth| to the new value we record its old
1653 % value in some register called |\full@width|. This value is
1654 % used later on when we package all columns together.
1655 % \changes{v1.0e}{1989/06/21}{Setting of \cs{columnwidth} removed.}
1656 % \changes{v1.5o}{1997/11/16}{Setting of \cs{columnwidth} added again
1657 % pr/2664.}
1658 % \begin{macrocode}
1659 \full@width\linewidth
1660 \linewidth\hsize
1661 \columnwidth\hsize
1662 }
1663 % \end{macrocode}
1664 % \end{macro}
1665 %
1666 % \begin{macro}{\init@mult@footins}
1667 % This macro is used to set up the parameters associated
1668 % with footnote floats. It can be redefined by applications that
1669 % require different amount of spaces when typesetting footnotes.
1670 % \begin{macrocode}
1671 \def\init@mult@footins{%
1672 \multiply\count\footins\col@number
1673 \multiply\skip \footins\col@number
1674 }
1675 % \end{macrocode}
1676 % \end{macro}
1677 %
1678 % \begin{macro}{\set@mult@vsize}
1679 %
1680 % Since we have to set |\col@umber| columns on one page,
1681 % each with a height of |\@colroom|, we have to assign
1682 % $
1683 % |\vsize| = |\col@number| \times |\@colroom|
1684 % $
1685 % in order to collect enough material before entering the
1686 % |\output| routine again. In fact we have to add another
1687 % $
1688 % (|\col@number|-1) \times (|\baselineskip|-|\topskip|)
1689 % $
1690 % if you think about it.
1691 % \changes{v1.4p}{1992/11/26}{Macro added.}
1692 % \begin{macrocode}
1693 \def\set@mult@vsize#1{%
1694 \vsize\@colroom
1695 \@tempdima\baselineskip
1696 \advance\@tempdima-\topskip
1697 \advance\vsize\@tempdima
1698 \vsize\col@number\vsize
1699 \advance\vsize-\@tempdima
1700 % \end{macrocode}
1701 % But this might not be enough since we use |\vsplit| later to
1702 % extract the columns from the gathered material. Therefore we add
1703 % some `extra lines,' one for each column plus a corrective action
1704 % depending on the value of the `\texttt{collectmore}' counter.
1705 % The final value is assigned globally if |#1| is |\global| because
1706 % we want to use this macro later inside the output routine too.
1707 % \changes{v1.6c}{2003/04/08}{Collect one line per column more}
1708 % \begin{macrocode}
1709 \advance\vsize\col@number\baselineskip
1710 #1\advance\vsize
1711 \c@collectmore\baselineskip}
1712 % \end{macrocode}
1713 % \end{macro}
1714 %
1715 %
1716 % \begin{macro}{\multicol@leftmargin}
1717 % Here is the dimen register we need for saving away the outer
1718 % value of |\@totalleftmargin|.
1719 % \begin{macrocode}
1720 \newdimen\multicol@leftmargin
1721 % \end{macrocode}
1722 % \end{macro}
1723 %
1724 %
1725 % \begin{macro}{\@Mvi}
1726 % In versions prior to 1.8r the balancing at the end of the
1727 % environment was done by changing the output routine from
1728 % |\multi@column@out| to |\balance@column@out|. As it turned out
1729 % that this has a couple of issues when the last columns should not
1730 % be balanced after all (for example because they contained several
1731 % |\columnbreak| commands we now stay with one output routine for
1732 % the environment and only signal that we reached the end of the
1733 % environment by marking it with a special penalty that we can check
1734 % for later.
1735 % \changes{v1.8r}{2018/01/28}{Macro added}
1736 % \begin{macrocode}
1737 \mathchardef\@Mvi=10006 % 10005 is
1738 % \columnbreak
1739 % \end{macrocode}
1740 % \end{macro}
1741 %
1742 %
1743 % \begin{macro}{\endmulticols}
1744 % When the end of the \mc{} environment is sensed we have to
1745 % balance the gathered material. Depending on whether or not we are
1746 % inside a boxed multicol different things must happen. But first
1747 % we end the current paragraph with a |\par| command.
1748 % \begin{macrocode}
1749 \def\endmulticols{\par
1750 \if@boxedmulticols
1751 % \end{macrocode}
1752 % In boxed mode we have to close the box in which we have gathered
1753 % all material for the columns. But before we do this we need to
1754 % remove any space at the end of the box as we don't want to use
1755 % this in balancing. Because of the |\color@endgroup| this can't be
1756 % done later in |\balance@columns| as the color command will hide
1757 % it.
1758 % \changes{v1.8c}{2013/11/03}{Add \cs{color@endgroup} to prevent color leak}
1759 % \changes{v1.8f}{2014/06/19}{Discard spaces before adding \cs{color@endgroup}}
1760 % \begin{macrocode}
1761 \remove@discardable@items
1762 \color@endgroup\egroup
1763 % \end{macrocode}
1764 % Now we call |\balance@columns| the routine that balances material
1765 % stored in the box |\mult@box|.
1766 % \changes{v1.5?}{1994/?/?}{Splitting off zero box moved to
1767 % \cs{balance@columns}}
1768 % \begin{macrocode}
1769 \balance@columns
1770 % \end{macrocode}
1771 % After balancing the result has to be returned by the command
1772 % |\page@sofar|.
1773 % When the boxed multicol is returned to the page it can happen
1774 % that it doesn't fit onto it and \LaTeX{} therefore breaks
1775 % earlier. The problem in that case is that during the generation
1776 % \cs{hsize}, etc.\ got changed and this setting is still in effect
1777 % right now, and if this boxed multicol is within, say,
1778 % \texttt{multicols*} then its output routine gets very upset. We
1779 % therefore delay returning the result by saving it in box for now
1780 % until we have left the group below.
1781 % \changes{v1.9e}{2023/02/25}{Delay returning boxed multicols (gh/1002)}
1782 % \begin{macrocode}
1783 \global\setbox\mc@boxedresult\vbox{%
1784 % \end{macrocode}
1785 % We first update the mark structures and collect all marks that
1786 % need reinsertion once \mc{} has finished. Then we output the boxed
1787 % columns and finally we reinsert the marks.
1788 % \changes{v2.0a}{2024/11/10}{Use new mark mechanism}
1789 % \begin{macrocode}
1790 \mc@handle@marks@and@reinserts
1791 {in multicol (boxed mode)}%
1792 \page@sofar
1793 \mc@reinsert@marks
1794 }%
1795 % \end{macrocode}
1796 % This finishes the code for the ``boxed'' case.
1797 % \begin{macrocode}
1798 \else
1799 % \end{macrocode}
1800 % If there was a |\columnbreak| on the very last line all material
1801 % will have been moved to the |\colbreak@box|. Thus the galley
1802 % will be empty and no output routine gets called so that the text
1803 % is lost. To avoid this problem (though unlikely) we check if the
1804 % current galley is empty and the |\colbreak@box| contains text and
1805 % if so return that to the galley. If the galley is non-empty any
1806 % material in |\colbreak@box| is added in the output routine since
1807 % it needs to be put in front.
1808 % \changes{v1.6d}{2003/04/17}{Catch problem with \cs{columnbreak} in
1809 % last line}
1810 % \begin{macrocode}
1811 \ifdim\pagegoal=\maxdimen
1812 \ifvoid\colbreak@box\else
1813 \mult@info\@ne{Re-adding forced
1814 break(s) for splitting}%
1815 \unvbox\colbreak@box\fi
1816 \fi
1817 % \end{macrocode}
1818 % If we are in an unrestricted \mc{} environment we end the current
1819 % paragraph above with |\par| but this isn't sufficient since \TeX{}s
1820 % {\it page\_builder} will not totally empty the contribution
1821 % list.\footnote{This once caused a puzzling bug where some of the
1822 % material was balanced twice, resulting in some overprints. The
1823 % reason was the \cs{eject} which was placed at the end of
1824 % the contribution list. Then the {\it page\_builder} was called
1825 % (an explicit \cs{penalty} will empty the contribution
1826 % list), but the line with the \cs{eject} didn't fit onto
1827 % the current page. It was then reconsidered after the output
1828 % routine had ended, causing a second break after one line.}
1829 % Therefore we must also add an explicit |\penalty|. Now the
1830 % contribution list will be emptied and, if its material doesn't
1831 % all fit onto the current page then the output routine will be
1832 % called before we change it.
1833 % \changes{v1.3b}{1990/10/09}{Do \cs{penalty} with \cs{addpenalty}}
1834 % \changes{v1.4e}{1992/03/16}{But ignore \cs{@nobreak} in \cs{addpenalty}}
1835 % \changes{v1.5c}{1993/04/18}{Again use \cs{penalty}}
1836 % At this point we need to use |\penalty| not |\addpenalty| to
1837 % ensure that a) the recent contributions are emptied and b) that
1838 % the very last item on the main vertical list is a valid break
1839 % point so that \TeX{} breaks the page in case it is overfull.
1840 % \begin{macrocode}
1841 \penalty\z@
1842 % \end{macrocode}
1843 % Now it's safe to call the output routine in order to balance
1844 % the columns. We do this by calling it with a special penalty.
1845 % \changes{v1.8r}{2018/01/28}{Use special penalty to signal end of environment}
1846 % \begin{macrocode}
1847 \penalty-\@Mvi
1848 % \end{macrocode}
1849 % If the \mc{} environment body was completely empty or if a
1850 % multi-page \mc{} just ends at a page boundary we have the unusual
1851 % case that the |\eject| will have no effect (since the main
1852 % vertical list is empty)---thus no output routine is called at
1853 % all. As a result the material preceding the \mc{} (stored in
1854 % |\partial@page| will get lost if we don't put this back by hand.
1855 % \changes{v1.4m}{1992/09/04}{Check \cs{partial@page} being emptied}
1856 % \begin{macrocode}
1857 \ifvbox\partial@page
1858 \unvbox\partial@page\fi
1859 \fi
1860 % \end{macrocode}
1861 % The output routine above will take care of the |\vsize| and
1862 % reinsert the balanced columns, etc. But it can't reinsert the
1863 % |\footnotes| because we first have to restore the
1864 % |\footins| parameter since we are returning to one column
1865 % mode. This will be done in the next line of code; we simply close
1866 % the group started in |\multicols|.
1867 %
1868 % To fix an obscure bug which is the result of the current
1869 % definition of the |\begin| \ldots\ |\end| macros, we check that
1870 % we are still (logically speaking) in the \mc{} environment. If,
1871 % for example, we forget to close some environment inside the
1872 % \mc{} environment, the following |\endgroup| would be
1873 % incorrectly considered to be the closing of this environment.
1874 % \changes{v1.3c}{1991/03/14}{Check closing env.}
1875 % \begin{macrocode}
1876 \@checkend{multicols}%
1877 \endgroup
1878 % \end{macrocode}
1879 % We also set the `\textsf{unbalance}' counter to its default. This is
1880 % done globally since \LaTeX{} counters are always changed this
1881 % way.\footnote{Actually, we are still in a group started by the
1882 % \cs{begin} macro, so \cs{global} must be used
1883 % anyway.}
1884 % \begin{macrocode}
1885 \global\c@unbalance\z@
1886 % \end{macrocode}
1887 % Now it's time to return any footnotes if we are in unrestricted
1888 % mode. In boxed mode footnotes are kept inside, but in that case
1889 % we have to first return the saved box to the page and then write
1890 % another column status into the \texttt{.aux}
1891 % file to support \cs{docolaction} in case we have nested
1892 % environments.
1893 % \changes{v1.8s}{2018/04/20}{Support for \cs{docolaction} (issue/39)}
1894 % \changes{v1.9e}{2023/02/25}{Delay returning boxed multicols (gh/1002)}
1895 % \begin{macrocode}
1896 \if@boxedmulticols
1897 \unvbox\mc@boxedresult
1898 \mc@col@status@write
1899 \else
1900 \reinsert@footnotes
1901 % \end{macrocode}
1902 % We also take a look at the amount of free space on the current
1903 % page to see if it's time for a page break. The vertical space
1904 % added thereafter will vanish if |\enough@room| starts a new
1905 % page.
1906 %
1907 % But there is one catch. If the |\end{multicols}| is at the top of
1908 % which can happen if there is a break point just before it (such
1909 % as end ending environment) which was chosen. In that case we
1910 % would do the next page using the internal |\vsize| for multicol
1911 % collection which is a disaster. So we better catch this
1912 % case. Fortunately we can detect it by looking at |\pagegoal|.
1913 % \changes{v1.5x}{2000/05/05}{Detect and fix problem if a multicols
1914 % ends at the top of a page}
1915 % \begin{macrocode}
1916 \ifdim \pagegoal=\maxdimen
1917 \global\vsize\@colroom
1918 \else
1919 \enough@room\postmulticols
1920 \fi
1921 \fi
1922 \addvspace\multicolsep
1923 % \end{macrocode}
1924 % There is one more thing to do: the balanced result of the
1925 % environment is supposed to have a |\prevdepth| of zero as we
1926 % backed up by its real prevdepth within |\page@sofar|. However if
1927 % the balancing happened in the output routine then \TeX{} reverts
1928 % to the |\prevdepth| that was current before the OR once the OR has
1929 % finished. In short |\prevdepth| is something you can't set
1930 % globally it is always local to the current list being built. Thus
1931 % we need to set it back to zero here to avoid incorrect spacing.
1932 % \changes{v1.8h}{2014/09/12}{Set \cs{prevdepdth} for current vlist
1933 % when returning from multicols environment}
1934 % \begin{macrocode}
1935 \prevdepth\z@
1936 % \end{macrocode}
1937 % If statistics are required we finally report that we have
1938 % finished everything.
1939 % \begin{macrocode}
1940 \mult@info\z@
1941 {Ending environment
1942 \if@boxedmulticols
1943 \space(boxed mode)\fi
1944 }}
1945 % \end{macrocode}
1946 % \end{macro}
1947 %
1948 %
1949 % \begin{macro}{\c@unbalance}
1950 % \SpecialMainIndex{\c@collectmore}
1951 % \SpecialMainIndex{\col@number}
1952 % \SpecialMainIndex{\doublecol@number}
1953 % \SpecialMainIndex{\premulticols}
1954 % \SpecialMainIndex{\multicoltolerance}
1955 % \SpecialMainIndex{\multicolpretolerance}
1956 % \SpecialMainIndex{\page@free}
1957 % \SpecialMainIndex{\premulticols}
1958 % \SpecialMainIndex{\postmulticols}
1959 % \SpecialMainIndex{\multicolsep}
1960 % \SpecialMainIndex{\multicolbaselineskip}
1961 % \SpecialMainIndex{\partial@page}
1962 % \SpecialMainIndex{\mc@boxedresult}
1963 % Let us end this section by allocating all the registers used so
1964 % far.
1965 % \begin{macrocode}
1966 \newcount\c@unbalance
1967 \newcount\c@collectmore
1968 % \end{macrocode}
1969 % In the new \LaTeX{} release |\col@number| is already allocated by
1970 % the kernel, so we don't allocate it again.
1971 % \begin{macrocode}
1972 %\newcount\col@number
1973 \newcount\doublecol@number
1974 \newcount\multicoltolerance
1975 \newcount\multicolpretolerance
1976 \newdimen\full@width
1977 \newdimen\page@free
1978 \newdimen\premulticols
1979 \newdimen\postmulticols
1980 \newskip\multicolsep
1981 \newskip\multicolbaselineskip
1982 \newbox\partial@page
1983 \newbox\last@line
1984 \newbox\mc@boxedresult
1985 % \end{macrocode}
1986 % And here are their default values:
1987 % \begin{macrocode}
1988 \c@unbalance = 0
1989 \c@collectmore = 0
1990 % \end{macrocode}
1991 % To allow checking whether some macro is used within the \mc{}
1992 % environment the counter |\col@number| gets a default of |1|
1993 % outside the environment.
1994 % \changes{v1.3d}{1991/10/23}{\cs{col@number} set to one}
1995 % \changes{v1.8o}{2016/02/08}{\cs{col@number} already initialized in the
1996 % kernel, so not initializing it in the package
1997 % in case the document is in two-column (pr/4435)}
1998 % \begin{macrocode}
1999 %\col@number = 1
2000 \multicoltolerance = 9999
2001 \multicolpretolerance = -1
2002 \premulticols = 50pt
2003 \postmulticols= 20pt
2004 \multicolsep = 12pt plus 4pt minus 3pt
2005 \multicolbaselineskip=0pt
2006 % \end{macrocode}
2007 % \end{macro}
2008 %
2009 % \end{multicols}
2010 %
2011 % \begin{multicols}{2}[\subsection{The output routines}]
2012 %
2013 % We first start with some simple macros. When typesetting the page we
2014 % save the columns either in the box registers 0, 2, 4,\ldots\
2015 % (locally) or 1, 3, 5,\ldots\ (globally). This is \PlainTeX{} policy
2016 % to avoid an overflow of the save stack.
2017 %
2018 % \begin{macro}{\process@cols}
2019 % Therefore we define a |\process@cols| macro to help us in
2020 % using these registers in the output routines below. It has two
2021 % arguments: the first one is a number; the second one is the
2022 % processing information. It loops starting with |\count@=#1|
2023 % (|\count@| is a scratch register defined in \PlainTeX),
2024 % processes argument |#2|, adds two to |\count@|,
2025 % processes argument |#2| again, etc.\ until |\count@| is
2026 % higher than |\doublecol@number|. It might be easier to
2027 % understand it through an example, so we define it now and
2028 % explain its usage afterwards.
2029 % \begin{macrocode}
2030 \def\process@cols#1#2{\count@#1\relax
2031 \loop
2032 %<*debug>
2033 \typeout{Looking at box \the\count@}
2034 %</debug>
2035 #2%
2036 \advance\count@\tw@
2037 \ifnum\count@<\doublecol@number
2038 \repeat}
2039 % \end{macrocode}
2040 % \end{macro}
2041 %
2042 %
2043 % \begin{macro}{\page@sofar}
2044 % We now define |\page@sofar| to give an example of the
2045 % |\process@cols| macro. |\page@sofar| should output everything
2046 % prepared by the balancing routine |\balance@columns|.
2047 % \begin{macrocode}
2048 \def\page@sofar{%
2049 % \end{macrocode}
2050 % |\balance@columns| prepares its output in the even numbered
2051 % scratch box registers.
2052 % Now we output the columns gathered assuming that they are saved
2053 % in the box registers 2 (left column), 4 (second column), \ldots\
2054 % However, the last column (i.e.\ the right-most) should be saved in
2055 % box register 0.\footnote{You will see the reason for this numbering
2056 % when we look at the output routines
2057 % \cs{multi@column@out} and
2058 % \cs{balance@columns@out}.}
2059 % First we ensure that the columns have equal width. We use
2060 % |\process@cols| for this purpose, starting with
2061 % $|\count@|=|\mult@rightbox|$. Therefore |\count@| loops through
2062 % |\mult@rightbox|, $|\mult@rightbox| + 2$,\ldots
2063 % (to |\doublecol@number|).
2064 % \changes{v1.5a}{1992/11/04}{New box mechanism}
2065 % \begin{macrocode}
2066 \process@cols\mult@rightbox
2067 % \end{macrocode}
2068 % We have to check if the box in question is void, because the
2069 % operation |\wd|\meta{number} on a void box will \emph{not} change
2070 % its dimension (sigh).
2071 % \changes{v1.5h}{1994/08/26}{Check for void boxes}
2072 % \changes{v1.5i}{1994/10/02}{But don't remove original code.}
2073 % \begin{macrocode}
2074 {\ifvoid\count@
2075 \setbox\count@\hbox to\hsize{}%
2076 \else
2077 \wd\count@\hsize
2078 \fi}%
2079 % \end{macrocode}
2080 % Now we give some tracing information.
2081 % \changes{v1.4l}{1992/08/17}{use \cs{multicol@leftmargin} instead of
2082 % \cs{@totalleftmargin}}
2083 % \changes{v1.6b}{2003/03/15}{Different info display}
2084 % \begin{macrocode}
2085 \count@\col@number \advance\count@\m@ne
2086 \mult@info\z@
2087 {Column spec: \the\full@width\space = indent
2088 + columns + sep =\MessageBreak
2089 \the\multicol@leftmargin\space
2090 + \the\col@number\space
2091 x \the\hsize\space
2092 + \the\count@\space
2093 x \the\columnsep
2094 }%
2095 % \end{macrocode}
2096 % At this point we should always be in vertical mode.
2097 % \begin{macrocode}
2098 \ifvmode\else\errmessage{Multicol Error}\fi
2099 % \end{macrocode}
2100 %
2101 %
2102 % Now we put all columns together in an |\hbox| of width
2103 % |\full@width| (shifting it by |\multicol@leftmargin| to the right
2104 % so that it will be placed correctly if we are within a list
2105 % environment)
2106 % and separating the columns with a rule if desired.
2107 % \changes{v1.5a}{1992/11/04}{New box mechanism}
2108 % \changes{v1.5z}{2000/07/10}{Ensure that column rule has always
2109 % \cs{normalcolor}}
2110 % \changes{v1.6f}{2006/09/19}{Make the color of the rule a hook}
2111 % \changes{v1.4l}{1992/08/17}{use \cs{multicol@leftmargin} instead of
2112 % \cs{@totalleftmargin}}
2113 %
2114 % The box containing the columns has a large height and thus will
2115 % always result in using |\lineskip| if the normal |\baselineskip|
2116 % calculations are used. We therefore better cancel that process.
2117 % \changes{v1.5z1}{2003/02/17}{Suppress interline glue at this point}
2118 % \begin{macrocode}
2119 \nointerlineskip
2120 % \end{macrocode}
2121 % As mentioned earlier we want to have the reference point of the
2122 % box we put on the page being at the baseline of the last line of
2123 % the columns but we also want to ensure that the box has no depth
2124 % so that any following skip is automatically starting from that
2125 % baseline.
2126 % We achieve this by recording the depths of all columns and then
2127 % finally backing up by the maximum. (perhaps a simpler method
2128 % would be to assemble the box in a register and set the depth of
2129 % that box to zero (not checked).
2130 %
2131 % We need a global scratch register for this; using standard \TeX{}
2132 % conventions we choose |\dimen2| and initialize it with the depth
2133 % of the character ``p'' since that is one of the depths that
2134 % compete for the maximum.
2135 % \changes{v1.6a}{2003/03/15}{Preparing for adjusting \cs{prevdepth}}
2136 % \changes{v1.9f}{2023/03/30}{Make column min depth customizable (gh/698)}
2137 % \begin{macrocode}
2138 \setbox\z@\hbox{\multicolmindepthstring}\global\dimen\tw@\dp\z@
2139 % \end{macrocode}
2140 %
2141 % \changes{v2.0a}{2024/11/12}{Addedd tagging socket}
2142 % \begin{macrocode}
2143 \UseTaggingSocket{page@sofar}%
2144 % \end{macrocode}
2145 %
2146 % \begin{macrocode}
2147 \moveright\multicol@leftmargin
2148 \hbox to\full@width{%
2149 % \end{macrocode}
2150 % If the document is written in a language that is typeset right-to-left
2151 % then, of course, the multicol columns should be also typeset
2152 % right-to-left. To support this we call |\mc@align@columns| which
2153 % with execute different code depending on the typesetting direction.
2154 % \changes{v1.7b}{2011/12/18}{RL language support fixed}
2155 % \begin{macrocode}
2156 \mc@align@columns
2157 % \end{macrocode}
2158 % The depths of the columns depend on their last lines. To ensure
2159 % that we will always get a similar look as far as the rules are
2160 % concerned we force the depth to be at least the depth of a
2161 % letter~`p' or more exactly \cs{multicolmindepthstring} (which is what
2162 % we set |\dimen2| to above).
2163 % \changes{v1.9f}{2023/03/30}{Make column min depth customizable (gh/698)}
2164 % \begin{macrocode}
2165 \rlap{\phantom \multicolmindepthstring}%
2166 }%
2167 % \end{macrocode}
2168 % The processed material might consist of a last line with a
2169 % descender in which case the |\prevdepth| will be
2170 % non-zero. However, this material is getting reformatted now so
2171 % that this value is likely to be wrong. We therefore normalize the
2172 % situation by pretending that the depth is zero. However, if |\page@sofar|
2173 % is being called inside the OR then setting |\prevdepth| here has
2174 % no long-lasting effect, we therefore have to repeat this once we
2175 % return to the main vertical list. Here we set it only for those
2176 % cases where the command is used within a list and then followed by
2177 % something else.
2178 % \changes{v1.8g}{2014/08/24}{Resetting \cs{prevdepth} in the right place}
2179 % \begin{macrocode}
2180 \prevdepth\z@
2181 % \end{macrocode}
2182 % Now after typesetting the box we back up to its baseline by using
2183 % the value stored in |\dimen2| (which will hold the largest depth
2184 % found on any column).
2185 % \changes{v1.8g}{2003/03/15}{Now adjusting \cs{prevdepth}}
2186 % \begin{macrocode}
2187 \kern-\dimen\tw@
2188 % \end{macrocode}
2189 % However, in case one of the columns was unusually deep \TeX{} may
2190 % have tried some corrective actions in which case backing up by
2191 % the saved value will not bring us back to the baseline. A good
2192 % indication for this is a depth of |\@maxdepth| though it is not an
2193 % absolute proof. If the option \texttt{grid} is used
2194 % |\mc@gridwarn| will expand to this, otherwise to |\maxdimen| in
2195 % which case this warning will not show up.
2196 % \changes{v1.8g}{2014/08/24}{Warn if value is exceeded not when equal}
2197 % \begin{macrocode}
2198 \ifdim\dimen\tw@ > \mc@gridwarn
2199 \PackageWarning{multicol}%
2200 {Very deep columns!\MessageBreak
2201 Grid alignment might be broken}%
2202 \fi
2203 }
2204 % \end{macrocode}
2205 % \end{macro}
2206 %
2207 %
2208 % \begin{macro}{\multicolmindepthstring}
2209 % The default minimum depth of each column corresponds to the depth
2210 % of a `p' in the current font. This makes sense for Latin-based
2211 % languages and was hard-wired initially, but for Asian languages
2212 % it is better to use a zero depth (and alternatively one might
2213 % want to use the depth of a strut or a parentheses). So we now
2214 % offer a way to adjust this while maintaining backward
2215 % compatibility. Use \cs{renewcommand} to alter it.
2216 % \changes{v1.9f}{2023/03/30}{Make column min depth customizable (gh/698)}
2217 % \begin{macrocode}
2218 \def\multicolmindepthstring{p}
2219 % \end{macrocode}
2220 % \end{macro}
2221 %
2222 %
2223 %
2224 % \begin{macro}{\columnseprulecolor}
2225 % \changes{v1.6f}{2006/09/19}{Make the color of the rule a hook}
2226 % By default the vertical rule between columns will be in |\normalcolor|.
2227 % \begin{macrocode}
2228 \def\columnseprulecolor{\normalcolor}
2229 % \end{macrocode}
2230 % \end{macro}
2231 %
2232 % \begin{macro}{\reinsert@footnotes}
2233 % Before we tackle the bigger output routines we define just one
2234 % more macro which will help us to find our way through the
2235 % mysteries later. |\reinsert@footnotes| will do what its name
2236 % indicates: it reinserts the footnotes present in
2237 % |\footinbox| so that they will be reprocessed by \TeX{}'s
2238 % {\it page\_builder}.
2239 %
2240 % Instead of actually reinserting the footnotes we insert an empty
2241 % footnote. This will trigger insertion mechanism as well and since
2242 % the old footnotes are still in their box and we are on a fresh page
2243 % |\skip| |footins| should be correctly taken into account.
2244 % \changes{v1.3c}{1990/03/03}{\cs{unbox}ing avoided.}
2245 % \begin{macrocode}
2246 \def\reinsert@footnotes{\ifvoid\footins\else
2247 \insert\footins{}\fi}
2248 % \end{macrocode}
2249 % \end{macro}
2250 %
2251 %
2252 % \begin{macro}{\vfilmaxdepth}
2253 % This curious definition is used as the space at the bottom of a
2254 % column if we implement |\raggedcolumns|. Normally one only appends
2255 % |\vfill| in that case but this is actually wrong for columns that
2256 % are more or less full: by adding a glue at the bottom such a
2257 % column doesn't have any depth any more but without it the material
2258 % would be allowed a depth of |\@maxdepth|. So we allow shrinking by
2259 % that amount. This only makes a difference if the box would
2260 % otherwise become overfull and shrinking never exceeds the
2261 % specified value, so we should be fine.
2262 % \changes{v1.8h}{2014/09/12}{Macro added (pr/4395)}
2263 % \changes{v1.8j}{2015/03/07}{Use only `0.0001fil' for stretching}
2264 % \changes{v1.8x}{2019/10/15}{Use \cs{@maxdepth} not \cs{maxdepth} (gh/190)}
2265 % \begin{macrocode}
2266 \def\vfilmaxdepth{\vskip \z@ \@plus .0001fil
2267 \@minus \@maxdepth}
2268 % \end{macrocode}
2269 % \end{macro}
2270 %
2271 %
2272 % \begin{macro}{\multi@column@out}
2273 % Now we can't postpone the difficulties any longer. The
2274 % |\multi@column@out| routine will be called in two situations.
2275 % Either the page is full (i.e., we have collected enough material
2276 % to generate all the required columns) or a float or marginpar or
2277 % a |\clearpage| is
2278 % sensed. In the latter case the |\outputpenalty| is less
2279 % than $-10000$, otherwise the penalty which triggered the output
2280 % routine is higher. Therefore it's easy to distinguish both
2281 % cases: we simply test this register.
2282 % \changes{v1.5c}{1993/04/18}{Support \cs{clearpage}}
2283 % \begin{macrocode}
2284 \def\multi@column@out{%
2285 \ifnum\outputpenalty <-\@M
2286 % \end{macrocode}
2287 % If this was a |\clearpage|, a float or a marginpar we call
2288 % |\speci@ls|
2289 % \begin{macrocode}
2290 \speci@ls \else
2291 % \end{macrocode}
2292 % otherwise we construct the final page.
2293 % \changes{v1.5u}{1999/05/25}{Support \cs{columnbreak}}
2294 % \changes{v1.5v}{1999/07/18}{Added debug statements for
2295 % column break support}
2296 % For the next block of code see comments in section~\ref{sec:colbreak}.
2297 % \begin{macrocode}
2298 \ifvoid\colbreak@box\else
2299 \mult@info\@ne{Re-adding forced
2300 break(s) for splitting}%
2301 \setbox\@cclv\vbox{%
2302 \unvbox\colbreak@box
2303 \penalty-\@Mv
2304 \unvbox\@cclv}%
2305 \fi
2306 % \end{macrocode}
2307 % Let us now consider the
2308 % normal case. We have to |\vsplit| the columns from the
2309 % accumulated material in box 255. Therefore we first assign
2310 % appropriate values to |\splittopskip| and |\splitmaxdepth|.
2311 % \begin{macrocode}
2312 \splittopskip\topskip
2313 \splitmaxdepth\@maxdepth
2314 % \end{macrocode}
2315 % We also need to restrict |\boxmaxdepth| so that re-boxing is not
2316 % generating boxes with arbitrary depth.
2317 % \changes{v1.8j}{2015/03/07}{Set \cs{boxmaxdepth}}
2318 % \begin{macrocode}
2319 \boxmaxdepth\@maxdepth
2320 % \end{macrocode}
2321 % Then we calculate the current column height (in |\dimen@|).
2322 % Note that the height of |\partial@page| is already
2323 % subtracted from |\@colroom| so we can use its value as a
2324 % starter.
2325 % \begin{macrocode}
2326 \dimen@\@colroom
2327 % \end{macrocode}
2328 % But we must also subtract the space occupied by footnotes on the
2329 % current page. Note that we first have to reset the skip register
2330 % to its normal value.
2331 % Again, the actual action is carried out in a utility macro, so that
2332 % other applications can modify it.
2333 % \changes{v1.5?}{1994/?/?}{Use \cs{leave@mult@footins}}
2334 % \begin{macrocode}
2335 \divide\skip\footins\col@number
2336 \ifvoid\footins \else
2337 \leave@mult@footins
2338 \fi
2339 % \end{macrocode}
2340 % And there is one more adjustment that we have to make: if the
2341 % user has issue a |\enlargethispage| command then the height the
2342 % |\@kludgeins| box will be the negation of the size by which the
2343 % page should be enlarged. If the star form of this command has
2344 % been used then we also need to shrink the resulting column.
2345 %
2346 % That local change will be reverted at the end of the output
2347 % routine So for the next page the original state will be
2348 % reestablished. However, in theory there is a possibility to sneak
2349 % in a whole multicols environment into the running header
2350 % definition. If that happens then it will also be affected by this
2351 % change---too bad I think.
2352 % \changes{v1.8a}{2011/12/20}{Support for \cs{enlargethispage}}
2353 % \changes{v1.8v}{2018/12/27}{Removed dead code, the case where this
2354 % can go wrong is too obscure to worry about it (gh/101)}
2355 % \begin{macrocode}
2356 \ifvbox \@kludgeins
2357 \advance \dimen@ -\ht\@kludgeins
2358 % \end{macrocode}
2359 % The star form of |\enlargethispage| makes the width of the box
2360 % greater than zero (sneaky isn't it?).
2361 % \begin{macrocode}
2362 \ifdim \wd\@kludgeins>\z@
2363 \shr@nkingtrue
2364 \fi
2365 \fi
2366 % \end{macrocode}
2367 % Now we are able to |\vsplit| off all but the last column.
2368 % Recall that these columns should be saved in the box registers 2,
2369 % 4,\ldots\ (plus offset).
2370 % \changes{v1.5a}{1992/11/04}{New box mechanism}
2371 % \begin{macrocode}
2372 \process@cols\mult@firstbox{%
2373 \setbox\count@
2374 \vsplit\@cclv to\dimen@
2375 % \end{macrocode}
2376 % If |\raggedcolumns| is in force we add a |vfill| at the bottom by
2377 % unboxing the split box.
2378 % But we need to unbox anyway to ensure that at the end of the box
2379 % we do not have unwanted space. This can sneak in, in certain
2380 % situations, for example, if two lists follow each other and we
2381 % break between them. While such space is usually zero it still has
2382 % an effect because it hides depth of the last line in the column
2383 % and that will result in incorrect placement.
2384 % \changes{v1.3c}{1990/03/03}{\cs{unbox}ing avoided.}
2385 % \changes{v1.8b}{2013/08/28}{And 20odd years later conclude that
2386 % this was wrong and unboxing is always needed.}
2387 % \changes{v1.8b}{2013/08/28}{Remove discardable items at the
2388 % end of split boxes}
2389 % \changes{v1.8h}{2014/09/12}{Use \cs{vfilmaxdepth}}
2390 % \changes{v1.8k}{2015/03/22}{\cs{remove@discardable@items} removed}
2391 % \begin{macrocode}
2392 \setbox\count@
2393 \vbox to\dimen@
2394 {\unvbox\count@
2395 \ifshr@nking
2396 \vfilmaxdepth\fi}%
2397 }%
2398 % \end{macrocode}
2399 % Then the last column follows.
2400 % \changes{v1.5a}{1992/11/04}{New box mechanism}
2401 % \changes{v1.8b}{2013/08/28}{Remove discardable items at the
2402 % end of split boxes}
2403 % \changes{v1.8h}{2014/09/12}{Use \cs{vfilmaxdepth}}
2404 % \changes{v1.8k}{2015/03/22}{\cs{remove@discardable@items} removed}
2405 % \begin{macrocode}
2406 \setbox\mult@rightbox
2407 \vsplit\@cclv to\dimen@
2408 \setbox\mult@rightbox\vbox to\dimen@
2409 {\unvbox\mult@rightbox
2410 \ifshr@nking\vfilmaxdepth\fi}%
2411 % \end{macrocode}
2412 % Having done this we hope that box 255 is emptied. If not, we
2413 % reinsert its contents.
2414 % \changes{v1.8a}{2011/12/20}{Only re-add output penalty if it was
2415 % explicitly set}
2416 % \begin{macrocode}
2417 \ifvoid\@cclv \else
2418 \unvbox\@cclv
2419 \ifnum\outputpenalty=\@M
2420 \else
2421 \penalty\outputpenalty
2422 \fi
2423 % \end{macrocode}
2424 % In this case a footnote that happens to fall into the leftover
2425 % bit will be typeset on the wrong page. Therefore we warn the user
2426 % if the current page contains footnotes. The older versions of
2427 % \mc{} produced this warning regardless of whether or not
2428 % footnotes were present, resulting in many unnecessary warnings.
2429 % \changes{v1.3c}{1991/02/17}{Check if footnotes are actually present
2430 % before issuing a warning.}
2431 % \begin{macrocode}
2432 \ifvoid\footins\else
2433 \PackageWarning{multicol}%
2434 {I moved some lines to
2435 the next page.\MessageBreak
2436 Footnotes on page
2437 \thepage\space might be wrong}%
2438 \fi
2439 % \end{macrocode}
2440 %
2441 % If the `\textsf{tracingmulticols}' counter is 4 or higher we also
2442 % add a rule.
2443 % \begin{macrocode}
2444 \ifnum \c@tracingmulticols>\thr@@
2445 \hrule\allowbreak \fi
2446 \fi
2447 % \end{macrocode}
2448 % With a little more effort we could have done better. If we had,
2449 % for example, recorded the shrinkage of the material in
2450 % |\partial@page| it would be now possible to try higher
2451 % values for |\dimen@| (i.e.\ the column height) to overcome
2452 % the problem with the nonempty box 255. But this would make the
2453 % code even more complex so I skipped it in the current
2454 % implementation.
2455 %
2456 % Now we use \LaTeX{}'s standard output
2457 % mechanism.\footnote{This will produce a lot of overhead since both
2458 % output routines are held in memory. The correct
2459 % solution would be to redesign the whole output
2460 % routine used in \LaTeX.}
2461 % Admittedly this is a funny way to do it.
2462 %
2463 % Within the OR |\boxmaxdepth| needs to be unrestricted so we set it back
2464 % now as it was changed above.
2465 % \changes{v1.8p}{2016/04/07}{Reset \cs{boxmaxdepth}}
2466 % \begin{macrocode}
2467 \boxmaxdepth\maxdimen
2468 \setbox\@cclv\vbox
2469 {%
2470 % \end{macrocode}
2471 % If we make a page while still inside the \mc{} environment we
2472 % have to handle column and page mark structures.
2473 % \changes{v2.0a}{2024/11/10}{Use new mark mechanism}
2474 % \begin{macrocode}
2475 \mc@handle@col@andpage@marks
2476 {in multicol OR (full page)}%
2477 \unvbox\partial@page
2478 \page@sofar
2479 }%
2480 % \end{macrocode}
2481 % The macro |\@makecol| adds all floats assigned for the current
2482 % page to this page. |\@outputpage| ships out the resulting box.
2483 % Note that it is just possible that such floats are present even
2484 % if we do not allow any inside a \mc{} environment.
2485 % \begin{macrocode}
2486 \@makecol\@outputpage
2487 % \end{macrocode}
2488 % Now we reset |\@colroom| to |\@colht| which is \LaTeX's
2489 % saved value of |\textheight|.
2490 % We also have to reset the recorded position of the last
2491 % |\marginpar| as well as the recorded size of in-text floats
2492 % as we are now on a new page.
2493 % \changes{v1.8d}{2014/04/23}{Reset \cs{@mparbottom} after page finishes}
2494 % \changes{v1.8n}{2015/08/19}{Reset \cs{@textfloatsheight} after page finishes}
2495 % \begin{macrocode}
2496 \global\@colroom\@colht
2497 \global \@mparbottom \z@
2498 \global \@textfloatsheight \z@
2499 % \end{macrocode}
2500 % Then we process deferred floats waiting for their chance to be
2501 % placed on the next page.
2502 % \begin{macrocode}
2503 \process@deferreds
2504 \@whilesw\if@fcolmade\fi{\@outputpage
2505 \global\@colroom\@colht
2506 \process@deferreds}%
2507 % \end{macrocode}
2508 % If the user is interested in statistics we inform him about the
2509 % amount of space reserved for floats.
2510 % \begin{macrocode}
2511 \mult@info\@ne
2512 {Colroom:\MessageBreak
2513 \the\@colht\space
2514 after float space removed
2515 = \the\@colroom \@gobble}%
2516 % \end{macrocode}
2517 % Having done all this we must prepare to tackle the next page.
2518 % Therefore we assign a new value to |\vsize|. New, because
2519 % |\partial@page| is now empty and |\@colroom| might be
2520 % reduced by the space reserved for floats.
2521 % \changes{v1.4p}{1992/11/26}{Use different \cs{vsize} setting}
2522 % \begin{macrocode}
2523 \set@mult@vsize \global
2524 % \end{macrocode}
2525 % The |\footins| skip register will be adjusted when the output
2526 % group is closed.
2527 % \changes{v1.3c}{1991/03/03}{Unnecessary code removed}
2528 % \begin{macrocode}
2529 \fi}
2530 % \end{macrocode}
2531 % \end{macro}
2532 %
2533 %
2534 %
2535 % \begin{macro}{\leave@mult@footins}
2536 % This macro is used to subtract the amount of space
2537 % occupied by footnotes for the current space from the
2538 % space available for the current column. The space current column
2539 % is stored in |\dimen@|. See above for the description of the default
2540 % action.
2541 % \changes{v1.5?}{1994/?/?}{Macro added}
2542 % \begin{macrocode}
2543 \def\leave@mult@footins{%
2544 \advance\dimen@-\skip\footins
2545 \advance\dimen@-\ht\footins
2546 }
2547 % \end{macrocode}
2548 % \end{macro}
2549 %
2550 %
2551 %
2552 % \begin{macro}{\speci@ls}
2553 % We left out two macros: |\process@deferreds| and
2554 % |\speci@ls|.
2555 % \changes{v1.5c}{1993/04/18}{Support \cs{clearpage}}
2556 % \begin{macrocode}
2557 \def\speci@ls{%
2558 \ifnum\outputpenalty <-\@Mi
2559 % \end{macrocode}
2560 % If the document ends in the middle of a multicols environment, e.g.,
2561 % if the user forgot the |\end{multicols}|, \TeX{} adds a very
2562 % negative penalty to the end of the galley which is intended to signal
2563 % the output routine that it is time to prepare for shipping out
2564 % everything remaining. Since inside multicols the output routine of
2565 % \LaTeX{} is disabled sometimes we better check for this case: if we
2566 % find a very negative penalty we produce an error message and run
2567 % the default output routine for this case.
2568 % \changes{v1.5s}{1998/09/10}{check for \cs{stop} penalty pr/2873}
2569 % \begin{macrocode}
2570 \ifnum \outputpenalty<-\@MM
2571 \PackageError{multicol}{Document end
2572 inside multicols environment}\@ehd
2573 \@specialoutput
2574 \else
2575 % \end{macrocode}
2576 % \changes{v1.5u}{1999/05/25}{Support \cs{columnbreak}}
2577 % \changes{v1.5v}{1999/07/18}{Added debug statements for
2578 % column break support}
2579 % For the next block of code see comments in section~\ref{sec:colbreak}.
2580 % \begin{macrocode}
2581 \ifnum\outputpenalty = -\@Mv
2582 \mult@info\@ne{Forced column
2583 break seen}%
2584 \global\advance\vsize-\pagetotal
2585 \global\setbox\colbreak@box
2586 \vbox{%
2587 \ifvoid\colbreak@box
2588 \else
2589 \unvbox\colbreak@box
2590 \penalty-\@Mv
2591 \fi
2592 % \end{macrocode}
2593 % As this is the place of a forced break we now remove vertical
2594 % white space just in front of it (or some of it at least) as it is
2595 % quite likely that the break is not exactly in the right place,
2596 % e.g., after a display environment (if LaTeX would break here by
2597 % its own it would break before the space following the display).
2598 %
2599 % Thus we rebox box 255 once (using |\@maxdepth| and calling
2600 % |\remove@discardable@items| inside). The depth of 255 will then
2601 % give us the depth the box would have had if it would have been a
2602 % natural break. We then unbox 255 to get it into the
2603 % |\colbreak@box| and then back up by this depth. This will position
2604 % the bottom of the box at its natural baseline which is useful for
2605 % balancing later on.
2606 % \changes{v1.8k}{2015/03/21}{Remove discardable items just before a
2607 % forced break}
2608 % \changes{v1.8x}{2019/10/15}{Use \cs{@maxdepth} not \cs{maxdepth} (gh/190)}
2609 % \begin{macrocode}
2610 \boxmaxdepth\@maxdepth
2611 \setbox\@cclv\vbox{%
2612 \unvbox\@cclv
2613 \remove@discardable@items}%
2614 \dimen@\dp\@cclv
2615 \unvbox\@cclv
2616 \kern-\dimen@
2617 }%
2618 \reinsert@footnotes
2619 \else
2620 % \end{macrocode}
2621 % Another special case is reaching the end of the \mc{}
2622 % environment which is signaled by |-\@Mvi|.
2623 % \changes{v1.8r}{2018/01/28}{Handling end of env through special penalty}
2624 % \begin{macrocode}
2625 \ifnum\outputpenalty = -\@Mvi
2626 \mult@info\@ne{End penalty of
2627 multicols seen}%
2628 % \end{macrocode}
2629 % If we are at this point then we have to run the balancing code
2630 % (which was previously its own output routine). First we pretend
2631 % that we had a normal forced breakpoint and then call
2632 % |\balance@column@out|. The latter may be let to |\multi@column@out|
2633 % if we are inside \texttt{multicols*} in which case we would get a loop
2634 % if the |\outputpenalty| is not changed---this could be cleaned up
2635 % in a better way; basically it is like this, because of the older
2636 % code was using different ORs and I simply reused most of it.
2637 % \begin{macrocode}
2638 \outputpenalty\@M % pretend we had
2639 % a natural
2640 % forced break
2641 \balance@columns@out
2642 \else
2643 % \end{macrocode}
2644 % If we encounter a float or a marginpar in the current
2645 % implementation we simply warn the user that this is not allowed.
2646 % Then we reinsert the page and its footnotes.
2647 % \begin{macrocode}
2648 \PackageWarningNoLine{multicol}%
2649 {Floats and marginpars not
2650 allowed inside `multicols'
2651 environment!}%
2652 \unvbox\@cclv\reinsert@footnotes
2653 % \end{macrocode}
2654 % Additionally we empty the |\@currlist| to avoid later error
2655 % messages when the \LaTeX{} output routine is again in force.
2656 % But first we have to place the boxes back onto the
2657 % |\@freelist|. (|\@elt|s default is |\relax| so
2658 % this is possible with |\xdef|.)
2659 % \changes{v1.2a}{1990/02/05}{Float boxes freed.}
2660 % \begin{macrocode}
2661 \xdef\@freelist{\@freelist
2662 \@currlist}%
2663 \gdef\@currlist{}%
2664 \fi
2665 \fi
2666 \fi
2667 % \end{macrocode}
2668 % If the penalty is $-10001$ it will come from a |\clearpage| and
2669 % we will execute |\@doclearpage| to get rid of any deferred
2670 % floats.
2671 % \begin{macrocode}
2672 \else \@doclearpage \fi
2673 }
2674 % \end{macrocode}
2675 % \end{macro}
2676 %
2677 % \begin{macro}{\process@deferreds}
2678 % |\process@deferreds| is a simplified version of \LaTeX{}'s
2679 % |\@startpage|. We first call the macro
2680 % |\@floatplacement| to save the current user parameters in
2681 % internal registers. Then we start a new group and save the
2682 % |\@deferlist| temporarily in the macro |\@tempb|.
2683 % \begin{macrocode}
2684 \def\process@deferreds{%
2685 \@floatplacement
2686 \@tryfcolumn\@deferlist
2687 \if@fcolmade\else
2688 \begingroup
2689 \let\@tempb\@deferlist
2690 % \end{macrocode}
2691 % Our next action is to (globally) empty |\@deferlist| and
2692 % assign a new meaning to |\@elt|. Here |\@scolelt| is a
2693 % macro that looks at the boxes in a list to decide whether they
2694 % should be placed on the next page (i.e.\ on |\@toplist| or
2695 % |\@botlist|) or should wait for further processing.
2696 % \begin{macrocode}
2697 \gdef\@deferlist{}%
2698 \let\@elt\@scolelt
2699 % \end{macrocode}
2700 % Now we call |\@tempb| which has the form
2701 % \begin{center}
2702 % |\@elt|\meta{box register}|\@elt|^^A
2703 % \meta{box register}\ldots{}
2704 % \end{center}
2705 % So |\@elt| (i.e.\ |\@scolelt|) will distribute the
2706 % boxes to the three lists.
2707 % \begin{macrocode}
2708 \@tempb \endgroup
2709 \fi}
2710 % \end{macrocode}
2711 % \end{macro}
2712 %
2713 %
2714 %
2715 % \begin{macro}{ifshr@nking}
2716 % \begin{macro}{\raggedcolumns}
2717 % \begin{macro}{\flushcolumns}
2718 % \changes{v1.1a}{1989/09/20}{\cs{flushedcolumns} renamed to \cs{flushcolumns}.}
2719 % The |\raggedcolumns| and |\flushcolumns|
2720 % declarations are defined with the help of a new |\if...|
2721 % macro.
2722 % \begin{macrocode}
2723 \newif\ifshr@nking
2724 % \end{macrocode}
2725 % The actual definitions are simple: we just switch to \textsf{true}
2726 % or \textsf{false} depending on the desired action. To avoid extra
2727 % spaces in the output we enclose these changes in
2728 % |\@bsphack|\ldots{}\allowbreak|\@esphack|.
2729 % \begin{macrocode}
2730 \def\raggedcolumns{%
2731 \@bsphack\shr@nkingtrue\@esphack}
2732 \def\flushcolumns{%
2733 \@bsphack\shr@nkingfalse\@esphack}
2734 % \end{macrocode}
2735 % \end{macro}
2736 % \end{macro}
2737 % \end{macro}
2738 %
2739 %
2740 % \begin{macro}{\balance@columns@out}
2741 % Now for the last part of the show: the column balancing output
2742 % routine. Since this code is called with an explicit penalty
2743 % (|\eject|) there is no need to check for something special (eg
2744 % floats). We start by balancing the material gathered.
2745 % \begin{macrocode}
2746 \def\balance@columns@out{%
2747 % \end{macrocode}
2748 % For this we need to put the contents of box 255 into |\mult@box|.
2749 % \changes{v1.5u}{1999/05/25}{Support \cs{columnbreak}}
2750 % \changes{v1.5v}{1999/07/18}{Added debug statements for column break
2751 % support} For the next block of code see also comments in
2752 % section~\ref{sec:colbreak}. All forced breaks except the last are
2753 % inside |\colbreak@box| so all we have to do is to concatenate this
2754 % box with box |\@cclv| and put a penalty in between. Here we test if
2755 % |\colbreak@box| is void so that the message is only generated if
2756 % we really add forced breaks and the penalty.
2757 % \changes{v1.8k}{2015/03/21}{Use \cs{@Mv} and not \cs{break} in case
2758 % this forced break is not used on this page}
2759 % \begin{macrocode}
2760 \setbox\mult@box\vbox{%
2761 \ifvoid\colbreak@box\else
2762 \unvbox\colbreak@box
2763 \penalty-\@Mv
2764 \mult@info\@ne{Re-adding
2765 forced break(s) in balancing}%
2766 \fi
2767 \unvbox\@cclv
2768 % \end{macrocode}
2769 % The last column again is a forced break, so here we discard white
2770 % space as well as that is normally unwanted.
2771 % \changes{v1.8k}{2015/03/21}{Add \cs{remove@discardable@items} at the
2772 % end of the last column when balancing.}
2773 % \begin{macrocode}
2774 \remove@discardable@items
2775 }%
2776 \balance@columns
2777 % \end{macrocode}
2778 % If during balancing the columns got too long the flag
2779 % |\iftoo@bad| is set to true.
2780 % \changes{v1.8a}{2011/12/20}{Balancing concept improved}
2781 % \begin{macrocode}
2782 \iftoo@bad
2783 \mult@info\@ne
2784 {Balancing failed ...
2785 cut a normal page}%
2786 % \end{macrocode}
2787 % In that case we put the material back in box 255 so that we can
2788 % cut a normal page. The curious set of
2789 % |\vskip|s we add is necessary to cancel out the |\splittopskip|
2790 % that got added for balancing.
2791 % \begin{macrocode}
2792 \setbox\@cclv\vbox
2793 {\vskip\topskip
2794 \vskip-\splittopskip
2795 \unvbox\mult@box
2796 % \end{macrocode}
2797 % We also have to re-add the end of environment penalty since after
2798 % this page we may want balance the remaining material.
2799 % \changes{v1.8r}{2018/01/28}{Re-add the final penalty}
2800 % \begin{macrocode}
2801 \penalty-\@Mvi
2802 }%
2803 % \end{macrocode}
2804 % We then call the standard multicol output routine which will
2805 % produce a normal page for us (remember we are still within the
2806 % OR so some part of the code in |\multi@column@out| is actually not
2807 % doing anything---perhaps this should be cleaned up at some point).
2808 % This also means that if there was an |\enlargethispage| present
2809 % it will apply to this page as |\multi@column@out| will look at
2810 % the status of |\@kludgeins|.
2811 % \begin{macrocode}
2812 \multi@column@out
2813 % \end{macrocode}
2814 % Because balancing made the columns too long we are sure that there
2815 % will be some material remaining which was put back onto the main
2816 % vertical list by |\multi@column@out|. This will also put the
2817 % explicit |\eject| penalty back so the current
2818 % |\balance@columns@out| output routine will be called again (so we
2819 % better do not add another penalty or else the OR will be called
2820 % twice and we may get scrambled results).
2821 % \changes{v1.8k}{2015/03/21}{No additional penalty here}
2822 % \begin{macrocode}
2823 \else
2824 % \end{macrocode}
2825 % If the balancing went ok, we are in the position
2826 % to apply |\page@sofar|. But first we have to set |\vsize| to a
2827 % value suitable for one column output.
2828 % \begin{macrocode}
2829 \global\vsize\@colroom
2830 \global\advance\vsize\ht\partial@page
2831 % \end{macrocode}
2832 % We also have to look at |\@kludgeins| and generate a new
2833 % |\insert| in case there was one present due to an
2834 % |\enlargethispage| command.
2835 % \changes{v1.8a}{2011/12/20}{Support for \cs{enlargethispage}}
2836 % \begin{macrocode}
2837 \ifvbox\@kludgeins
2838 \insert\@kludgeins
2839 {\unvbox\@kludgeins}\fi
2840 % \end{macrocode}
2841 % Then we |\unvbox| the |\partial@page| (which may be void if we
2842 % are not processing the first page of this \mc{} environment.
2843 % \begin{macrocode}
2844 \unvbox\partial@page
2845 % \end{macrocode}
2846 % We then handle mark structures of the columns, return the gathered
2847 % material to the main vertical list and then also reinsert the
2848 % first and last marks that have been found in the columns.
2849 % \changes{v2.0a}{2024/11/10}{Use new mark mechanism}
2850 % \begin{macrocode}
2851 \mc@handle@marks@and@reinserts
2852 {in multicol OR (balancing)}%
2853 \page@sofar
2854 \mc@reinsert@marks
2855 % \end{macrocode}
2856 %
2857 % \begin{macrocode}
2858 % \end{macrocode}
2859 % We need to add a penalty at this point which allows to break at
2860 % this point since calling the output routine may have removed the
2861 % only permissible break point thereby ``glueing'' any following
2862 % skip to the balanced box. In case there are any weird settings
2863 % for |\multicolsep| etc. this could produce funny results.
2864 % \changes{v1.5c}{1993/04/18}{added penalty at output routine exit}
2865 % \begin{macrocode}
2866 \penalty\z@
2867 \fi
2868 }
2869 % \end{macrocode}
2870 % As we already know, reinserting of footnotes will be done in the
2871 % macro |\endmulticols|.
2872 % \end{macro}
2873 %
2874 %
2875 % \begin{macro}{\balance@columns}
2876 % This macro now does the actual balancing.
2877 % \begin{macrocode}
2878 \def\balance@columns{%
2879 % \end{macrocode}
2880 % We start by adding a forced break point at the very beginning, so that we
2881 % can split the box to height zero later on, thereby adding a known
2882 % |\splittopskip| glue at the beginning.
2883 % \changes{v1.8k}{2015/03/21}{\cs{remove@discardable@items} removed}
2884 % \begin{macrocode}
2885 \setbox\mult@box\vbox{%
2886 \penalty-\@M
2887 \unvbox\mult@box
2888 }%
2889 % \end{macrocode}
2890 % Then follow values assignments to get the |\vsplit|ting right.
2891 % We use the natural part of |\topskip| as the natural part for
2892 % |\splittopskip| and allow for a bit of undershoot and overshoot
2893 % by adding some stretch and shrink.
2894 % \changes{v1.5?}{1994/?/?}{Allow columns to come out a bit long or short}
2895 % \changes{v1.8x}{2019/10/15}{Use \cs{@maxdepth} not \cs{maxdepth} (gh/190)}
2896 % \begin{macrocode}
2897 \@tempdima\topskip
2898 \splittopskip\@tempdima
2899 \@plus\multicolundershoot
2900 \@minus\multicolovershoot
2901 \splitmaxdepth\@maxdepth
2902 % \end{macrocode}
2903 % We also have to set |\boxmaxdepth| which normally allows to
2904 % build boxes with arbitrary depth, but as we are building text
2905 % columns we really want to restrict the depth. This is necessary as
2906 % we sometimes rebox the boxes generated by |\vsplit| and then the
2907 % restriction posed by |\splitmaxdepth| gets lost.
2908 % \changes{v1.8h}{2014/09/12}{All column boxes should obey
2909 % \cs{maxdepth} (pr/4395)}
2910 % \changes{v1.8x}{2019/10/15}{Use \cs{@maxdepth} not \cs{maxdepth} (gh/190)}
2911 % \begin{macrocode}
2912 \boxmaxdepth\@maxdepth
2913 % \end{macrocode}
2914 % The next step is a bit tricky: when \TeX{} assembles material in
2915 % a box, the first line isn't preceded by interline glue, i.e.
2916 % there is no parameter like |\boxtopskip| in \TeX{}. This means
2917 % that the baseline of the first line in our box is at some
2918 % unpredictable point depending on the height of the largest
2919 % character in this line. But of course we want all columns to
2920 % align properly at the baselines of their first lines. For this
2921 % reason we have opened |\mult@box| with a |\penalty| \textsf{-10000}.
2922 % This will now allow us to split off from |\mult@box| a tiny bit
2923 % (in fact nothing since the first possible break-point is the
2924 % first item in the box). The result is that |\splittopskip| is
2925 % inserted at the top of |\mult@box| which is exactly what we like
2926 % to achieve.
2927 % \changes{v1.5?}{1994/?/?}{Do splitting to zero here}
2928 % \begin{macrocode}
2929 \setbox\@tempboxa\vsplit\mult@box to\z@
2930 % \end{macrocode}
2931 % Next we try to find a suitable starting point for the calculation
2932 % of the column height. It should be less than the height finally
2933 % chosen, but large enough to reach this final value in only a few
2934 % iterations. The formula which is now implemented will try to
2935 % start with the nearest value which is a multiple of
2936 % |\baselineskip|. The coding is slightly tricky in \TeX{} and
2937 % there are perhaps better ways \ldots
2938 % \changes{v1.4d}{1992/03/04}{New algorithm for start height}
2939 % \begin{macrocode}
2940 \@tempdima\ht\mult@box
2941 \advance\@tempdima\dp\mult@box
2942 \divide\@tempdima\col@number
2943 % \end{macrocode}
2944 % The code above sets |\@tempdima| to the length of a column if we
2945 % simply divide the whole box into equal pieces. To get to the next
2946 % lower multiple of |\baselineskip| we convert this dimen to a
2947 % number (the number of scaled points) then divide this by
2948 % |\baselineskip| (also in scaled points) and then multiply this
2949 % result with |\baselineskip| assigning the result to |\dimen@|.
2950 % This makes |\dimen@| $\leq$ to |\@tempdimena|.
2951 % \begin{macrocode}
2952 \count@\@tempdima
2953 \divide\count@\baselineskip
2954 \dimen@\count@\baselineskip
2955 % \end{macrocode}
2956 % Next step is to correct our result by taking into account the
2957 % difference between |\topskip| and |\baselineskip|. We start by
2958 % adding |\topskip|; if this makes the result too large then we
2959 % have to subtract one |\baselineskip|.
2960 % \begin{macrocode}
2961 \advance\dimen@\topskip
2962 \ifdim \dimen@ >\@tempdima
2963 \advance\dimen@-\baselineskip
2964 \fi
2965 % \end{macrocode}
2966 % As a further restriction we want to see a minimum number of
2967 % rows in the balanced result based on the setting of the counter
2968 % |minrows|. If the starting value is lower we adjust.
2969 % \changes{v1.8w}{2019/03/01}{Provide minrows counter for balancing}
2970 % \begin{macrocode}
2971 \@tempdima\dimexpr
2972 \topskip +\c@minrows\baselineskip
2973 -\baselineskip\relax
2974 \ifnum\dimen@<\@tempdima
2975 \mult@info\@ne
2976 {Start value
2977 \the\dimen@ \space ->
2978 \the\@tempdima \space
2979 (corrected for minrows)}%
2980 \dimen@\@tempdima
2981 \fi
2982 % \end{macrocode}
2983 % At the user's request we start with a higher value (or lower, but
2984 % this usually only increases the number of tries).
2985 % \begin{macrocode}
2986 \advance\dimen@\c@unbalance\baselineskip
2987 % \end{macrocode}
2988 % We type out statistics if we were asked to do so.
2989 % \changes{v1.4f}{1992/04/28}{\cs{on@line} added to tracing info}
2990 % \begin{macrocode}
2991 \mult@info\@ne
2992 {Balance columns\on@line:
2993 \ifnum\c@unbalance=\z@\else
2994 (off balance=\number\c@unbalance)\fi
2995 \@gobbletwo}%
2996 % \end{macrocode}
2997 % But we don't allow nonsense values for a start.
2998 % \begin{macrocode}
2999 \ifnum\dimen@<\topskip
3000 \mult@info\@ne
3001 {Start value
3002 \the\dimen@ \space ->
3003 \the\topskip \space (corrected)}%
3004 \dimen@\topskip
3005 \fi
3006 % \end{macrocode}
3007 % Now we try to find the final column height. We start by setting
3008 % |\vbadness| to infinity (i.e.\ $10000$) to suppress
3009 % underfull box reports while we are trying to find an acceptable
3010 % solution. We do not need to do it in a group since at the end of
3011 % the output routine everything will be restored. The setting of
3012 % the final columns will nearly always produce underfull boxes with
3013 % badness $10000$ so there is no point in warning the user about
3014 % it.
3015 % \changes{v1.2a}{1990/02/05}{Group around main loop removed.}
3016 % \begin{macrocode}
3017 \vbadness\@M
3018 % \end{macrocode}
3019 % We also allow for overfull boxes while we trying to split the
3020 % columns. They can easily happen if we have objects with unusual depth.
3021 % \changes{v1.8h}{2014/09/12}{Do not report overfull}
3022 % \begin{macrocode}
3023 \vfuzz \maxdimen
3024 % \end{macrocode}
3025 % The variable |\last@try| will hold the dimension used in the
3026 % previous trial splitting. We initialize it with a negative value.
3027 % \changes{v1.5?}{1994/?/?}{Initialize \cs{last@try}}
3028 % \begin{macrocode}
3029 \last@try-\p@
3030 \loop
3031 % \end{macrocode}
3032 % In order not to clutter up \TeX{}'s valuable main memory with
3033 % things that are no longer needed, we empty all globally used box
3034 % registers. This is necessary if we return to this point after an
3035 % unsuccessful trial. We use |\process@cols| for this purpose,
3036 % starting with |\mult@grightbox|. Note the extra braces around
3037 % this macro call. They are needed since \PlainTeX{}'s
3038 % |\loop|\ldots{}\allowbreak|\repeat| mechanism cannot be nested on
3039 % the same level of grouping.
3040 % \changes{v1.5a}{1992/11/04}{New box mechanism}
3041 % \begin{macrocode}
3042 {\process@cols\mult@grightbox
3043 {\global\setbox\count@
3044 \box\voidb@x}}%
3045 % \end{macrocode}
3046 % The contents of box |\mult@box| are now copied globally to
3047 % box~|\mult@grightbox|. (This will be the right-most column, as
3048 % we shall see later.)
3049 % \begin{macrocode}
3050 \global\setbox\mult@grightbox
3051 \copy\mult@box
3052 % \end{macrocode}
3053 % We start with the assumption that the trial will be successful.
3054 % If we end up with a solution that is too bad we set
3055 % |too@bad| to \texttt{true}. We also assume that all forced breaks
3056 % (if any) will be used during balancing. If this is not the case
3057 % we record this in |forcedbreak@leftover|.
3058 % \changes{v1.5b}{1992/11/05}{New badness mechanism}
3059 % \changes{v1.8k}{2015/03/21}{Init \cs{ifforcedbreak@leftover}}
3060 % \begin{macrocode}
3061 %<*badness>
3062 \too@badfalse
3063 \forcedbreak@leftoverfalse
3064 %</badness>
3065 % \end{macrocode}
3066 % Using |\vsplit| we extract the other columns from box register
3067 % |\mult@grightbox|. This leaves box register |\mult@box|
3068 % untouched so that we can start over again if this trial was
3069 % unsuccessful.
3070 % \begin{macrocode}
3071 {\process@cols\mult@gfirstbox{%
3072 \global\setbox\count@
3073 \vsplit\mult@grightbox to\dimen@
3074 % \end{macrocode}
3075 % After splitting we need to ensure that there isn't any space at
3076 % the bottom, so we rebox once more.
3077 % \changes{v1.8b}{2013/08/28}{Remove discardable items at the
3078 % end of split boxes}
3079 % \changes{v1.8k}{2015/03/21}{Do not use \cs{remove@discardable@items}
3080 % here}
3081 % \begin{macrocode}
3082 \global\setbox\count@
3083 \vbox to\dimen@
3084 {\unvbox\count@}%
3085 % \end{macrocode}
3086 % After every split we check the badness of the resulting column,
3087 % normally the amount of extra white in the column.
3088 % \begin{macrocode}
3089 %<*badness>
3090 \ifnum\c@tracingmulticols>\@ne
3091 \@tempcnta\count@
3092 \advance\@tempcnta-\mult@grightbox
3093 \divide\@tempcnta \tw@
3094 \message{^^JColumn
3095 \number\@tempcnta\space
3096 badness: \the\badness\space}%
3097 \fi
3098 % \end{macrocode}
3099 % If this badness is larger than the allowed column badness
3100 % we reject this solution by setting |too@bad| to \texttt{true}.
3101 % \begin{macrocode}
3102 \ifnum\badness>\c@columnbadness
3103 \ifnum\c@tracingmulticols>\@ne
3104 \message{too bad
3105 (>\the\c@columnbadness)}%
3106 \fi
3107 \too@badtrue
3108 \fi
3109 %</badness>
3110 }}%
3111 % \end{macrocode}
3112 % There is one subtle point here: while all other constructed boxes
3113 % have a depth that is determined by |\splitmaxdepth| and/or
3114 % |\boxmaxdepth| the last box
3115 % will get a natural depth disregarding the original setting and
3116 % the value of |\splitmaxdepth| or |\boxmaxdepth|. This means that
3117 % we may end up with a very large depth in box |\mult@grightbox|
3118 % which would
3119 % make the result of the testing incorrect. So we change the value
3120 % by unboxing the box into itself.
3121 % \begin{macrocode}
3122 \global\setbox\mult@grightbox
3123 \vbox{\unvbox\mult@grightbox}%
3124 % \end{macrocode}
3125 % We also save a copy |\mult@gfirstbox| at its ``natural'' size
3126 % for later use.
3127 % \begin{macrocode}
3128 \setbox\mult@nat@firstbox
3129 \vbox{\unvcopy\mult@gfirstbox}%
3130 % \end{macrocode}
3131 % After |\process@cols| has done its job we have the following
3132 % situation:
3133 % \begin{center}
3134 % \begin{tabular}{r@{$\:\:\longleftarrow\:\:$}l}
3135 % box |\mult@rightbox| & all material \\
3136 % box |\mult@firstbox| & first column \\
3137 % box |\mult@firstbox|${}+2$ & second column \\
3138 % \multicolumn{1}{c}{$\vdots$} &
3139 % \multicolumn{1}{c}{$\vdots$} \\
3140 % box |\mult@grightbox| & last column
3141 % \end{tabular}
3142 % \end{center}
3143 % We report the height of the first column, in brackets
3144 % the natural size is given.
3145 % \changes{v1.5?}{1994/?/?}{Show natural size}
3146 % \begin{macrocode}
3147 \ifnum\c@tracingmulticols>\@ne
3148 \message{^^JFirst column
3149 = \the\dimen@\space
3150 (\the\ht\mult@nat@firstbox)}\fi
3151 % \end{macrocode}
3152 % If |\raggedcolumns| is in force older releases of this file also
3153 % shrank the first column to its natural height at this point.
3154 % This was done so that the first column doesn't run short compared
3155 % to later columns but it is actually producing incorrect results
3156 % (overprinting of text) in boundary cases, so since version v1.5q
3157 % |\raggedcolumns| means allows for all columns to run slightly short.
3158 % \changes{v1.5q}{1998/01/19}{Do not reset \cs{mult@gfirstbox} (pr2739)}
3159 % \begin{macrocode}
3160 % \ifshr@nking
3161 % \global\setbox\mult@gfirstbox
3162 % \copy\mult@nat@firstbox
3163 % \fi
3164 % \end{macrocode}
3165 % Then we give information about the last column.\footnote{With
3166 % \TeX{} version 3.141 it is now possible to use \LaTeX's
3167 % \cs{newlinechar} in the \cs{message} command, but
3168 % people with older \TeX{} versions will now get
3169 % \texttt{\string^\string^J} instead of a new line on the screen.}
3170 % \changes{v1.4a}{1992/02/12}{Changed to proper \cs{endlinechar} in\cs{message}}
3171 % \begin{macrocode}
3172 \ifnum\c@tracingmulticols>\@ne
3173 \message{<> last column =
3174 \the\ht\mult@grightbox^^J}%
3175 % \end{macrocode}
3176 % Some tracing code that we don't compile into the production version
3177 % unless asked for. It will produce huge listings of the boxes
3178 % involved in balancing in the transcript file.
3179 % \changes{v1.6f}{2004/07/03}{need to use \cs{mult@grightbox} in the loop}
3180 % \begin{macrocode}
3181 %<*debug>
3182 \ifnum\c@tracingmulticols>4
3183 {\showoutput
3184 \batchmode
3185 \process@cols\mult@grightbox
3186 {\showbox\count@}}%
3187 \errorstopmode
3188 \fi
3189 %</debug>
3190 \fi
3191 % \end{macrocode}
3192 % We check whether our trial was successful. The test used is very
3193 % simple: we merely compare the first and the last column. Thus
3194 % the intermediate columns may be longer than the first if
3195 % |\raggedcolumns| is used. If the right-most column is
3196 % longer than the first then we start over with a larger value for
3197 % |\dimen@|.
3198 % \changes{v1.3c}{1991/03/03}{\cs{global}\cs{advance} left over from older code}
3199 % \begin{macrocode}
3200 \ifdim\ht\mult@grightbox >\dimen@
3201 % \end{macrocode}
3202 % If the height of the last box is too large we mark this trial as
3203 % unsuccessful.
3204 % \changes{v1.5v}{1999/07/18}{Added tracing statements for
3205 % trial unsuccessful}
3206 % \begin{macrocode}
3207 %<*badness>
3208 \too@badtrue
3209 \ifnum\c@tracingmulticols>\@ne
3210 \typeout{Rejected: last
3211 column too large!}%
3212 \fi
3213 \else
3214 % \end{macrocode}
3215 % \changes{v1.5v}{1999/07/18}{Check last column if it contains forced
3216 % break and reject trial if that is the case}
3217 %
3218 % To ensure that there isn't a forced break in the last column we
3219 % try to split off a box of size |\maxdimen| from |\mult@grightbox|
3220 % (or rather from a copy of it). This should result in a void box
3221 % after the split, unless there was a forced break somewhere within
3222 % the column in which case the material after the break would have
3223 % stayed in the box.
3224 % \begin{macrocode}
3225 \setbox\@tempboxa
3226 \copy\mult@grightbox
3227 \setbox\z@\vsplit\@tempboxa to\maxdimen
3228 \ifvoid\@tempboxa
3229 % \end{macrocode}
3230 % Thus if |\@tempboxa| is void we have a valid solution.
3231 % In this case we take a closer
3232 % look at the last column to decide if this column should be made
3233 % as long as all other columns or if it should be allowed to be
3234 % shorter.
3235 % For this we first have to rebox the column into a box of the
3236 % appropriate height. If tracing is enabled we then display the
3237 % badness for this box.
3238 % \begin{macrocode}
3239 \global\setbox\mult@grightbox
3240 \vbox to\dimen@
3241 {\unvbox\mult@grightbox}%
3242 \ifnum\c@tracingmulticols>\@ne
3243 \message{Final badness:
3244 \the\badness}%
3245 \fi
3246 % \end{macrocode}
3247 % We then compare this badness with the allowed badness for the final
3248 % column. If it does not exceed this value we use the box, otherwise
3249 % we rebox it once more and add some glue at the bottom.
3250 % \changes{v1.8h}{2014/09/12}{Use \cs{vfilmaxdepth}}
3251 % \changes{v1.8j}{2015/03/07}{Use \cs{vfil} in this case}
3252 % \begin{macrocode}
3253 \ifnum\badness>\c@finalcolumnbadness
3254 \global\setbox\mult@grightbox
3255 \vbox to\dimen@
3256 {\unvbox\mult@grightbox\vfil}%
3257 \ifnum\c@tracingmulticols>\@ne
3258 \message{ setting natural
3259 (> \the\c@finalcolumnbadness)}%
3260 \fi
3261 \fi
3262 % \end{macrocode}
3263 % If |\@tempboxa| above was not void our trial was unsuccessful and
3264 % we report this fact and try again.
3265 % \changes{v1.6f}{2004/07/03}{\texttt{/colbreak} guard in the wrong position}
3266 % \begin{macrocode}
3267 \else
3268 % \end{macrocode}
3269 % If we have unprocessed forced breaks we normally reiterate with a
3270 % larger column size to fit them in eventually. However, if there
3271 % are simply too many of them (e.g., 3 forced breaks but only 2
3272 % columns to balance) then this will never succeed and we would
3273 % continue growing the columns until we hit the largest possible
3274 % column size. So in addition we check how big the column size is
3275 % compared to available room and if we exceed this by
3276 % |\maxbalancingoverflow| we give up and instead of balancing cut
3277 % another normal page. To be indicate this case we set
3278 % |forcedbreak@leftover| to true.
3279 % \changes{v1.8k}{2015/03/21}{Watch out for columns growing too far in
3280 % case of forced breaks}
3281 % \changes{v1.8l}{2015/03/25}{Added additional tracing if column overflows}
3282 % \begin{macrocode}
3283 \@tempdima\@colroom
3284 \advance\@tempdima \maxbalancingoverflow
3285 \ifdim \dimen@ < \@tempdima
3286 \too@badtrue
3287 \ifnum\c@tracingmulticols>\@ne
3288 \typeout{Rejected: unprocessed
3289 forced break(s) in last column!}%
3290 \fi
3291 \else
3292 \forcedbreak@leftovertrue
3293 \ifnum\c@tracingmulticols>\@ne
3294 \typeout{Failed: columns too large
3295 with unprocessed forced break(s)!}%
3296 \fi
3297 \fi
3298 \fi
3299 \fi
3300 % \end{macrocode}
3301 % If the natural height of the first box is smaller than the
3302 % current trial size but is larger than the previous trial size it
3303 % is likely that we have missed a potentially better
3304 % solution. (This could have happened if for some reason our first
3305 % trial size was too high.) In that case we dismiss this trial and
3306 % restart using the natural height for the next trial.
3307 % \begin{macrocode}
3308 \ifdim\ht\mult@nat@firstbox<\dimen@
3309 \ifdim\ht\mult@nat@firstbox>\last@try
3310 \too@badtrue
3311 \ifnum\c@tracingmulticols>\@ne
3312 \typeout{Retry: using natural
3313 height of first column!}%
3314 \fi
3315 \dimen@\ht\mult@nat@firstbox
3316 \last@try\dimen@
3317 \advance\dimen@-\p@
3318 \fi
3319 \fi
3320 % \end{macrocode}
3321 % Finally the switch |too@bad| is tested. If it was made true
3322 % either earlier on or due to a rightmost column being too large
3323 % we try again with a slightly larger value for |\dimen@|.
3324 % \begin{macrocode}
3325 \iftoo@bad
3326 %</badness>
3327 \advance\dimen@\p@
3328 \repeat
3329 % \end{macrocode}
3330 % If we come out of the loop with the switch |forcedbreak@leftover|
3331 % set to true then balancing has failed and we should cut a normal
3332 % page. We indicate this below with |\too@badtrue| when any of the
3333 % columns get too high, so we set this flag here too in order to
3334 % get the same processing logic.\footnote{Should get cleaned up as
3335 % we now have two different routes to reach this part of the
3336 % processing.}
3337 % \changes{v1.8k}{2015/03/21}{}
3338 % \begin{macrocode}
3339 \ifforcedbreak@leftover
3340 \too@badtrue
3341 \else
3342 % \end{macrocode}
3343 % At that point |\dimen@| holds the height that was determined by
3344 % the balancing loop.
3345 % If that height for the columns turns out to be larger
3346 % than the available space (which is |\@colroom|) we squeeze the
3347 % columns into the space assuming that they will have enough
3348 % shrinkability to allow this.\footnote{This might be wrong, since
3349 % the shrinkability that accounts for the amount of material might
3350 % be present only in some columns. But it is better to try then to
3351 % give up directly.}
3352 % However, this squeezing should only be done if we are balancing
3353 % columns on the main galley and \emph{not} if we are building a
3354 % boxed multicol (in the latter case the current |\@colroom| is
3355 % irrelevant since the produced box might be moved anywhere at a
3356 % later stage).
3357 % \changes{v1.3c}{1991/03/03}{Limit column height to \cs{@colroom}}
3358 % \changes{v1.5q}{1998/01/19}{Removed setting \cs{dimen@} (pr2739)}
3359 % \changes{v1.5y}{2000/06/10}{Limit column height only in unrestricted
3360 % mode (pr/3212)}
3361 % \begin{macrocode}
3362 \if@boxedmulticols\else
3363 \ifdim\dimen@>\@colroom
3364 \dimen@\@colroom
3365 \fi
3366 \fi
3367 % \end{macrocode}
3368 % Then we move the contents of the odd-numbered box registers to
3369 % the even-numbered ones, shrinking them if requested.
3370 % We have to use |\vbox| not |\vtop| (as it was done in
3371 % the first versions) since otherwise the resulting boxes will have
3372 % no height (\TB\/ page 81). This would mean that extra
3373 % |\topskip| is added when the boxes are returned to the
3374 % page-builder via |\page@sofar|.
3375 % \changes{v1.3a}{1990/05/20}{Changed \cs{vtop} to \cs{vbox}.}
3376 % \begin{macrocode}
3377 \process@cols\mult@rightbox
3378 {\@tempcnta\count@
3379 \advance\@tempcnta\@ne
3380 % \end{macrocode}
3381 % when putting the final column together we want overfull
3382 % information:
3383 % \begin{macrocode}
3384 \vfuzz\z@
3385 \setbox\count@\vbox to\dimen@
3386 {%
3387 % \end{macrocode}
3388 %
3389 % \changes{v1.8h}{2014/09/12}{Use \cs{vfilmaxdepth}}
3390 % \begin{macrocode}
3391 \vskip \z@
3392 \@plus-\multicolundershoot
3393 \@minus-\multicolovershoot
3394 \unvbox\@tempcnta
3395 \ifshr@nking\vfilmaxdepth\fi
3396 }%
3397 % \end{macrocode}
3398 % If the resulting box is overfull there was too much material to
3399 % fit into the available space. The question though is how much? If
3400 % it wasn't more than |\maxbalancingoverflow| we accept it still to
3401 % avoid getting very little material for the next page (which we
3402 % would then have difficulties to balance).
3403 % \changes{v1.8a}{2011/12/20}{Balancing concept improved}
3404 % \begin{macrocode}
3405 \ifnum\badness>\@M
3406 \vfuzz\maxdimen % no overfull warning
3407 \setbox\@tempboxa \vbox to\dimen@
3408 {\vskip-\maxbalancingoverflow
3409 \unvcopy\count@}%
3410 \ifnum\badness>\@M
3411 \mult@info\@ne
3412 {Balanced column more than
3413 \the\maxbalancingoverflow\space
3414 too large}%
3415 % \end{macrocode}
3416 % Fail the balancing attempt:
3417 % \begin{macrocode}
3418 \too@badtrue
3419 \else
3420 % \end{macrocode}
3421 % Otherwise report that there is a problem but within the accepted
3422 % boundary.
3423 % \begin{macrocode}
3424 \mult@info\@ne
3425 {Balanced column
3426 too large, but less than
3427 \the\maxbalancingoverflow}%
3428 \fi
3429 \fi
3430 }%
3431 % \end{macrocode}
3432 % Finally end the |\ifforcedbreak@leftover| conditional.
3433 % \changes{v1.8k}{2015/03/21}{Finish the new conditional}
3434 % \begin{macrocode}
3435 \fi
3436 }
3437 % \end{macrocode}
3438 % \end{macro}
3439 %
3440 %
3441 % \begin{macro}{\maxbalancingoverflow}
3442 % Amount that balancing is allowed to overflow the available column
3443 % space. We default to 12pt which means about one line in most
3444 % layouts.
3445 % \changes{v1.8a}{2011/12/20}{\cs{maxbalancingoverflow} parameter added}
3446 % \begin{macrocode}
3447 \newdimen\maxbalancingoverflow
3448 \maxbalancingoverflow=12pt
3449 % \end{macrocode}
3450 % \end{macro}
3451
3452 % \end{multicols}
3453 %
3454 % \begin{multicols}{2}[\subsection{The box allocations}]
3455 %
3456 % \begin{macro}{\mult@rightbox}
3457 % \begin{macro}{\mult@grightbox}
3458 % \begin{macro}{\mult@firstbox}
3459 % \begin{macro}{\mult@gfirstbox}
3460 % Early releases of these macros used the first box registers
3461 % 0, 2, 4,\ldots\ for global boxes and 1, 3, 5,\ldots\ for the
3462 % corresponding local boxes. (You might still find some traces
3463 % of this setup in the documentation, sigh.) This produced a problem
3464 % at the moment we had more than 5 columns because then officially
3465 % allocated boxes were overwritten by the algorithm.
3466 % The new release now uses private box registers.
3467 %
3468 % There was in fact a bug in the new implementation because at one
3469 % point \LaTeX{} started to use the extended registers and so
3470 % jumped from below 255 to above omitting the boxes allocated for
3471 % inserts and the output page box.
3472 %
3473 % So nowadays we really have to check if we get the full sequence
3474 % of boxes allocated without holes (i.e., $2\times\textit{max
3475 % cols}+1$) and if not alter the allocation registers to start
3476 % allocating after 255. This is all done quite low-level by looking
3477 % directly at the values of the allocation counters.
3478 % \changes{v1.8y}{2019/12/09}{Allow for 20 columns (gh/237)}
3479 % \changes{v1.9b}{2021/10/22}{Drop one unnecessary box allocation (gh/701)}
3480 % \begin{macrocode}
3481 \ifnum\numexpr
3482 \count20-\count14-1<40
3483 % this is = 2 * 20
3484 \count14=\@cclv
3485 \fi
3486 % \end{macrocode}
3487 %
3488 % \begin{macrocode}
3489 \newbox\mult@rightbox
3490 \newbox\mult@grightbox
3491 \newbox\mult@firstbox
3492 \newbox\mult@gfirstbox
3493 \newbox\@tempa\newbox\@tempa
3494 \newbox\@tempa\newbox\@tempa
3495 \newbox\@tempa\newbox\@tempa
3496 \newbox\@tempa\newbox\@tempa
3497 \newbox\@tempa\newbox\@tempa
3498 \newbox\@tempa\newbox\@tempa
3499 \newbox\@tempa\newbox\@tempa
3500 \newbox\@tempa\newbox\@tempa
3501 \newbox\@tempa\newbox\@tempa
3502 \newbox\@tempa\newbox\@tempa
3503 \newbox\@tempa\newbox\@tempa
3504 \newbox\@tempa\newbox\@tempa
3505 \newbox\@tempa\newbox\@tempa
3506 \newbox\@tempa\newbox\@tempa
3507 \newbox\@tempa\newbox\@tempa
3508 \newbox\@tempa\newbox\@tempa
3509 \newbox\@tempa\newbox\@tempa
3510 \newbox\@tempa\newbox\@tempa
3511 \let\@tempa\relax
3512 % \end{macrocode}
3513 % \end{macro}
3514 % \end{macro}
3515 % \end{macro}
3516 % \end{macro}
3517 %
3518 %
3519 % \end{multicols}
3520 %
3521 %
3522 % \begin{multicols}{2}[\section{New macros and hacks for version 1.2}]
3523 %
3524 % \begin{macro}{\emergencystretch}
3525 % \begin{macro}{\setemergencystretch}
3526 % If we don't use \TeX{} 3.0 |\emergencystretch| is undefined
3527 % so in this case we simply add it as an unused \meta{dimen}
3528 % register.
3529 % \changes{v1.4j}{1992/06/25}{Setting of \cs{emergencystretch} on top
3530 % removed.}
3531 % \begin{macrocode}
3532 \@ifundefined{emergencystretch}
3533 {\newdimen\emergencystretch}{}
3534 % \end{macrocode}
3535 % \changes{v1.2a}{1990/02/05}{Macro added.}
3536 % My tests showed that the following formula worked pretty well.
3537 % Nevertheless the |\setemergencystretch| macro also gets
3538 % |\hsize| as second argument to enable the user to try
3539 % different formulae.
3540 % \begin{macrocode}
3541 \def\setemergencystretch#1#2{%
3542 \emergencystretch 4pt
3543 \multiply\emergencystretch#1}
3544 % \end{macrocode}
3545 % \end{macro}
3546 % \end{macro}
3547
3548
3549 % \begin{macro}{\set@floatcmds}
3550 % \changes{v1.2a}{1990/02/05}{Macro added.}
3551 % \changes{v1.5g}{1994/06/07}{Updated since floats have changed}
3552 % \changes{v1.5j}{1994/06/07}{Updated since floats have changed again}
3553 % \changes{v1.5l}{1995/10/19}{Added \cs{@largefloatcheck}}
3554 % \changes{v1.6g}{2006/02/23}{Added \cs{@minipagefalse}}
3555 % \changes{v1.6h}{2008/12/05}{Use \cs{@endfloatbox} to better support
3556 % the modifications done by the float package}
3557 % \changes{v1.9i}{2024/09/14}{Added tagging support}
3558 % Even if this should be used as a hook we use a |@| in the
3559 % name since it is more for experts.
3560 % For now we test if the socket is already defined
3561 % \begin{macrocode}
3562 \def\set@floatcmds{%
3563 \let\@dblfloat\@dbflt
3564 \def\end@dblfloat{\@endfloatbox
3565 \UseTaggingSocket{float/end}%
3566 \@largefloatcheck
3567 \outer@nobreak
3568 % \end{macrocode}
3569 % This is cheap (deferring the floats until after the current page)
3570 % but any other solution would go deep into \LaTeX's output
3571 % routine and I don't like to work on it until I know which parts
3572 % of the output routine have to be reimplemented anyway for
3573 % \LaTeX3.
3574 % \begin{macrocode}
3575 \ifnum\@floatpenalty<\z@
3576 % \end{macrocode}
3577 % We have to add the float to the |\@deferlist| because we assume
3578 % that outside the \mc{} environment we are in one column mode.
3579 % This is not entirely correct, I already used the \mc{}
3580 % environment inside of \LaTeX{}s |\twocolumn| declaration but it
3581 % will do for most applications.
3582 % \begin{macrocode}
3583 \@cons\@deferlist\@currbox
3584 \fi
3585 \ifnum\@floatpenalty=-\@Mii
3586 \@Esphack
3587 \UseTaggingSocket{float/hmode/end}%
3588 \fi}}
3589 % \end{macrocode}
3590 % \end{macro}
3591 %
3592 %
3593 %
3594 % \begin{macro}{\remove@discardable@items}
3595 % There are situations when we may have some space at the end of a
3596 % column and this macro here will attempt to get rid of it. The
3597 % typical \LaTeX{} sequence is a series of self-canceling glues so
3598 % if we remove them recursively we are usually fine.
3599 %
3600 % Special care is needed with handling |\vspace*| as that
3601 % corresponds to |\penalty10000|, |\vskip <skip>|, followed by
3602 % |\vskip 0pt|. If we see this sequence going backwards in the
3603 % vertical list we assume that this is a ``desired'' space. We
3604 % therefore stop the recursion and reinsert the spaces.
3605 %
3606 % As the multicol code sometimes add an explicit penalty at the end
3607 % of a column we first attempt to remove it in case it is there.
3608 % \changes{v1.8m}{2015/03/31}{Another rewrite of
3609 % \cs{remove@discardable@items} hopefully okay now}
3610 % \begin{macrocode}
3611 \skip0=0pt
3612 \edef\the@zero@skip{\the\skip0}
3613 \def\remove@discardable@items{%
3614 \unpenalty
3615 % \end{macrocode}
3616 % Save a previous skip (if there) and then remove it, we can't
3617 % really tell the difference between no skip an a skip of zero but
3618 % that's life.
3619 % \begin{macrocode}
3620 \edef\@tempa{\the\lastskip}%
3621 %\typeout{s1=\@tempa}%
3622 \unskip
3623 % \end{macrocode}
3624 % If it was a zero skip (or none) we save the next previous skip
3625 % (if any).
3626 % \begin{macrocode}
3627 \ifx\@tempa\the@zero@skip
3628 \edef\@tempb{\the\lastskip}%
3629 %\typeout{s2=\@tempb}%
3630 % \end{macrocode}
3631 % If this one again was zero (or more likely not there in the first
3632 % place) we stop.
3633 % \begin{macrocode}
3634 \ifx\@tempb\the@zero@skip
3635 \else
3636 % \end{macrocode}
3637 % Otherwise we remove this ``real'' skip. Then we look if it was
3638 % preceded by a penalty of 10000 (i.e., a |\nobreak|)
3639 % \begin{macrocode}
3640 \unskip
3641 %\typeout{p=\lastpenalty}%
3642 \ifnum \lastpenalty=\@M
3643 % \end{macrocode}
3644 % If so this was a |\vspace*| or something equivalent to
3645 % it. Therefore we reintroduce the skips and stop. Otherwise we
3646 % recurse.
3647 % \begin{macrocode}
3648 \vskip\@tempb\vskip\@tempa\relax
3649 \else
3650 \remove@discardable@items
3651 \fi
3652 \fi
3653 \else
3654 % \end{macrocode}
3655 % If the first skip was a non-zero skip we recurse as well.
3656 % \begin{macrocode}
3657 \remove@discardable@items
3658 \fi
3659 }
3660 % \end{macrocode}
3661 % \end{macro}
3662 %
3663 % \begin{macrocode}
3664 %<*badness>
3665 \newif\iftoo@bad
3666 \def\too@badtrue{\global\let\iftoo@bad\iftrue}
3667 \def\too@badfalse{\global\let\iftoo@bad\iffalse}
3668 % \end{macrocode}
3669 %
3670 % \changes{v1.8k}{2015/03/21}{The new switch}
3671 % \begin{macrocode}
3672 \newif\ifforcedbreak@leftover
3673 % \end{macrocode}
3674 %
3675 % \begin{macro}{\c@minrows}
3676 % \changes{v1.8w}{2019/03/01}{Provide minrows counter for balancing}
3677 % \begin{macrocode}
3678 \newcount\c@minrows
3679 \c@minrows=1
3680 % \end{macrocode}
3681 % \end{macro}
3682 %
3683 % \begin{macro}{\c@columnbadness}
3684 % \begin{macro}{\c@finalcolumnbadness}
3685 % \begin{macrocode}
3686 \newcount\c@columnbadness
3687 \c@columnbadness=10000
3688 \newcount\c@finalcolumnbadness
3689 \c@finalcolumnbadness=9999
3690
3691 \newdimen\last@try
3692
3693 % \end{macrocode}
3694 %
3695 % \changes{v1.5z1}{2003/02/17}{Change wrong default for
3696 % \cs{multicolovershoot} to zero (pr/3465).}
3697 % \begin{macrocode}
3698 \newdimen\multicolovershoot
3699 \newdimen\multicolundershoot
3700 \multicolovershoot=0pt
3701 \multicolundershoot=2pt
3702 \newbox\mult@nat@firstbox
3703 %</badness>
3704 % \end{macrocode}
3705 % \end{macro}
3706 % \end{macro}
3707 %
3708 % \begin{macro}{\mult@info}
3709 % A helper for producing info messages
3710 % \begin{macrocode}
3711 \def\mult@info#1#2{%
3712 \ifnum\c@tracingmulticols>#1%
3713 \GenericWarning
3714 {(multicol)\@spaces\@spaces}%
3715 {Package multicol: #2}%
3716 \fi
3717 }
3718 % \end{macrocode}
3719 % \end{macro}
3720 %
3721 % \end{multicols}
3722 %
3723 %
3724 % \begin{multicols}{2}[\section{Fixing the
3725 % \cs{columnwidth}}]
3726 %
3727 % \begin{macro}{\@footnotetext}
3728 % \changes{v1.5o}{1997/11/16}{Redefinition added pr/2664.}
3729 % \changes{v1.5r}{1998/08/17}{Use \cs{@footnotetext} but with
3730 % local change to \cs{columnwidth}.}
3731 % \begin{macro}{\mult@footnotetext}
3732 % \changes{v1.5r}{1998/08/17}{Macro removed again.}
3733 % If we store the current column width in |\columnwidth| we have
3734 % to redefine the internal |\@footnotetext| macro to use |\textwidth|
3735 % for the width of the footnotes rather than using the original
3736 % definition.
3737 %
3738 % Starting with version v1.5r this is now done in a way that the original
3739 % definition is still used, except that locally |\columnwidth| is set to
3740 % |\textwidth|.
3741 %
3742 % This solves two problems: first redefinitions of |\@footnotetext|
3743 % done by a class will correctly survive and second if multicols is
3744 % used inside a minipage environment the special definition of
3745 % |\@footnotetext| in that environment will be picked up and not the
3746 % one for the main galley (the latter would result in all footnotes
3747 % getting lost in that case).
3748 %
3749 % See the definition of the |\multicols| command further up for the exact
3750 % code.
3751 % \end{macro}
3752 % \end{macro}
3753 % \end{multicols}
3754 %
3755 %
3756 % \section{Further extensions}
3757 %
3758 % This section contains code for extensions added to this package
3759 % over time. Not all of them may be active, some might sit dormant and
3760 % wait for being activated in some later release.
3761 %
3762 % \subsection{Not balancing the columns}
3763 %
3764 % This is fairly trivial to implement. we just have to disable the
3765 % balancing output routine and replace it by the one that ships out
3766 % the other pages.
3767 %
3768 % \begin{macro}{\multicols*}
3769 % \changes{v1.5q}{1998/01/19}{Macro added}
3770 % The code for this environment was suggested by Matthias Clasen.
3771 % \begin{macrocode}
3772 %<*nobalance>
3773 \@namedef{multicols*}{%
3774 % \end{macrocode}
3775 % If we are not on the main galley, i.e., inside a box of some
3776 % sort, that approach will not work since we don't have a vertical
3777 % size for the box so we better warn that we balance anyway.
3778 % \begin{macrocode}
3779 \ifinner
3780 \PackageWarning{multicol}%
3781 {multicols* inside a box does
3782 not make sense.\MessageBreak
3783 Going to balance anyway}%
3784 \else
3785 % \end{macrocode}
3786 % If we aren't balancing we change the |\balance@columns@out| to
3787 % work like the normal output routine that cuts normal
3788 % pages. However, there is a catch: In case the last page we cut
3789 % (after seeing the end of the environment) is actually larger than
3790 % a page (for example, if it contains more |\columnbreak| commands
3791 % than columns) we end up with some leftover material that is
3792 % returned to the main galley, but now the environment end penalty
3793 % is missing. So we add another one here too. Of course that
3794 % shouldn't be done if there is really only a single final page,
3795 % but fortunately in that case we have just finished a page and any
3796 % penalty on the recent contributions will be discarded, thus the
3797 % extra one is harmless---puh.
3798 % \changes{v1.8t}{2018/06/26}{Re-add end penalty for
3799 % \texttt{multicols*} environment to guard against leftovers (gh/53)}
3800 % \begin{macrocode}
3801 \def\balance@columns@out
3802 {\multi@column@out \penalty-\@Mvi }%
3803 \fi
3804 \begin{multicols}
3805 }
3806 % \end{macrocode}
3807 % \end{macro}
3808 %
3809 % \begin{macro}{\endmulticols*}
3810 % When ending the environment we simply end the inner
3811 % \texttt{multicols} environment, except that we better also stick
3812 % in some stretchable vertical glue so that the last column still
3813 % containing text is not vertically stretched out.
3814 %
3815 % We do this as follows: first we ensure that we are back in vertical mode
3816 % and then we cancel out |\lastskip| if it was
3817 % positive (in case of a negative glue we assume that it was
3818 % deliberate, for a deliberate positive glue one needs to use
3819 % |\vspace*|). We can't simply use |\remove@discardable@items| here
3820 % as this only works inside boxes but we are here on the main
3821 % vertical list.
3822 %
3823 % Then we back up by |\prevdepth| but not more than |\boxmaxdepth|
3824 % so that a baseline of the last box is now at the bottom. This way
3825 % the material will align properly in case something like |\vfill|
3826 % spreads it out after all.
3827 % Finally we append |\vfil| to put white space at the bottom of the
3828 % column, but we only do this if we aren't anyway doing |\raggedcolumns|.
3829 % \changes{v1.5q}{1998/01/19}{Macro added}
3830 % \changes{v1.8h}{2014/09/12}{Use \cs{vfilmaxdepth}}
3831 % \changes{v1.8i}{2014/10/28}{Add \cs{null} to hide the final fill and only add
3832 % vertical space if not doing \cs{raggedcolumns}}
3833 % \changes{v1.8j}{2015/03/07}{Redesign the whole approach.}
3834 % \changes{v1.8k}{2015/03/21}{And a bit more redesign because of the
3835 % change in \cs{remove@discardable@items}}
3836 % \changes{v1.8o}{2016/02/08}{Ensure we are back in vmode before using
3837 % \cs{prevdepth} (pr/4448)}
3838 % \begin{macrocode}
3839 \@namedef{endmulticols*}{%
3840 \par
3841 \ifdim\lastskip>\z@ \vskip-\lastskip \fi
3842 \ifdim \prevdepth>\z@
3843 \vskip-\ifdim\prevdepth>\boxmaxdepth
3844 \boxmaxdepth
3845 \else \prevdepth \fi
3846 \fi
3847 \ifshr@nking\else
3848 \vfil
3849 \fi
3850 \end{multicols}}
3851 %</nobalance>
3852 % \end{macrocode}
3853 % \end{macro}
3854 %
3855 %
3856 % \subsection{Manual column breaking} \label{sec:colbreak}
3857 %
3858 % The problem with manual page breaks within \mc{} is the fact that
3859 % during collection of material for all columns a page-forcing penalty
3860 % (i.e. -10000 or higher) would stop the collecting pass which is not
3861 % quite what is desired. On the other hand, using a penalty like -9999
3862 % would mean that there would be occasions where the |\vsplit|ing
3863 % operations within \mc{} would ignore that penalty and still choose a
3864 % different break point.
3865 %
3866 % For this reason the current implementation
3867 % uses a completely different approach. In a nutshell it extends the \LaTeX{}
3868 % output routine handling by introducing an additional penalty flag
3869 % (i.e., a penalty which is forcing but higher than -10000 so that the
3870 % output routine can look at this value and thus knows why it has been
3871 % called).
3872 %
3873 % Inside the output routine we test for this value and if it appears
3874 % we do two things: save the galley up to this point in a special box
3875 % for later use and reduce the |\vsize| by the height of the material
3876 % seen. This way the forcing penalty is now hidden in that box and we
3877 % can restart the collection process for the remaining
3878 % columns. (This is done in |\speci@ls| above.)
3879 %
3880 % In the output routines that do the |\vsplit|ting either for
3881 % balancing or for a full page we simply combine box~255 with the
3882 % saved box thus getting a single box for splitting which now
3883 % contains forcing breaks in the right positions.
3884 %
3885 %
3886 % \begin{macro}{\columnbreak}
3887 % \changes{v1.5u}{1999/05/25}{Macro added}
3888 % \changes{v1.9a}{2021/10/08}{Added optional argument for conditional break}
3889 % \changes{v1.9b}{2021/10/28}{Corrected error message text (gh/703)}
3890 % |\columnbreak| is modeled after |\pagebreak| except that we
3891 % generate a penalty -10005.
3892 % \begin{macrocode}
3893 \mathchardef\@Mv=10005
3894 \newcommand\columnbreak[1][4]{%
3895 % \end{macrocode}
3896 % We have to ensure that it is only used within a \mc{}
3897 % environment since if that penalty would be seen by the unmodified
3898 % \LaTeX{} output routine strange things would happen.
3899 % \begin{macrocode}
3900 \ifnum\col@number<\tw@
3901 \PackageError{multicol}%
3902 {\noexpand\columnbreak outside multicols}%
3903 {This command can only be used within
3904 a multicols or multicols* environment.}%
3905 \else
3906 % \end{macrocode}
3907 % Increasingly lower penalty based on argument value. This is like
3908 % \cs{pagebreak} but we use other penalty values as the \LaTeX{}
3909 % defaults are rather useless for pagination.
3910 % \begin{macrocode}
3911 \edef\mc@break@pen
3912 {-\ifcase#1\@m\or 3333\or 6666\or 9999\else\@Mv\fi\relax}%
3913 \ifvmode
3914 \penalty \mc@break@pen
3915 \else
3916 \@bsphack
3917 \vadjust{\penalty \mc@break@pen}%
3918 \@esphack
3919 \fi
3920 \fi}
3921 % \end{macrocode}
3922 % \end{macro}
3923 %
3924 %
3925 %
3926 % \begin{macro}{\newcolumn}
3927 % \changes{v1.9a}{2021/10/08}{Macro added}
3928 % This is modeled after \cs{newpage} but for column breaks.
3929 % \begin{macrocode}
3930 \newcommand\newcolumn{%
3931 \ifnum\col@number<\tw@
3932 \PackageError{multicol}%
3933 {\noexpand\newcolumn outside multicols}%
3934 {This command can only be used within
3935 a multicols or multicols* environment.}%
3936 \else
3937 \ifvmode
3938 % \end{macrocode}
3939 % We need to guard the \cs{vfill} from disappearing.
3940 % \changes{v1.9d}{2021/12/05}{Guard the \cs{vfill} (sx/624940)}
3941 % \begin{macrocode}
3942 \nobreak\vfill\kern\z@\penalty -\@Mv\relax
3943 \else
3944 \@bsphack
3945 \vadjust{\nobreak\vfill\kern\z@\penalty -\@Mv\relax}%
3946 \@esphack
3947 \fi
3948 \fi}
3949 % \end{macrocode}
3950 % \end{macro}
3951 %
3952 % \begin{macro}{\colbreak@box}
3953 % \changes{v1.5u}{1999/05/25}{Macro added}
3954 % Need a box to collect the galley up to the column break.
3955 % \begin{macrocode}
3956 \newbox\colbreak@box
3957 %</package>
3958 % \end{macrocode}
3959 % \end{macro}
3960 %
3961 %
3962 % \subsection{Supporting right-to-left languages} \label{sec:RL}
3963 %
3964 %
3965 % \changes{v1.7a}{2010/10/24}{RL language support added}
3966 % \changes{v1.7b}{2011/12/18}{RL language support fixed}
3967 %
3968 % \begin{macro}{\LR@column@boxes}
3969 % |\LR@column@boxes| is called when we are assembling the columns for left
3970 % to right typesetting. When we start we are inside an |\hbox| of
3971 % full width.
3972 % Left to right typesetting is fairly easy, we basically output
3973 % each column box intermixed with vertical rules and proper
3974 % spacing. As this happens inside a box of a defined width the
3975 % rules and the columns automatically get into the right positions.
3976 % \begin{macrocode}
3977 \def\LR@column@boxes{%
3978 % \end{macrocode}
3979 % We loop through the columns with |\process@cols|
3980 % \begin{macrocode}
3981 \process@cols\mult@firstbox{%
3982 % \end{macrocode}
3983 % If the depth of the current box is larger than the maximum found
3984 % so far in |\dimen2| we update that register for later use.
3985 % \changes{v1.6a}{2003/03/15}{Preparing for adjusting \cs{prevdepth}}
3986 % \begin{macrocode}
3987 \ifdim\dp\count@>\dimen\tw@
3988 \global\dimen\tw@\dp\count@ \fi
3989 % \end{macrocode}
3990 % If the \texttt{colaction} option is given we write out status
3991 % information about the current column, otherwise the next command
3992 % does nothing.
3993 % \changes{v1.8e}{2014/04/23}{Support \cs{docolaction}}
3994 % \begin{macrocode}
3995 \mc@col@status@write
3996 % \end{macrocode}
3997 % The typeset box followed by the column rule material
3998 % \begin{macrocode}
3999 \box\count@
4000 \hss{\columnseprulecolor\vrule
4001 \@width\columnseprule}\hss}%
4002 % \end{macrocode}
4003 % As you will have noticed, we started with box register
4004 % |\mult@firstbox| (i.e.\
4005 % the left column). So this time |\count@| looped through 2,
4006 % 4,\ldots\ (plus the appropriate offset).
4007 % Finally we add box |\mult@rightbox| and we are done.
4008 % \changes{v1.5a}{1992/11/04}{New box mechanism}
4009 % \changes{v1.6a}{2003/03/15}{Preparing for adjusting \cs{prevdepth}}
4010 % Again we may have to update |\dimen\tw@|.
4011 % \begin{macrocode}
4012 \ifdim\dp\mult@rightbox>\dimen\tw@
4013 \global\dimen\tw@\dp\mult@rightbox \fi
4014 % \end{macrocode}
4015 % If the \texttt{colaction} option is given we write out status
4016 % information about the last column, otherwise the next command
4017 % does nothing.
4018 % \changes{v1.8e}{2014/04/23}{Support \cs{docolaction}}
4019 % \begin{macrocode}
4020 \mc@lastcol@status@write
4021 \box\mult@rightbox
4022 }
4023 % \end{macrocode}
4024 % \end{macro}
4025
4026 % \begin{macro}{\RL@column@boxes}
4027 % Assembling the boxes for right to left typesetting is far more
4028 % complicated. When I first tried to build a solution for this my
4029 % thinking was that all that is necessary to do is to reverse the
4030 % order of the columns. But such an approach produces a subtle bug:
4031 % If we work this way then the first column put on the page will be
4032 % the last column of the text to read. and this means that the
4033 % order in which \TeX{} executes write statements or assembles mark
4034 % material will not happen in the order of the textual flow. So if,
4035 % for example each column contains a section command then these
4036 % sections will appear in reverse order in the table of content.
4037 %
4038 % For this reason some amount of gymnastics is needed to add the
4039 % columns in their natural flow.
4040 % \begin{macrocode}
4041 \def\RL@column@boxes{%
4042 % \end{macrocode}
4043 % First step is to put all rules in the right place (without adding
4044 % the comes which are instead represented by a space of |\hsize|.
4045 % \begin{macrocode}
4046 \process@cols\mult@firstbox{%
4047 \hskip\hsize
4048 \hss{\columnseprulecolor\vrule
4049 \@width\columnseprule}\hss
4050 }%
4051 \hskip\hsize
4052 % \end{macrocode}
4053 % At this point in the code our typesetting reference point is at
4054 % the right end of the rightmost column (or rather where that column
4055 % should appear).
4056 %
4057 % We are now typesetting all columns by first backing up by their
4058 % width (which is |\hsize|) then typesetting the box and then
4059 % backing up again, but this time further, i.e., also across the
4060 % column separation. That will then enable us to typeset the next
4061 % column using the same approach until we are done with all but the
4062 % final column.
4063 % \begin{macrocode}
4064 \process@cols\mult@firstbox{%
4065 \ifdim\dp\count@>\dimen\tw@
4066 \global\dimen\tw@\dp\count@ \fi
4067 \hskip-\hsize
4068 % \end{macrocode}
4069 %
4070 % \changes{v1.8e}{2014/04/23}{Support \cs{docolaction}}
4071 % \begin{macrocode}
4072 \mc@col@status@write
4073 \box\count@
4074 \hskip-\hsize
4075 \hskip-\columnsep
4076 }%
4077 % \end{macrocode}
4078 % The approach for the final column is similar only that we do not
4079 % have to back up over any column gap.
4080 % \begin{macrocode}
4081 \ifdim\dp\mult@rightbox>\dimen\tw@
4082 \global\dimen\tw@\dp\mult@rightbox \fi
4083 \hskip-\hsize
4084 % \end{macrocode}
4085 %
4086 % \changes{v1.8e}{2014/04/23}{Support \cs{docolaction}}
4087 % \begin{macrocode}
4088 \mc@lastcol@status@write
4089 \box\mult@rightbox
4090 \hskip-\hsize
4091 % \end{macrocode}
4092 % However we do have to move the reference point to its right
4093 % place: to make the rules appear at the expected places, we should
4094 % get the typesetting position to the far right again. As we at the
4095 % moment at the far left we skip to the far right like this:
4096 % \begin{macrocode}
4097 \hskip\full@width
4098 }
4099 % \end{macrocode}
4100 % \end{macro}
4101 %
4102 % \begin{macro}{\RLmulticolcolumns}
4103 % \begin{macro}{\LRmulticolcolumns}
4104 % \begin{macro}{\mc@align@columns}
4105 % Macros to switch between left-right and right-left typesetting. In LR
4106 % typesetting the |\LR@column@boxes| is used to combine
4107 % the columns. When typesetting right to left the |\RL@column@boxes|
4108 % is used instead.
4109 % \begin{macrocode}
4110 \newcommand\RLmulticolcolumns
4111 {\let\mc@align@columns
4112 \RL@column@boxes}
4113 \newcommand\LRmulticolcolumns
4114 {\let\mc@align@columns
4115 \LR@column@boxes}
4116 % \end{macrocode}
4117 % The default is left-to-right:
4118 % \begin{macrocode}
4119 \LRmulticolcolumns
4120 % \end{macrocode}
4121 % \end{macro}
4122 % \end{macro}
4123 % \end{macro}
4124 %
4125 %
4126 % \subsection{Supporting \texttt{\textbackslash docolaction}}
4127 %
4128 % \changes{v1.8e}{2014/04/23}{Support \cs{docolaction}}
4129 %
4130 % Whenever we want to do something that depends on the current
4131 % column we execute \verb=\docolaction=. This command takes one
4132 % optional and three mandatory arguments. The mandatory ones denote
4133 % what to do if this is a ``left'', ``middle'', or ``right'' column
4134 % and the optional one is simply there to say what to do if we don't
4135 % know (default is to use the ``left'' column action in that case).
4136 %
4137 % \begin{macro}{\mc@col@check@num}
4138 % We use one counter \verb=\mc@col@check@num= to generate us unique
4139 % label names. Each time we execute \verb=\docolaction= we increment
4140 % this counter to get a new name.
4141 % \begin{macrocode}
4142 \newcount\mc@col@check@num
4143 % \end{macrocode}
4144 % \end{macro}
4145 %
4146 % The generated ``labels'' are named
4147 %\begin{verbatim}
4148 %\mc@col-\the\mc@col@check@num
4149 %\end{verbatim}
4150 % and they hold as values the
4151 % numbers 1, 2, or 3 denoting the current column type.
4152
4153 % \begin{macro}{\docolaction}
4154 %
4155 % The \verb=\docolaction= scans for a star and optional argument
4156 % and 3 mandatory ones, but we do this in chunks (not having xparse
4157 % available).\footnote{We can do better now, as
4158 % \cs{NewDocumentCommand} is part of the kernel. So this should be
4159 % cleaned up one day.}
4160 %
4161 % \changes{v1.8u}{2018/11/09}{Support star with \cs{docolaction}}
4162 % \begin{macrocode}
4163 \newcommand\docolaction{%
4164 % \end{macrocode}
4165 % First check is the support is enabled.
4166 % \begin{macrocode}
4167 \ifx\mc@col@status@write\relax
4168 \PackageError{multicol}%
4169 {Option 'colaction' not selected}%
4170 {\string\docolaction\space
4171 requires the use of the 'colaction'
4172 option on the package}%
4173 \fi
4174 % \end{macrocode}
4175 % Then prepare \verb=\mc@col@type=.
4176 % \begin{macrocode}
4177 \global\advance\mc@col@check@num\@ne
4178 \edef\mc@col@type{\expandafter\ifx
4179 \csname mc@col-\the\mc@col@check@num
4180 \endcsname\relax
4181 0\else
4182 \csname mc@col-\the\mc@col@check@num
4183 \endcsname
4184 \fi}%
4185 % \end{macrocode}
4186 % Finally check for a star, record this information and then call
4187 % \verb=\@docolaction= to do the rest.
4188 % \begin{macrocode}
4189 \@ifstar
4190 {\@docolactionstartrue \@docolaction}%
4191 {\@docolactionstarfalse\@docolaction}%
4192 }
4193 % \end{macrocode}
4194 %
4195 % \begin{macrocode}
4196 \newcommand\@docolaction[4][1]{%
4197 % \end{macrocode}
4198 %
4199 % How does the column number get associated with our label? We
4200 % do this by writing another line into the aux file. Here are the
4201 % preparations.
4202 % \begin{macrocode}
4203 \edef\@docolactioncheck{\write\@auxout
4204 {\string\mc@set@col@status
4205 {mc@col-\the\mc@col@check@num}%
4206 {\mc@col@type}}}%
4207 % \end{macrocode}
4208 % Where we do the actual \verb=\write= depends on the whether or
4209 % not we gave seen a \texttt{*}. If yes, we do it first and then
4210 % execute the code argument, otherwise we execute that code first
4211 % and check at the point after that.
4212 % \begin{macrocode}
4213 \if@docolactionstar \@docolactioncheck \fi
4214 % \end{macrocode}
4215 % We prefix with 0 so that an unknown label (that returns
4216 % \verb=\relax=) will result in case 0
4217 % \begin{macrocode}
4218 \ifcase \mc@col@type\relax
4219 % \end{macrocode}
4220 % If column is unknown we use the default action or the action
4221 % denoted by the optional argument (so that arg can take the value
4222 % 1, 2, 3).
4223 % \begin{macrocode}
4224 \ifcase #1\or #2\or#3\or#4\fi
4225 \or
4226 % \end{macrocode}
4227 % Otherwise we know (or think we know) that this is a first, middle,
4228 % or last column:
4229 % \begin{macrocode}
4230 #2% % 1 First col
4231 \or
4232 #3% % 2 any middle col
4233 \or
4234 #4% % 3 last col
4235 \else
4236 \ERRORwrongdefaultgiven
4237 \fi
4238 % \end{macrocode}
4239 % \begin{macrocode}
4240 \if@docolactionstar \else \@docolactioncheck \fi
4241 }
4242 % \end{macrocode}
4243 % \end{macro}
4244 %
4245 % Here is the if used above:
4246 % \begin{macrocode}
4247 \newif\if@docolactionstar
4248 % \end{macrocode}
4249 %
4250 % Because of extra data writing to the aux file the aux file will
4251 % now contain something like the following after the document is
4252 % processed the first time:
4253 %\begin{verbatim}
4254 %\relax
4255 %\mc@col@status{1}
4256 %\mc@set@col@status{lcol-1}{0}
4257 %\mc@col@status{2}
4258 %\mc@set@col@status{lcol-2}{0}
4259 %\mc@col@status{3}
4260 %\mc@set@col@status{lcol-3}{0}
4261 %\mc@col@status{1}
4262 %\mc@col@status{2}
4263 %\mc@col@status{3}
4264 %\mc@set@col@status{lcol-4}{0}
4265 %\end{verbatim}
4266 % The \verb=\mc@col@status= line denotes the column type and has been
4267 % written out just before corresponding the column box was placed
4268 % onto the page.
4269 % The\verb=\mc@set@col@status= lines have been written out as part
4270 % of shipping the column boxes out, e.g.,
4271 % \verb=\mc@set@col@status{lcol-1}{0}= was therefore somewhere within
4272 % the first column as it appears between \verb=\mc@col@status{1}=
4273 % and \verb=\mc@col@status{2}=
4274 % The second argument in that line is the value used in the previous
4275 % run (or zero if there was no previous run. We can use this to
4276 % determine if a rerun is necessary.
4277 %
4278 % Thus with this knowledge we can set things up to get the labels
4279 % working.
4280 %
4281 %
4282 % \begin{macro}{\mc@col@status}
4283 %
4284 % When the aux file is read in \verb=\mc@col@status= is used to set
4285 % \verb=\mc@curr@col@status=:
4286 % \begin{macrocode}
4287 \def\mc@col@status#1{%
4288 \gdef\mc@curr@col@status{#1}}
4289 % \end{macrocode}
4290 % \end{macro}
4291 %
4292 %
4293 %
4294 % \begin{macro}{\mc@set@col@status}
4295 %
4296 % And when \verb=\mc@set@col@status= is executed we can simply set
4297 % up the label by associating it with the \verb=\mc@curr@col@status=
4298 % and ignore the second argument:
4299 % \begin{macrocode}
4300 \def\mc@set@col@status#1#2{%
4301 \global\expandafter\let\csname #1\endcsname
4302 \mc@curr@col@status}
4303 % \end{macrocode}
4304 % The above definition is being used when the \texttt{.aux} file is
4305 % read in at the beginning. At the end we need a different
4306 % definition to test if another typesetting run is needed. There we
4307 % compare the value used in the current run (stored in the second
4308 % argument) with the value used on the next run. If those two values
4309 % differ we set \verb=@tempswa= to false which will trigger the
4310 % ``Label(s) may have changed'' warning.
4311 % \begin{macrocode}
4312 \AtEndDocument{\def\mc@set@col@status#1#2{%
4313 \ifnum #2=\mc@curr@col@status\else
4314 \@tempswatrue
4315 \fi}%
4316 }
4317 % \end{macrocode}
4318 % \end{macro}
4319 %
4320 %
4321 % \begin{macro}{mc@firstcol}
4322 % Finally, as part of determining in which column we are, we used a
4323 % switch inside |\mc@col@status@write| to determine if we are in the
4324 % first column or not.
4325 % \begin{macrocode}
4326 \newif\ifmc@firstcol
4327 \mc@firstcoltrue
4328 % \end{macrocode}
4329 % \end{macro}
4330 %
4331 %
4332 %
4333 %
4334 % \subsection{Using the new mark mechanism}
4335 %
4336 % \subsubsection{Helpers}
4337 %
4338 % \changes{v2.0a}{2024/11/10}{Using the new mark mechanism}
4339 % \begin{macrocode}
4340 \ExplSyntaxOn
4341 %<@@=mc>
4342 % \end{macrocode}
4343 %
4344 %
4345 %
4346 % \begin{macro}{\g_@@_curr_col_int}
4347 % Counter for tracking the current column number. At the start of a
4348 % \mc{} environment is holds the number of columns for
4349 % which there is currently \texttt{mcol-...} data (i.e., the number
4350 % of columns in the last \mc{} environment).
4351 % \begin{macrocode}
4352 \int_new:N \g_@@_curr_col_int
4353 % \end{macrocode}
4354 % \end{macro}
4355 %
4356 %
4357 %
4358 % \begin{macro}{\@@_debug_marks:n}
4359 % For now we reuse the internal debugging interface of ltmarks,
4360 % this will probably change
4361 % \begin{macrocode}
4362 \cs_new:Npn \@@_debug_marks:n #1 { \__mark_debug:n {#1} }
4363 % \end{macrocode}
4364 % \end{macro}
4365 %
4366 % \begin{macro}{\@@_update_mcol_structures:}
4367 %
4368 % Helper function to update the \texttt{mcol-...} regions when we
4369 % finish a page while within a \mc{} environment or when we finish
4370 % the \mc{} and return the balanced columns back to the galley.
4371 %
4372 % \begin{macrocode}
4373 \cs_new_protected:Npn \@@_update_mcol_structures: {
4374 \@@_debug_marks:n
4375 { \typeout{Marks:~ update~ mcol~ structures~ (multicol)} }
4376 % \end{macrocode}
4377 % It might be possible that there was a previous \mc{} (either
4378 % before the current one, or a boxed one inside) and that one might
4379 % have had more columns than the current one. If so, we should make these
4380 % column structures an error as they are no longer valid (or at
4381 % least empty them, not sure what is better).
4382 % \begin{macrocode}
4383 \int_step_inline:nnn {\col@number + 1} { \g_@@_curr_col_int }
4384 { \mark_set_structure_to_err:n { mcol - ##1 } }
4385 % \end{macrocode}
4386 %
4387 % There is no need to do anything to \texttt{mcol-1} up to
4388 % \texttt{mcol-\meta{\cs{col@number}}} because those regions get
4389 % new data in a second.
4390 %
4391 % Once we have done this we reset the column counter for further
4392 % processing.
4393 % \begin{macrocode}
4394 \int_gset:Nn \g_@@_curr_col_int {1}
4395 % \end{macrocode}
4396 % Now we loop through all the assembled column material, using the
4397 % \texttt{column} and \texttt{previous-column} regions as an
4398 % intermediate holding area.
4399 % \begin{macrocode}
4400 \process@cols\mult@firstbox
4401 {
4402 \mark_update_structure_from_material:nn
4403 %fmi {mcol}
4404 {column}
4405 {\unvcopy\count@}
4406 % \end{macrocode}
4407 % Once the \texttt{column} region got updated we copy it to
4408 % \texttt{mcol-\meta{\cs{\detokenize{g_@@_curr_col_int}}}} and then increment
4409 % the counter.
4410 % \begin{macrocode}
4411 \mark_copy_structure:nn
4412 {mcol - \int_use:N\g_@@_curr_col_int }
4413 %fmi {mcol}
4414 {column}
4415 \int_gincr:N \g_@@_curr_col_int
4416 }
4417 % \end{macrocode}
4418 % The above loop takes care of all columns, except for the last one
4419 % (which is stored in \cs{mult@rightbox}. So we have to do that separately.
4420 % \begin{macrocode}
4421 \mark_update_structure_from_material:nn
4422 %fmi {mcol}
4423 {column}
4424 {\unvcopy\mult@rightbox}
4425 \mark_copy_structure:nn
4426 { mcol - \int_use:N\g_@@_curr_col_int }
4427 %fmi {mcol}
4428 {column}
4429 % \end{macrocode}
4430 % Two more aliases to take care of: \texttt{first-column} and
4431 % \texttt{last-column} and we are done:
4432 % \begin{macrocode}
4433 \mark_copy_structure:nn{first-column}{mcol-1}
4434 %fmi \mark_copy_structure:nn{last-column} {mcol}
4435 \mark_copy_structure:nn{last-column} {column}
4436 }
4437 % \end{macrocode}
4438 % \end{macro}
4439 %
4440
4441
4442 % \begin{macro}{\@@_update_page_structures:}
4443 % If we are making a page while inside a \mc{} environment, we also
4444 % have to take care of the page region.
4445 % \begin{macrocode}
4446 \cs_new_protected:Npn \@@_update_page_structures: {
4447 \@@_debug_marks:n
4448 {
4449 \typeout{Marks:~ update~ page~ structure~ (multicol)}
4450 }
4451 % \end{macrocode}
4452 % Since for the \texttt{page} region we are only interested in the
4453 % top, first, and last marks on the whole page regardless in which
4454 % column they appear, we simply string together all columns in a big
4455 % box and update the structure from that.
4456 % \begin{macrocode}
4457 \mark_update_structure_from_material:nn
4458 {page}
4459 {
4460 % \end{macrocode}
4461 % And we better not forget the \cs{partial@page} in case this is
4462 % the first page of the \mc{} (on later pages this box will be void).
4463 % \begin{macrocode}
4464 \unvcopy\partial@page
4465 \process@cols \mult@firstbox { \unvcopy\count@ }
4466 \unvcopy\mult@rightbox
4467 }
4468 }
4469 % \end{macrocode}
4470 % \end{macro}
4471 %
4472
4473 % \begin{macro}{\@@_prepare_mark_reinserts:}
4474 %
4475 % When finishing a \mc{} environment, we have to return marks (in
4476 % then boxed columns) to the current page so that they are
4477 % available when that page is produced.
4478 % This is what this helper does.
4479 % \begin{macrocode}
4480 \cs_new_protected:Npn \@@_prepare_mark_reinserts: {
4481 \@@_debug_marks:n
4482 { \typeout{Marks:~ prepare~ for~ reinserting~ marks~ (multicol)} }
4483 % \end{macrocode}
4484 % We are only interested in the top, first, and last marks of each
4485 % class regardless in which column they appeared, so we can copy
4486 % all column boxes together and process them in one go. The result
4487 % is returned in \cs{\detokenize{l_@@_first_marks_tl}} and
4488 % \cs{\detokenize{l_@@_last_marks_tl}} in form of \cs{\detokenize{mark_insert:nn}} statements
4489 % so we can later execute such token lists directly (and if there
4490 % were no marks they will be empty).
4491 % \begin{macrocode}
4492 \mark_get_marks_for_reinsertion:nNN
4493 {
4494 \process@cols \mult@firstbox { \unvcopy\count@ }
4495 \unvcopy\mult@rightbox
4496 }
4497 \l_@@_first_marks_tl
4498 \l_@@_last_marks_tl
4499 }
4500 % \end{macrocode}
4501 % And here are the token lists used above.
4502 % \begin{macrocode}
4503 \tl_new:N \l_@@_first_marks_tl
4504 \tl_new:N \l_@@_last_marks_tl
4505 % \end{macrocode}
4506 % \end{macro}
4507 %
4508 %
4509 % \begin{macro}{\mc@reinsert@marks}
4510 % So reinserting it just means executing the token lists (with some
4511 % surrounding debugging statements).
4512 % \begin{macrocode}
4513 \cs_new_protected:Npn \mc@reinsert@marks{
4514 \@@_debug_marks:n
4515 { \typeout{Marks:~ --~ reinsert~ marks~ (multicol)} }
4516 \l_@@_first_marks_tl \l_@@_last_marks_tl
4517 \@@_debug_marks:n
4518 { \typeout{Marks:~ --~ finished~ reinserting~ marks~ (multicol)} }
4519 }
4520 % \end{macrocode}
4521 % \end{macro}
4522 %
4523 %
4524 % \subsubsection{Interfaces used in the \mc{} code and its output routines}
4525 %
4526 %
4527 % \begin{macro}{\mc@prepare@mark@regions}
4528 % When a \mc{} environment starts we need to clear the column
4529 % region as it may contain quite old data. Otherwise, that data
4530 % would be used to generate the top marks and that would be
4531 % obviously wrong for the first column.
4532 % \begin{macrocode}
4533 \cs_new_protected:Npn \mc@prepare@mark@regions {
4534 % \end{macrocode}
4535 % However, before we do this we need to save away the current
4536 % column data in case this is a boxed multicol, because when that
4537 % finishes we need to restore the previous state --- this is not
4538 % necessary for a normal \mc{} environment, because there the
4539 % \texttt{column} region is overwritten in the standard output
4540 % routine by copying the \texttt{page} region.
4541 % \begin{macrocode}
4542 \legacy_if:nT { @boxedmulticols }
4543 { \mark_copy_structure:nn{saved-column}{column} }
4544 \@@_debug_marks:n
4545 { \typeout{Marks:~ empty~ mcol~ regions~ (multicol)} }
4546 \mark_clear_structure:n {column}
4547 }
4548 % \end{macrocode}
4549 % \end{macro}
4550 %
4551 %
4552 %
4553 % \begin{macro}{\mc@handle@marks@and@reinserts}
4554 %
4555 % If a \mc{} ends the columns are balanced and then returned to the
4556 % galley. in that situation we do have to prepare the
4557 % \texttt{mcol-...} regions and also do this reinsertion.
4558 % \begin{macrocode}
4559 \cs_new_protected:Npn \mc@handle@marks@and@reinserts #1 {
4560 \@@_update_mcol_structures:
4561 % \end{macrocode}
4562 % Show the current region status when debugging:
4563 % \begin{macrocode}
4564 \@@_debug_marks:n { \__mark_status:nn {#1} {\the\col@number} }
4565 % \end{macrocode}
4566 % Then prepare for reinserting the marks:
4567 % \begin{macrocode}
4568 \@@_prepare_mark_reinserts:
4569 % \end{macrocode}
4570 % Finally, we restore the \texttt{column} region in case this was a
4571 % boxed \mc{} environment.
4572 % \begin{macrocode}
4573 \legacy_if:nT { @boxedmulticols }
4574 { \mark_copy_structure:nn {column}{saved-column} }
4575 }
4576
4577 % \end{macrocode}
4578 % \end{macro}
4579 %
4580 %
4581 %
4582 % \begin{macro}{\mc@handle@col@andpage@marks}
4583 % If a page is generated while we are processing a \mc{} then we
4584 % have to update the \texttt{mcol-...} regions but also the
4585 % \texttt{page} region.
4586 % \begin{macrocode}
4587 \cs_new_protected:Npn \mc@handle@col@andpage@marks #1 {
4588 \@@_update_mcol_structures:
4589 \@@_update_page_structures:
4590 % \end{macrocode}
4591 % Once done we display the current status of the marks.
4592 % \begin{macrocode}
4593 \@@_debug_marks:n { \__mark_status:nn {#1} {\the\col@number} }
4594 }
4595 % \end{macrocode}
4596 % \end{macro}
4597 %
4598 %
4599 %
4600 %
4601 % \begin{macrocode}
4602 %<@@=>
4603 % \end{macrocode}
4604 %
4605 %
4606 %
4607 %
4608 %
4609 %
4610 %
4611 % \subsection{Tagging support}
4612 %
4613 % Here we collect adjustments necessary for tagging support, e.g.,
4614 % before the 2024-11-01 release it was necessary to allocate two
4615 % float sockets. By now they are part of the kernel and their plugs
4616 % are currently defined by \pkg{tagpdf}.
4617 % \begin{macrocode}
4618 %\IfSocketExistsF {tagsupport/float/end}
4619 % {
4620 % \NewSocket{tagsupport/float/end}{0}
4621 % \NewSocket{tagsupport/float/hmode/end}{0}
4622 % }
4623 % \end{macrocode}
4624 %
4625 % This one is only relevant for \pkg{multicol} so declared here.
4626 % \begin{macrocode}
4627 \NewSocket{tagsupport/page@sofar}{0}
4628 % \end{macrocode}
4629 %
4630 % The plug definition for now just uses the definition from
4631 % \pkg{tagpdf}. There has to be a decision where such plugs should be
4632 % implemented: in the package (like now) then they functions used
4633 % from tagpdf should become public, in \pkg{tagpdf}, or in
4634 % \texttt{lttagging} in the kernel.
4635 % \changes{v2.0a}{2024/11/12}{Added tagging socket and use public tagpdf function}
4636 % \begin{macrocode}
4637 \NewSocketPlug {tagsupport/page@sofar}{default}
4638 {
4639 % \__tag_check_typeout_v:n {====>~In~\string\page@sofar} % some similar debug message if wanted.
4640 \process@cols\mult@firstbox
4641 { \tag_mc_add_missing_to_stream:Nn \count@ {multicol} }
4642 \tag_mc_add_missing_to_stream:Nn \mult@rightbox {multicol}
4643 }
4644 % \end{macrocode}
4645 % In the \LuaTeX{} engine there is no need to do anything special
4646 % in this socket.
4647 % \begin{macrocode}
4648 \sys_if_engine_luatex:TF
4649 { \AssignSocketPlug{tagsupport/page@sofar}{noop} }
4650 { \AssignSocketPlug{tagsupport/page@sofar}{default} }
4651 % \end{macrocode}
4652 %
4653 %
4654 %
4655 % \begin{macrocode}
4656 \ExplSyntaxOff
4657 % \end{macrocode}
4658 %
4659 %
4660 %
4661 % \Finale
4662 %
4663 \endinput