| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \begmulti {Balanced columns <2024-10-31>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ----------------------------- |
| 6 |
\`\_betweencolumns` or \`\_leftofcolumns` or \`\_rightofcolumns` include |
| 7 |
a material printed between columns or left of all columns or right of all |
| 8 |
columns respectively. The \^`\_betweencolumns` must include a |
| 9 |
stretchability or a material with exactly \^`\colsep` width. You can |
| 10 |
redefine these macros. For example the rule between columns can be reached |
| 11 |
by `\_def\_betweencolumns{\hss\vrule\hss}`.\nl |
| 12 |
\`\_multiskip` puts its material at the start and at the end of |
| 13 |
\^`\begmulti`...\^`\endmulti`. |
| 14 |
\_cod ----------------------------- |
| 15 |
|
| 16 |
\_def\_betweencolumns{\_hss} \_def\_leftofcolumns{} \_def\_rightofcolumns{} |
| 17 |
\_def\_multiskip{\_medskip} % space above and below \begmulti...\endmulti |
| 18 |
|
| 19 |
\_doc ----------------------------- |
| 20 |
The code used here is documented in detail in the \"\TeX/book naruby", pages 244--246, |
| 21 |
free available, \url{http://petr.olsak.net/tbn.html}, but in Czech. |
| 22 |
Roughly speaking, macros complete all material between |
| 23 |
\`\begmulti``<num-columns>` and \`\endmulti` |
| 24 |
into one `\vbox 6`. Then the macro measures the amount of free space at the current |
| 25 |
page using `\pagegoal` and `\pagtotal` and does `\vsplit` of `\vbox 6` to |
| 26 |
columns with a height of such free space. This is done only if we have |
| 27 |
enough amount of material in `\vbox 6` to fill the full page by columns. |
| 28 |
This is repeated in a loop until we have less amount of material in `\vbox 6`. |
| 29 |
Then we run \`\_balancecolumns` which balances the last part of the columns. |
| 30 |
Each part of printed material is distributed to the main vertical list as |
| 31 |
`\hbox{<columns>}` and we need not do any change in the output routine. |
| 32 |
|
| 33 |
If you have paragraphs in \^`\begmulti`... \^`\endmulti` environment then |
| 34 |
you may say `\raggedright` inside this environment and you can re-assign |
| 35 |
`\widowpenalty` and `\clubppenalty` (they are set to 10000 in \OpTeX/). |
| 36 |
\_cod ----------------------------- |
| 37 |
|
| 38 |
\_def\_begmulti #1 {\_par\_bgroup\_wipeepar |
| 39 |
\_ifnum\_lastpenalty>10000 \_vskip4.5\_baselineskip\_penalty9999 \_vskip-4.5\_baselineskip \_fi |
| 40 |
\_multiskip \_def\_Ncols{#1} |
| 41 |
\_setbox6=\_vbox\_bgroup\_bgroup \_let\_setxhsize=\_relax \_penalty-99 |
| 42 |
%% \hsize := column width = (\hsize+\colsep) / n - \colsep |
| 43 |
\_setbox0=\_hbox{\_leftofcolumns\_rightofcolumns}% |
| 44 |
\_advance\_hsize by-\_wd0 \_advance\_hsize by\_colsep |
| 45 |
\_divide\_hsize by\_Ncols \_advance\_hsize by-\_colsep |
| 46 |
} |
| 47 |
\_def\_endmulti{\_vskip-\_prevdepth\_vfil |
| 48 |
\_ea\_egroup\_ea\_egroup\_ea\_baselineskip\_the\_baselineskip\_relax |
| 49 |
\_dimen0=.8\_maxdimen \_tmpnum=\_dimen0 \_divide\_tmpnum by\_baselineskip |
| 50 |
\_splittopskip=\_baselineskip |
| 51 |
\_setbox1=\_vsplit6 to0pt % initialize first \splittopskip in \box6 |
| 52 |
%% \dimen1 := the free space on the page |
| 53 |
\_penalty0 % initialize \_pageoal |
| 54 |
\_ifdim\_pagegoal=\_maxdimen \_setcolsize\_vsize |
| 55 |
\_else \_setcolsize{\_dimexpr\_pagegoal-\_pagetotal}\_fi |
| 56 |
\_ifdim \_dimen1<2\_baselineskip \_vfil\_break \_setcolsize\_vsize \_fi |
| 57 |
\_setsplitsize % sets \_dimen0 = \_ht6 |
| 58 |
\_divide\_dimen0 by\_Ncols \_relax |
| 59 |
%% split the material to more pages? |
| 60 |
\_ifdim \_dimen0>\_dimen1 \_splitpart |
| 61 |
\_else \_balancecolumns \_fi % only balancing |
| 62 |
\_multiskip \_egroup |
| 63 |
} |
| 64 |
|
| 65 |
\_doc ----------------------------- |
| 66 |
Splitting columns... |
| 67 |
\_cod ----------------------------- |
| 68 |
|
| 69 |
\_def\_makecolumns{\_bgroup % full page, destination height: \dimen1 |
| 70 |
\_vbadness=20000 \_splitmaxdepth=\_maxdepth \_dimen6=\_wd6 |
| 71 |
\_createcolumns |
| 72 |
\_printcolumns |
| 73 |
\_egroup |
| 74 |
} |
| 75 |
\_def\_splitpart{% |
| 76 |
\_makecolumns % full page |
| 77 |
\_vskip 0pt plus 1fil minus\_baselineskip \_break |
| 78 |
\_setsplitsize % sets \_dimen0 = \_ht6 |
| 79 |
\_divide\_dimen0 by\_Ncols \_relax |
| 80 |
\_ifx\_balancecolumns\_flushcolumns \_advance\_dimen0 by-.5\_vsize \_fi |
| 81 |
\_setcolsize\_vsize \_dimen2=\_dimen1 |
| 82 |
\_advance\_dimen2 by-\_baselineskip |
| 83 |
%% split the material to more pages? |
| 84 |
\_ifvoid6 \_else |
| 85 |
\_ifdim \_dimen0>\_dimen2 \_ea\_ea\_ea \_splitpart |
| 86 |
\_else \_balancecolumns % last balancing |
| 87 |
\_fi \_fi |
| 88 |
} |
| 89 |
|
| 90 |
\_doc ----------------------------- |
| 91 |
Final balancing of the columns. |
| 92 |
\_cod ----------------------------- |
| 93 |
|
| 94 |
\_def\_balancecolumns{\_bgroup \_setbox7=\_copy6 % destination height: \dimen0 |
| 95 |
\_ifdim\_dimen0>\_baselineskip \_else \_dimen0=\_baselineskip \_fi |
| 96 |
\_vbadness=20000 \_dimen6=\_wd6 \_dimen1=\_dimen0 |
| 97 |
\_def\_trybalance{\_createcolumns |
| 98 |
\_ifvoid6 \_else |
| 99 |
\_advance \_dimen1 by.2\_baselineskip |
| 100 |
\_setbox6=\_copy7 |
| 101 |
\_ea \_trybalance \_fi}\_trybalance |
| 102 |
\_printcolumns |
| 103 |
\_egroup |
| 104 |
} |
| 105 |
|
| 106 |
\_doc ----------------------------- |
| 107 |
\`\_setcolsize``<dimen>` sets initial value `\dimen1=<size>` which is |
| 108 |
used as height of columns at given page. |
| 109 |
The correction `\splittopskip`$-$`\topskip` is done if the columns start |
| 110 |
at the top of the page.\nl |
| 111 |
\`\_createcolumns` prepares columns with given height `\dimen1` side |
| 112 |
by side to the `\box1`.\nl |
| 113 |
\`\_printcolumns` prints the columns prepared in `\box1`. |
| 114 |
The first `\hbox{}` moves typesetting point to the next baseline. |
| 115 |
Next negative skip ensures that the first line from |
| 116 |
split columns is at this position. |
| 117 |
\_cod ----------------------------- |
| 118 |
|
| 119 |
\_def\_setcolsize #1{\_dimen1=#1\_relax |
| 120 |
\_ifdim\_dimen1=\_vsize |
| 121 |
\_advance \_dimen1 by \_splittopskip \_advance \_dimen1 by-\_topskip \_fi |
| 122 |
} |
| 123 |
\_def\_createcolumns{% |
| 124 |
\_setbox1=\_hbox{\_leftofcolumns}\_tmpnum=0 |
| 125 |
\_loop \_ifnum\_Ncols>\_tmpnum |
| 126 |
\_advance\_tmpnum by1 |
| 127 |
\_setbox1=\_hbox{\_unhbox1 |
| 128 |
\_ifvoid6 \_hbox to\_dimen6{\_hss}\_else \_vsplit6 to\_dimen1 \_fi |
| 129 |
\_ifnum\_Ncols=\_tmpnum \_rightofcolumns \_else \_betweencolumns \_fi}% |
| 130 |
\_repeat |
| 131 |
} |
| 132 |
\_def\_printcolumns{% |
| 133 |
\_hbox{}\_nobreak\_vskip-\_splittopskip \_nointerlineskip |
| 134 |
\_hbox to\_hsize{\_unhbox1}% |
| 135 |
} |
| 136 |
\_public \begmulti \endmulti ; |
| 137 |
|
| 138 |
\_doc ----------------------------- |
| 139 |
The accumulated `\box6` (before its splitting to pages and columns) can |
| 140 |
be very large and we cannot measure it with classical \TeX/ methods because |
| 141 |
we can get \"dimension too lagre" error or arithmetic overflow. |
| 142 |
The \TeX/ limit for dimensions is about 5.75\,m. For example, |
| 143 |
the `\box6` of the index in this document has about 6.5\,m height. |
| 144 |
|
| 145 |
The \`\_rawht``<box-num>` macro solves this problem. |
| 146 |
It expands to the height plus depth of |
| 147 |
given `\vbox` as an integer in points (i.e. the the fraction part of points is |
| 148 |
stripped and unit isn't appended). The limit $2^{31}$ of such an integer |
| 149 |
allows us to measure boxes with many kilometers height. |
| 150 |
|
| 151 |
The trick used in the \`\_setsplitsize` macro sets `\dimen0` to the maximum of |
| 152 |
`.8\maxdimen` and actual `\ht6`, because 13100 is `.8\maxdimen` in points. |
| 153 |
This is sufficient because if the `\dimen0` is big then we get $n$ |
| 154 |
columns for full page. This is not the last page where exact calculations |
| 155 |
for balancing is needed. |
| 156 |
\_cod ----------------------------- |
| 157 |
|
| 158 |
\_def\_setsplitsize{% |
| 159 |
\_ifnum \_rawht6 > 13100 \_dimen0 = .8\_maxdimen |
| 160 |
\_else \_dimen0=\_ht6 \_def\_rawht6{0}\_fi |
| 161 |
} |
| 162 |
\_def\_rawht{\_directlua{optex.raw_ht()}} |
| 163 |
|
| 164 |
\_endcode % ------------------------------------- |
| 165 |
|
| 166 |
2024-10-31 \splitmaxdepth set, bug fixed |
| 167 |
2024-09-06 \_rawht implemented, more robust measurement of big boxes |
| 168 |
2022-11-26 \at least three lines in the beginning of \begmulti at the page |
| 169 |
2022-05-05 `\_betweencolumns` etc. introduced. |
| 170 |
2021-05-20 Colors inside \begmulti...\endmuti, bug fixed |
| 171 |
2020-03-26 Introduced |