| 1 |
% lua-widow-control |
| 2 |
% https://github.com/gucci-on-fleek/lua-widow-control |
| 3 |
% SPDX-License-Identifier: MPL-2.0+ OR CC-BY-SA-4.0+ |
| 4 |
% SPDX-FileCopyrightText: 2022 Max Chernoff |
| 5 |
|
| 6 |
\documentclass[final]{ltugboat} |
| 7 |
|
| 8 |
% This is the LaTeX source for the following article: |
| 9 |
% @article{tb135chernoff-widows, |
| 10 |
% title={Updates to \textsf{lua-widow-control}}, |
| 11 |
% author={Chernoff, Max}, |
| 12 |
% journal={TUGboat}, |
| 13 |
% volume={43}, |
| 14 |
% number={3}, |
| 15 |
% pages={340--342}, |
| 16 |
% year={2022}, |
| 17 |
% month=nov, |
| 18 |
% DOI={10.47397/tb/43-3/tb135chernoff-lwc}, |
| 19 |
% } |
| 20 |
% Please refer to the PDF on tug.org for the authoritative version. |
| 21 |
|
| 22 |
% Set the publication info |
| 23 |
\vol 43, 3. |
| 24 |
\issyear 2022. |
| 25 |
\issueseqno=135 |
| 26 |
\setcounter{page}{340} |
| 27 |
\PrelimDraftfalse |
| 28 |
|
| 29 |
\hbadness=10000 |
| 30 |
\def\LuaMeta{Lua\-Meta\-} |
| 31 |
\usepackage{graphicx} |
| 32 |
\usepackage{tabularx} |
| 33 |
\usepackage{microtype} |
| 34 |
\usepackage[hidelinks,pdfa]{hyperref} |
| 35 |
|
| 36 |
\title[Updates to \textsf{lua-widow-control}] |
| 37 |
{Updates to ``Automatically removing widows and orphans |
| 38 |
with \textsf{lua-widow-control}'', \TUB\ \textbf{43}:1} |
| 39 |
\author{Max Chernoff} |
| 40 |
\EDITORnoaddress |
| 41 |
\personalURL{https://ctan.org/pkg/lua-widow-control} |
| 42 |
|
| 43 |
% Sneaky way to always force expansion |
| 44 |
\directlua{ |
| 45 |
forcelwc = 1 |
| 46 |
luatexbase.add_to_callback("pre_output_filter", function() |
| 47 |
if forcelwc >= 1 and tex.outputpenalty >= -9000 then |
| 48 |
tex.outputpenalty = 1 |
| 49 |
end |
| 50 |
return true |
| 51 |
end, "interlinepenalty") |
| 52 |
} |
| 53 |
|
| 54 |
\def\forcelwc#1{\directlua{forcelwc = #1}} |
| 55 |
|
| 56 |
\usepackage[draft, debug, draftoffset=1in - 10pt]{lua-widow-control} |
| 57 |
|
| 58 |
\makeatletter |
| 59 |
\usepackage{color} |
| 60 |
\definecolor{expanded}{rgb}{0.00, 0.70, 0.25} |
| 61 |
\definecolor{failure} {rgb}{0.90, 0.00, 0.25} |
| 62 |
\definecolor{moved} {rgb}{0.25, 0.25, 1.00} |
| 63 |
|
| 64 |
\ExplSyntaxOn |
| 65 |
\seq_new:N \l_sec_seq |
| 66 |
\tl_new:N \l_sec_tl |
| 67 |
\def\modifiedsection[#1]{ |
| 68 |
\regex_split:nnN { \. } { #1 } \l_sec_seq |
| 69 |
\setcounter{section}{0\seq_item:Nn \l_sec_seq { 1 }} |
| 70 |
\setcounter{subsection}{0\seq_item:Nn \l_sec_seq { 2 }} |
| 71 |
\setcounter{subsubsection}{0\seq_item:Nn \l_sec_seq { 3 }} |
| 72 |
|
| 73 |
\int_case:nn { \seq_count:N \l_sec_seq } { |
| 74 |
1 { \tl_set:Nn \l_sec_tl { section } } |
| 75 |
2 { \tl_set:Nn \l_sec_tl { subsection } } |
| 76 |
3 { \tl_set:Nn \l_sec_tl { subsubsection } } |
| 77 |
} |
| 78 |
|
| 79 |
\addtocounter{\l_sec_tl}{-1} |
| 80 |
|
| 81 |
\TB@startsection{ |
| 82 |
{\l_sec_tl} |
| 83 |
{\seq_count:N \l_sec_seq}% |
| 84 |
{\z@}% |
| 85 |
{-8\p@ \@plus-2\p@ \@minus-2\p@}% |
| 86 |
{4\p@}{\normalsize\bf\raggedright\hyphenpenalty\@M\tubsechook}% |
| 87 |
} |
| 88 |
} |
| 89 |
\ExplSyntaxOff |
| 90 |
\makeatother |
| 91 |
|
| 92 |
% Let the macro names in section headings be in boldface |
| 93 |
\usepackage{lmodern} |
| 94 |
\DeclareRobustCommand{\cs}[1]{\texttt{\textbackslash#1}} |
| 95 |
\def\dots{\ensuremath{\mathellipsis}} |
| 96 |
|
| 97 |
\begin{document} |
| 98 |
|
| 99 |
\maketitle |
| 100 |
|
| 101 |
A request from \textit{Zpravodaj}, the journal of the Czech\slash Slovak |
| 102 |
\TeX\ group, to republish the subject article |
| 103 |
led to these updates. The section numbers here correspond to those in |
| 104 |
the original article. |
| 105 |
|
| 106 |
\modifiedsection[3.3]{Clubs} |
| 107 |
|
| 108 |
In the original article, I discussed the origin of the typographical terms |
| 109 |
``widow'', ``orphan'', and ``club''. The first two terms are fairly well-known, |
| 110 |
but I had this to say regarding the third: |
| 111 |
|
| 112 |
\begin{quote} |
| 113 |
\textsl{The \TeX{}book} never refers to ``orphans'' as such; rather, it |
| 114 |
refers to them as ``clubs''. This term is remarkably rare: I could only find |
| 115 |
a \emph{single} source published before \textsl{The \TeX{}book} \Dash a |
| 116 |
compilation article about the definition of ``widow'' \Dash that mentions |
| 117 |
a ``club line'' [\dots] |
| 118 |
|
| 119 |
\hspace*{-2pt}I spent a few hours searching through Google Books and my university library |
| 120 |
catalogue, but I could not find a single additional source. If anyone has |
| 121 |
any more information on the definition of a ``club line'' or why Knuth chose |
| 122 |
to use this archaic Scottish term in \TeX{}, please let me know! |
| 123 |
\end{quote} |
| 124 |
|
| 125 |
\noindent |
| 126 |
Conveniently, Don Knuth\Dash the creator of \TeX{}\Dash read my plea and sent |
| 127 |
me this reply: |
| 128 |
|
| 129 |
\begin{quote}\emergencystretch=3em |
| 130 |
I cannot remember where I found the term ``club line''. Evidently |
| 131 |
the books that I scoured in 1977 and 1978 had taught me only that an |
| 132 |
isolated line, caused by breaking between pages in the midst of a |
| 133 |
paragraph, was called a ``widow''; hence \TeX78 had only |
| 134 |
``\cs{chpar4}'' to |
| 135 |
change the ``\texttt{widowpenalty}''. Sometime between then and \TeX82 |
| 136 |
I must |
| 137 |
have come across what appeared to be an authoritative source that |
| 138 |
distinguished between widows at the beginning of a paragraph and |
| 139 |
orphans or club lines at the end. I may have felt that the term |
| 140 |
``orphan'' was somewhat pejorative, who knows? |
| 141 |
\end{quote} |
| 142 |
|
| 143 |
\noindent |
| 144 |
So this (somewhat) resolves the question of where the term ``club'' came |
| 145 |
from. |
| 146 |
|
| 147 |
\modifiedsection[9]{Options} |
| 148 |
The overview to the ``options'' section stated that: |
| 149 |
|
| 150 |
\begin{quote} |
| 151 |
Plain \TeX/Op\TeX\quad Some options are set by modifying |
| 152 |
a register, while others must be set manually |
| 153 |
using \cs{directlua}. |
| 154 |
\end{quote} |
| 155 |
|
| 156 |
\noindent However, this is no longer true. Now, commands are provided for all |
| 157 |
options in all formats, so you no longer need to use ugly \cs{directlua} |
| 158 |
commands in your documents. The old commands still work, although they will |
| 159 |
likely be removed at some point in the future. |
| 160 |
|
| 161 |
\modifiedsection[9.5]{Penalties} |
| 162 |
\cs{brokenpenalty} now also exists as a \LaTeX{} and \ConTeXt{} key. |
| 163 |
\textsf{lua-widow-control} will pick up on the values of \cs{widowpenalty}, |
| 164 |
\cs{clubpenalty}, and \cs{brokenpenalty} regardless of how you set them, so the |
| 165 |
use of these dedicated keys is entirely optional. |
| 166 |
|
| 167 |
\modifiedsection[9.6]{\cs{nobreak} behaviour} |
| 168 |
The Plain/Op\TeX\ command is now: |
| 169 |
|
| 170 |
\smallskip |
| 171 |
\verb+\lwcnobreak{+\meta{value}\verb+}+ |
| 172 |
|
| 173 |
\modifiedsection[9.8]{Draft mode} |
| 174 |
Since v2.2.0, \textsf{lua-widow-control} has a ``draft mode'' which shows how |
| 175 |
\textsf{lua-widow-control} processes pages. |
| 176 |
|
| 177 |
\smallskip |
| 178 |
\noindent\begin{tabularx}{\linewidth}{@{}Xl@{}} |
| 179 |
Plain \TeX{}\slash\OpTeX{} & |
| 180 |
\cs{lwcdraft 1} \\ |
| 181 |
\LaTeX{} & |
| 182 |
\cs{lwcsetup\{draft\}} \\ |
| 183 |
\ConTeXt{} & |
| 184 |
\cs{setuplwc[draft=start]} \\ |
| 185 |
\end{tabularx} |
| 186 |
\smallskip |
| 187 |
|
| 188 |
Draft mode has been used for |
| 189 |
typesetting this article. It has two main features: |
| 190 |
|
| 191 |
First, it colours lines in the document according to their status. Any |
| 192 |
\textcolor{failure}{remaining widows and orphans will be coloured red}, any |
| 193 |
\textcolor{expanded}{expanded paragraphs will be coloured green}, and any |
| 194 |
\textcolor{moved}{lines moved to the next page will be coloured blue}. |
| 195 |
|
| 196 |
Second, this draft mode shows the paragraph costs at the end each |
| 197 |
paragraph, in the margin. |
| 198 |
|
| 199 |
This draft mode leads to a neat trick: if you don't quite trust |
| 200 |
\textsf{lua-widow-control}, or you're writing a document whose final version |
| 201 |
will need to be compilable by both pdf\LaTeX{} and Lua\LaTeX, you can load the |
| 202 |
package with: |
| 203 |
|
| 204 |
\begin{verbatim} |
| 205 |
\usepackage[draft, disable] |
| 206 |
{lua-widow-control} |
| 207 |
\end{verbatim} |
| 208 |
|
| 209 |
This way, all the widows and orphans will be coloured red and listed in your log |
| 210 |
file. When you go through the document to try and manually remove the widows and |
| 211 |
orphans \Dash whether through the \cs{looseness} trick or by rewriting certain |
| 212 |
lines \Dash you can easily find the best paragraphs to modify by looking at the |
| 213 |
paragraph costs in the margins. If you're less cautious, you can compile your |
| 214 |
document with \textsf{lua-widow-control} enabled as normal and inspect all the |
| 215 |
green paragraphs to see if they look acceptable to you. |
| 216 |
|
| 217 |
You can also toggle the paragraph colouring and cost displays individually: |
| 218 |
|
| 219 |
\smallskip |
| 220 |
\noindent\begin{tabularx}{\linewidth}{@{}Xl@{}} |
| 221 |
Plain \TeX{}\slash & |
| 222 |
\cs{lwcshowcosts 1} \\ |
| 223 |
\OpTeX{} & \cs{lwcshowcolours 0} \\[4pt] |
| 224 |
\LaTeX{} & |
| 225 |
\cs{lwcsetup\{showcosts=true\}} \\ |
| 226 |
& \cs{lwcsetup\{showcolours=false\}} \\[4pt] |
| 227 |
\ConTeXt{} & |
| 228 |
\cs{setuplwc[showcosts=start]} \\ |
| 229 |
& \cs{setuplwc[showcolours=stop]} \\ |
| 230 |
\end{tabularx} |
| 231 |
\smallskip |
| 232 |
|
| 233 |
To demonstrate the new draft mode, I have tricked \textsf{lua-widow-control} |
| 234 |
into thinking that every column in this article ends in a widow, even when they |
| 235 |
actually don't. This means that \textsf{lua-widow-control} is attempting to |
| 236 |
expand paragraphs on every column. This gives terrible page breaks and often |
| 237 |
creates new widows and orphans, but it's a good demonstration of how |
| 238 |
\textsf{lua-widow-control} works. |
| 239 |
|
| 240 |
\modifiedsection[10]{Presets} |
| 241 |
|
| 242 |
The original article stated that ``presets are \LaTeX{}-only''. However, |
| 243 |
\textsf{lua-widow-control} now supports presets with both \LaTeX{} and |
| 244 |
\ConTeXt{} using the following commands: |
| 245 |
|
| 246 |
\smallskip |
| 247 |
\begin{tabular}{@{}rl@{}} |
| 248 |
\LaTeX{} & |
| 249 |
\cs{lwcsetup\{\meta{preset}\}} \\ |
| 250 |
\ConTeXt{} & |
| 251 |
\cs{setuplwc[\meta{preset}]} \\ |
| 252 |
\end{tabular} |
| 253 |
|
| 254 |
\modifiedsection[11]{Compatibility} |
| 255 |
This quote: |
| 256 |
|
| 257 |
\begin{quote} |
| 258 |
It doesn't modify [\dots], inserts/floats, |
| 259 |
\end{quote} |
| 260 |
|
| 261 |
\noindent |
| 262 |
isn't strictly true since v2.1.2 since \textsf{lua-widow-control} now handles |
| 263 |
moving footnotes. |
| 264 |
|
| 265 |
\hspace*{-.1em}This statement is also no longer true: |
| 266 |
|
| 267 |
\begin{quote} |
| 268 |
there are a few issues with \ConTeXt{} [\dots] \textsf{lua-widow-control} is |
| 269 |
inevitably more reliable with Plain \TeX{} and \LaTeX{} than with \ConTeXt{}. |
| 270 |
\end{quote} |
| 271 |
|
| 272 |
\noindent All issues with \ConTeXt{} \Dash including |
| 273 |
grid snapping \Dash have now been resolved. \textsf{lua-widow-control} should be |
| 274 |
equally reliable with all formats. |
| 275 |
|
| 276 |
\modifiedsection[11.1]{Formats} |
| 277 |
|
| 278 |
In addition to the previously-mentioned formats\slash engines, |
| 279 |
\textsf{lua-widow-control} now has preliminary support for \LuaMeta\LaTeX{} and |
| 280 |
\LuaMeta{}Plain.\tbsurlfootnote{github.com/zauguin/luametalatex} |
| 281 |
Aside from a few minor bugs, the \LuaMeta\LaTeX{} and \LuaMeta{}Plain versions work identically to their respective |
| 282 |
Lua\LaTeX{} versions. With this addition, \textsf{lua-widow-control} now supports seven different format\slash engine combinations. |
| 283 |
|
| 284 |
\modifiedsection[11.3]{Performance} |
| 285 |
|
| 286 |
Earlier versions of \textsf{lua-widow-control} had some memory leaks. These |
| 287 |
weren't noticeable for small documents, although it could cause slowdowns for |
| 288 |
documents larger than a few hundred pages. However, I have implemented a new |
| 289 |
testing suite to ensure that there are no memory leaks, so |
| 290 |
\textsf{lua-widow-control} can now easily compile documents $>10\,000$ pages |
| 291 |
long. |
| 292 |
|
| 293 |
\modifiedsection[13.4]{Footnotes} |
| 294 |
|
| 295 |
Earlier versions of \textsf{lua-widow-control} completely ignored inserts. This |
| 296 |
meant that if a moved line had associated footnotes, \textsf{lua-widow-control} |
| 297 |
would move the ``footnote mark'' but not the associated ``footnote text''. |
| 298 |
\textsf{lua-widow-control} now handles footnotes correctly through the |
| 299 |
mechanism detailed in the next section. |
| 300 |
|
| 301 |
\subsubsection{Inserts} |
| 302 |
|
| 303 |
Before we go into the details of how \textsf{lua-widow-control} handles |
| 304 |
footnotes, we need to look at what footnotes actually are to \TeX{}. |
| 305 |
Every \cs{footnote} command ultimately expands to something like |
| 306 |
\cs{insert\meta{class}}\allowbreak\verb|{|\meta{content}\verb|}|, |
| 307 |
\noindent where \meta{class} is an insertion class number, defined as |
| 308 |
\cs{footins} in this case (in Plain \TeX\ and \LaTeX). Inserts can be found in |
| 309 |
horizontal mode (footnotes) or in vertical mode (\cs{topins} in Plain \TeX{} and |
| 310 |
floats in \LaTeX{}), but they cannot be inside boxes. Each of these insert types is |
| 311 |
assigned a different class number, but the mechanism is otherwise identical. |
| 312 |
\textsf{lua-widow-control} treats all inserts identically, although it safely |
| 313 |
ignores vertical mode inserts since they are only ever found between paragraphs. |
| 314 |
|
| 315 |
But what does \cs{insert} do exactly? When \TeX{} sees an \cs{insert} primitive |
| 316 |
in horizontal mode (when typesetting a paragraph), it does two things: first, it |
| 317 |
processes the insert's content and saves it invisibly just below the current |
| 318 |
line. Second, it effectively adds the insert content's height to the height of |
| 319 |
the material on the current page. Also, for the first insert on a page, the glue |
| 320 |
in \cs{skip}\meta{class} is added to the current height. All this is done to |
| 321 |
ensure that there is sufficient room for the insert on the page whenever the |
| 322 |
line is output onto the page. |
| 323 |
|
| 324 |
If there is absolutely no way to make the insert |
| 325 |
fit on the page \Dash say, if you placed an entire paragraph in a footnote on |
| 326 |
the last line of a page \Dash then \TeX{} will begrudgingly ``split'' the |
| 327 |
insert, placing the first part on the current page and ``holding over'' the |
| 328 |
second part until the next page. |
| 329 |
|
| 330 |
There are some other \TeX{}nicalities involving \cs{count}\meta{class} and |
| 331 |
\cs{dimen}\meta{class}, but they mostly don't affect \textsf{lua-widow-control}. |
| 332 |
See Chapter~15 in \TB\ or some other reference for all the details. |
| 333 |
|
| 334 |
After \TeX{} has chosen the breakpoints for a paragraph, it adds the |
| 335 |
chosen lines one by one to the current page. Whenever the accumulated |
| 336 |
page height is ``close enough'' to the target page height (normally |
| 337 |
\cs{vsize}) % but really \cs{pagegoal} |
| 338 |
the \cs{output} token list (often called |
| 339 |
the ``output routine'') is expanded. |
| 340 |
|
| 341 |
But before \cs{output} is called, \TeX{} goes through the page contents and |
| 342 |
moves the contents of any saved inserts into \cs{vbox}es corresponding to the |
| 343 |
inserts' classes, namely \cs{box}\meta{class}, so \cs{output} can work |
| 344 |
with them. |
| 345 |
|
| 346 |
And that's pretty much it on the engine side. Actually placing the inserts on |
| 347 |
the page is reserved for the output routine, which is defined by the format. |
| 348 |
This too is a complicated process, although thankfully not one that |
| 349 |
\textsf{lua-widow-control} needs to worry about. |
| 350 |
|
| 351 |
\subsubsection{LuaMeta\TeX{}} |
| 352 |
|
| 353 |
The \LuaMeta\TeX{} engine treats inserts slightly differently than traditional \TeX{} |
| 354 |
engines. The first major difference is that insertions have dedicated registers; so |
| 355 |
instead of \cs{box}\meta{class}, \LuaMeta\TeX{} has \cs{insertbox}\meta{class}; |
| 356 |
instead of \cs{count}\meta{class}, \LuaMeta\TeX{} has |
| 357 |
\cs{insertmultiplier}\meta{class}; etc. The second major difference is that |
| 358 |
\LuaMeta\TeX{} will pick up inserts that are inside of boxes, meaning that |
| 359 |
placing footnotes in things like tables or frames should mostly just work as |
| 360 |
expected. |
| 361 |
|
| 362 |
There are |
| 363 |
also a few new parameters and other minor changes, but the overall mechanism is |
| 364 |
still quite similar to traditional \TeX{}. |
| 365 |
|
| 366 |
\subsubsection{Paragraph breaking} |
| 367 |
|
| 368 |
As stated in the original article, \textsf{lua-widow-control} intercepts |
| 369 |
\TeX{}'s output immediately before the output routine. However, this is |
| 370 |
\emph{after} all the inserts on the page have been processed and boxed. This is |
| 371 |
a bit of a problem because if we move a line to the next page, we need to move |
| 372 |
the associated insert; however, the insert is already gone. |
| 373 |
|
| 374 |
To solve this problem, immediately after \TeX{} has naturally broken a |
| 375 |
paragraph, \textsf{lua-widow-control} copies and stores all its inserts. Then, |
| 376 |
\textsf{lua-widow-control} tags the first element of each line (usually a glyph) |
| 377 |
with a Lua\TeX{} attribute that contains the indices for the first and last |
| 378 |
associated insert. \textsf{lua-widow-control} also tags each line inside the |
| 379 |
insert's content with its corresponding index so that it can be found later. |
| 380 |
|
| 381 |
\subsubsection{Page breaking} |
| 382 |
|
| 383 |
Here, we follow the same algorithm as in the original article. However, when we |
| 384 |
move the last line of the page to the next page, we first need to inspect the |
| 385 |
line to see if any of its contents have been marked with an insert index. If so, |
| 386 |
we need to move the corresponding insert to the next page. To do so, we unpack the |
| 387 |
attributes value to get all the inserts associated with this line. |
| 388 |
|
| 389 |
Using the stored insert indices and class, we can iterate through |
| 390 |
\cs{box}\meta{class} and delete any lines that match one of the current line's |
| 391 |
indices. We also need to iterate through the internal \TeX{} box % technically a list |
| 392 |
\verb|hold_head|\Dash the box that holds any inserts split onto the next page |
| 393 |
\Dash and delete any matching lines. We can safely delete any of these lines |
| 394 |
since they are still stored in the original \cs{insert} nodes that we copied |
| 395 |
earlier. |
| 396 |
|
| 397 |
Now, we can retrieve all of our previously-stored inserts and add them to the |
| 398 |
next page, immediately after the moved line. Then, when \TeX{} builds that page, it will find these inserts and move their contents to the appropriate boxes. |
| 399 |
|
| 400 |
\modifiedsection[16]{Known issues} |
| 401 |
The following two bugs have now been fully resolved: |
| 402 |
|
| 403 |
\begin{itemize} |
| 404 |
\item When running under LuaMeta\TeX, the log may contain [\dots] |
| 405 |
\item \TeX\ may warn about overfull \cs{vbox}es [\dots] |
| 406 |
\end{itemize} |
| 407 |
|
| 408 |
The fundamental limitations previously listed still exist; however, these two |
| 409 |
bugs along with a few dozen others have all been fixed since the original |
| 410 |
article was published. At this point, all \emph{known} bugs have been resolved; |
| 411 |
some bugs certainly still remain, but I'd feel quite confident using |
| 412 |
\textsf{lua-widow-control} in your everyday documents. |
| 413 |
|
| 414 |
There is, however, one new issue: |
| 415 |
|
| 416 |
\begin{itemize} |
| 417 |
\item \textsf{lua-widow-control} won't properly move footnotes if there are |
| 418 |
multiple different ``classes'' of inserts on the same line. To the |
| 419 |
best of my knowledge, this shouldn't happen in any real-world |
| 420 |
documents. If this happens to be an issue for you, please let me know; |
| 421 |
this problem is relatively easy to fix, although it will add |
| 422 |
considerable complexity for what I think isn't a real issue. |
| 423 |
\end{itemize} |
| 424 |
|
| 425 |
\advance\signaturewidth by 37pt |
| 426 |
\makesignature |
| 427 |
|
| 428 |
\end{document} |