Debian Codegraph @

texmf-dist/source/luatex/lua-widow-control/lwc-manual.tex

raw
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 \doifnot{\contextmark}{LMTX}{
7 \errhelp{LMTX/MkXL is required to compile this file.}
8 \errmessage{Fatal error, exiting.}
9 }
10
11 \environment lwc-manual
12
13 \usemodule[lua-widow-control]
14 \usebtxdataset[\jobname.bib]
15
16 \useURL[projecturl][https://github.com/gucci-on-fleek/lua-widow-control]
17 \useURL[download][https://github.com/gucci-on-fleek/lua-widow-control/releases/latest]
18 \useURL[ctan][https://www.ctan.org/pkg/lua-widow-control]
19
20 % Abbreviations
21 \def\lwc/{\sans{lua-\allowbreak widow-\allowbreak control}}
22 \def\Lwc/{\sans{Lua-\allowbreak widow-\allowbreak control}}
23 \def\estretch/{\tex{emergency\allowbreak stretch}}
24 \def\openalty/{\tex{output\allowbreak penalty}}
25 \def\waos/{widows and orphans}
26 \def\wao/{widow and orphan}
27 \def\woo/{widow or orphan}
28 \def\mkxl/{\ConTeXt~Mk\acronym{XL}}
29 \def\mkiv/{\ConTeXt~Mk\acronym{IV}}
30 \define\OpTeX{Op\kern-.05em\TeX}
31 \def\LuaMeta{Lua\-Meta\-}
32 \def\LuaMetaTeX{\LuaMeta\TeX}
33 \def\plainop/{Plain~\TeX{}/\OpTeX{}}
34 \let\q=\quotation
35
36 \def\titlecite[#1]{\cite[title][#1]\cite[#1]}
37
38 \definetype[inlineTEX][option=tex, escape={$,$}, lines=hyphenated]
39 \definetype[inlineLUA][option=lua, escape={$,$}, lines=hyphenated]
40
41 \input lwc-manual-samples
42
43 \definecolor[expanded][r=0.00, g=0.70, b=0.25]
44 \definecolor[failure] [r=0.90, g=0.00, b=0.25]
45 \definecolor[moved] [r=0.25, g=0.25, b=1.00]
46 \definecolor[cost] [r=0.50, g=0.50, b=0.50]
47
48 \startdocument[
49 title=lua-widow-control,
50 author=Max Chernoff,
51 version=3.0.1, %%version
52 github=https://github.com/gucci-on-fleek/lua-widow-control,
53 ctan=https://www.ctan.org/pkg/lua-widow-control,
54 ]
55
56 \Lwc/ is a Plain~\TeX/\LaTeX/\ConTeXt{}/\OpTeX{} package that removes \waos/ without any user intervention. Using the power of \LuaTeX{}, it does so \emph{without} stretching any glue or shortening any pages or columns. Instead, \lwc/ automatically lengthens a paragraph on a page or column where a \woo/ would otherwise occur.
57
58 \section{Quick Start}
59 Ensure that your \TeX~Live/Mik\TeX{} distribution is up-to-date. Then, \LaTeX{} users just need to place \inlineTEX{\usepackage{lua-widow-control}} in the preamble of their document. For more details, see the \goto{Usage sections}[sec:usage].
60
61 \subject{Contents}
62 \placecontent[criterium=all]
63
64 \section{Preliminaries}
65
66 This manual begins with a brief introduction to widows, orphans, and \lwc/. For an extended introduction and discussion of these topics, please see the \emph{Zpravodaj} article\cite[zpravodaj] distributed with this manual (Links: \goto{local}[lwc-zpravodaj::], \goto{\acronym{CTAN}}[url(http://mirrors.ctan.org/macros/luatex/generic/lua-widow-control/lwc-zpravodaj.pdf)], \goto{GitHub}[url(https://github.com/gucci-on-fleek/lua-widow-control/releases/latest/download/lwc-zpravodaj.pdf)]). This same material is also available in two \emph{TUGboat} articles\cite[tugboat1, tugboat2] (Links: \goto{local}[tb133chernoff-widows::], \goto{\acronym{CTAN}}[url(http://mirrors.ctan.org/macros/luatex/generic/lua-widow-control/tb133chernoff-widows.pdf)], \goto{GitHub}[url(https://github.com/gucci-on-fleek/lua-widow-control/releases/latest/download/tb133chernoff-widows.pdf)]) if you prefer. You can also skip ahead to the \goto{installation instructions on \at{page}[sec:install]}[sec:install] or the \goto{usage section starting at \at{page}[sec:usage]}[sec:usage].
67
68 \section[sec:motivation]{Motivation}
69
70 Unmodified \TeX{} has only two familiar ways of dealing with \waos/: it can
71 either shorten a page by one line, or it can stretch vertical
72 whitespace. \TeX{} was designed for mathematical and scientific typesetting,
73 where a typical page has multiple section headings, tables, figures, and
74 equations. For this style of document, \TeX's default behaviour works quite
75 well, since the slight stretching of whitespace between the various document
76 elements is nearly imperceptible; however, for prose or other documents
77 composed almost entirely of paragraphs, there is little vertical whitespace
78 to stretch.
79
80 \Lwc/ offers an alternative method of removing \waos/: instead of shortening a page or stretching vertical whitespace, \lwc/ simply chooses a paragraph to lengthen by one~line such that the \woo/ is eliminated.
81
82 \section{Widows and Orphans}
83 \subsection{Widows}
84 A \q{widow} occurs when the majority of a paragraph is on one page or column,
85 but the last line is on the following page or column. It not only looks
86 quite odd for a lone line to be at the start of the page, but it makes a
87 paragraph harder to read since the separation of a paragraph and
88 its last line disconnects the two, causing the reader to lose context for
89 the widowed line.
90
91 \subsection{Orphans}
92 An \q{orphan} occurs when the first line of a paragraph is at the end
93 of the page or column preceding the remainder of the paragraph. They are not
94 as distracting for the reader, but they are still not ideal.
95 Visually, \waos/ are about equally disruptive; however, orphans tend not to
96 decrease the legibility of a text as much as widows, so some authors choose
97 to ignore them.
98
99 \subsection{Broken Hyphens}
100 \q{Broken} hyphens occur whenever a page break occurs in a
101 hyphenated word. These are not related to \waos/; however,
102 breaking a word across two pages is at least as disruptive for the reader
103 as \waos/. \TeX{} identifies broken hyphens in the same ways as \waos/, so
104 \lwc/ treats broken hyphens in the same way.
105
106 \startplacefigure[location={here, top, bottom}, title={A visual comparison of \waos/.}, reference=tab:widow]
107 \getbuffer[widow-orphan]
108 \stopplacefigure
109
110 \section{\TeX's Pagination}
111 \subsection{Algorithm}
112 It is tricky to understand how \lwc/ works if you aren't familiar with how
113 \TeX{} breaks pages and columns. For a full description, you should
114 consult Chapter~15 of \titlecite[texbook] (\q{How \TeX{} Makes Lines
115 into Pages}); however, this goes into much more detail than most users
116 require, so here is a \emph{very} simplified summary of \TeX{}'s page
117 breaking algorithm:
118
119 \TeX{} fills the page with lines and other objects until the next object
120 will no longer fit. Once no more objects will fit, \TeX{} will align the
121 bottom of the last line with the bottom of the page by stretching any
122 available vertical spaces if (in \LaTeX) \tex{flushbottom} is set;
123 otherwise, it will
124 break the page and leave the bottom empty.
125
126 However, some objects have \q{penalties} attached. Penalties encourage or
127 discourage page breaks from occurring at specific places. For example,
128 \LaTeX{} sets a negative penalty before section headings to encourage a
129 page break there; conversely, it sets a positive penalty after section
130 headings to discourage breaking.
131
132 To reduce \waos/, \TeX{} sets weakly-positive penalties between the
133 first and second lines of a paragraph to prevent orphans, and between the
134 penultimate and final lines to prevent widows.
135
136 \subsection{Behaviour}
137
138 Due to these \q{penalties} attached to \waos/, \TeX{} tries to avoid
139 creating them. Widows and orphans with small penalties attached---like
140 \LaTeX's default values of 150---are only lightly coupled to the rest
141 of the paragraph, while \waos/ with large penalties---values of
142 10\,000 or more---are treated as infinitely bad and are thus
143 unbreakable. Intermediate values behave just as you would expect,
144 discouraging page breaks proportional to their value.
145
146 However, when these lines are moved as a group, \TeX{} will have to make a page or column with less lines. \about[sec:demo] goes into further detail about how \TeX{} deals with these too-short pages or columns.
147
148 \startpostponing
149 \startTEXpage[
150 align=normal,
151 width=100cm,
152 autowidth=force,
153 offset=5pt,
154 pagestate=start,
155 ]
156 \veryraggedcenter
157 \setupTABLE[row][first][style=\ssbf, align=middle]
158 \setupTABLE[frame=off, offset=5pt]
159 \startTABLE
160 \NC Ignore
161 \NC Shorten
162 \NC Stretch
163 \NC \Lwc/ \NC\NR
164
165 \NC \typesetbuffer[ignore][frame=on,page=1]
166 \NC \typesetbuffer[shorten][frame=on,page=1]
167 \NC \typesetbuffer[stretch][frame=on,page=1]
168 \NC \typesetbuffer[lwc][frame=on,page=1]
169 \NC\NR
170
171 \NC \typesetbuffer[ignore][frame=on,page=2]
172 \NC \typesetbuffer[shorten][frame=on,page=2]
173 \NC \typesetbuffer[stretch][frame=on,page=2]
174 \NC \typesetbuffer[lwc][frame=on,page=2]
175 \NC\NR
176
177 \NC \typebuffer[ignore-code][option=TEX]
178 \NC \typebuffer[shorten-code][option=TEX]
179 \NC \typebuffer[stretch-code][option=TEX]
180 \NC \typebuffer[lwc-code][option=TEX]
181 \NC\NR
182 \stopTABLE
183
184 \placefloatcaption[table][reference=tab:demo, title={A visual comparison of various automated widow handling techniques.}]
185 \stopTEXpage
186 \stoppostponing
187
188 \section[sec:demo]{Demonstration}
189
190 Although \TeX{}'s page breaking algorithm is reasonably
191 straightforward, it can lead to
192 complex behaviour when \waos/ are involved. The usual
193 choices, when rewriting is not possible, are to ignore them,
194 stretch some glue, or shorten the
195 page. \in{Table}[tab:demo] has a visual comparison of these
196 options, which we'll discuss in the following:
197
198 \subsection{\q{Ignore}}
199
200 As you can see, the last line of the page is on a separate page from the
201 rest of its paragraph, creating a widow. This is usually highly
202 distracting for the reader, so it is best avoided for the reasons previously
203 discussed.
204
205 \subsection{\q{Shorten}}
206
207 This page did not leave any widows, but it did shorten the previous page
208 by one line. Sometimes this is acceptable, but usually it looks bad because
209 pages will then have different text-block heights. This can make the pages
210 look quite uneven, especially when typesetting with columns or in a book
211 with facing pages.
212
213 \subsection{\q{Stretch}}
214
215 This page also has no widows and it has a flush bottom margin. However,
216 the space between each pair of paragraphs had to be stretched.
217
218 If this page had many equations, headings, and other elements with
219 natural space between them, the stretched out space would be much less
220 noticeable. \TeX{} was designed for mathematical typesetting, so it makes
221 sense that this is its default behaviour. However, in a page with mostly
222 text, these paragraph gaps look unsightly.
223
224 Also, this method is incompatible with grid typesetting, where
225 all glue stretching must be quantised to the height of a line.
226
227 \subsection{\q{\lwc/}}
228
229 \Lwc/ has none of these issues: it eliminates the widows in a document
230 while keeping a flush bottom margin and constant paragraph spacing.
231
232 To do so, \lwc/ lengthened the second paragraph by one line. If you look
233 closely, you can see that this stretched the interword spaces. This
234 stretching is noticeable when typesetting in a narrow text block, but
235 is mostly imperceptible with larger widths.
236
237 \Lwc/ automatically finds the \q{best} paragraph to stretch, so the
238 increase in interword spaces should almost always be minimal.
239
240 \section[sec:install]{Installation}
241 Most up-to-date \TeX~Live and Mik\TeX{} systems should already have \lwc/ installed. However, a manual installation may occasionally be required.
242
243 \subsection{\TeX~Live}
244 Run \type{tlmgr install lua-widow-control} in a terminal, or install using the \q{\TeX~Live Manager} \acronym{GUI}.
245
246 \subsection{Mik\TeX}
247 Run \type{mpm --install=lua-widow-control} in a terminal, or install using the \q{Mik\TeX{} Maintenance} \acronym{GUI}.
248
249 \subsection{Manual}
250 Currently, \mkxl/ (\LuaMetaTeX{}) users must manually install the package. Most other users will be better served by using the \lwc/ supplied by \TeX~Live and Mik\TeX{}; however, all users may manually install the package if desired. The procedure should be fairly similar regardless of your \acronym{OS}, \TeX{} distribution, or format.
251
252 \subsection{Steps}
253 \startitemize[N]
254 \item Download \type{lua-widow-control.tds.zip} from \goto{\acronym{CTAN}}[url(ctan)], \goto{GitHub}[url(download)] or the \goto{\ConTeXt{}~Garden}[url(https://modules.contextgarden.net/cgi-bin/module.cgi/action=view/id=127)].
255 \item Unzip the release into your \type{TEXMFLOCAL/} directory. (You can find its location by running \type{kpsewhich --var-value TEXMFHOME} in a terminal)
256 \item Refresh the filename database: \startitemize[1]
257 \item \ConTeXt: \type{mtxrun --generate}
258 \item \TeX~Live: \type{mktexlsr}
259 \item Mik\TeX: \type{initexmf --update-fndb}
260 \stopitemize
261 \stopitemize
262
263 \section{Dependencies}
264 \Lwc/ does have a few dependencies; however, these will almost certainly be met by all but the most minimal of \TeX{} installations.
265
266 \subsection{Plain~\TeX{}}
267 \Lwc/ requires \LuaTeX{} ($\ge$ 0.85) and the most recent version of \sans{luatexbase} (2015/10/04). Any version of \TeX~Live $\ge$ 2016 will meet these requirements.
268
269 \subsection{\LaTeX{}}
270 \Lwc/ requires \LuaTeX{} ($\ge$ 0.85), \LaTeX{} ($\ge$ 2020/10/01), and \sans{microtype} (any version). Any version of \TeX~Live $\ge$ 2021 will meet these requirements.
271
272 \Lwc/ also supports a \q{legacy} mode for older \LaTeX{} kernels. This uses an older version of the \LaTeX{} code while still using the most recent Lua code. This mode requires \LuaTeX{} ($\ge$ 0.85), \LaTeX{} ($\ge$ 2015/01/01), \sans{microtype} (any version), and \sans{etoolbox} (any version). Any version of \TeX~Live $\ge$ 2016 will meet these requirements.
273
274 Please note that when running in legacy mode, you cannot use the key--value interface. This legacy interface is undocumented, but mostly the same as the \q{Plain~\TeX{}} interface.
275
276 \subsection{\ConTeXt{}}
277 \Lwc/ supports both \mkxl/ (\LuaMetaTeX{}) and \mkiv/ (\LuaTeX{}).
278
279 \subsection{\OpTeX{}}
280 \Lwc/ works with any version of \OpTeX{} and has no dependencies.
281
282 \subsection{LuaMeta\TeX{}}
283
284 \Lwc/ has preliminary support for LuaMeta\LaTeX{} and LuaMetaPlain. All features should work identically to the \LuaTeX{}-based version, although there are a few minor bugs. You should always make sure to use the latest engine, format, and \lwc/ since these formats are under rapid development.
285
286
287 \section[sec:usage]{Loading the Package}
288
289 \startTABLE[setups=commandtable]
290 \NC Plain~\TeX{}
291 \NC\inlineTEX{\input lua-widow-control}
292 \NC\NR
293 \NC \LaTeX{}
294 \NC\inlineTEX{\usepackage{lua-widow-control}}
295 \NC\NR
296 \NC \ConTeXt{}
297 \NC\inlineTEX{\usemodule[lua-widow-control]}
298 \NC\NR
299 \NC \OpTeX{}
300 \NC\inlineTEX{\load[lua-widow-control]}
301 \NC\NR
302 \stopTABLE
303
304 \section{Options}
305
306 \Lwc/ is automatically enabled with the default settings as soon as you load it. Most users should not need to configure \lwc/; however, the packages provides a few commands.
307
308 \subsection{Overview}
309
310 \LaTeX{} users can set the options either when loading the package (\inlineTEX{\usepackage[$\meta{options}$]{lua-widow-control}}) or at any point using \inlineTEX{\lwcsetup{$\meta{options}$}}.
311
312 \ConTeXt{} users should use the \inlineTEX{\setuplwc[$\meta{options}$]} command for setting options at any point.
313
314 Plain~\TeX{} and \OpTeX{} are a little different. Some options require you to set a register (i.e., \inlineTEX{\lwcemergencystretch = $\meta{dimension}$}), while others use macro arguments (i.e., \inlineTEX{\lwcnobreak{$\meta{option}$}}).
315
316 \subsection{Disabling}
317
318 You may want to disable \lwc/ for certain portions of your
319 document. You can do so with the following commands:
320
321 \startTABLE[setups=commandtable]
322 \NC \plainop/
323 \NC\inlineTEX{\lwcdisable}
324 \NC\NR
325 \NC \LaTeX{}
326 \NC\inlineTEX{\lwcsetup{disable}}
327 \NC\NR
328 \NC \ConTeXt{}
329 \NC\inlineTEX{\setuplwc[state = stop]}
330 \NC\NR
331 \stopTABLE
332
333 This prevents \lwc/ from stretching any paragraphs that follow. If a page
334 has earlier paragraphs where \lwc/ was still enabled and a \woo/ is
335 detected, \lwc/ will still attempt to remove the \woo/.
336
337 \subsection{Enabling}
338
339 \Lwc/ is enabled as soon as the package is loaded. If you
340 have previously disabled it, you will need to re-enable it to save new paragraphs.
341
342 \startTABLE[setups=commandtable]
343 \NC \plainop/
344 \NC\inlineTEX{\lwcenable}
345 \NC\NR
346 \NC \LaTeX{}
347 \NC\inlineTEX{\lwcsetup{enable}}
348 \NC\NR
349 \NC \ConTeXt{}
350 \NC\inlineTEX{\setuplwc[state = start]}
351 \NC\NR
352 \stopTABLE
353
354 \subsection{Automatically disabling}
355
356 You may want to disable \lwc/ for certain commands where
357 stretching is undesirable such as section headings. Of course, manually
358 disabling and
359 then enabling \lwc/ multiple times
360 throughout a document would quickly become tedious, so \lwc/ provides
361 some options to do this automatically for you.
362
363 \Lwc/ automatically patches the default \LaTeX{}, \ConTeXt{},
364 Plain~\TeX{}, \OpTeX{}, \sans{memoir}, \sans{\acronym{KOMA}-Script}, and
365 \sans{titlesec} section commands, so you don't need to patch these.
366 Any others, though, you'll need to patch yourself.
367
368 \startTABLE[setups=commandtable]
369 \NC \plainop/
370 \NC \inlineTEX{\lwcdisablecmd $\meta{\backslash macro}$}
371 \NC\NR
372 \NC \LaTeX{}
373 \NC \inlineTEX{\lwcsetup{disablecmds = {$\meta{macronameone}$, $\meta{macronametwo}$}}}
374 \NC\NR
375 \NC \ConTeXt{}
376 \NC \inlineTEX{\prependtoks\lwc$\hskip-0.5em\relax$@patch@pre\to\everybefore$\hskip-0.5em\relax\meta{hook}$}
377 \NC\NR\NC\NC \inlineTEX{\prependtoks\lwc$\hskip-0.5em\relax$@patch@pre\to\everyafter$\hskip-0.5em\relax\meta{hook}$}
378 \NC\NR
379 \stopTABLE
380
381 The Plain~\TeX{}, \OpTeX{}, and \ConTeXt{} commands \emph{append} to the list of patched commands: they simply patch the provided commands while leaving the original patches in place. The \LaTeX{} option \emph{sets} the list of patched commands: it replaces the default list with the provided list.
382
383 \subsection{\estretch/}
384
385 \Lwc/ defaults to an \estretch/ value of 3~em for stretched paragraphs,
386 but you can configure this.
387
388 \Lwc/ will only use the \estretch/ when it cannot lengthen a paragraph
389 in any other way, so it is fairly safe to set this to a large value.
390 \TeX{} accumulates badness when \estretch/ is
391 used, so it's pretty rare that a paragraph that
392 requires any \estretch/ will actually be used on the page.
393
394 \startTABLE[setups=commandtable]
395 \NC \plainop/
396 \NC\inlineTEX{\lwcemergencystretch = $\meta{dimension}$}
397 \NC\NR
398 \NC \LaTeX{}
399 \NC\inlineTEX{\lwcsetup{emergencystretch = $\meta{dimension}$}}
400 \NC\NR
401 \NC \ConTeXt{}
402 \NC\inlineTEX{\setuplwc[emergencystretch = $\meta{dimension}$]}
403 \NC\NR
404 \stopTABLE
405
406 \subsection{Penalties}
407
408 You can also manually adjust the penalties that \TeX{} assigns to \waos/.
409 Usually, the defaults are fine, but there are a few circumstances where you
410 may want to change them.
411
412 \startTABLE[setups=commandtable]
413 \NC \plainop/
414 \NC\inlineTEX{\widowpenalty = $\meta{integer}$}
415 \NC\NR\NC\NC\inlineTEX{\clubpenalty = $\meta{integer}$}
416 \NC\NR\NC\NC\inlineTEX{\brokenpenalty = $\meta{integer}$}
417 \NC\NR
418 \NC \LaTeX{}
419 \NC\inlineTEX{\lwcsetup{widowpenalty = $\meta{integer}$}}
420 \NC\NR\NC\NC\inlineTEX{\lwcsetup{orphanpenalty = $\meta{integer}$}}
421 \NC\NR\NC\NC\inlineTEX{\lwcsetup{brokenpenalty = $\meta{integer}$}}
422 \NC\NR
423 \NC \ConTeXt{}
424 \NC\inlineTEX{\setuplwc[widowpenalty = $\meta{integer}$]}
425 \NC\NR\NC\NC\inlineTEX{\setuplwc[orphanpenalty = $\meta{integer}$]}
426 \NC\NR\NC\NC\inlineTEX{\setuplwc[brokenpenalty = $\meta{integer}$]}
427 \NC\NR
428 \stopTABLE
429
430 \page % TODO
431
432 The value of these penalties determines how much \TeX{} should attempt
433 to stretch glue before passing the \woo/ to \lwc/. If you set the values to~1
434 (default), \TeX{} will stretch nothing and immediately trigger \lwc/;
435 if you set the values to 10\,000, \TeX{} will stretch infinitely and
436 \lwc/ will never be triggered. If you set the value to some intermediate
437 number, \TeX{} will first attempt to stretch some glue to remove the \woo/;
438 only if it fails will \lwc/ come in and lengthen a paragraph. As a special
439 case, if you set the values to~0, both \TeX{} and \lwc/ will completely
440 ignore the \woo/.
441
442 \Lwc/ will pick up on the values of \tex{widowpenalty}, \tex{clubpenalty}, and
443 \tex{brokenpenalty} regardless of how you set them, so the use of these
444 dedicated keys is entirely optional.
445
446 \subsection{\tex{nobreak} Behaviour}
447
448 When \lwc/ encounters an orphan, it removes it by moving the orphaned
449 line to the next page. The majority of the time, this is an appropriate
450 solution. However, if the orphan is immediately preceded by a section
451 heading (or \tex{nobreak}\slash\tex{penalty 10000}), \lwc/ would na\"ively separate a section heading from
452 the paragraph that follows. This is almost always undesirable, so \lwc/
453 provides some options to configure this.
454
455 \startTABLE[setups=commandtable]
456 \NC \plainop/
457 \NC\inlineTEX{\lwcnobreak{$\meta{value}$}}
458 \NC\NR
459 \NC \LaTeX{}
460 \NC\inlineTEX{\lwcsetup{nobreak = $\meta{value}$}}
461 \NC\NR
462 \NC \ConTeXt{}
463 \NC\inlineTEX{\setuplwc[nobreak = $\meta{value}$]}
464 \NC\NR
465 \stopTABLE
466
467 The default value, \type{keep}, \emph{keep}s the section heading with
468 the orphan by moving both to the next page. The advantage to this option
469 is that it removes the orphan and retains any \tex{nobreak}s; the
470 disadvantage is that moving the section heading can create a large blank
471 space at the end of the page.
472
473 The value \type{split} \emph{split}s up the section heading and the
474 orphan by moving the orphan to the next page while leaving the heading
475 behind. This is usually a bad idea, but exists for the sake of
476 flexibility.
477
478 The value \type{warn} causes \lwc/ to give up on the page and do nothing,
479 leaving an orphaned line. \Lwc/ \emph{warn}s the user so that they can
480 manually remove the orphan.
481
482 \startplacefigure[location={here, top, bottom}, title={A visual comparison of the various \type{nobreak} options, where each box represents a different page.}, reference=tab:nobreak]
483 \getbuffer[nobreak]
484 \stopplacefigure
485
486 \subsection{Maximum Cost}
487
488 \Lwc/ ranks each paragraph on the page by how much it would \q{cost} to
489 lengthen that paragraph. By default, \lwc/ selects the paragraph on
490 the page with the lowest cost; however, you can configure it to only
491 select paragraphs below a selected cost.
492
493 If there aren't any paragraphs below the set threshold, then \lwc/ won't
494 remove the \woo/ and will instead issue a warning.
495
496 \startTABLE[setups=commandtable]
497 \NC \plainop/
498 \NC\inlineTEX{\lwcmaxcost = $\meta{integer}$}
499 \NC\NR
500 \NC \LaTeX{}
501 \NC\inlineTEX{\lwcsetup{max-cost = $\meta{integer}$}}
502 \NC\NR
503 \NC \ConTeXt{}
504 \NC\inlineTEX{\setuplwc[maxcost = $\meta{integer}$]}
505 \NC\NR
506 \stopTABLE
507
508 Based on my testing, \type{max-cost} values less than 1\,000
509 cause completely imperceptible changes in interword spacing; values less
510 than 5\,000 are only noticeable if you are specifically trying to pick out the
511 expanded paragraph on the page; values less than 15\,000 are typically
512 acceptable; and larger values may become distracting. \Lwc/ defaults to an
513 infinite \type{max-cost}, although the \q{strict} and \q{balanced} modes
514 sets the values to~5\,000 and 10\,000 respectively.
515
516 \Lwc/ uses a \q{cost function} $C$ that is initially defined as \startformula
517 C = \frac{d}{\sqrt{l}}
518 \stopformula where $d$ is the total demerits of the paragraph, and $l$ is the number of lines in the paragraph.
519
520 \subsection{Draft Mode}
521
522 \setuplwc[draft=start]
523 \definecolor[lwc_expanded][s=0]
524 \definecolor[lwc_failure][s=0]
525 \definecolor[lwc_moved][s=0]
526 You can use the draft mode to visualize how \lwc/ processes pages. Any \color[failure]{remaining widows and orphans will be coloured red}, any \color[expanded]{expanded paragraphs will be coloured green}, and any \color[moved]{lines moved to the next page will be coloured blue}. In addition, the \color[cost]{cost of each paragraph is shown in the right margin} as it is here.
527
528 \startTABLE[setups=commandtable]
529 \NC \plainop/
530 \NC\inlineTEX{\lwcdraft 1}
531 \NC\NR
532 \NC \LaTeX{}
533 \NC\inlineTEX{\lwcsetup{draft}}
534 \NC\NR
535 \NC \ConTeXt{}
536 \NC\inlineTEX{\setuplwc[draft = start]}
537 \NC\NR
538 \stopTABLE
539
540 Advanced users may also customize the colours used by modifying the \inlineLUA{lwc.colours} table. The table keys are currently \type{expanded}, \type{failure}, \type{moved}, and \type{cost}. The table values are \acronym{RGB} 3-tuples, where each element is a float between zero and one.
541
542 You can also show or hide the costs and colours independently of each other.
543
544 \startTABLE[setups=commandtable]
545 \NC \plainop/
546 \NC\inlineTEX{\lwcshowcosts 1}
547 \NC\NR\NC\NC\inlineTEX{\lwcshowcolours 1}
548 \NC\NR
549 \NC \LaTeX{}
550 \NC\inlineTEX{\lwcsetup{showcosts = true}}
551 \NC\NR\NC\NC\inlineTEX{\lwcsetup{showcolours = true}}
552 \NC\NR
553 \NC \ConTeXt{}
554 \NC\inlineTEX{\setuplwc[showcosts = start]}
555 \NC\NR\NC\NC\inlineTEX{\setuplwc[showcolours = start]}
556 \NC\NR
557 \stopTABLE
558
559 Plain~\TeX{} users will need to modify their output routines to be able to see the costs. Before you ship out the page, you should call \inlineTEX{\lwcpreshipout} with a box number as its argument. For example, here is some sample code to correctly reproduce the standard Plain~\TeX{} output routine:
560
561 \startTEX
562 \newbox\tempshipoutbox
563 \output={
564 \setbox\tempshipoutbox=\vbox{
565 \makeheadline
566 \pagebody
567 \makefootline
568 }
569 \lwcpreshipout\the\tempshipoutbox
570 \shipout\box\tempshipoutbox
571 \advancepageno
572 }
573 \stopTEX
574
575 Setting the output routine like this is automatic in all other formats. \Lwc/ does not modify the output routine in Plain~\TeX{} since most of its users likely have their own output routines.
576
577 \subsection{Draft Offset}
578
579 In draft mode, the paragraph costs are printed in the margins, offset a certain distance from the page edge. By default, this offset is set to \unit{1 inch}; however, you can change this to any value that you want:
580
581 \startTABLE[setups=commandtable]
582 \NC \plainop/
583 \NC\inlineTEX{\lwcdraftoffset = $\meta{dimension}$}
584 \NC\NR
585 \NC \LaTeX{}
586 \NC\inlineTEX{\lwcsetup{draftoffset = $\meta{dimension}$}}
587 \NC\NR
588 \NC \ConTeXt{}
589 \NC\inlineTEX{\setuplwc[draftoffset = $\meta{dimension}$]}
590 \NC\NR
591 \stopTABLE
592
593 \setuplwc[draft=stop]
594 \subsection{Debug Mode}
595
596 \Lwc/ offers a \q{debug} mode that prints extra information in the log files. This may be helpful to understand how \lwc/ is processing paragraphs and pages, although the information is likely inscrutable unless you are the package's author. If you are reporting an issue with \lwc/, make sure to compile your document with debug mode enabled!
597
598 \startTABLE[setups=commandtable]
599 \NC \plainop/
600 \NC\inlineTEX{\lwcdebug 1}
601 \NC\NR\NC\NC\inlineTEX{\lwcdebug 0}
602 \NC\NR
603 \NC \LaTeX{}
604 \NC\inlineTEX{\lwcsetup{debug = true}}
605 \NC\NR\NC\NC\inlineTEX{\lwcsetup{debug = false}}
606 \NC\NR
607 \NC \ConTeXt{}
608 \NC\inlineTEX{\setuplwc[debug = start]}
609 \NC\NR\NC\NC\inlineTEX{\setuplwc[debug = stop]}
610 \NC\NR
611 \stopTABLE
612
613 \section{Presets}
614
615 As you can see, \lwc/ provides quite a few options. Luckily, there are a few
616 presets that you can use to set multiple options at once. These presets are
617 a good starting point for most documents, and you can always manually
618 override individual options.
619
620 Presets are available only with \LaTeX{} and \ConTeXt{}.
621
622 \startTABLE[setups=commandtable]
623 \NC \LaTeX{}
624 \NC\inlineTEX{\lwcsetup{$\meta{preset}$}}\NC\NR
625 \NC \ConTeXt{}
626 \NC\inlineTEX{\setuplwc[$\meta{preset}$]}\NC\NR
627 \stopTABLE
628
629 \subsection{\type{default}}
630
631 If you use \lwc/ without any options, it defaults to this preset. In default
632 mode, \lwc/ takes all possible measures to remove \waos/ and will not
633 attempt to stretch any vertical glue. This usually
634 removes~$\mathord{>}\,95\%$ of all
635 possible \waos/. The catch here is that this mode is quite aggressive, so
636 it often leaves behind some fairly \q{spacey} paragraphs.
637
638 This mode is good if you want to remove (nearly) all \waos/ from your
639 document, without fine-tuning the results.
640
641 \subsection{\type{strict}}
642
643 \Lwc/ also offers a strict mode. This greatly restricts \lwc/'s tolerance
644 and makes it so that it will only lengthen paragraphs where the change will
645 be imperceptible.
646
647 The caveat with strict mode is that---depending on the document---
648 \lwc/ will be able to remove less than a third of the \waos/.
649 For the \waos/ that can't be automatically removed, a warning will be
650 printed to your terminal and log file so that a human can manually fix the
651 situation.
652
653 This mode is good if you want the best possible typesetting and are willing
654 to do some manual editing.
655
656 \subsection{\type{balanced}}
657
658 Balanced mode sits somewhere between default mode and strict mode. This mode
659 first lets \TeX{} stretch a little glue to remove the \woo/; only if that
660 fails will it then trigger \lwc/. Even then, the maximum paragraph cost is
661 capped. Here, \lwc/ can usually remove 90\% of a document's
662 potential \waos/, and it does so while making a minimal visual impact.
663
664 This mode is recommended for most users who care about their document's
665 typography. This mode is not the default since it doesn't remove all
666 \waos/: it
667 still requires a little manual intervention.
668
669 % %
670 \startplacetable[location={here, top, bottom}, title={\Lwc/ options set by each mode.}, reference=tab:modes]
671 \startTABLE[setups=booktabs, style=tt, loffset=0.5em]
672 \NC {\rm Option} \NC default \NC balanced \NC strict \NC\NR
673 \NC max-cost \NC $\infty$ \NC 10000 \NC 5000 \NC\NR
674 \NC emergencystretch \NC 3em \NC 1em \NC 0pt \NC\NR
675 \NC nobreak \NC keep \NC keep \NC warn \NC\NR
676 \NC widowpenalty \NC 1 \NC 500 \NC 1 \NC\NR
677 \NC orphanpenalty \NC 1 \NC 500 \NC 1 \NC\NR
678 \NC brokenpenalty \NC 1 \NC 500 \NC 1 \NC\NR
679 \stopTABLE
680 \stopplacetable
681
682 \section{Lua Interface}
683
684 \Lwc/ provides a few public functions and tables that you can safely use or modify as documented. Any Lua interfaces that aren't documented below are subject to change at any time, but ff you do want to use an undocumented interface, please let me know and can easily document it.
685
686 \subsection{Costs}
687
688 \Lwc/ uses a \q{cost function} to select which paragraph to expand. Typically, this depends on the number of lines and demerits in the broken paragraph; however, you can redefine this function to do something else if you want. The default function is defined as:
689
690 \startLUA
691 --- The "cost function" to use.
692 ---
693 --- @param demerits number The demerits of the broken paragraph
694 --- @param lines number The number of lines in the broken paragraph
695 --- @param nat_demerits number The demerits of the naturally-broken paragraph
696 --- @param nat_lines number The number of lines in the naturally-broken paragraph
697 --- @param head node The head of the broken paragraph
698 --- @return number cost The cost of the broken paragraph
699 function lwc.paragraph_cost(demerits, lines, nat_demerits, nat_lines, head)
700 return demerits / math.sqrt(lines)
701 end
702 \stopLUA
703
704 \subsection{Activation}
705
706 Typically, \lwc/ determines if there are \waos/ at the end of a page by checking the \tex{outputpenalty} register. However, you can use a custom check if you want. The default check is defined as:
707
708 \startLUA
709 --- Determines if we should "activate" lwc for the current page/column.
710 ---
711 --- @param penalty number The \outputpenalty for the current page/column
712 --- @param paragraphs table<table<string, node|number>> The `paragraphs` table
713 --- @param head node The head of the current page/column
714 --- @return boolean activate True if lwc should move the last line on this page
715 function lwc.should_remove_widows(penalty, paragraphs, head)
716 return is_matching_penalty(penalty)
717 end
718 \stopLUA
719
720 By setting a custom activation and cost function, you can transform \lwc/ from a \wao/ remover into a custom layout customization tool.
721
722 \section{Compatibility}
723
724 The \lwc/ implementation is almost entirely in Lua, with only a minimal
725 \TeX{} footprint. It
726 doesn't modify the output routine, \tex{everypar}, and
727 it doesn't insert any whatsits. This means that it should be compatible with
728 nearly any \TeX{} package, class, and format. Most changes that \lwc/ makes
729 are not observable on the \TeX{} side.
730
731 However, on the Lua side, \lwc/ modifies much of a page's internal
732 structure.
733 This should not affect any \TeX{} code; however, it may surprise
734 Lua code that modifies or depends on the page's low-level structure. This
735 does not matter for Plain~\TeX{} or \LaTeX{}, where even most Lua-based
736 packages don't depend on the node list structure; nevertheless, there are
737 a few issues with \ConTeXt{}.
738
739 However, on the Lua side, \lwc/ modifies much of a page's internal
740 structure. This should not affect any \TeX{} code; however, it may surprise
741 Lua code that modifies or depends on the page's low-level structure. This
742 does not affect Plain~\TeX{} or \LaTeX{} where even most Lua-based packages
743 don't depend on the node list structure. \ConTeXt{} \emph{does} depend on
744 this internal node structure; however, I have carefully tested the package
745 to ensure that this causes no issues.
746
747 Finally, keep in mind that adding \lwc/ to a document will almost certainly
748 change its page break locations.
749
750 \subsection{Formats}
751
752 \Lwc/ runs on all known Lua\TeX{}-based formats: Plain~Lua\TeX{},
753 Lua\LaTeX{}, \ConTeXt{} Mk\acronym{IV}, and~\OpTeX{}. Unless otherwise
754 documented, all features should work equally well in all formats.
755
756 \Lwc/ is also fully-compatible with the \LuaMeta\TeX{}-based formats:
757 \ConTeXt{} Mk\acronym{XL}\slash\acronym{LMTX}, \LuaMeta\LaTeX{}, and
758 \LuaMeta{}Plain. \ConTeXt{} Mk\acronym{XL} works equally
759 well as \ConTeXt{} Mk\acronym{IV} and Lua\LaTeX{}; however, \LuaMeta\LaTeX{}
760 and \LuaMeta{}Plain support is still quite early. All features should work,
761 although there are still a few minor bugs.
762
763 All told, \lwc/ supports 7 different format\slash{}engine combinations.
764
765 \subsection{Columns}
766
767 Since \TeX{} and the formats implement column breaking and page
768 breaking through the
769 same internal mechanisms, \lwc/ removes \waos/ between columns just
770 as it does with \waos/ between pages.
771
772 \Lwc/ is known to work with the \LaTeX{} class option \type{twocolumn}
773 and the two-column output routine from Chapter~23 of \titlecite[texbook].
774
775 \subsection{Performance}
776
777 \Lwc/ runs entirely in a single pass, without depending on any
778 \type{.aux} files or the like. Thus, it shouldn't meaningfully
779 increase compile times. Although \lwc/ internally breaks each paragraph
780 twice, modern computers break paragraphs near-instantaneously, so you
781 are not likely to notice any slowdown.
782
783 \subsection{\eTeX{} penalties}
784
785 Knuth's original \TeX{} has three basic line penalties:
786 \tex{interlinepenalty}, which
787 is inserted between all lines; \tex{club\-penalty}, which is inserted after
788 the first line; and \tex{widow\-penalty}, which is inserted before the last
789 line. The \eTeX{} extensions generalize these commands with a
790 syntax similar to \tex{parshape}: with \tex{widow\-penalties} you can set the
791 penalty between the last, second last, and $n$th last lines of a paragraph;
792 \tex{inter\-line\-penalties} and \tex{club\-penalties} behave similarly.
793
794 \Lwc/ makes no explicit attempts to support these new -\type{penalties}
795 commands. Specifically, if you give a line a penalty that matches either
796 \tex{widowpenalty} or \tex{clubpenalty}, \lwc/ will treat the lines
797 exactly as it would a \woo/. So while these commands won't break \lwc/, they
798 are likely to lead to some unexpected behaviour.
799
800 \subsection{Grids}
801
802 \Lwc/ is fully compatible with the grid snapping features of \ConTeXt{} \mkiv/ and \mkxl/.
803
804 \subsection{Footnotes}
805
806 If there are footnotes (or any other type of inline \tex{insert}) present in the moved line, \lwc/ will move both the \q{footnote mark} and the \q{footnote text} such that both are on the same page. However, this may lead to an odd blank space at the bottom of the page since \lwc/ needs to move both the line and its footnotes. Footnotes cause the same page-breaking issues in unmodified Plain~\TeX{} and \LaTeX{}, so this is mostly unavoidable.
807
808 \section{Stability}
809
810 The documented interfaces of \lwc/ can be considered stable: I'm not planning on removing or modifying any existing options or commands in any way that would break documents.
811
812 However, \lwc/'s page breaking \emph{is} subject to change. I will attempt to keep page breaks the same whenever reasonable; however, I will \emph{rarely} make modifications to the algorithm when I can improve the output quality. Any such changes will be clearly noted in the release notes.
813
814 \section{Short last lines}
815
816 When lengthening a paragraph with \tex{looseness}, it is common advice to insert
817 ties (\type{~}) between the last few words of the paragraph to avoid
818 overly-short last lines\cite[texbook]. \Lwc/ does this automatically,
819 but instead of using ties or \tex{hbox}es, it uses the
820 \tex{par\allowbreak fill\allowbreak skip}
821 parameter. When lengthening a paragraph
822 (and only when lengthening a paragraph---remember, \lwc/ doesn't
823 interfere with \TeX{}'s output unless it detects a \woo/), \lwc/ sets
824 \tex{parfillskip} to \type{0.75\hsize plus 0.05\hsize minus 0.75\hsize}.
825 This normally makes the last line of a paragraph be at least
826 20\% of the overall paragraph's width, thus preventing
827 ultra-short~lines.
828
829 \section[sec:issues]{Known Issues}
830
831 \startitemize
832 \item When a three-line paragraph is at the end of a page forming a
833 widow, \lwc/ will remove the widow; however, it will leave an orphan.
834 This issue is inherent to any process that removes widows through
835 paragraph expansion and is thus unavoidable. Orphans are considered
836 to be better than widows\cite[elements], so this is still an
837 improvement.
838
839 \item \Lwc/ only attempts to expand paragraphs; it never attempts to shrink them. See the \emph{TUGboat}~article\cite[tugboat1] ยง15.3 for further discussion. \githubissue{33}
840
841 \item \Lwc/ can only expand paragraphs that fit completely on a page. This is unavoidable due to the one-page-at-a-time model: you can't modify the bottom half of a paragraph since its top half has already shipped out, and you can't expand the top half of a paragraph since that can't remove orphans. This only causes issues if your document has paragraphs so long that a page only has two half-paragraphs and zero whole paragraphs.
842
843 \item Sometimes a \woo/ cannot be eliminated because no paragraph has
844 enough stretch. Sometimes this can be remediated by
845 increasing \lwc/'s \estretch/; however, some pages just don't have
846 any suitable paragraph.
847
848 Long paragraphs with short words tend to be stretchier than short
849 paragraphs with long words since these long paragraphs have more
850 interword glue. Narrow columns also stretch more easily than wide columns
851 since you need to expand a paragraph by less to make a new line.
852
853 \item \Lwc/ only attempts to expand paragraphs on a page with
854 a \woo/. A global system like in \titlecite[global] would solve this;
855 however, this is both \acronym{NP}-complete\cite[plass] and
856 impossible to solve in a single pass. Very rarely would such a
857 system remove widows or orphans that \lwc/ cannot.
858 \stopitemize
859
860 \section{Contributions}
861
862 If you have any issues with \lwc/, please create an issue at the \goto{project's GitHub page}[url(projecturl)]. Or, if you think that you can solve any of the \about[sec:issues] or add any new features, \goto{submit a \acronym{PR}}[url(projecturl)]. Thanks!
863
864 \section{License}
865
866 \Lwc/ is licensed under the \goto{\emph{Mozilla Public License}, version 2.0}[url(https://www.mozilla.org/en-US/MPL/2.0/)] or greater. The documentation is licensed under \goto{\acronym{CC-BY-SA}, version 4.0}[url(https://creativecommons.org/licenses/by-sa/4.0/legalcode)] or greater as well as the \acronym{MPL}.
867
868 Please note that a compiled document is \bold{not} considered to be an \q{Executable Form} as defined by the \acronym{MPL}. The \acronym{MPL} and \acronym{CC-BY-SA} licenses \bold{only} apply to you if you distribute the \lwc/ source code or documentation.
869
870 \section{References}
871
872 \placelistofpublications
873
874 \page
875
876 \setuphead[subsection][
877 alternative=normal,
878 style=\ssitb,
879 after={\blank[disable, penalty:10000]}
880 ]
881 \markdownInput{../../CHANGELOG.md}
882
883 \page
884
885 \setuplayout[
886 width=middle,
887 backspace=1in,
888 height=9.25in,
889 ]
890 \section[sec:implementation]{Implementation}
891
892 From here and until the end of this manual is the raw source code of \lwc/. This is primarily of interest to developers; most users need not read further.
893
894 This code vaguely resembles the typical \LaTeX{} literate programming style, although I use extensive inline comments instead of arcane \sans{docstrip} macros. Hopefully this is useful as a reference for advanced \lwc/ users as well as anyone doing extensive node manipulation in \LuaTeX{}.
895
896 If want to offer any improvements to the code below, please open an issue or a \acronym{PR} on \goto{GitHub}[url(projecturl)].
897
898 \usemodule[scite]
899 \setupbodyfont[10pt]
900 \setuphead[subsection][
901 page=yes,
902 continue=yes,
903 ]
904
905 \subsection{lua-widow-control.lua}
906
907 \typeLUAfile{../source/lua-widow-control.lua}
908
909 \subsection{lua-widow-control.tex}
910
911 \typeTEXfile{../source/lua-widow-control.tex}
912
913 \subsection{lua-widow-control.sty}
914
915 \typeTEXfile{../source/lua-widow-control.sty}
916
917 \subsection{lua-widow-control-2022-02-22.sty}
918
919 \typeTEXfile{../source/lua-widow-control-2022-02-22.sty}
920
921 \subsection{t-lua-widow-control.mkxl}
922
923 \typeTEXfile{../source/t-lua-widow-control.mkxl}
924
925 \subsection{lua-widow-control.opm}
926
927 \typeTEXfile{../source/lua-widow-control.opm}
928
929 \subsection{Demo from \in{Table}[tab:demo]}
930
931 \typeTEXfile{\jobname-demo-text.tmp}
932
933 \stopdocument