summaryrefslogtreecommitdiff
path: root/Master
diff options
context:
space:
mode:
Diffstat (limited to 'Master')
-rw-r--r--Master/texmf-dist/doc/luatex/lua-widow-control/README.md4
-rw-r--r--Master/texmf-dist/doc/luatex/lua-widow-control/lua-widow-control.pdfbin508349 -> 579606 bytes
-rw-r--r--Master/texmf-dist/doc/luatex/lua-widow-control/lwc-zpravodaj.pdfbin0 -> 377831 bytes
-rw-r--r--Master/texmf-dist/doc/luatex/lua-widow-control/tb133chernoff-widows.pdfbin391526 -> 519495 bytes
-rw-r--r--Master/texmf-dist/source/luatex/lua-widow-control/CHANGELOG.md20
-rw-r--r--Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.bib27
-rw-r--r--Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.mkxl2
-rw-r--r--Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.tex159
-rw-r--r--Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj-figure.ctx161
-rw-r--r--Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj.bib237
-rw-r--r--Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj.ltx1720
-rw-r--r--Master/texmf-dist/source/luatex/lua-widow-control/tb133chernoff-widows.ltx23
-rw-r--r--Master/texmf-dist/tex/context/third/lua-widow-control/t-lua-widow-control.mkxl86
-rw-r--r--Master/texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control-2022-02-22.sty4
-rw-r--r--Master/texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control.sty43
-rw-r--r--Master/texmf-dist/tex/luatex/lua-widow-control/lua-widow-control.lua824
-rw-r--r--Master/texmf-dist/tex/luatex/lua-widow-control/lua-widow-control.tex32
-rw-r--r--Master/texmf-dist/tex/optex/lua-widow-control/lua-widow-control.opm12
18 files changed, 3040 insertions, 314 deletions
diff --git a/Master/texmf-dist/doc/luatex/lua-widow-control/README.md b/Master/texmf-dist/doc/luatex/lua-widow-control/README.md
index 4360229236a..93f94449f5d 100644
--- a/Master/texmf-dist/doc/luatex/lua-widow-control/README.md
+++ b/Master/texmf-dist/doc/luatex/lua-widow-control/README.md
@@ -9,7 +9,7 @@ lua-widow-control
Lua-widow-control is a Plain TeX/LaTeX/ConTeXt/OpTeX package that removes widows and orphans without any user intervention. Using the power of LuaTeX, it does so _without_ stretching any glue or shortening any pages or columns. Instead, lua-widow-control automatically lengthens a paragraph on a page or column where a widow or orphan would otherwise occur.
-Please see the [**package manual**](https://github.com/gucci-on-fleek/lua-widow-control/releases/latest/download/lua-widow-control.pdf) for usage details or the [***TUGboat* article**](https://github.com/gucci-on-fleek/lua-widow-control/releases/latest/download/tb133chernoff-widows.pdf) for background information and discussion.
+Please see the [**package manual**](https://github.com/gucci-on-fleek/lua-widow-control/releases/latest/download/lua-widow-control.pdf) for usage details or the [***TUGboat***](https://github.com/gucci-on-fleek/lua-widow-control/releases/latest/download/tb133chernoff-widows.pdf) or [***Zpravodaj* articles**](https://github.com/gucci-on-fleek/lua-widow-control/releases/latest/download/lwc-zpravodaj.pdf) for background information and discussion.
Usage
-----
@@ -48,4 +48,4 @@ Lua-widow-control is licensed under the [_Mozilla Public License_, version 2.0](
Please note that a compiled document is absolutely **not** considered to be an "Executable Form" as defined by the MPL. The use of lua-widow-control in a document does not place **any** obligations on the document's author or distributors. The MPL and CC-BY-SA licenses **only** apply to you if you distribute the lua-widow-control source code or documentation.
---
-_v2.2.2 (2022-08-23)_ <!--%%version %%dashdate-->
+_v3.0.0 (2022-11-22)_ <!--%%version %%dashdate-->
diff --git a/Master/texmf-dist/doc/luatex/lua-widow-control/lua-widow-control.pdf b/Master/texmf-dist/doc/luatex/lua-widow-control/lua-widow-control.pdf
index b984764d8f6..ba428f1f016 100644
--- a/Master/texmf-dist/doc/luatex/lua-widow-control/lua-widow-control.pdf
+++ b/Master/texmf-dist/doc/luatex/lua-widow-control/lua-widow-control.pdf
Binary files differ
diff --git a/Master/texmf-dist/doc/luatex/lua-widow-control/lwc-zpravodaj.pdf b/Master/texmf-dist/doc/luatex/lua-widow-control/lwc-zpravodaj.pdf
new file mode 100644
index 00000000000..1c37d25bf2f
--- /dev/null
+++ b/Master/texmf-dist/doc/luatex/lua-widow-control/lwc-zpravodaj.pdf
Binary files differ
diff --git a/Master/texmf-dist/doc/luatex/lua-widow-control/tb133chernoff-widows.pdf b/Master/texmf-dist/doc/luatex/lua-widow-control/tb133chernoff-widows.pdf
index 2595747b8a6..e77e4df285a 100644
--- a/Master/texmf-dist/doc/luatex/lua-widow-control/tb133chernoff-widows.pdf
+++ b/Master/texmf-dist/doc/luatex/lua-widow-control/tb133chernoff-widows.pdf
Binary files differ
diff --git a/Master/texmf-dist/source/luatex/lua-widow-control/CHANGELOG.md b/Master/texmf-dist/source/luatex/lua-widow-control/CHANGELOG.md
index 0ac3878e04b..37eee32f119 100644
--- a/Master/texmf-dist/source/luatex/lua-widow-control/CHANGELOG.md
+++ b/Master/texmf-dist/source/luatex/lua-widow-control/CHANGELOG.md
@@ -9,7 +9,25 @@ Changelog
All notable changes to lua-widow-control will be listed here, in reverse chronological order. **Changes listed in bold** are important changes: they either remove options or commands, or may change the location of page breaks.
-## v2.2.2 (2022-08-23)
+## v3.0.0 (2022-11-22)
+
+- Add the new _TUGboat_ and _Zpravodaj_ articles.
+
+- Add and document the public Lua interfaces.
+
+- Change `\parfillskip` settings for lengthened paragraphs to more strongly prevent short last lines. **May affect page breaks.**
+
+- Add the ability to configure the horizontal offset for the paragraph costs printed in draft mode.
+
+- Add support for [LuaMetaLaTeX and LuaMetaPlain](https://github.com/zauguin/luametalatex). All features should work identically to the LuaTeX-based version, although there are a few minor bugs. ([#40](https://github.com/gucci-on-fleek/lua-widow-control/pull/40))
+
+- Fully support inserts/footnotes in LuaMetaTeX ([#38](https://github.com/gucci-on-fleek/lua-widow-control/issues/38)).
+
+- Add support for presets in ConTeXt.
+
+- Add support for node colouring in ConTeXt and OpTeX ([#39](https://github.com/gucci-on-fleek/lua-widow-control/issues/39)).
+
+## [v2.2.2 (2022-08-23)](https://github.com/gucci-on-fleek/lua-widow-control/releases/tag/release-6c91837c205572a78a0bcaf9c80b8e475ef71689)
- Add preliminary support for inserts/footnotes in LuaMetaTeX ([#38](https://github.com/gucci-on-fleek/lua-widow-control/issues/38)).
diff --git a/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.bib b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.bib
index 9c4a172aa05..9a8d174df7a 100644
--- a/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.bib
+++ b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.bib
@@ -41,7 +41,7 @@
url={https://tug.org/docs/plass/plass-thesis.pdf}
}
-@article{article,
+@article{tugboat1,
title={Automatically removing widows and orphans with
{\tt lua-widow-control}},
author={Chernoff, Max},
@@ -53,3 +53,28 @@
pages={28--39},
DOI={10.47397/tb/43-1/tb133chernoff-widows},
}
+
+@article{tugboat2,
+ title={Updates to \sans{lua-widow-control}},
+ author={Chernoff, Max},
+ journal={TUGboat},
+ volume={43},
+ number={3},
+ pages={340--342},
+ year={2022},
+ month=nov,
+ DOI={10.47397/tb/43-3/tb135chernoff-lwc},
+}
+
+@article{zpravodaj,
+ title={Automatically removing widows and orphans with
+ \sans{lua-widow-control}},
+ author={Chernoff, Max},
+ journal={Zpravodaj Československého sdružení uživatelů TeXu},
+ volume={2022},
+ number={1--4},
+ pages={49--76},
+ year={2022},
+ month=nov,
+ DOI={10.5300/2022-1-4/49},
+}
diff --git a/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.mkxl b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.mkxl
index e4f49861b9e..9d74e405904 100644
--- a/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.mkxl
+++ b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.mkxl
@@ -313,6 +313,8 @@ interfaces.implement {
%%%%%%%%%%%%%%%%%%%%%%%%
\usebtxdefinitions[apa]
+\resetsetups[apa:list:sameauthor]
+
\setupbtx[apa:list][
stopper:initials=,
separator:initials=\btxnbsp,
diff --git a/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.tex b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.tex
index 2b5be55196d..ef464722253 100644
--- a/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.tex
+++ b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-manual.tex
@@ -28,7 +28,8 @@
\def\mkxl/{\ConTeXt~Mk\acronym{XL}}
\def\mkiv/{\ConTeXt~Mk\acronym{IV}}
\define\OpTeX{Op\kern-.05em\TeX}
-\def\LuaMetaTeX{LuaMeta\TeX}
+\def\LuaMeta{Lua\-Meta\-}
+\def\LuaMetaTeX{\LuaMeta\TeX}
\def\plainop/{Plain~\TeX{}/\OpTeX{}}
\let\q=\quotation
@@ -42,6 +43,7 @@
\definecolor[expanded][r=0.00, g=0.70, b=0.25]
\definecolor[failure] [r=0.90, g=0.00, b=0.25]
\definecolor[moved] [r=0.25, g=0.25, b=1.00]
+\definecolor[cost] [r=0.50, g=0.50, b=0.50]
\usemodule[markdown]
\define[4]\markdownRendererLinkPrototype{\goto{#1}[url(#3)]}
@@ -64,7 +66,7 @@
\startdocument[
title=lua-widow-control,
author=Max Chernoff,
- version=2.2.2, %%version
+ version=3.0.0, %%version
github=https://github.com/gucci-on-fleek/lua-widow-control,
ctan=https://www.ctan.org/pkg/lua-widow-control,
]
@@ -79,7 +81,7 @@ Ensure that your \TeX~Live/Mik\TeX{} distribution is up-to-date. Then, \LaTeX{}
\section{Preliminaries}
-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{TUGboat} article\cite[article] distributed with this manual (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)]). 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].
+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].
\section[sec:motivation]{Motivation}
@@ -295,6 +297,10 @@ Please note that when running in legacy mode, you cannot use the key--value inte
\subsection{\OpTeX{}}
\Lwc/ works with any version of \OpTeX{} and has no dependencies.
+\subsection{LuaMeta\TeX{}}
+
+\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.
+
\section[sec:usage]{Loading the Package}
@@ -449,6 +455,10 @@ only if it fails will \lwc/ come in and lengthen a paragraph. As a special
case, if you set the values to~0, both \TeX{} and \lwc/ will completely
ignore the \woo/.
+\Lwc/ will pick up on the values of \tex{widowpenalty}, \tex{clubpenalty}, and
+\tex{brokenpenalty} regardless of how you set them, so the use of these
+dedicated keys is entirely optional.
+
\subsection{\tex{nobreak} Behaviour}
When \lwc/ encounters an orphan, it removes it by moving the orphaned
@@ -521,11 +531,15 @@ sets the values to~5\,000 and 10\,000 respectively.
\Lwc/ uses a \q{cost function} $C$ that is initially defined as \startformula
C = \frac{d}{\sqrt{l}}
-\stopformula where $d$ is the total demerits of the paragraph, and $l$ is the number of lines in the paragraph; however, advanced users may also set a custom cost function by redefining the \inlineLUA{lwc.paragraph_cost(demerits, lines)} function.
+\stopformula where $d$ is the total demerits of the paragraph, and $l$ is the number of lines in the paragraph.
\subsection{Draft Mode}
-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 cost of each paragraph is shown in the right margin.
+\setuplwc[draft=start]
+\definecolor[lwc_expanded][s=0]
+\definecolor[lwc_failure][s=0]
+\definecolor[lwc_moved][s=0]
+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.
\startTABLE[setups=commandtable]
\NC \plainop/
@@ -539,11 +553,63 @@ You can use the draft mode to visualize how \lwc/ processes pages. Any \color[fa
\NC\NR
\stopTABLE
-Advanced users may also customize the colours used by modifying the \inlineLUA{lwc.colours} table. The table keys are currently \type{expanded}, \type{failure}, and \type{moved}. The table values are \acronym{RGB} 3-tuples, where each element is a float between zero and one.
+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.
+
+You can also show or hide the costs and colours independently of each other.
+
+\startTABLE[setups=commandtable]
+ \NC \plainop/
+ \NC\inlineTEX{\lwcshowcosts 1}
+ \NC\NR\NC\NC\inlineTEX{\lwcshowcolours 1}
+ \NC\NR
+ \NC \LaTeX{}
+ \NC\inlineTEX{\lwcsetup{showcosts = true}}
+ \NC\NR\NC\NC\inlineTEX{\lwcsetup{showcolours = true}}
+ \NC\NR
+ \NC \ConTeXt{}
+ \NC\inlineTEX{\setuplwc[showcosts = start]}
+ \NC\NR\NC\NC\inlineTEX{\setuplwc[showcolours = start]}
+ \NC\NR
+\stopTABLE
+
+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:
+
+\startTEX
+\newbox\tempshipoutbox
+\output={
+ \setbox\tempshipoutbox=\vbox{
+ \makeheadline
+ \pagebody
+ \makefootline
+ }
+ \lwcpreshipout\the\tempshipoutbox
+ \shipout\box\tempshipoutbox
+ \advancepageno
+}
+\stopTEX
+
+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.
+\subsection{Draft Offset}
+
+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:
+
+\startTABLE[setups=commandtable]
+ \NC \plainop/
+ \NC\inlineTEX{\lwcdraftoffset = $\meta{dimension}$}
+ \NC\NR
+ \NC \LaTeX{}
+ \NC\inlineTEX{\lwcsetup{draftoffset = $\meta{dimension}$}}
+ \NC\NR
+ \NC \ConTeXt{}
+ \NC\inlineTEX{\setuplwc[draftoffset = $\meta{dimension}$]}
+ \NC\NR
+\stopTABLE
+
+\setuplwc[draft=stop]
\subsection{Debug Mode}
-\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!
+\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!
\startTABLE[setups=commandtable]
\NC \plainop/
@@ -567,11 +633,13 @@ presets that you can use to set multiple options at once. These presets are
a good starting point for most documents, and you can always manually
override individual options.
-Currently, these presets are \LaTeX{}-only.
+Presets are available only with \LaTeX{} and \ConTeXt{}.
\startTABLE[setups=commandtable]
\NC \LaTeX{}
\NC\inlineTEX{\lwcsetup{$\meta{preset}$}}\NC\NR
+ \NC \ConTeXt{}
+ \NC\inlineTEX{\setuplwc[$\meta{preset}$]}\NC\NR
\stopTABLE
\subsection{\type{default}}
@@ -627,11 +695,51 @@ still requires a little manual intervention.
\stopTABLE
\stopplacetable
+\section{Lua Interface}
+
+\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.
+
+\subsection{Costs}
+
+\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:
+
+\startLUA
+--- The "cost function" to use.
+---
+--- @param demerits number The demerits of the broken paragraph
+--- @param lines number The number of lines in the broken paragraph
+--- @param nat_demerits number The demerits of the naturally-broken paragraph
+--- @param nat_lines number The number of lines in the naturally-broken paragraph
+--- @param head node The head of the broken paragraph
+--- @return number cost The cost of the broken paragraph
+function lwc.paragraph_cost(demerits, lines, nat_demerits, nat_lines, head)
+ return demerits / math.sqrt(lines)
+end
+\stopLUA
+
+\subsection{Activation}
+
+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:
+
+\startLUA
+--- Determines if we should "activate" lwc for the current page/column.
+---
+--- @param penalty number The \outputpenalty for the current page/column
+--- @param paragraphs table<table<string, node|number>> The `paragraphs` table
+--- @param head node The head of the current page/column
+--- @return boolean activate True if lwc should move the last line on this page
+function lwc.should_remove_widows(penalty, paragraphs, head)
+ return is_matching_penalty(penalty)
+end
+\stopLUA
+
+By setting a custom activation and cost function, you can transform \lwc/ from a \wao/ remover into a custom layout customization tool.
+
\section{Compatibility}
The \lwc/ implementation is almost entirely in Lua, with only a minimal
\TeX{} footprint. It
-doesn't modify the output routine, inserts\slash floats, \tex{everypar}, and
+doesn't modify the output routine, \tex{everypar}, and
it doesn't insert any whatsits. This means that it should be compatible with
nearly any \TeX{} package, class, and format. Most changes that \lwc/ makes
are not observable on the \TeX{} side.
@@ -644,15 +752,32 @@ does not matter for Plain~\TeX{} or \LaTeX{}, where even most Lua-based
packages don't depend on the node list structure; nevertheless, there are
a few issues with \ConTeXt{}.
-Simple \ConTeXt{} documents tend to be fine, but many advanced
-\ConTeXt{} features rely heavily on Lua and can thus be disturbed by
-\lwc/. This is not a huge issue---the \lwc/ manual is
-written in \ConTeXt{}---but \lwc/ is inevitably more reliable
-with Plain \TeX{} and \LaTeX{} than with \ConTeXt{}.
+However, on the Lua side, \lwc/ modifies much of a page's internal
+structure. This should not affect any \TeX{} code; however, it may surprise
+Lua code that modifies or depends on the page's low-level structure. This
+does not affect Plain~\TeX{} or \LaTeX{} where even most Lua-based packages
+don't depend on the node list structure. \ConTeXt{} \emph{does} depend on
+this internal node structure; however, I have carefully tested the package
+to ensure that this causes no issues.
Finally, keep in mind that adding \lwc/ to a document will almost certainly
change its page break locations.
+\subsection{Formats}
+
+\Lwc/ runs on all known Lua\TeX{}-based formats: Plain~Lua\TeX{},
+Lua\LaTeX{}, \ConTeXt{} Mk\acronym{IV}, and~\OpTeX{}. Unless otherwise
+documented, all features should work equally well in all formats.
+
+\Lwc/ is also fully-compatible with the \LuaMeta\TeX{}-based formats:
+\ConTeXt{} Mk\acronym{XL}\slash\acronym{LMTX}, \LuaMeta\LaTeX{}, and
+\LuaMeta{}Plain. \ConTeXt{} Mk\acronym{XL} works equally
+well as \ConTeXt{} Mk\acronym{IV} and Lua\LaTeX{}; however, \LuaMeta\LaTeX{}
+and \LuaMeta{}Plain support is still quite early. All features should work,
+although there are still a few minor bugs.
+
+All told, \lwc/ supports 7 different format\slash{}engine combinations.
+
\subsection{Columns}
Since \TeX{} and the formats implement column breaking and page
@@ -712,7 +837,7 @@ but instead of using ties or \tex{hbox}es, it uses the
parameter. When lengthening a paragraph
(and only when lengthening a paragraph---remember, \lwc/ doesn't
interfere with \TeX{}'s output unless it detects a \woo/), \lwc/ sets
-\tex{parfillskip} to \type{0pt plus 0.8\hsize}.
+\tex{parfillskip} to \type{0.75\hsize plus 0.05\hsize minus 0.75\hsize}.
This normally makes the last line of a paragraph be at least
20\% of the overall paragraph's width, thus preventing
ultra-short~lines.
@@ -746,10 +871,6 @@ ultra-short~lines.
however, this is both \acronym{NP}-complete\cite[plass] and
impossible to solve in a single pass. Very rarely would such a
system remove widows or orphans that \lwc/ cannot.
-
- \item \Lwc/ does not move footnotes in \mkxl/ due to limitations with the \LuaMetaTeX{} engine. \githubissue{38}
-
- \item Draft mode does not colour any text in \mkxl/. Draft mode will not colour the first line of a page in \mkiv/ and \OpTeX{}. \githubissue{39}
\stopitemize
\section{Contributions}
diff --git a/Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj-figure.ctx b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj-figure.ctx
new file mode 100644
index 00000000000..9ab907dea20
--- /dev/null
+++ b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj-figure.ctx
@@ -0,0 +1,161 @@
+% lua-widow-control
+% https://github.com/gucci-on-fleek/lua-widow-control
+% SPDX-License-Identifier: MPL-2.0+ OR CC-BY-SA-4.0+
+% SPDX-FileCopyrightText: 2022 Max Chernoff
+
+\startbuffer[demo-text]
+ \definepapersize[smallpaper][
+ width=6cm,
+ height=8.3cm
+ ]
+ \setuppapersize[smallpaper]
+
+ \def\lwc/{\sans{lua-\allowbreak widow-\allowbreak control}}
+ \def\Lwc/{\sans{lua-\allowbreak widow-\allowbreak control}}
+
+ \setupbodyfont[9pt]
+ \setupindenting[yes, 2em]
+ \setupalign[tolerant]
+
+ \definecolor[midlightgray][s=0.75]
+ \definepalet[layout][grid=midlightgray]
+ \showgrid[nonumber, none, lines]
+
+ \definefontfeature[default][default][expansion=quality,protrusion=quality]
+
+ \usetypescript[modern-base]
+ \setupbodyfont[reset,modern]
+
+ \setupalign[hz,hanging,tolerant]
+
+ \setuplanguage[en][spacing=packed]
+
+ \setuplayout[
+ topspace=0.1cm,
+ backspace=0.1cm,
+ width=middle,
+ height=\dimexpr 21\baselineskip + 1.1pt,
+ header=0pt,
+ footer=0pt,
+ ]
+
+ \starttext
+ % This text has been carefully adjusted to produce a good demonstration. Be careful before modifying it.
+ \Lwc/ can remove most widows and orphans from a document, \emph{without}
+ stretching any glue or shortening any pages.
+
+ \ifx\uselooseness\undefined\else\uselooseness\fi It does so by automatically lengthening a paragraph on a page where a
+ widow or orphan would otherwise occur. While \TeX{} breaks paragraphs
+ into their natural length, \lwc/ is breaking the paragraph 1~line
+ longer than its natural length. \TeX{}'s paragraph is output to the
+ page, but \lwc/'s paragraph is just stored for later. When a widow or
+ orphan occurs, \lwc/ can take over. It selects the previously-saved
+ paragraph with the least badness; then, it replaces \TeX{}'s paragraph
+ with its saved paragraph. This increases the text block height of the
+ page by 1~line.
+
+ Now, the last line of the current page can be pushed to the top of the
+ next page. This removes the widow or the orphan without creating any
+ additional work.
+ \stoptext
+ \stoptext
+\stopbuffer
+\savebuffer[list=demo-text]
+
+\startbuffer[shorten]
+ \parskip=0pt
+ \input lwc-zpravodaj-figure-demo-text.tmp
+\stopbuffer
+
+\startbuffer[shorten-code]
+ \parskip=0pt
+ \clubpenalty=10000
+ \widowpenalty=10000
+\stopbuffer
+
+\startbuffer[stretch]
+ \parskip=0pt plus 1fill
+ \input lwc-zpravodaj-figure-demo-text.tmp
+\stopbuffer
+
+\startbuffer[stretch-code]
+ \parskip=0pt plus 1fill
+ \clubpenalty=10000
+ \widowpenalty=10000
+\stopbuffer
+
+\startbuffer[ignore]
+ \startsetups[*default]
+ \clubpenalty=0
+ \widowpenalty=0
+ \displaywidowpenalty=0
+ \interlinepenalty=0
+ \brokenpenalty=0
+ \stopsetups
+
+ \setups[*default]
+
+ \input lwc-zpravodaj-figure-demo-text.tmp
+\stopbuffer
+
+\startbuffer[ignore-code]
+ \parskip=0pt
+ \clubpenalty=0
+ \widowpenalty=0
+\stopbuffer
+
+\startbuffer[lwc]
+ \def\uselooseness{\looseness=1}
+ \input lwc-zpravodaj-figure-demo-text.tmp
+\stopbuffer
+
+% We're pretending that we're using LaTeX for the demo since that's what
+% 99% of users will be using.
+\startbuffer[lwc-code]
+ \usepackage
+ {lua-widow-control}
+\stopbuffer
+
+\setupbodyfont[9pt]
+
+\setupbackend[format=PDF/A-1b:2005] % Force PDF version <1.5
+
+\startTEXpage[
+ align=normal,
+ width=492.5733pt,
+ offset=0pt,
+]
+ \veryraggedcenter
+ \setupTABLE[row][1][style=\bfa, align=middle, offset=0pt]
+ % \setupTABLE[row][4][toffset=2ex]
+ \setupTABLE[frame=off, distance=0em]
+ \startTABLE
+ \NC Ignore
+ \NC Shorten
+ \NC Stretch
+ \NC \sans{lua-widow-control}
+ \NC\NR
+
+ \NC \typesetbuffer[ignore][frame=on, page=1, scale=700]
+ \NC \typesetbuffer[shorten][frame=on, page=1, scale=700]
+ \NC \typesetbuffer[stretch][frame=on, page=1, scale=700]
+ \NC \typesetbuffer[lwc][frame=on, page=1, scale=700]
+ \NC\NR
+
+ \NC \clip[height=2cm]{\typesetbuffer[ignore]
+ [frame=on, page=2, scale=700]}
+ \NC \clip[height=2cm]{\typesetbuffer[shorten]
+ [frame=on, page=2, scale=700]}
+ \NC \clip[height=2cm]{\typesetbuffer[stretch]
+ [frame=on, page=2, scale=700]}
+ \NC \clip[height=2cm]{\typesetbuffer[lwc]
+ [frame=on, page=2, scale=700]}
+ \NC\NR
+
+ \NC \typebuffer[ignore-code]
+ \NC \typebuffer[shorten-code]
+ \NC \typebuffer[stretch-code]
+ \NC \typebuffer[lwc-code]
+ \NC\NR
+ \stopTABLE
+\stopTEXpage
diff --git a/Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj.bib b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj.bib
new file mode 100644
index 00000000000..6bbe904c0da
--- /dev/null
+++ b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj.bib
@@ -0,0 +1,237 @@
+% lua-widow-control
+% https://github.com/gucci-on-fleek/lua-widow-control
+% SPDX-License-Identifier: MPL-2.0+ OR CC-BY-SA-4.0+
+% SPDX-FileCopyrightText: 2022 Max Chernoff
+
+@book{texbook,
+ author = {Donald E. Knuth},
+ title = {The \TeX{}book},
+ publisher = {Addison--Wesley},
+ year = {2021},
+}
+
+@online{widows-and-orphans,
+ author = {Frank Mittelbach},
+ year = {2021},
+ month = {03},
+ title = {The \textsf{widows-and-orphans} package},
+ url = {https://ctan.org/pkg/widows-and-orphans},
+ urldate = {2022-11-08},
+}
+
+@online{widow-assist,
+ author = {jeremie},
+ title = {Paragraph callback to help with widows\slash orphans hand tuning},
+ url = {https://tex.stackexchange.com/q/372062},
+ year = {2017},
+ month = {07},
+ urldate = {2022-11-08},
+}
+
+@book{elements,
+ title = {The Elements of Typographic Style},
+ author = {Bringhurst, R.},
+ edition = {3rd},
+ publisher = {Hartley \& Marks},
+ year = {2004},
+}
+
+@online{oed-club,
+ title={club, n.},
+ journal={OED Online},
+ publisher={Oxford University Press},
+ month={09},
+ year={2021},
+ url={https://www.oed.com/view/Entry/34788},
+ author={{Oxford English Dictionary}},
+ urldate = {2022-11-08},
+}
+
+@online{oed-line,
+ title={line at end of paragraph},
+ journal={OED Online},
+ publisher={Oxford University Press},
+ month={12},
+ year={2021},
+ url={https://www.oed.com/view/th/class/195380},
+ author={{Oxford English Dictionary}},
+ urldate = {2022-11-08},
+}
+
+@online{oed-widow,
+ title={widow, n.},
+ journal={OED Online},
+ publisher={Oxford University Press},
+ month={12},
+ year={2021},
+ url={https://www.oed.com/view/Entry/228912},
+ author={{Oxford English Dictionary}},
+ urldate = {2022-11-08},
+}
+
+@article{widowhistory,
+ title={The Typographical Widow},
+ subtitle={Who is she? What is she?},
+ author={Karl Brown},
+ journal={Bulletin of the {New York} Public Library},
+ publisher={The {New York} Public Library},
+ location={New York},
+ volume={52},
+ number={1},
+ year={1948},
+ month={01},
+ pages={3--25},
+ url={https://hdl.handle.net/2027/uc1.b3310084}
+}
+
+@article{widowhistory2,
+ title={The Typographical Widow: Encore},
+ subtitle={Encore},
+ author={Karl Brown},
+ journal={Bulletin of the {New York} Public Library},
+ publisher={The {New York} Public Library},
+ location={New York},
+ volume={52},
+ number={9},
+ year={1948},
+ month={09},
+ pages={458-466},
+ urlnewline=1,
+ url={https://hdl.handle.net/2027/uc1.b3310084}
+}
+
+@book{old,
+ title={Mechanick exercises},
+ subtitle={The doctrine of handy-works applied to the art of printing},
+ volume={2},
+ author={Moxon, Joseph},
+ year={1683},
+ location={London},
+ pages={394},
+ url={https://archive.org/details/mechanickexercis00moxo_0}
+}
+
+@article{global,
+ url = {https://doi.org/10.1111/coin.12165},
+ year = {2018},
+ month = {03},
+ publisher = {Wiley},
+ volume = {35},
+ number = {2},
+ pages = {242--284},
+ author = {Frank Mittelbach},
+ title = {A general framework for globally optimized pagination},
+ journal = {Computational Intelligence}
+}
+
+@book{backwards1,
+ title={The Layout Book},
+ author={Ambrose, G. and Harris, P.},
+ isbn={9782940373536},
+ series={Advanced Level Series},
+ year={2007},
+ publisher={Bloomsbury Academic}
+}
+
+@book{backwards2,
+ title={Typography Essentials Revised and Updated},
+ subtitle={100 Design Principles for Working with Type},
+ author={Saltz, I.},
+ isbn={9781631596483},
+ year={2019},
+ publisher={Rockport Publishers}
+}
+
+@book{backwards3,
+ title={Advanced Typography: From Knowledge to Mastery},
+ author={Hunt, R.},
+ isbn={9781350055926},
+ lccn={2020024110},
+ year={2020},
+ publisher={Bloomsbury Publishing}
+}
+
+@article{gutenberg,
+ title={All Books (sorted by popularity)},
+ url={https://www.gutenberg.org/ebooks/search/?sort_order=downloads},
+ year={2022},
+ month={03},
+ journal={Project Gutenberg}
+}
+
+@online{etex,
+ title={The {\eTeX} manual},
+ author={{The \NTS{} Team}},
+ url={https://ctan.org/pkg/etex},
+ year={1998},
+ % month={02},
+ urldate = {2022-11-08},
+}
+
+@phdthesis{plass,
+ title={Optimal pagination techniques for automatic typesetting systems},
+ author={Plass, Michael Frederick},
+ year={1981},
+ school={Stanford University},
+ urlnewline=1,
+ url={https://tug.org/docs/plass/plass-thesis.pdf}
+}
+
+@article{tb133chernoff-widows,
+ title={Automatically removing widows and orphans with
+ \texttt{lua-widow-control}},
+ author={Chernoff, Max},
+ journal={TUGboat},
+ volume={43},
+ number={1},
+ pages={28--39},
+ year={2022},
+ month={05},
+ DOI={10.47397/tb/43-1/tb133chernoff-widows},
+}
+
+@online{luametalatex,
+ year={2022},
+ month={10},
+ url={https://github.com/zauguin/luametalatex},
+ author={Marcel Krüger},
+ title={\texttt{luametalatex}},
+ urldate = {2022-11-08},
+}
+
+@Book{naruby,
+ author = "Petr Ol{\v{s}}{\'a}k",
+ title = "{\TeX}book naruby. [{\TeX}book inside out]",
+ publisher = "Konvoj",
+ address = "Brno, Czech Republic",
+ pages = "467",
+ year = "1997",
+ ISBN = "80-85615-64-9",
+ ISBN-13 = "978-80-85615-64-7",
+ bibsource = "http://www.math.utah.edu/pub/tex/bib/texbook3.bib",
+ URL = "https://petr.olsak.net/ftp/olsak/tbn/tbn.pdf",
+}
+
+@article{mittelbach,
+ author = "Frank Mittelbach",
+ title = "Managing forlorn paragraph lines (a.k.a.~widows and
+ orphans) in {\LaTeX}",
+ journal = "TUGboat",
+ volume = "39",
+ number = "3",
+ pages = "246--251",
+ year = "2018",
+ ISSN = "0896-3207",
+ ISSN-L = "0896-3207",
+ URL = "https://tug.org/TUGboat/tb39-3/tb123mitt-widows.pdf",
+ issue = "123",
+}
+
+@PHDTHESIS{thanh,
+ author = {Thành, Hàn Thế},
+ title = {Micro-typographic extensions to the \TeX{} typesetting system},
+ date = {2001},
+ institution = {The Faculty of Informatics, Masaryk University},
+ location = {Brno},
+ url = {http://www.pragma-ade.nl/pdftex/thesis.pdf},
+}
diff --git a/Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj.ltx b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj.ltx
new file mode 100644
index 00000000000..1a00292e2f9
--- /dev/null
+++ b/Master/texmf-dist/source/luatex/lua-widow-control/lwc-zpravodaj.ltx
@@ -0,0 +1,1720 @@
+% lua-widow-control
+% https://github.com/gucci-on-fleek/lua-widow-control
+% SPDX-License-Identifier: MPL-2.0+ OR CC-BY-SA-4.0+
+% SPDX-FileCopyrightText: 2022 Max Chernoff
+
+% This is the LaTeX source for the following article:
+% @article{zpravodaj-lwc,
+% title={Automatically Removing Widows and Orphans with
+% \textsf{lua-widow-control}},
+% author={Chernoff, Max},
+% journal={Zpravodaj Československého sdružení uživatelů TeXu},
+% number={1--4},
+% pages={49--76},
+% year={2022},
+% month=nov,
+% DOI={10.5300/2022-1-4/49},
+% }
+% Please refer to the PDF on cstug.cz for the authoritative version.
+
+% Compiling:
+% context lwc-zpravodaj-figure.ctx
+% lualatex lwc-zpravodaj.ltx
+% biber lwc-zpravodaj
+% lualatex lwc-zpravodaj.ltx
+% lualatex lwc-zpravodaj.ltx
+
+\RequirePackage{luatex85}
+\PassOptionsToPackage{main=english}{babel}
+\PassOptionsToPackage{shorthands=off}{babel}
+
+\makeatletter
+\disable@package@load{fontenc}
+\makeatother
+
+\let\oldlooseness=\looseness
+
+\documentclass{csbulletin}
+
+% Hyperlinks
+\usepackage[implicit=false, hidelinks]{hyperref}
+\setcounter{secnumdepth}{3}
+
+% Load lwc
+\usepackage[balanced, draftoffset=\oddsidemargin + 1in + 10pt]{lua-widow-control}
+
+% Table Stuff
+\usepackage{tabularx}
+\usepackage{longtable}
+\usepackage{hhline}
+\usepackage{booktabs}
+\usepackage{graphicx}
+\AddToHook{env/tabularx/before}{\nobreak\smallskip\noindent}
+\AddToHook{env/tabularx/after}{\smallskip\pagebreak[1]}
+\AddToHook{env/tabular/before}{\nobreak\smallskip\noindent}
+\AddToHook{env/tabular/after}{\smallskip\pagebreak[1]}
+\AddToHook{env/longtable/before}{\nobreak\smallskip\noindent}
+\AddToHook{env/longtable/after}{\smallskip\pagebreak[1]}
+\setlength{\LTleft}{0pt}
+\renewcommand{\arraystretch}{1.15}
+
+% For the sideways figure
+\usepackage{rotating}
+
+% Rotate the sideways figure in the PDF viewer too
+\AddToHook{env/sidewaysfigure/end}{%
+ \latelua{pdf.setpageattributes('/Rotate 90')}%
+ \AddToHookNext{shipout/after}{%
+ \AddToHookNext{shipout/after}{%
+ \directlua{pdf.setpageattributes('/Rotate 0')}%
+ }%
+ }%
+}%
+% Let the macro names in section headings be in boldface
+\usepackage{lmodern}
+\AtBeginDocument{\def\cs#1{\texttt{\textbackslash#1}}}
+
+\directlua{
+ local percent = string.char(37)
+ luatexbase.add_to_callback("process_input_buffer", function(line) return line:gsub(percent .. "-" .. percent .. "-" ..percent .. "-", "\\kern0.1em---\\kern0.1em") end, "emdash")
+} % Add a little more kerning to em-dashes
+
+\makeatletter
+\DeclareRobustCommand{\eTeX}{%
+ \ifx\f@series\bfseries@rm%
+ \ensuremath{\boldsymbol{\varepsilon}}\mbox{-}\kern-.125em\TeX%
+ \else%
+ \ensuremath{\varepsilon}\mbox{-}\kern-.125em\TeX%
+ \fi%
+}
+\let\goodeTeX=\eTeX
+\makeatother
+
+% Other commands
+\usepackage{hologo}
+\def\ConTeXt{\Hologo{ConTeXt}}
+
+\usepackage{mathtools}
+
+% Abbreviations Most of these are just typewriter commands with `\allowbreak`s
+% added.
+\def\lwc/{\textsf{lua-\allowbreak widow-\allowbreak control}}
+\def\Lwc/{\textsf{lua-\allowbreak widow-\allowbreak control}}
+\def\estretch/{%
+ \texorpdfstring{\cs{emergency}\-\mbox{\ttfamily stretch}}{\textbackslash{}emergencystretch}%
+ }
+\def\openalty/{\cs{output}\-\mbox{\ttfamily penalty}}
+\def\waos/{widows and orphans}
+\def\wao/{widow and orphan}
+\def\woo/{widow or orphan}
+\def\woos/{widows or orphans}
+\def\latexuse/{%
+ \cs{use\-package\{lua-\allowbreak widow-\allowbreak control\}}
+}
+\def\lsness/{\texorpdfstring{%
+ \cs{loose}\-\mbox{\ttfamily ness}}{\textbackslash{}looseness}%
+}
+\def\plainop/{Plain~\TeX\slash\OpTeX{}}
+\newcommand{\LuaMeta}{Lua\-Meta\-}
+\newcommand{\q}[1]{\texorpdfstring{``#1''}{“#1”}}
+
+\def\inlineurl[#1]#2{\href{https://#1}{#2}\footnote{\raggedright\href{https://#1}{\ttfamily #1}}}
+
+\def\longs/{\char"017F}
+\def\endofline#1{\unskip\nobreak\hskip\fontdimen2\font plus 1fill\hbox{#1}}
+
+% TUGboat compatibility
+\def\tubsentencespace{\spacefactor=3000{}\space\ignorespaces}
+\DeclareRobustCommand\OpTeX{Op\kern-.05em\TeX}
+\DeclareRobustCommand\acro[1]{\textsc{\MakeLowercase{#1}}}
+\DeclareRobustCommand\meta[1]{%
+ \ensuremath{\langle}%
+ \ifmmode \expandafter\mbox \fi%
+ {\it #1\/}%
+ \ensuremath{\rangle}%
+}
+
+% Bibliography
+\usepackage[
+ backend=biber,
+ style=iso-authoryear,
+ sortlocale=en,
+ autolang=other,
+ bibencoding=UTF8,
+ mincitenames=2,
+ maxcitenames=2,
+]{biblatex}
+
+% Remove the "also" from "available also from"
+\DefineBibliographyStrings{english}{
+ urlalso = {available from},
+}
+
+\protected\def\titlecite#1{\citetitle{#1}~\cite{#1}}
+
+\let\cite=\parencite
+
+\addbibresource{lwc-zpravodaj.bib}
+\addbibresource{tugboat.bib}
+
+% Figures
+\makeatletter
+\renewcommand*{\fps@figure}{tb}
+\renewcommand*{\fps@table}{tb}
+\g@addto@macro\@floatboxreset\centering
+\makeatother
+
+% pgfplots
+\usepackage{pgfplots}
+\usepackage{pgfplotstable}
+\usetikzlibrary{patterns}
+
+\pgfkeys{
+ /pgf/number format/.cd,
+ sci generic={%
+ mantissa sep={\times},
+ exponent={10^{##1}}
+ },
+ 1000 sep={\,},
+}
+
+\pgfplotsset{
+ compat=1.18,
+ lua backend=true,
+ unbounded coords=discard,
+ filter discard warning=false,
+}
+
+
+\pgfplotstableread{tb133chernoff-widows-plot.dat}{\plotdata}
+
+% Final adjustments
+\def\spoj{\discretionary{-}{-}{-}}
+
+\makeatletter
+\csbul@start@page {49}
+\def\doi{10.5300/2022-1-4/\thepage}
+\csbul@webtrue
+\csbul@colortrue
+\makeatother
+
+\begin{document}
+ \title{Automatically Removing Widows and Orphans\\ with \lwc/}
+ \EnglishTitle{Automatically Removing Widows and Orphans\\ with \lwc/}
+ \author{Max Chernoff}
+ \podpis{Max Chernoff, mseven at telus dot net}
+
+ \maketitle
+
+ \begin{abstract}
+ The \textsf{lua-widow-control} package, for
+ plain~Lua\TeX\slash{}Lua\LaTeX\slash{}\ConTeXt\slash{}\OpTeX{},
+ removes widows and orphans without any user intervention.
+ Using the power of Lua\TeX{}, it does so without stretching any vertical glue
+ or shortening any pages or columns. Instead, \textsf{lua-widow-control}
+ automatically lengthens a paragraph on a page or column where a widow or
+ orphan would otherwise occur.
+
+ To use the \textsf{lua-widow-control} package, all that most \LaTeX{} users
+ need do is place \verb|\usepackage{lua-widow-control}| in their preamble.
+ No further changes are required.
+ \end{abstract}
+ \keywords: Lua\TeX{}, widows, orphans
+
+ \hypersetup{bookmarksdepth=10}
+ \section{Motivation}
+
+ {\let\thefootnote\relax\footnotetext{First published in \textsl{TUGboat} \textbf{43}:1 \cite{tb133chernoff-widows}, pp.~28--39. Reprinted, with additions and corrections, with permission.}}
+
+ \TeX{} provides top-notch typesetting: even 40 years after its first
+ release, no other program produces higher quality mathematical
+ typesetting, and its paragraph\spoj breaking algorithm is still
+ state-of-the-art. However, its page breaking is not quite as sophisticated
+ as its paragraph breaking and thus suffers from some minor issues.
+ \oldlooseness=-1
+
+ Unmodified \TeX{} has only two familiar ways of dealing with \waos/: it can
+ either shorten a page by one line, or it can stretch vertical
+ whitespace. \TeX{} was designed for mathematical and scientific typesetting,
+ where a typical page has multiple section headings, tables, figures, and
+ equations. For this style of document, \TeX's default behaviour works quite
+ well, since the slight stretching of whitespace between the various document
+ elements is nearly imperceptible; however, for prose or other documents
+ composed almost entirely of paragraphs, there is little vertical whitespace
+ to stretch.
+
+ Since no ready-made, fully-automated solution to remove
+ \waos/ from all types of documents was available, I decided to
+ create \lwc/.
+ \oldlooseness=-1
+
+ \section{What are \waos/?}
+
+ \subsection{Widows}
+
+ A \q{widow} occurs when the majority of a paragraph is on one page
+ or column,
+ but the last line is on the following page or column. It not only looks
+ quite odd for a lone line to be at the start of the page, but it makes a
+ paragraph harder to read since the separation of a paragraph and
+ its last line disconnects the two, causing the reader to lose context for
+ the widowed line.
+
+ \subsection{Orphans}
+
+ An \q{orphan} occurs when the first line of a paragraph is at the end
+ of the page or column preceding the remainder of the paragraph. They are not
+ as distracting for the reader, but they are still not ideal.
+ Visually, \waos/ are about equally disruptive; however, orphans tend not to
+ decrease the legibility of a text as much as widows, so many authors choose
+ to ignore them.
+
+ See Figure~\ref{tab:widow} for a visual reference.
+
+ \begin{figure}
+ \def\firstpage#1{%
+ \parfillskip=0pt\relax
+ \spaceskip=0.2em plus 1fill\relax
+ \hskip 3em\relax
+ #1%
+ }
+
+ \def\lastpage#1{%
+ \parfillskip=3em plus 1fill\relax
+ \spaceskip=0.2em plus 1fil\relax
+ #1%
+ }
+
+ \renewcommand{\arraystretch}{1}
+ \renewcommand{\doublerulesep}{0.5em}
+ \begin{tabularx}{\linewidth}{|X|@{\hskip\doublerulesep}|X|}
+ \multicolumn1c{\bfseries Widow} &
+ \multicolumn1c{\bfseries Orphan}
+ \\\hhline{-||-}%
+ \firstpage{A widow is when a paragraph's last line is
+ placed on a different page}%
+ &\vskip4pt\leavevmode\firstpage{An orphan is when the first}%
+ \\ \hhline{-||-}
+ \lastpage{than where it begins.}
+ & \lastpage{line of a paragraph occurs on the
+ page before all the other lines.} \\ \hhline{-||-}
+ \end{tabularx}
+ \caption{The difference between \waos/. If we imagine that each box is a
+ different page, then this roughly simulates how \waos/ appear.\oldlooseness=-1}
+ \label{tab:widow}
+ \end{figure}
+
+ \subsection{Broken hyphens}
+
+ \q{Broken} hyphens occur whenever a page break occurs in a
+ hyphenated word. These are not related to \waos/; however,
+ breaking a word across two pages is at least as disruptive for the reader
+ as \waos/. \TeX{} identifies broken hyphens in the same ways as \waos/, so
+ \lwc/ treats broken hyphens in the same way.
+
+ \section{History and etymology}
+
+ The concept of \waos/ is nearly as old as printing itself. In \titlecite{old},
+ a printers manual from 1683, we have:
+ \begin{quote}
+ Nor do good \emph{Compo\longs/iters} account it good Workman\longs/hip
+ to begin a \emph{Page} with a \emph{Break-line}, unle\longs/s it be a
+ very \longs/hort \emph{Break}, and cannot be gotten in the foregoing
+ \emph{Page}\,; but if it be a long \emph{Break}, he will let it be the
+ \emph{Direction-line} of the fore-going \emph{Page}, and \emph{Set} his
+ \emph{Direction} at the end of it. \endofline{(p.~226)}
+ \end{quote}
+ However, the terms \q{widow} and \q{orphan} are much newer.
+
+ \subsection{Widows}
+
+ The earliest published source that I could find referencing \q{widows} in
+ typography is \textsl{Webster's New International Dictionary} from~1934.
+ However, no one---not even the editors of the
+ dictionary~\cite{widowhistory}---seems to know how it got there. Even then,
+ the definition is somewhat different than it is now:
+ \begin{quote}
+ widow, n.~c.~\emph{Print}\@. A short line or single word carried over
+ from the foot of one column or page to the head of a succeeding column
+ or page.
+ \endofline{\cite{widowhistory}}
+ \end{quote}
+ Contrast this with the modern definition:
+ \begin{quote}
+ The stub-ends left when paragraphs end on the first line of a page are called
+ widows. They have a past but not a future, and they look foreshortened and forlorn.
+ \endofline{\cite{elements}}
+ \end{quote}
+ which includes a single lone line of any length.
+
+ \subsection{Orphans}
+
+ The term \q{orphan} is even more confusing. Its initial usage seems to have
+ occurred some time after \q{widow}~\cite{widowhistory}, and it is given many
+ contradictory definitions. Most sources define an orphan as a first line at
+ the bottom of the page and a widow as the last line at the
+ top~\cite{elements, widowhistory, widowhistory2, Isambert:TB31-1-12,
+ texbook, widows-and-orphans, oed-line, oed-widow}; however, some sources
+ define these two terms as \emph{exact opposites} of each other, with a widow
+ as a first line at the bottom of the page and an orphan as the last
+ line!~\cite{backwards1, widowhistory, backwards3, oed-line,
+ backwards2}\tubsentencespace
+ This usage is plain wrong; nevertheless, it is sufficiently common that you
+ need to be careful when you see the terms \q{widow} and \q{orphan}.
+
+ Similarly to the term \q{widow}, \titlecite{elements} provides a succinct
+ definition of the term \q{club}, along with a helpful mnemonic:
+ \begin{quote}
+ Isolated lines created when paragraphs begin on the last line of a page
+ are known as orphans. They have no past, but they do have a future.
+ \endofline{\cite{elements}}
+ \end{quote}
+
+ \subsection{Clubs}
+
+ \textsl{The \TeX{}book} never refers to \q{orphans} as such; rather, it
+ refers to them as \q{clubs}. This term is remarkably rare: I could only find
+ a \emph{single} source published before \textsl{The \TeX{}book}---a
+ compilation article about the definition of \q{widow}---that mentions
+ a \q{club line}:
+ \begin{quotation}
+ \noindent
+ The Dictionary staff informs me that they have no example of the use of
+ the word widow in the typographical sense.~[\dots]
+
+ Mr.~Watson of the technical staff says that
+ the Edinburgh printing houses referred to it as a \q{clubline}.
+ \endofline{\cite[p.~4]{widowhistory}}
+ \end{quotation}\medskip
+ \begin{quotation}
+ \noindent
+ To my knowledge, a `widow', or `widow-line,' is a short line, forming
+ the end of a paragraph, which is carried over from the foot of a page or
+ column to the top of the succeeding one.~[\dots]
+
+ To my personal knowledge, in typographical parlance in Edinburgh,
+ Scotland, the `widow' is called a `club-line.'
+ \endofline{\cite[p.~23]{widowhistory}}
+ \end{quotation}
+
+ Both quotes above are from separate authors, and they each define a \q{club}
+ like we define \q{widow}, not an \q{orphan}. In addition, they both mention
+ that the term is only used in Scotland. Even the extensive
+ \acro{OED}---which lists 17~full definitions and
+ 103~subdefinitions for the noun \q{club}---doesn't recognize the
+ phrase.~\cite{oed-club}
+
+ I spent a few hours searching through Google Books and my university library
+ catalogue, but I could not find a single additional source. However, Don
+ Knuth---the creator of \TeX{}---read the original article
+ \cite{tb133chernoff-widows} and sent me this reply:
+ \oldlooseness=-1
+
+ \begin{quote}
+ I cannot remember where I found the term \q{club line}. Evidently
+ the books that I scoured in 1977 and 1978 had taught me only that an
+ isolated line, caused by breaking between pages in the midst of a
+ paragraph, was called a \q{widow}; hence \TeX78 had only
+ \q{\cs{chpar4}} to
+ change the \q{\texttt{widowpenalty}}. Sometime between then and \TeX82
+ I must
+ have come across what appeared to be an authoritative source that
+ distinguished between widows at the beginning of a paragraph and
+ orphans or club lines at the end. I may have felt that the term
+ \q{orphan} was somewhat pejorative, who knows?\footnotemark
+ \end{quote}
+
+ \footnotetext{
+ Note that this definition is somewhat mistaken. Widows are located
+ either at the \emph{end} of a paragraph, or the beginning of a
+ \emph{page or column}. Likewise, orphans\slash{}clubs appear at the
+ \emph{beginning} of a paragraph or at the end of a \emph{page or column}.
+ }
+
+ \noindent
+ So this (somewhat) resolves the question of where the term \q{club} came
+ from.
+
+ \section{Pagination in \TeX}
+
+ Let's move on to looking at how \TeX{} treats these \waos/.
+
+ \subsection{Algorithm}
+
+ It is tricky to understand how \lwc/ works if you aren't familiar with how
+ \TeX{} breaks pages and columns. For a full description, you should consult
+ Chapter~15 of~\titlecite{texbook} (\q{How \TeX{} Makes
+ Lines into Pages}); however, this goes into much more detail than most users
+ require, so here is a \emph{very} simplified summary of \TeX{}'s page
+ breaking algorithm:
+
+ \TeX{} fills the page with lines and other objects until the next object
+ will no longer fit. Once no more objects will fit, \TeX{} will align the
+ bottom of the last line with the bottom of the page by stretching any
+ available vertical spaces if (in \LaTeX) \cs{flushbottom} is set;
+ otherwise, it will
+ break the page and leave the bottom empty.
+
+ However, some objects have penalties attached. Penalties encourage or
+ discourage page breaks from occurring at specific places. For example,
+ \LaTeX{} sets a negative penalty before section headings to encourage a
+ page break there; conversely, it sets a positive penalty after section
+ headings to discourage breaking.
+
+ To reduce \waos/, \TeX{} sets weakly-positive penalties between the
+ first and second lines of a paragraph to prevent orphans, and between the
+ penultimate and final lines to prevent widows.
+
+ One important note: once \TeX{} begins breaking a page, it never goes
+ back to modify any content on the page. Page breaking is a localized
+ algorithm, without any backtracking.
+
+ \subsection{Behaviour}
+
+ Merely describing the algorithm doesn't allow us to intuitively
+ understand how
+ \TeX{} deals with \waos/.
+
+ Due to the penalties attached to \waos/, \TeX{} tries to avoid
+ them. Widows and orphans with small penalties attached---like
+ \LaTeX's default values of 150---are only lightly coupled to the rest
+ of the paragraph, while \waos/ with large penalties---values of
+ 10\,000 or more---are treated as infinitely bad and are thus
+ unbreakable. Intermediate values behave just as you would expect,
+ discouraging page breaks proportional to their value.
+
+ When \TeX{} goes to break a page, it tries to avoid breaking at a
+ location with a high penalty. How it does so depends on a few settings:
+
+ \subsubsection{\cs{flushbottom} and \cs{normalbottom}}
+
+ With the settings \cs{normalbottom} (Plain \TeX{}) or \cs{flushbottom}
+ (\LaTeX{}), \TeX{} is willing to stretch any glue on the page by an amount
+ roughly commensurate to the magnitude of the penalty: for small
+ \cs{clubpenalty} and \cs{widowpenalty} values, \TeX{} will only slightly
+ stretch the glue on the page before creating a \woo/; for very large
+ penalties, \TeX{} will stretch the glue by a near-infinite amount.
+ \oldlooseness=-1
+
+ This corresponds to the \q{Stretch} column in
+ Figure~\ref{fig:demo}. It is
+ the default behaviour of Plain~\TeX{}, and of the standard \LaTeX{} classes
+ when the \verb|twocolumn| option is given.
+
+ \subsubsection{\cs{raggedbottom}}
+
+ When \cs{raggedbottom} is set, \TeX{} won't stretch any glue. Instead, for
+ sufficiently high \cs{clubpenalty} and
+ \cs{widowpenalty} values, \TeX{} will shorten the page or column by
+ one~line in order to prevent the \woo/ from being created.
+
+ This corresponds to the \q{Shorten} column in Figure~\ref{fig:demo} and is
+ the default behaviour of the \LaTeX{} classes when the
+ \verb|twocolumn| option is not given.
+
+ \section{\lsness/}\label{sec:looseness} Before we can continue further, we
+ need to discuss one more \TeX{} command: \lsness/. The following is
+ excerpted from Chapter~14 of~\titlecite{texbook} (\q{How \TeX{} Breaks
+ Paragraphs into Lines}):
+
+ \begin{quotation}
+ \noindent
+ If you set \lsness/\verb|=1|, \TeX{} will try to make the current
+ paragraph one line longer than its optimum length, provided that
+ there is a way to choose such breakpoints without exceeding the
+ tolerance you have specified for the badnesses of individual lines.
+ Similarly, if you set \lsness/\verb|=2|, \TeX{} will try to make the
+ paragraph two lines longer; and \lsness/\verb|=-1| causes an attempt
+ to make it shorter.~[\dots]
+
+ For example, you can set \lsness/\verb|=1| if you want to avoid a
+ lonely \q{club line} or \q{widow line} on some page that does not
+ have sufficiently flexible glue, or if you want the total number of
+ lines in some two-column document to come out to be an even number.
+
+ It's usually best to choose a paragraph that is already pretty
+ \q{full}, i.e., one whose last line doesn't have much white space,
+ since such paragraphs can generally be loosened without much harm.
+ You might also want to insert a tie between the last two words of
+ that paragraph, so that the loosened version will not end with only
+ one \q{widow word} on the orphans line; this tie will cover your
+ tracks, so that people will find it hard to detect the fact that you
+ have tampered with the spacing. On the other hand, \TeX{} can take
+ almost any sufficiently long paragraph and stretch it a bit, without
+ substantial~harm.
+ \oldlooseness=1
+ \end{quotation}
+
+ The \wao/ removal strategy suggested in the second paragraph works quite
+ well; however, it requires manual editing each and every time a page
+ or paragraph is rewritten or repositioned.
+
+ \begin{sidewaysfigure}[p!]
+ \includegraphics{lwc-zpravodaj-figure}
+ \caption{A visual comparison of various automated widow-handling
+ techniques.
+ }\label{fig:demo}
+ \end{sidewaysfigure}
+
+ \section{Alternate removal strategies}
+
+ There have been a few previous attempts to improve upon \TeX's
+ previously discussed \wao/-handling abilities; however, none of these
+ have been able to automatically remove \waos/ without stretching any vertical glue
+ or shortening any pages.
+
+ The articles \titlecite{Isambert:TB31-1-12} and \titlecite{mittelbach} both
+ begin with comprehensive discussions of the methods of preventing \waos/.
+ They agree that \waos/ are bad and ought to be avoided; however, they
+ differ in their solutions. \textsl{Strategies}~proposes an output routine
+ that reduces the length of facing pages by one line when necessary to
+ remove \waos/, while \textsl{Managing}~proposes that the author manually
+ rewrites or adjusts \lsness/ when needed.
+
+ The post \titlecite{widow-assist} contains a file
+ \verb|widow-assist.lua| that
+ automatically detects which paragraphs can be safely shortened or
+ lengthened by one line. The \textsf{widows-and-orphans}
+ package~\cite{widows-and-orphans} alerts the author to
+ the pages that contain widows or orphans. Combined, these packages make
+ it simple for the author to quickly remove \waos/ by adjusting the
+ values of \lsness/; however, it still requires the author to make manual
+ source changes after each revision.
+
+ Another article suggests a fully-automated solution to remove
+ \waos/~\cite{global}. This would seem to offer a complete solution; however,
+ it requires multiple passes, an external tool, and has not yet been publicly
+ released.
+
+ \Lwc/ is essentially a combination of
+ \verb|widow-assist.lua|~\cite{widow-assist} and
+ \textsf{widows-and-orphans}~\cite{widows-and-orphans}, although its
+ implementation is independent of both: when the \openalty/
+ value indicates
+ that a \woo/ has occurred, Lua is used to find a stretchable paragraph. What
+ \lwc/ mainly adds on top of these packages is automation: it eliminates the
+ requirement for any manual adjustments or changes to your document's
+ source.
+
+ \section{Visual comparison}
+
+ Although \TeX{}'s page breaking algorithm is reasonably
+ straightforward, it can lead to
+ complex behaviour when \waos/ are involved. The usual
+ choices, when rewriting is not possible, are to ignore them,
+ stretch some glue, or shorten the
+ page. Figure~\ref{fig:demo} has a visual comparison of these
+ options, which we'll discuss in the following:
+
+ \subsection{Ignore}
+
+ As you can see, the last line of the page is on a separate page from the
+ rest of its paragraph, creating a widow. This is usually highly
+ distracting for the reader, so it is best avoided for the reasons previously
+ discussed.
+
+ \subsection{Shorten}
+
+ This page did not leave any widows, but it did shorten the previous page
+ by one line. Sometimes this is acceptable, but usually it looks bad because
+ pages will then have different text-block heights. This can make the pages
+ look quite uneven, especially when typesetting with columns or in a book
+ with~facing~pages.
+ \oldlooseness=1
+
+ \subsection{Stretch}
+
+ This page also has no widows and it has a flush bottom margin. However,
+ the space between each pair of paragraphs had to be stretched.
+
+ If this page had many equations, headings, and other elements with
+ natural space between them, the stretched out space would be much less
+ noticeable. \TeX{} was designed for mathematical typesetting, so it makes
+ sense that this is its default behaviour. However, in a page with mostly
+ text, these paragraph gaps look unsightly.
+
+ Also, this method is incompatible with grid typesetting, where
+ all vertical glue stretching must be quantised to the height of a line.
+
+ \subsection{\lwc/}
+
+ \Lwc/ has none of these issues: it eliminates the widows in a document
+ while keeping a flush bottom margin and constant paragraph spacing.
+
+ To do so, \lwc/ lengthened the second paragraph in Figure~\ref{fig:demo} by
+ one line. If you look closely, you can see that this stretched the
+ interword spaces. This stretching is noticeable when typesetting in a
+ narrow text block, but is mostly imperceptible with larger widths.
+
+ \Lwc/ automatically finds the \q{best} paragraph to stretch, so the
+ increase in interword spaces should almost always be minimal.
+
+ \section{Installation and standard usage}
+
+ The \lwc/ package was first released in
+ October~2021. It is available in the default installations of both
+ MiK\TeX{} and \TeX{}~Live, although you will need recent versions
+ of either.
+
+ You may also download \lwc/ manually from either
+ \inlineurl[ctan.org/pkg/lua-widow-control]{\acro{CTAN},}
+ the \inlineurl%
+ [modules.contextgarden.net/cgi-bin/module.cgi/action=view/id=127]%
+ {\ConTeXt{} Garden,} or \inlineurl%
+ [github.com/gucci-on-fleek/lua-widow-control/releases/latest/]%
+ {GitHub,} although it is best if you can install it through your
+ \TeX~distribution.
+
+ As its name may suggest, \lwc/ \textit{requires} Lua\TeX{} or
+ \LuaMeta\TeX{} regardless of the format used.
+ With that in mind, using \lwc/ is quite simple:
+
+ \begin{tabular}{rl}
+ Plain \TeX{} &
+ \cs{input lua-widow-control}\phantom{\tt]} \\
+ \OpTeX {} &
+ \cs{load[lua-widow-control]} \\
+ \LaTeX{} &
+ \latexuse/ \\
+ \ConTeXt{} &
+ \cs{usemodule[lua-widow-control]} \\
+ \end{tabular}
+
+ And that's usually enough. Most users won't need to do anything else since
+ \lwc/ comes preconfigured and ready-to-go.
+
+ \section{Options}
+
+ Nevertheless, \lwc/ does have a few options.
+
+ \Lwc/ tries very hard to have a \q{natural} user interface with each
+ format, so how you set an option heavily depends on how you are running
+ \lwc/. Also note that not every option is available in every format.
+
+ Some general guidelines:
+
+ \begin{tabularx}\linewidth{rX}
+ \plainop/ & Specially-named \cs{lwc}\meta{option} commands and
+ registers are provided for all options. \\[4pt]
+ \LaTeX{} & Options can be set either as package options or at
+ any point in the document with \cs{lwcsetup}. \\[4pt]
+ \ConTeXt{} & Always use \cs{setuplwc}.
+ \end{tabularx}
+
+ \subsection{Disabling}
+
+ You may want to disable \lwc/ for certain portions of your
+ document. You can do so with the following commands:
+
+ \begin{tabular}{rl}
+ \plainop/ &
+ \cs{lwcdisable} \\
+ \LaTeX{} &
+ \cs{lwcsetup\{disable\}} \\
+ \ConTeXt{} &
+ \cs{setuplwc[state=stop]}\hphantom{\texttt{a}} \\
+ \end{tabular}
+
+ This prevents \lwc/ from stretching any paragraphs that follow. If a page
+ has earlier paragraphs where \lwc/ was still enabled and a \woo/ is
+ detected, \lwc/ will still attempt to remove the \woo/.
+
+ \subsection{Enabling}
+
+ \Lwc/ is enabled as soon as the package is loaded. If you have previously
+ disabled it, you will need to re-enable it to save new paragraphs.
+
+ \begin{tabular}{rl}
+ \plainop/ &
+ \cs{lwcenable} \\
+ \LaTeX{} &
+ \cs{lwcsetup\{enable\}} \\
+ \ConTeXt{} &
+ \cs{setuplwc[state=start]} \\
+ \end{tabular}
+
+ \subsection{Automatically disabling}
+
+ You may want to disable \lwc/ for certain commands where
+ stretching is undesirable such as section headings. Of course, manually
+ disabling and
+ then enabling \lwc/ multiple times
+ throughout a document would quickly become tedious, so \lwc/ provides
+ some options to do this automatically for you.
+
+ \Lwc/ automatically patches the default \LaTeX{}, \ConTeXt{},
+ Plain \TeX{}, \OpTeX{}, \textsf{\mbox{memoir}},
+ \textsf{KOMA-Script}, and \textsf{titlesec} section commands, so you don't
+ need to patch these. Any others, though, you'll need to patch yourself.
+
+ \begin{tabular}{rl}
+ \plainop/ & \cs{lwcdisablecmd\{\meta{\texttt{\char`\\}macro}\}} \\[4pt]
+ \LaTeX{} & \cs{lwcsetup\{disablecmds=\{\texttt{\meta{csnameone},%
+ \meta{csnametwo}\}\}}} \\[4pt]
+ \ConTeXt{} &
+ \cs{prependtoks\textbackslash{}lwc@patch@pre}\cs{to\textbackslash{}everybefore\meta{hook}} \\
+ & \cs{prependtoks\textbackslash{}lwc@patch@post}\cs{to\textbackslash{}everyafter\meta{hook}} \\
+ \end{tabular}
+
+ \subsection{\estretch/}
+
+ \Lwc/ defaults to an \estretch/ value of 3~em for stretched paragraphs,
+ but you can configure this.
+
+ \Lwc/ will only use the \estretch/ when it cannot extend a paragraph in
+ any other way, so it is fairly safe to set this to a large value. \TeX{}
+ accumulates badness when \estretch/ is used~\cite{Knuth:TB10-3-325}, so
+ it's pretty rare that a paragraph that requires any \estretch/ will
+ actually be used on the page.
+
+ \begin{tabular}{rr@=l}
+ \plainop/ & \cs{lwcemergencystretch} & \meta{dimension} \\
+ \LaTeX{} &
+ \cs{lwcsetup\{emergencystretch} & \meta{dimension}\texttt{\}} \\
+ \ConTeXt{} &
+ \cs{setuplwc[emergencystretch} & \meta{dimension}\texttt{]} \\
+ \end{tabular}
+
+ \subsection{Penalties}
+
+ You can also manually adjust the penalties that \TeX{} assigns to \waos/.
+ Usually, the defaults are fine, but there are a few circumstances where you
+ may want to change them.
+
+ \begin{longtable}{rr@{\texttt{=\meta{integer}}}l}
+ \plainop/ & \cs{widowpenalty} \\*
+ & \cs{clubpenalty} \\*
+ & \cs{brokenpenalty} \\[4pt]
+ \LaTeX{} &
+ \cs{lwcsetup\{ widowpenalty} & \texttt{\}} \\*
+ & \cs{lwcsetup\{orphanpenalty} & \texttt{\}} \\*
+ & \cs{lwcsetup\{brokenpenalty} & \texttt{\}} \\[4pt]
+ \leavevmode\hbox{\ConTeXt{}} &
+ \cs{setuplwc[ widowpenalty} & \texttt{]} \\*
+ & \cs{setuplwc[orphanpenalty} & \texttt{]} \\*
+ & \cs{setuplwc[brokenpenalty} & \texttt{]} \\
+ \end{longtable}
+
+ The value of these penalties determines how much \TeX{} should attempt to
+ stretch glue before passing the \woo/ to \lwc/. If you set the values to~1
+ (default), \TeX{} will stretch nothing and immediately trigger \lwc/; if you
+ set the values to 10\,000, \TeX{} will stretch infinitely and \lwc/ will
+ never be triggered. If you set the value to some intermediate number, \TeX{}
+ will first attempt to stretch some glue to remove the \woo/; only if it
+ fails will \lwc/ come in and lengthen a paragraph. As a special case, if you
+ set the values to~0, both \TeX{} and \lwc/ will completely ignore the \woo/.
+
+ \Lwc/ will pick up on the values of \cs{widowpenalty}, \cs{clubpenalty}, and
+ \cs{brokenpenalty} regardless of how you set them, so the use of these
+ dedicated keys is entirely optional.
+
+ \subsection{\cs{nobreak} behaviour}
+
+ When \lwc/ encounters an orphan, it removes it by moving the orphaned line
+ to the next page. The majority of the time, this is an appropriate solution.
+ However, if the orphan is immediately preceded by a section heading (or
+ \cs{nobreak}\slash\cs{penalty 10000}), \lwc/ would na\"ively separate a
+ section heading from the paragraph that follows. This is almost always
+ undesirable, so \lwc/ provides some options to configure this.
+
+ \begin{tabular}{rl}
+ \plainop/ &
+ \cs{lwcnobreak\{\meta{value}\}} \\
+ \LaTeX{} &
+ \cs{lwcsetup\{nobreak=\meta{value}\}\hphantom{"}} \\
+ \ConTeXt{} &
+ \cs{setuplwc[nobreak=\meta{value}]\hphantom{"}} \\
+ \end{tabular}
+
+ The default value, \texttt{keep}, \emph{keep}s the section heading with
+ the orphan by moving both to the next page. The advantage to this option
+ is that it removes the orphan and retains any \cs{nobreak}s; the
+ disadvantage is that moving the section heading can create a large blank
+ space at the end of the page.
+ The value \texttt{split} \emph{split}s up the section heading and the
+ orphan by moving the orphan to the next page while leaving the heading
+ behind. This is usually a bad idea, but exists for the sake of
+ flexibility.
+ The value \texttt{warn} causes \lwc/ to give up on the page and do nothing,
+ leaving an orphaned line. \Lwc/ \emph{warn}s the user so that they can
+ manually remove the orphan.
+
+ See Figure~\ref{tab:nobreak} for a visual reference.
+
+ \begin{figure}
+ \renewcommand{\arraystretch}{1}
+ \renewcommand{\doublerulesep}{0.5em}
+ \begin{tabularx}{\linewidth}{%
+ |X|@{\hskip\doublerulesep}|X|@{\hskip\doublerulesep}|X|%
+ }
+ \multicolumn1c{\ttfamily keep} &
+ \multicolumn1c{\ttfamily split} &
+ \multicolumn1c{\ttfamily warn}
+ \\ \hhline{-||-||-}
+ &
+ &
+ \textbf{Heading} \\
+ &
+ \textbf{Heading} &
+ The\hfill very\hfill first\hfill line
+ \\ \hhline{-||-||-}
+ \textbf{Heading} &
+ The\hfill very\hfill first\hfill line &
+ text\hfill text\hfill text\hfill text \\
+ The\hfill very\hfill first\hfill line &
+ text\hfill text\hfill text\hfill text &
+ last line. \\
+ text\hfill text\hfill text\hfill text &
+ last line. &
+ % Nothing
+ \\ \hhline{-||-||-}
+ \end{tabularx}
+ \caption{A visual comparison of the \texttt{nobreak} option values.}
+ \label{tab:nobreak}
+ \end{figure}
+
+ \subsection{Maximum cost}
+
+ \Lwc/ ranks each paragraph on the page by how much it would \q{cost} to
+ lengthen that paragraph. By default, \lwc/ selects the paragraph on
+ the page with the lowest cost; however, you can configure it to only
+ select paragraphs below a selected cost.
+
+ If there aren't any paragraphs below the set threshold, then \lwc/ won't
+ remove the \woo/ and will instead issue a warning.
+
+ \begin{tabular}{rr@{\texttt{=\meta{integer}}}l}
+ \plainop/ &
+ \cs{lwcmaxcost} \\
+ \LaTeX{} &
+ \cs{lwcsetup\{max-cost} & \texttt{\}} \\
+ \leavevmode\hbox{\ConTeXt{}} &
+ \cs{setuplwc[maxcost} & \texttt{]} \\
+ \end{tabular}
+
+ Based on my testing, \texttt{max-cost} values less than 1\,000 cause
+ completely imperceptible changes in interword spacing; values less than
+ 5\,000 are only noticeable if you are specifically trying to pick out the
+ expanded paragraph on the page; values less than 15\,000 are typically
+ acceptable; and larger values may become distracting. \Lwc/ defaults to an
+ infinite \texttt{max-cost}, although the \q{strict} and \q{balanced} modes
+ sets the values to~5\,000 and 10\,000, respectively.
+
+ \subsection{Draft mode}
+
+ \Lwc/ has a \q{draft mode} which shows how \lwc/ processes pages.
+
+ \begin{tabular}{rl}
+ Plain \TeX{}\slash\OpTeX{} &
+ \cs{lwcdraft 1} \\
+ \LaTeX{} &
+ \cs{lwcsetup\{draft\}} \\
+ \ConTeXt{} &
+ \cs{setuplwc[draft=start]} \\
+ \end{tabular}
+
+ The draft mode has two main features:
+
+ First, it colours lines in the document according to their status. Any
+ remaining widows and orphans will be coloured red, any expanded paragraphs
+ will be coloured green, and any lines moved to the next page will be
+ coloured blue.
+
+ Second, this draft mode shows the paragraph costs at the end of each
+ paragraph, in the margin.
+
+ This draft mode leads to a neat trick: if you don't quite trust \lwc/, or
+ you're writing a document whose final version will need to be compilable by
+ both pdf\LaTeX{} and Lua\LaTeX, you can load the package with:
+
+ \smallskip
+ \cs{usepackage[draft, disable]\{lua-widow-control\}}
+ \smallskip
+
+ \noindent This way, all the widows and orphans will be coloured red and
+ listed in your log file. When you go through the document to try and
+ manually remove the widows and orphans---whether through the \cs{looseness}
+ trick or by rewriting certain lines---you can easily find the best
+ paragraphs to modify by looking at the paragraph costs in the margins. If
+ you're less cautious, you can compile your document with
+ \textsf{lua-widow-control} enabled as normal and inspect all the green
+ paragraphs to see if they look acceptable to you.
+
+ You can also toggle the paragraph colouring and the cost displays
+ individually:
+
+ \begin{tabular}{rl}
+ \plainop/ &
+ \cs{lwcshowcosts 1} \\
+ & \cs{lwcshowcolours 0} \\[4pt]
+ \LaTeX{} &
+ \cs{lwcsetup\{showcosts=true\}} \\
+ & \cs{lwcsetup\{showcolours=false\}} \\[4pt]
+ \ConTeXt{} &
+ \cs{setuplwc[showcosts=start]} \\
+ & \cs{setuplwc[showcolours=stop]} \\
+ \end{tabular}
+
+ \section{Presets}
+
+ As you can see, \lwc/ provides quite a few options. Luckily, there are a few
+ presets that you can use to set multiple options at once. These presets are
+ a good starting point for most documents, and you can always manually
+ override individual options.
+
+ These presets are only available for \LaTeX{} and \ConTeXt{}.
+
+ \begin{tabular}{@{}rl@{}}
+ \LaTeX{} &
+ \cs{lwcsetup\{\meta{preset}\}} \\
+ \ConTeXt{} &
+ \cs{setuplwc[\meta{preset}]} \\
+ \end{tabular}
+
+ \subsection{\texttt{default}}
+
+ If you use \lwc/ without any options, it defaults to this preset. In default
+ mode, \lwc/ takes all possible measures to remove \waos/ and will not
+ attempt to stretch any vertical glue. This usually
+ removes~$\mathord{>}\,95\%$ of all
+ possible \waos/. The catch here is that this mode is quite aggressive, so
+ it often leaves behind some fairly \q{spacey} paragraphs.
+
+ This mode is good if you want to remove (nearly) all \waos/ from your
+ document, without fine-tuning the results.
+
+ \subsection{\texttt{strict}}
+
+ \Lwc/ also offers a strict mode. This greatly restricts \lwc/'s tolerance
+ and makes it so that it will only lengthen paragraphs where the change will
+ be imperceptible.
+
+ The caveat with strict mode is that---depending on the document---\lwc/ will
+ be able to remove less than a third of the \waos/. For the \waos/ that can't
+ be automatically removed, a warning will be printed to your terminal and log
+ file so that a human can manually fix the situation.
+
+ This mode is good if you want the best possible typesetting and are willing
+ to do some manual editing.
+
+ \subsection{\texttt{balanced}}
+
+ Balanced mode sits somewhere between default mode and strict mode. This mode
+ first lets \TeX{} stretch a little glue to remove the \woo/; only if that
+ fails will it then trigger \lwc/. Even then, the maximum paragraph cost is
+ capped. Here, \lwc/ can usually remove 90\% of a document's
+ potential \waos/, and it does so while making a minimal visual impact.
+ \oldlooseness=-1
+
+ This mode is recommended for most users who care about their document's
+ typography. This mode is not the default since it doesn't remove all
+ \waos/: it
+ still requires a little manual intervention.
+
+ \begin{table}
+ \caption{\Lwc/ options set by each mode.}\label{tab:modes}
+ \ttfamily\setlength{\tabcolsep}{4pt}
+ \begin{tabular}{l*3r}\toprule
+ \textrm{Option} & default & balanced & strict \\ \midrule
+ max-cost & $\infty$ & 10000 & 5000 \\
+ \rlap{emergencystretch} & 3em & 1em & 0pt \\
+ nobreak & keep & keep & warn \\
+ widowpenalty & 1 & 500 & 1 \\
+ orphanpenalty & 1 & 500 & 1 \\
+ brokenpenalty & 1 & 500 & 1 \\
+ \bottomrule\end{tabular}
+ \end{table}
+
+ \section{Compatibility}
+
+ The \lwc/ implementation is almost entirely in Lua, with only a minimal
+ \TeX{} footprint. It doesn't modify the output routine or \cs{everypar} and
+ it doesn't insert any whatsits. This means that it should be compatible with
+ nearly any \TeX{} package, class, and format. Most changes that \lwc/ makes
+ are not observable on the \TeX{} side.
+
+ However, on the Lua side, \lwc/ modifies much of a page's internal
+ structure. This should not affect any \TeX{} code; however, it may surprise
+ Lua code that modifies or depends on the page's low-level structure. This
+ does not affect Plain~\TeX{} or \LaTeX{} where even most Lua-based packages
+ don't depend on the node list structure. \ConTeXt{} \emph{does} depend on
+ this internal node structure; however, I have carefully tested the package
+ to ensure that this causes no issues.
+
+ Finally, keep in mind that adding \lwc/ to a document will almost certainly
+ change its page break locations.
+
+ \subsection{Formats}
+
+ \Lwc/ runs on all known Lua\TeX{}-based formats: Plain~Lua\TeX{},
+ Lua\LaTeX{}, \ConTeXt{} Mk\acro{IV}, and~\OpTeX{}. Unless otherwise
+ documented, all features should work equally well in all formats.
+
+ \Lwc/ is also fully-compatible with the \LuaMeta\TeX{}-based formats:
+ \ConTeXt{} Mk\acro{XL}\slash\acro{LMTX}, \LuaMeta\LaTeX{}, and
+ \LuaMeta{}Plain~\cite{luametalatex}. \ConTeXt{} Mk\acro{XL} works equally
+ well as \ConTeXt{} Mk\acro{IV} and Lua\LaTeX{}; however, \LuaMeta\LaTeX{}
+ and \LuaMeta{}Plain support is still quite early. All features should work,
+ although there are still a few minor bugs.
+
+ All told, \lwc/ supports 7 different format\slash{}engine combinations.
+
+ \subsection{Columns}
+
+ Since \TeX{} and the formats implement column breaking and page
+ breaking through the
+ same internal mechanisms, \lwc/ removes \waos/ between columns just
+ as it does with \waos/ between pages.
+
+ \Lwc/ is known to work with the \LaTeX{} class option \verb|twocolumn|
+ and the two-column output routine from Chapter~23 of \titlecite{texbook}.
+ \oldlooseness=-1
+
+ \subsection{Performance}
+
+ \Lwc/ runs entirely in a single pass, without depending on any
+ \verb|.aux| files or the like. Thus, it shouldn't meaningfully
+ increase compile times. Although \lwc/ internally breaks each paragraph
+ twice, modern computers break paragraphs near-instantaneously, so you
+ are not likely to notice any slowdown.
+
+ \Lwc/ has been carefully tested to ensure that there are no memory leaks, so
+ \lwc/ can now easily compile documents $>10\,000$ pages long.
+
+ \vspace*{-1pt}
+
+ \subsection{\eTeX{} penalties}
+
+ Knuth's original \TeX{} has three basic line penalties:
+ \cs{interlinepenalty}, which
+ is inserted between all lines; \cs{club\-penalty}, which is inserted after
+ the first line; and \cs{widow\-penalty}, which is inserted before the last
+ line. The \eTeX{} extensions~\cite{etex} generalize these commands with a
+ syntax similar to \cs{parshape}: with \cs{widow\-penalties} you can set the
+ penalty between the last, second last, and $n$th last lines of a paragraph;
+ \cs{inter\-line\-penalties} and \cs{club\-penalties} behave similarly.
+
+ The \lwc/ package makes no explicit attempts to support these new
+ -\texttt{penalties} commands. Specifically, if you give a line a penalty
+ that matches either \cs{widowpenalty} or \cs{clubpenalty}, \lwc/ will treat
+ the lines exactly as it would a \woo/. So while these commands won't break
+ \lwc/, they are likely to lead to some unexpected behaviour.
+
+ \vspace*{-3pt}
+
+ \section{Short last lines}
+
+ \vspace*{-1pt}
+
+ When lengthening a paragraph with \lsness/, it is common advice to insert
+ ties (\verb|~|) between the last few words of the paragraph to avoid
+ overly-short last lines \cite{texbook}. \Lwc/ does this automatically,
+ but instead of using ties or \cs{hbox}es, it uses the
+ \cs{par\allowbreak fill\allowbreak skip}
+ parameter~\cite{texbook, Wermuth:2018:ECP, naruby}. When lengthening a paragraph
+ (and only when lengthening a paragraph---remember, \lwc/ doesn't
+ interfere with \TeX{}'s output unless it detects a \woo/), \lwc/ sets
+ \cs{parfillskip} to \verb|0.75\hsize plus 0.05\hsize minus 0.75\hsize|.
+ This normally makes the last line of a paragraph be at least
+ 20\% of the overall paragraph's width, thus preventing
+ ultra-short~lines.
+
+ \vspace*{-3pt}
+
+ \section{How it works}
+
+ \vspace*{-1pt}
+
+ \Lwc/ uses a fairly simple algorithm to eliminate \waos/, but there
+ are a few subtleties.
+
+ \vspace*{-1pt}
+
+ \subsection{Setup}
+
+ \Lwc/ sets the \cs{club\-penalty}, \cs{widow\-penalty}, and
+ \cs{broken\-penalty} parameters to sentinel values of~1. This will signal
+ to \lwc/ when a \woo/ occurs, yet it is small enough that it won't stretch
+ any glue.
+
+ \Lwc/ also enables Lua\TeX{}'s micro\-typographic
+ extensions~\cite{thanh}. This isn't strictly necessary;
+ however, it significantly increases the number of paragraphs that can
+ be acceptably \q{loosened}.
+
+ That is all that happens on the \TeX{} end. The rest of \lwc/ is pure Lua.
+
+ \subsection{Paragraph breaking}
+
+ First, \lwc/ hooks into the paragraph breaking process, before any output
+ routines or page breaking.
+
+ Before a paragraph is broken by \TeX{}, \lwc/ grabs the unbroken
+ paragraph. Then \lwc/ breaks the paragraph one line longer than its natural
+ length and stores it for later. It does this in the background,
+ \emph{without} interfering with how \TeX{} breaks paragraphs into their
+ natural length.
+
+ After \TeX{} has broken its paragraph into its natural length, \lwc/
+ appears again. Before the broken paragraph is added to the main
+ vertical list, \lwc/ \q{tags} the first and last nodes of the paragraph
+ using a Lua\TeX{} attribute. These attributes associate the
+ previously-saved lengthened paragraph with the naturally-typeset
+ paragraph on the page.
+
+ \subsection{Page breaking}
+
+ \Lwc/ intercepts \cs{box255} (the \cs{vbox} output by \TeX) immediately
+ before the output routine runs,
+ after all the paragraphs have been typeset.
+
+ First, \lwc/ looks at the \openalty/ of the page or column. If the page
+ was broken at a \woo/, the \openalty/ will be equal to either
+ the \cs{widowpenalty} or the \cs{clubpenalty}. If the \openalty/ does not
+ indicate a \woo/, \lwc/ will stop and return \cs{box255} unmodified to
+ the output~routine, and \TeX{} continues as normal.
+
+ Otherwise, we assume that we have a \woo/ on the page,
+ meaning that we should lengthen the page by 1~line. We iterate through
+ the list of saved paragraphs to find the lengthened paragraph with the
+ least cost. After we've selected a good paragraph, we traverse
+ through the page to find the original version of this paragraph---the
+ one that unmodified \TeX{} originally typeset. Having found the original
+ paragraph, we splice in the lengthened paragraph in place of the original.
+ \oldlooseness=-1
+
+ Since the page is now 1~line longer than it was before, we pull the last
+ line off the page to bring it back to its original length, and place
+ that line onto the top of \TeX's \q{recent contributions} list. When
+ the next page begins, this line will be inserted before all other
+ paragraphs, right at the top. Now, we can return the new, widow-free page
+ (updated \cs{box255}) to the output routine, which proceeds
+ as normal.
+
+ \subsection{Footnotes}
+ Earlier versions of \textsf{lua-widow-control} completely ignored inserts.
+ This meant that if a moved line had associated footnotes,
+ \textsf{lua-widow-control} would move the \q{footnote mark} but not the
+ associated \q{footnote text}. \textsf{lua-widow-control} now handles
+ footnotes correctly through the mechanism detailed in the next section.
+
+ \subsubsection{Inserts}
+
+ Before we go into the details of how \textsf{lua-widow-control} handles
+ footnotes, we need to look at what footnotes actually are to \TeX{}. Every
+ \cs{footnote} command ultimately expands to something like
+ \cs{insert\meta{class}}\allowbreak\verb|{|\meta{content}\verb|}|, where
+ \meta{class} is an insertion class number, defined as \cs{footins} in this
+ case (in Plain \TeX\ and \LaTeX). Inserts can be found in horizontal mode
+ (footnotes) or in vertical mode (\cs{topins} in Plain \TeX{} and floats in
+ \LaTeX{}), but they cannot be inside boxes. Each of these insert types is
+ assigned a different class number, but the mechanism is otherwise identical.
+ \textsf{lua-widow-control} treats all inserts identically, although it
+ safely ignores vertical mode inserts since they are only ever found between
+ paragraphs.
+
+ But what does \cs{insert} do exactly? When \TeX{} sees an \cs{insert}
+ primitive in horizontal mode (when typesetting a paragraph), it does two
+ things: first, it processes the insert's content and saves it invisibly just
+ below the current line. Second, it effectively adds the insert content's
+ height to the height of the material on the current page. Also, for the
+ first insert on a page, the glue in \cs{skip}\meta{class} is added to the
+ current height. All this is done to ensure that there is sufficient room for
+ the insert on the page whenever the line is output onto the page.
+
+ If there is absolutely no way to make the insert fit on the page---say,
+ if you placed an entire paragraph in a footnote on the last line of a
+ page---then \TeX{} will begrudgingly \q{split} the insert, placing the first
+ part on the current page and \q{holding over} the second part until the next
+ page.
+
+ There are some other \TeX{}nicalities involving \cs{count}\meta{class} and
+ \cs{dimen}\meta{class}, but they mostly don't affect
+ \textsf{lua-widow-control}. See Chapter~15 in \textsl{The \TeX{}book} or another reference for
+ all the details.
+
+ After \TeX{} has chosen the breakpoints for a paragraph, it adds the chosen
+ lines one by one to the current page. Whenever the accumulated page height
+ is \q{close enough} to the target page height (normally \cs{vsize}) the
+ \cs{output} token list (often called the ``output routine'') is expanded.
+
+ But before \cs{output} is called, \TeX{} goes through the page contents and
+ moves the contents of any saved inserts into \cs{vbox}es corresponding to
+ the inserts' classes, namely \cs{box}\meta{class}, so \cs{output} can work
+ with them.
+
+ And that's pretty much it on the engine side. Actually placing the inserts
+ on the page is reserved for the output routine, which is defined by the
+ format. This too is a complicated process, although thankfully not one that
+ \textsf{lua-widow-control} needs to worry about.
+
+ \subsubsection{\LuaMeta\TeX{}}
+
+ The \LuaMeta\TeX{} engine treats inserts slightly differently than
+ traditional \TeX{} engines. The first major difference is that insertions
+ have dedicated registers; so instead of \cs{box}\meta{class}, \LuaMeta\TeX{}
+ has \cs{insertbox}\meta{class}; instead of \cs{count}\meta{class},
+ \LuaMeta\TeX{} has \cs{insertmultiplier}\meta{class}; etc. The second major
+ difference is that \LuaMeta\TeX{} will pick up inserts that are inside of
+ boxes, meaning that placing footnotes in things like tables or frames should
+ mostly just work as expected.
+
+ There are also a few new parameters and other minor changes, but the overall
+ mechanism is still quite similar to traditional \TeX{}.
+
+ \subsubsection{Paragraph breaking}
+
+ As stated in the original article~\cite{tb133chernoff-widows},
+ \textsf{lua-widow-control} intercepts \TeX{}'s output immediately before
+ the output routine. However, this is \emph{after} all the inserts on the
+ page have been processed and boxed. This is a bit of a problem because if
+ we move a line to the next page, we need to move the associated insert;
+ however, the insert is already gone.
+
+ To solve this problem, immediately after \TeX{} has naturally broken a
+ paragraph, \textsf{lua-widow-control} copies and stores all its inserts.
+ Then, \textsf{lua-widow-control} tags the first element of each line
+ (usually a glyph) with a Lua\TeX{} attribute that contains the indices for
+ the first and last associated insert. \textsf{lua-widow-control} also tags
+ each line inside the insert's content with its corresponding index so that
+ it can be found later.
+
+ \subsubsection{Page breaking}
+
+ Here, we follow the same algorithm as in the original
+ article~\cite{tb133chernoff-widows}. However, when we move the last line of
+ the page to the next page, we first need to inspect the line to see if any
+ of its contents have been marked with an insert index. If so, we need to
+ move the corresponding insert to the next page. To do so, we unpack the
+ attributes value to get all the inserts associated with this line.
+
+ Using the stored insert indices and class, we can iterate through
+ \cs{box}\meta{class} and delete any lines that match one of the current
+ line's indices. We also need to iterate through the internal \TeX{} box
+ \verb|hold_head|---the box that holds any inserts split onto the next
+ page---and delete any matching lines. We can safely delete any of these
+ lines since they are still stored in the original \cs{insert} nodes that we
+ copied earlier.
+
+ Now, we can retrieve all of our previously-stored inserts and add them to
+ the 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
+
+ \section{Choosing the \q{best} paragraph}
+
+ As we discussed previously, \lwc/ lengthens the paragraph with the lowest
+ cost. However, assigning a cost to each paragraph is not quite as simple as
+ it sounds. Before we look at how \lwc/ assigns costs, let's look at how
+ \TeX{} scores paragraphs when breaking them naturally.
+
+ \subsection{How \TeX{} scores paragraphs}
+
+ All glue in \TeX{} has a certain natural size: the size that it would be
+ in an ideal scenario. However, most glue also has stretch and shrink
+ components so that the glue can change in size to adapt to its
+ surroundings. For each line, \TeX{} individually sums the total
+ stretch/shrink for the line and the stretch/shrink that was actually used.
+ We define the stretch/shrink ratio~$r$ as the quotient of the
+ stretch/shrink used and the stretch/shrink available. Then the badness~$b$
+ of a line is approximately defined as
+ \begin{equation*}
+ b = 100r^3.
+ \end{equation*}
+ This is the badness referenced in the commonly-seen
+ \texttt{Underfull \cs{hbox}
+ (badness 1234)} warnings that \TeX{} produces.
+
+ \TeX{} calculates the badness for each line individually; however, we also
+ need to assess the paragraph as a whole. To do so, \TeX{} defines the
+ demerits for a whole paragraph~$d$ as approximately\footnotemark{} the sum
+ of the squared badnesses for each line. The natural paragraph that \TeX{}
+ breaks is the one that minimizes~$d$.
+
+ \footnotetext{We ignore any additional demerits or penalties that
+ \TeX{} may add.}
+
+ One important thing to realize is that demerits grow incredibly fast:
+ demerits are proportional to the \emph{sixth} power of glue stretch. This
+ means that you can expect to see extremely large demerit values, even for
+ a relatively \q{good} paragraph.
+
+ \subsection{Possible cost functions}
+
+ Now, let's return to how \lwc/ assigns costs to each paragraph. This is
+ surprisingly more complicated than it sounds, so we'll go through a few
+ possible cost functions first.
+
+ Here, we use $c$~for the cost of a paragraph, $d$~for the total demerits,
+ and $l$~for the number of lines (\cs{prevgraf}).
+
+ \subsubsection{The original implementation}
+
+ The original implementation of \lwc/ used the simple cost function
+ \begin{equation*}
+ c = d.
+ \end{equation*}
+ This cost function works reasonably well, but has one major issue: it
+ doesn't take into account the number of lines in the paragraph. The demerits
+ for a paragraph is the sum of the demerits for each line. This means this
+ cost function will prefer using shorter paragraphs since they tend to have
+ fewer demerits. However, long paragraphs tend to have much more available
+ glue stretch, so this strategy can lead to suboptimal solutions.
+
+ \subsubsection{Scaling by the number of lines}
+
+ Once I realized this issue, I tried correcting it by dividing by the number
+ of lines in the paragraph to get the average demerits instead of the total
+ demerits:
+ \begin{equation*}
+ c = \frac{d}{l}
+ \end{equation*}
+ This works better than the previous function, but still has an issue.
+ If we have a fairly bad ten-line paragraph with total demerits $10d$ and an
+ almost-equally bad two-line paragraph with total demerits $2d + 1$, then by
+ this cost function, the ten-line paragraph will have a lower cost and will
+ be chosen. This means that our page now has ten bad lines instead of two bad
+ lines, which is not ideal.
+
+ \subsubsection{Current implementation}
+
+ Our first cost function, $c=dl^0$, doesn't consider the number of lines at
+ all, while our second cost function, $c=dl^{-1}$, considers the number of
+ lines too much. Splitting the difference between the two functions, we get
+ the current implementation:
+ \begin{equation*}
+ c = \frac{d}{\sqrt{l}}
+ \end{equation*}
+
+ This solves the issue with the previous function, but it adds a new issue: given a
+ short paragraph with a large number of demerits per line and a long
+ paragraph with fairly few average demerits per line, this function will often
+ choose the shorter line. Although this sounds bad, in practice it gives much
+ better results since very bad short paragraphs are \emph{much} less noticeable than
+ slightly bad long paragraphs.
+
+ Of course, this new function may still not be quite perfect. \Lwc/ uses the
+ \texttt{lwc.paragraph\_cost(demerits, lines)} Lua function to calculate a
+ paragraph's cost; if you want, you can redefine this function to anything
+ that you want.
+ \oldlooseness=-1
+
+ % I didn't arrive at this function through any sort of scientific testing;
+ % rather, I picked the simplest function that I could think of that satisfies
+ % the following two properties:
+ % \begin{itemize}
+ % \item Given a long paragraph and a short paragraph with different
+ % average badnesses per line, prefer the one with the least average
+ % badness.
+ % \item Given two paragraphs with equal average badnesses per line,
+ % prefer the shorter one.
+ % \end{itemize}
+
+ \vspace*{-3pt}
+
+ \section{Quantitative analysis}
+
+ \vspace*{-1pt}
+
+ \begin{figure}
+ \begin{tikzpicture}\begin{axis}[
+ ybar interval,
+ xticklabels={1, ..., 15,
+ {$\,\ge\! 16$}},
+ x tick label style={font=\small},
+ y tick label style={font=\small},
+ enlarge y limits=upper,
+ enlarge x limits={abs=1},
+ grid=none,
+ scaled y ticks=base 10:-3,
+ ytick scale label code/.code={},
+ xlabel={Paragraph length (lines)},
+ ylabel={Count (thousands)}
+ ]
+ \addplot+ [
+ draw=black,
+ fill=black!10,
+ semithick,
+ ] table {
+ Length Count
+ 1 4429
+ 2 3704
+ 3 2045
+ 4 1320
+ 5 894
+ 6 717
+ 7 498
+ 8 406
+ 9 379
+ 10 251
+ 11 175
+ 12 152
+ 13 111
+ 14 95
+ 15 79
+ 16 437
+ 18 0
+ };
+
+ \filldraw [fill=black!25, draw=black] (16, 0) rectangle (18, 437);
+ \end{axis}\end{tikzpicture}
+ \caption{Histogram of natural paragraph lengths in the sample text.}
+ \label{fig:hist}
+ \end{figure}
+
+ Let's look at some statistics for \lwc/. For testing, I
+ downloaded the top~ten books on \textsl{Project Gutenberg},\footnotemark{}
+ converted them to \LaTeX{} using \textsf{pandoc}, concatenated them into a
+ single \textsf{article} file, and compiled twice. This gives us a \acro{PDF}
+ with 1\,381~pages, 15\,692~paragraphs, 61\,865~lines, and 399~\waos/
+ (if they aren't removed).
+ \footnotetext{\textsl{Frankenstein},
+ \textsl{Pride and Prejudice},
+ \textsl{Alice's Adventures in Wonderland},
+ \textsl{The Great Gatsby},
+ \textsl{The Adventures of Sherlock Holmes},
+ \textsl{Simple Sabotage Field Manual},
+ \textsl{A Tale of Two Cities},
+ \textsl{The Picture of Dorian Gray},
+ \textsl{Moby Dick},
+ and \textsl{A Doll's House}.
+ }
+
+ This is a fairly challenging test: almost every third page has a \woo/, over
+ half of the paragraphs have two lines or fewer, and the text block is set to
+ the fairly wide \textsf{article} defaults. An average document is
+ much less challenging for \lwc/, so we can consider this to be a
+ worst-case scenario.
+
+ \vspace*{-1pt}
+
+ \subsection{Widows and orphans removed}
+
+ \begin{figure}
+ \begin{tikzpicture}\begin{axis}[
+ ybar=0pt,
+ bar width=0.8,
+ xtick=data,
+ ylabel={Widows and orphans removed},
+ xticklabels={
+ \shortstack[c]{\hfill Maximum\\\hfill possible},
+ \textsf{lwc} \texttt{default},
+ \texttt{balanced},
+ \LaTeX{},
+ \texttt{strict},
+ },
+ x tick label style={
+ font=\small,
+ rotate=45,
+ anchor=east,
+ },
+ enlarge x limits=0.2,
+ ]
+ \addplot+ [
+ draw=black,
+ fill=black!10,
+ semithick,
+ ] table [x expr=\coordindex, y index=0] {
+ 399
+ 392
+ 348
+ 179
+ 52
+ };
+
+ \filldraw [fill=black!25, draw=black]
+ (-0.4, 0) rectangle (0.4, 399);
+ \end{axis}\end{tikzpicture}
+ \divide\abovecaptionskip by 2
+ \caption{The number of \waos/ removed by each method.}\label{fig:modes}
+ \end{figure}
+
+ When we run \LaTeX{} with its default settings on the file, 179~(47\%) of
+ the \waos/ are removed. When we add \lwc/ with default settings, we remove
+ 392~(98\%). Switching to strict mode, we can only remove 52~(13\%) of the
+ \waos/. In balanced mode, we remove 348~(87\%). See Figure~\ref{fig:modes}
+ for a visual comparison.
+
+ \subsection{Paragraph costs}
+
+ \begin{figure}
+ \begin{tikzpicture}\begin{axis}[
+ xlabel={Percentile},
+ ylabel={Cost},
+ ymode=log,
+ legend entries={Long, Natural},
+ legend pos=north west,
+ cycle list={
+ {black, thick},
+ {black!35, thick},
+ },
+ ]
+ \addplot+ table [x=Percentile, y=Long] {\plotdata};
+ \addplot+ table [x=Percentile, y=Natural] {\plotdata};
+
+ \end{axis}\end{tikzpicture}
+ \divide\abovecaptionskip by 2
+ \caption{Paragraph costs by percentile rank for naturally-broken and
+ one-line lengthened paragraphs.}\label{fig:costs}
+ \end{figure}
+
+ The last section showed us that \lwc/ is quite effective at removing \waos/,
+ so now let's look at the paragraphs that \lwc/ expands. As \TeX{}
+ processes a document, \lwc/ is recording the costs for the naturally-broken
+ and expanded versions of each paragraph in the document. Costs don't
+ mean that much on their own, but a lower cost is always better.
+
+ As you can see in Figure~\ref{fig:costs}, the
+ lengthened paragraphs tend to have \emph{much} higher costs than the
+ naturally-broken paragraphs. This is not surprising, since (as we've seen)
+ a paragraph's demerits scale with the sixth power of glue stretch, so even
+ a small amount of glue stretch can cause a huge increase in demerits.
+
+ The empty space on the left of the \q{long} line is from the paragraphs
+ that \lwc/ was unable to lengthen at any cost. Lua\TeX{} assigns these
+ paragraphs zero~demerits, so they disappear on a logarithmic plot.
+
+ \subsection{Lengthening vs.\ shortening paragraphs}
+ \begin{figure}[t]
+ \begin{tikzpicture}\begin{axis}[
+ xbar stacked,
+ height=0.15\linewidth,
+ width=\dimexpr\linewidth-1em,
+ scale only axis,
+ bar width=1,
+ enlargelimits=false,
+ xmin=0,
+ ymin=-0.5,
+ ymax=1,
+ ymajorticks=false,
+ xtick style={draw=none},
+ xlabel={Paragraphs (thousands)},
+ scaled x ticks=base 10:-3,
+ xtick scale label code/.code={},
+ legend style={at={(0.5,1)}, anchor=north},
+ legend columns=5,
+ legend cell align=left,
+ legend style={
+ /tikz/every even column/.append style={column sep=1em},
+ draw=none,
+ fill=none,
+ },
+ legend entries={
+ {$n=1$},
+ {$n$},
+ {$n+1$},
+ {$n\pm1$},
+ {$n-1$}
+ },
+ ]
+ \addplot [fill=black!10 ] coordinates {(4429, 0)}; % One
+ \addplot [fill=white ] coordinates {(4474, 0)}; % None
+ \addplot [pattern=north east lines] coordinates {(5457, 0)}; % Long
+ \addplot [pattern=crosshatch ] coordinates {( 482, 0)}; % Both
+ \addplot [pattern=north west lines] coordinates {( 850, 0)}; % Short
+ \end{axis}\end{tikzpicture}
+ \divide\abovecaptionskip by 2
+ \caption{The number of paragraphs in the test sample that
+ (respectively) have exactly
+ one line, cannot be stretched or shrunk, can be only stretched
+ by one~line, can be either stretched or shrunk, and can be
+ only shrunk.}
+ \label{fig:stretchshrink}
+ \end{figure}
+
+ Figure~\ref{fig:stretchshrink} shows the number of paragraphs that \lwc/
+ could potentially stretch or shrink. The one-line paragraphs are broken out
+ separately since this test sample has an anomalous number of them.
+ Otherwise, we can see that \lwc/ is capable of stretching the majority of
+ paragraphs.
+
+ We can also see that of non-single-line paragraphs, only about 8\%
+ of paragraphs can only be shrunk (the last segment of
+ Figure~\ref{fig:stretchshrink}), and this is in a document where 13\%
+ of paragraphs have at least eight~lines. Most documents rarely have
+ such long paragraphs, and it is these long paragraphs that are the
+ easiest to shrink.
+ \oldlooseness=-1
+
+ Because of this, \lwc/ doesn't even attempt to shrink paragraphs; it
+ only stretches them.
+
+ \vspace*{-10pt}
+
+ \section{Known issues}
+
+ \vspace*{-5pt}
+
+ \Lwc/ is quite stable these days. At this point, all \emph{known} bugs have
+ been resolved; some bugs certainly still remain, but I'd feel quite
+ confident using \textsf{lua-widow-control} in your everyday documents. There
+ are, however, some fundamental limitations due to how \lwc/ operates:
+
+ \begin{itemize}
+ \item When a three-line paragraph is at the end of a page forming a
+ widow, \lwc/ will remove the widow; however, it will leave an orphan.
+ This issue is inherent to any process that removes widows through
+ paragraph expansion and is thus unavoidable. Orphans are considered
+ to be better than widows~\cite{elements}, so this is still an
+ improvement.
+
+ \item Sometimes a \woo/ cannot be eliminated because no paragraph has
+ enough stretch. Sometimes this can be remediated by
+ increasing \lwc/'s \estretch/; however, some pages just don't have
+ any suitable paragraph.
+
+ Long paragraphs with short words tend to be stretchier than short
+ paragraphs with long words since these long paragraphs have more
+ interword glue. Narrow columns also stretch more easily than wide
+ columns since you need to expand a paragraph by less to make a new line.
+
+ \item \Lwc/ only attempts to expand paragraphs on a page with a \woo/. A
+ global system like in~\titlecite{global} would solve this;
+ however, this is both \acro{NP}-complete~\cite{plass} and
+ impossible to solve in a single pass. Very rarely would such a
+ system remove \woos/ that \lwc/ cannot.
+
+ \item \textsf{lua-widow-control} won't properly move footnotes if there
+ are multiple different ``classes'' of inserts on the same line. To the
+ best of my knowledge, this shouldn't happen in any real-world
+ documents. If this happens to be an issue for you, please let me know;
+ this problem is relatively easy to fix, although it will add
+ considerable complexity for what I think isn't a real issue.
+ \oldlooseness=-1
+ \end{itemize}
+
+ \section{Conclusion}
+
+ All this probably makes \lwc/ look quite complicated, and this is true to
+ some extent. However, this complexity is hidden from the end~user:
+ as stated at the outset, most
+ users merely need to place \latexuse/ in their \LaTeX{} document
+ preamble, and \lwc/ will remove all the troublesome \waos/, without needing
+ any manual intervention.
+
+ Should you have any issues, questions, or suggestions for \lwc/, please
+ visit the project's GitHub page:
+ \href{https://github.com/gucci-on-fleek/lua-widow-control}
+ {\ttfamily https://github.com/gucci-on-fleek\discretionary{-}{-}{-}lua-widow-control}.
+ Any feedback is greatly appreciated!
+
+ \let\macro=\cs
+ \AtNextBibliography{\small}
+ \printbibliography
+
+ \section*{Automatické odstraňování vdov a sirotků pomocí balíčku \textsf{lua-widow-control}}
+ \begin{otherlanguage}{czech}
+ Balíček \textsf{lua-widow-control} pro
+ Lua\TeX\slash{}Lua\LaTeX\slash{}\ConTeXt\slash{}\OpTeX{} odstraňuje vdovy
+ a sirotky bez dalšího zásahu uživatele. Využívá přitom sílu Lua\TeX u a přitom
+ nenatahuje žádné vertikální mezery a ani nezkracuje stránky nebo sloupce.
+ Namísto toho balíček automaticky prodlužuje některý z odstavců na té stránce
+ nebo sloupci, kde by se vdova nebo sirotek vyskytli.
+
+ Pro použití balíčku postačí většině uživatelů \LaTeX u uvést v~preambuli
+ dokumentu \verb|\usepackage{lua-widow-control}|. Žádné další změny
+ v~dokumentu nejsou zapotřebí.
+ \end{otherlanguage}
+ \klicovaslova: Lua\TeX, vdova, sirotek
+
+\end{document}
diff --git a/Master/texmf-dist/source/luatex/lua-widow-control/tb133chernoff-widows.ltx b/Master/texmf-dist/source/luatex/lua-widow-control/tb133chernoff-widows.ltx
index 7cd18ab68cc..bcae2c01c65 100644
--- a/Master/texmf-dist/source/luatex/lua-widow-control/tb133chernoff-widows.ltx
+++ b/Master/texmf-dist/source/luatex/lua-widow-control/tb133chernoff-widows.ltx
@@ -21,11 +21,11 @@
% Please refer to the PDF on tug.org for the authoritative version.
% Compiling:
-% context tb1333chernoff-widows-figure.ctx
-% lualatex tb1333chernoff-widows.ltx
-% bibtex tb1333chernoff-widows
-% lualatex tb1333chernoff-widows.ltx
-% lualatex tb1333chernoff-widows.ltx
+% context tb133chernoff-widows-figure.ctx
+% lualatex tb133chernoff-widows.ltx
+% bibtex tb133chernoff-widows
+% lualatex tb133chernoff-widows.ltx
+% lualatex tb133chernoff-widows.ltx
% The original article was built with the final/frozen TeX Live 2021.
% Set the publication info
@@ -72,9 +72,9 @@
\def\lwc/{\textsf{lua-\allowbreak widow-\allowbreak control}}
\def\Lwc/{\textsf{lua-\allowbreak widow-\allowbreak control}}
\def\estretch/{%
- \texorpdfstring{\cs{emergency}\-\mbox{\tt stretch}}{\textbackslash{}emergencystretch}%
+ \texorpdfstring{\cs{emergency}\-\mbox{\ttfamily stretch}}{\textbackslash{}emergencystretch}%
}
-\def\openalty/{\cs{output}\-\mbox{\tt penalty}}
+\def\openalty/{\cs{output}\-\mbox{\ttfamily penalty}}
\def\waos/{widows and orphans}
\def\wao/{widow and orphan}
\def\woo/{widow or orphan}
@@ -83,7 +83,7 @@
\cs{use\-package\{lua-\allowbreak widow-\allowbreak control\}}
}
\def\lsness/{\texorpdfstring{%
- \cs{loose}\-\mbox{\tt ness}}{\textbackslash{}looseness}%
+ \cs{loose}\-\mbox{\ttfamily ness}}{\textbackslash{}looseness}%
}
\def\plainop/{Plain~\TeX\slash\OpTeX{}}
\newcommand{\LuaMetaTeX}{Lua\-Meta\-\TeX{}}
@@ -177,6 +177,9 @@
pdfdate={2022-05}
}
+% TODO: Temporary fix
+\def\Thanh{H{\`a}n Th\^e\llap{\raise0.5ex\hbox{\'{\relax}}} Th{\`a}nh}
+
\begin{document}
\maketitle
@@ -1291,7 +1294,7 @@
xlabel={Percentile},
ylabel={Cost},
ymode=log,
- legend entries={Natural, Long},
+ legend entries={Long, Natural},
legend pos=north west,
cycle list={
{black, thick},
@@ -1443,7 +1446,7 @@
Should you have any issues, questions, or suggestions for \lwc/, please
visit the project's GitHub page:
\href{https://github.com/gucci-on-fleek/lua-widow-control}
- {\tt github.com/gucci-on-fleek/lua-widow-control}.
+ {\ttfamily github.com/gucci-on-fleek/lua-widow-control}.
Any feedback is greatly appreciated!
\bibliographystyle{tugboat}
diff --git a/Master/texmf-dist/tex/context/third/lua-widow-control/t-lua-widow-control.mkxl b/Master/texmf-dist/tex/context/third/lua-widow-control/t-lua-widow-control.mkxl
index 1293e08090e..b4d35eb15f5 100644
--- a/Master/texmf-dist/tex/context/third/lua-widow-control/t-lua-widow-control.mkxl
+++ b/Master/texmf-dist/tex/context/third/lua-widow-control/t-lua-widow-control.mkxl
@@ -1,24 +1,29 @@
%D \module
%D [ file=t-lua-widow-control,
-%D version=2.2.2, %%version
+%D version=3.0.0, %%version
%D title=lua-widow-control,
%D subtitle=\ConTeXt module for lua-widow-control,
%D author=Max Chernoff,
-%D date=2022-08-23, %%dashdate
+%D date=2022-11-22, %%dashdate
%D copyright=Max Chernoff,
%D license=MPL-2.0+,
%D url=https://github.com/gucci-on-fleek/lua-widow-control]
\startmodule[lua-widow-control]
\unprotect
+% Preliminaries
\installnamespace{lwc}
-\installcommandhandler \????lwc {lwc} \????lwc
+\installswitchcommandhandler \????lwc {lwc} \????lwc
+% Set up the options
\newdimen\lwc_emergency_stretch
+\newdimen\lwc_draft_offset
\newcount\lwc_max_cost
-\appendtoks
+
+\starttexdefinition lwc_set_parameters
\lwc_emergency_stretch=\lwcparameter{emergencystretch}
+ \lwc_draft_offset=\lwcparameter{draftoffset}
\doifelse{\lwcparameter{\c!state}}\v!start{
\lwc_enable
@@ -28,7 +33,18 @@
\lwc_debug{\lwcparameter{debug}}
- \lwc_draft{\lwcparameter{draft}}
+ \doif{\lwcparameter{draft}}\v!start{
+ \setlwcparameter{showcosts}{\v!start}
+ \setlwcparameter{showcolours}{\v!start}
+ }
+
+ \doif{\lwcparameter{draft}}\v!stop{
+ \setlwcparameter{showcosts}{\v!stop}
+ \setlwcparameter{showcolours}{\v!stop}
+ }
+
+ \lwc_show_costs{\lwcparameter{showcosts}}
+ \lwc_show_colours{\lwcparameter{showcolours}}
\lwc_nobreak{\lwcparameter{nobreak}}
@@ -55,22 +71,69 @@
\stopsetups
\setups[*default]
-\to\everysetuplwc
+\stoptexdefinition
+% Load the main Lua file
\ctxloadluafile{lua-widow-control}
-\setuplwc[
+% Define the presets
+\definelwc[default][
emergencystretch=3em,
- \c!state=\v!start,
- debug=\v!stop,
- draft=\v!stop,
+ maxcost=2147483647,
+ nobreak=keep,
orphanpenalty=1,
widowpenalty=1,
brokenpenalty=1,
+]
+
+\definelwc[strict][
+ emergencystretch=0pt,
+ maxcost=5000,
+ nobreak=warn,
+ widowpenalty=1,
+ orphanpenalty=1,
+ brokenpenalty=1,
+]
+
+\definelwc[balanced][
+ emergencystretch=1em,
+ maxcost=10000
nobreak=keep,
- maxcost=2147483647,
+ widowpenalty=500,
+ orphanpenalty=500,
+ brokenpenalty=500,
]
+% Set up the default options
+\setuplwc[
+ \c!state=\v!start,
+ debug=\v!stop,
+ draft=,
+ showcosts=\v!stop,
+ showcolours=\v!stop,
+ draftoffset=1in,
+]
+
+\setuplwc[default]
+
+\appendtoks
+ \ifcase\lwcsetupmode
+ % can't happen
+ \or % \setuplwc[name][key=value]
+ \ifx\previouslwc\currentlwc
+ \let\currentlwc\currentlwc
+ \lwc_set_parameters
+ \fi
+ \or % \setuplwc[key=value]
+ \let\currentlwc\previouslwc
+ \lwc_set_parameters
+ \or % \setuplwc[name]
+ \glet\currentlwc\currentlwc % global
+ \lwc_set_parameters
+ \fi
+\to \everysetuplwc
+
+\lwc_set_parameters
% Here, we enable font expansion/contraction. It isn't strictly necessary for
% \lwc/'s functionality; however, it is required for the
@@ -97,6 +160,7 @@
\fi%
}
+% Add the default patches
\prependtoks\lwc_patch_pre\to\everybeforesectionheadhandle % Sectioning
\prependtoks\lwc_patch_post\to\everyaftersectionheadhandle
diff --git a/Master/texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control-2022-02-22.sty b/Master/texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control-2022-02-22.sty
index bdfa099947c..d7f8601e60a 100644
--- a/Master/texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control-2022-02-22.sty
+++ b/Master/texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control-2022-02-22.sty
@@ -12,8 +12,8 @@
% report a real version number here for debugging.
\PackageInfo{lua-widow-control}{%
Real version:
- 2022/08/23 %%slashdate
- v2.2.2 %%version
+ 2022/11/22 %%slashdate
+ v3.0.0 %%version
}
\PackageWarning{lua-widow-control}{%
diff --git a/Master/texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control.sty b/Master/texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control.sty
index f5db525ab40..ad3e95a28ae 100644
--- a/Master/texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control.sty
+++ b/Master/texmf-dist/tex/lualatex/lua-widow-control/lua-widow-control.sty
@@ -13,7 +13,7 @@
\DeclareRelease{}{0000-00-00}{lua-widow-control-2022-02-22.sty}
\DeclareRelease{v1.1.6}{2022-02-22}{lua-widow-control-2022-02-22.sty}
-\DeclareCurrentRelease{v2.2.2}{2022-08-23} %%version %%dashdate
+\DeclareCurrentRelease{v3.0.0}{2022-11-22} %%version %%dashdate
% If this version of LaTeX doesn't support command hooks, then we load
% the last v1.1.X version of the package.
@@ -23,8 +23,8 @@
\ProvidesExplPackage
{lua-widow-control}
- {2022/08/23} %%slashdate
- {v2.2.2} %%version
+ {2022/11/22} %%slashdate
+ {v3.0.0} %%version
{Use Lua to remove widows and orphans}
% Message and String Constants
@@ -76,6 +76,10 @@
emergencystretch .value_required:n = true,
emergencystretch .initial:x = \dim_max:nn { 3em } { 30pt },
+ draftoffset .dim_gset:N = \g__lwc_draftoffset_dim,
+ draftoffset .value_required:n = true,
+ draftoffset .initial:x = 1in,
+
max-cost .int_gset:N = \g__lwc_maxcost_int,
max-cost .value_required:n = true,
max-cost .initial:x = \c_max_int,
@@ -145,6 +149,14 @@
\fi
}
+\prg_new_conditional:Nnn \__lwc_if_lmtx: { T, F, TF } {
+ \int_compare:nNnTF { \tex_luatexversion:D } > { 200 } {
+ \prg_return_true:
+ } {
+ \prg_return_false:
+ }
+}
+
% Expansion of some parts of the document, such as section headings, is quite
% undesirable, so we'll disable \lwc/ for certain commands.
\int_new:N \g__lwc_disable_int
@@ -197,6 +209,12 @@
\clist_map_function:NN \g__lwc_disablecmds_cl \__lwc_patch_cmd:n
}
+\__lwc_if_lmtx:T {
+ \int_gset:Nn \normalizelinemode {
+ \numexpression\normalizelinemode bor 2\relax
+ }
+}
+
%%% Class and package-specifc patches
% KOMA-Script
@@ -226,9 +244,22 @@
debug .default:n = true,
debug .value_required:n = false,
- draft .choice:,
- draft / true .code:n = \__lwc_draft:n { true },
- draft / false .code:n = \__lwc_draft:n { false },
+ showcolours .choice:,
+ showcolours / true .code:n = \__lwc_show_colours:n { true },
+ showcolours / false .code:n = \__lwc_show_colours:n { false },
+ showcolours .default:n = true,
+ showcolours .value_required:n = false,
+
+ showcosts .choice:,
+ showcosts / true .code:n = \__lwc_show_costs:n { true },
+ showcosts / false .code:n = \__lwc_show_costs:n { false },
+ showcosts .default:n = true,
+ showcosts .value_required:n = false,
+
+ draft .meta:n = {
+ showcolours = { #1 },
+ showcosts = { #1 },
+ },
draft .default:n = true,
draft .value_required:n = false,
diff --git a/Master/texmf-dist/tex/luatex/lua-widow-control/lua-widow-control.lua b/Master/texmf-dist/tex/luatex/lua-widow-control/lua-widow-control.lua
index 1ef86174c4f..59ae84ee3e2 100644
--- a/Master/texmf-dist/tex/luatex/lua-widow-control/lua-widow-control.lua
+++ b/Master/texmf-dist/tex/luatex/lua-widow-control/lua-widow-control.lua
@@ -44,7 +44,14 @@ local function debug(title, text)
local filler = 15 - #title
if text then
- write_nl(write_log, "LWC (" .. title .. string_rep(" ", filler) .. "): " .. text)
+ write_nl(
+ write_log,
+ "LWC (" ..
+ title ..
+ string_rep(" ", filler) ..
+ "): " ..
+ text
+ )
else
write_nl(write_log, "LWC: " .. string_rep(" ", 18) .. title)
end
@@ -52,24 +59,28 @@ end
--[[
- \lwc/ is intended to be format-agonistic. It only runs on Lua\TeX{},
- but there are still some slight differences between formats. Here, we
- detect the format name then set some flags for later processing.
+ \lwc/ is intended to be format-agonistic. It only runs on Lua\TeX{} and
+ LuaMeta\TeX{}, but there are still some slight differences between formats.
+ Here, we detect the format name then set some flags for later processing.
]]
local format = tex.formatname
local context, latex, plain, optex, lmtx
+if status.luatex_engine == "luametatex" then
+ lmtx = true
+end
+
if format:find("cont") then -- cont-en, cont-fr, cont-nl, ...
context = true
- if status.luatex_engine == "luametatex" then
- lmtx = true
- end
elseif format:find("latex") then -- lualatex, lualatex-dev, ...
latex = true
-elseif format == "luatex" or format == "luahbtex" then -- Plain
+elseif format == "luatex" or
+ format == "luahbtex" or
+ format:find("plain")
+then -- Plain
plain = true
elseif format:find("optex") then -- OpTeX
- optex = true
+ optex = _G.optex
end
--[[
@@ -89,22 +100,35 @@ local line_subid = 1
local linebreakpenalty_subid = 1
local par_id = id_from_name("par") or id_from_name("local_par")
local penalty_id = id_from_name("penalty")
+local parfill_subids = {
+ parfillleftskip = 17,
+ parfillrightskip = 16,
+ parinitleftskip = 19,
+ parinitrightskip = 18,
+}
+local vlist_id = id_from_name("vlist")
-- Local versions of globals
local abs = math.abs
local copy = node.copy
local copy_list = node.copy_list or node.copylist
+local effective_glue = node.effective_glue or node.effectiveglue
local find_attribute = node.find_attribute or node.findattribute
local free = node.free
local free_list = node.flush_list or node.flushlist
local get_attribute = node.get_attribute or node.getattribute
+local hpack = node.hpack
local insert_token = token.put_next or token.putnext
+local is_node = node.is_node or node.isnode
local last = node.slide
local linebreak = tex.linebreak
local new_node = node.new
local remove = node.remove
local set_attribute = node.set_attribute or node.setattribute
-local string_char = string.char
+local str_byte = string.byte
+local str_char = string.char
+local str_format = string.format
+local subtype = node.subtype
local tex_box = tex.box
local tex_count = tex.count
local tex_dimen = tex.dimen
@@ -119,8 +143,6 @@ local iftrue = token.create("iftrue")
local INFINITY = 10000
local INSERT_CLASS_MULTIPLE = 1000 * 1000
local INSERT_FIRST_MULTIPLE = 1000
-local llap_offset = math.max(tex.dimen.parindent, tex.sp("12pt"))
-local min_col_width = tex.sp("250pt")
local PAGE_MULTIPLE = 100
local SINGLE_LINE = 50
@@ -128,8 +150,10 @@ lwc.colours = {
expanded = {0.00, 0.70, 0.25},
failure = {0.90, 0.00, 0.25},
moved = {0.25, 0.25, 1.00},
+ cost = {0.50, 0.50, 0.50},
}
+
--[[ Package/module initialization.
Here, we replace any format/engine-specific variables/functions with some
@@ -137,13 +161,15 @@ lwc.colours = {
worrying about any format/engine differences.
]]
local contrib_head,
+ draft_offset,
emergencystretch,
hold_head,
info,
insert_attribute,
max_cost,
- pagenum,
+ page_head,
paragraph_attribute,
+ set_whatsit_field,
shrink_order,
stretch_order,
warning
@@ -155,11 +181,15 @@ if lmtx then
shrink_order = "shrinkorder"
stretch_order = "stretchorder"
hold_head = "holdhead"
+ page_head = "pagehead"
+ set_whatsit_field = node.setwhatsitfield
else
contrib_head = "contrib_head"
shrink_order = "shrink_order"
stretch_order = "stretch_order"
hold_head = "hold_head"
+ page_head = "page_head"
+ set_whatsit_field = node.setfield
end
if context then
@@ -177,20 +207,20 @@ if context then
end
paragraph_attribute = attributes.public(lwc.name .. "_paragraph")
insert_attribute = attributes.public(lwc.name .. "_insert")
- pagenum = function() return tex_count["realpageno"] end
- -- Dimen names
+ -- Dimen/count names
emergencystretch = "lwc_emergency_stretch"
+ draft_offset = "lwc_draft_offset"
max_cost = "lwc_max_cost"
elseif plain or latex or optex then
- pagenum = function() return tex_count[0] end
-
-- Dimen names
if tex.isdimen("g__lwc_emergencystretch_dim") then
emergencystretch = "g__lwc_emergencystretch_dim"
+ draft_offset = "g__lwc_draftoffset_dim"
max_cost = "g__lwc_maxcost_int"
else
emergencystretch = "lwcemergencystretch"
+ draft_offset = "lwcdraftoffset"
max_cost = "lwcmaxcost"
end
@@ -198,8 +228,8 @@ elseif plain or latex or optex then
debug("Plain/LaTeX")
luatexbase.provides_module {
name = lwc.name,
- date = "2022/08/23", --%%slashdate
- version = "2.2.2", --%%version
+ date = "2022/11/22", --%%slashdate
+ version = "3.0.0", --%%version
description = [[
This module provides a LuaTeX-based solution to prevent
@@ -225,30 +255,46 @@ else -- This shouldn't ever happen
Please use LaTeX, Plain TeX, ConTeXt or OpTeX.]]
end
+-- We can't get the value of \\horigin from Lua, but we can guess it
+-- based on the format.
+local horigin
+if optex or (lmtx and context) then
+ horigin = 0
+else
+ horigin = tex.sp("1in")
+end
+
+-- Plain is the only format without a `pre_shipout_filter`
+if plain then
+ luatexbase.create_callback('pre_shipout_filter', 'list')
+end
+
+
--[[ Select the fonts
- We want to use cmr7 for the draft mode cost displays, and the easiest
+ We want to use cmr6 for the draft mode cost displays, and the easiest
way to do so is to just hardcode the font id's. This relies on some
- implementation details; however, it is very unlikely to ever be an issue
+ implementation details; however, it is very unlikely to ever be an issue.
]]
local SMALL_FONT
if plain then
- SMALL_FONT = 4
+ SMALL_FONT = 5
elseif latex then
- SMALL_FONT = 7
+ SMALL_FONT = 6
elseif optex then
- SMALL_FONT = 7
+ SMALL_FONT = 8
elseif context then
- SMALL_FONT = 3
+ SMALL_FONT = fonts.definers.define({
+ name = "LMRoman6-Regular",
+ size = tex.sp("6pt"),
+ })
end
---[[ Table to hold the alternate paragraph versions.
-
- This is global(ish) mutable state, which isn't ideal, but any other way of
- passing this data around would be even worse.
- ]]
-local paragraphs = {}
-local inserts = {}
+-- Global variables
+local paragraphs = {} -- The expanded paragraphs on each page
+local inserts = {} -- Copies of all the inserts on each page
+local costs = {} -- All of the paragraph costs for the document
+local pagenum = 1 -- The current page/column number
--[[ Function definitions
]]
@@ -256,11 +302,13 @@ local inserts = {}
--- Gets the current paragraph and page locations
--- @return string
local function get_location()
- return "At " .. pagenum() .. "/" .. #paragraphs
+ return "At " .. pagenum .. "/" .. #paragraphs
end
---- Prints the starting glyphs and glue of an `hlist`
+--- Prints the starting glyphs and glue of an `hlist`.
+---
+--- Useful for debugging purposes.
---
--- @param head node
--- @return nil
@@ -271,7 +319,7 @@ local function get_chars(head)
for n in traverse(head) do
if n.id == glyph_id then
if n.char < 127 then -- Only ASCII
- chars = chars .. string_char(n.char)
+ chars = chars .. str_char(n.char)
else
chars = chars .. "#" -- Replacement for an unknown glyph
end
@@ -291,8 +339,11 @@ end
---
--- @param demerits number The demerits of the broken paragraph
--- @param lines number The number of lines in the broken paragraph
---- @return number The cost of the broken paragraph
-function lwc.paragraph_cost(demerits, lines)
+--- @param nat_demerits number The demerits of the naturally-broken paragraph
+--- @param nat_lines number The number of lines in the naturally-broken paragraph
+--- @param head node The head of the broken paragraph
+--- @return number cost The cost of the broken paragraph
+function lwc.paragraph_cost(demerits, lines, nat_demerits, nat_lines, head)
return demerits / math.sqrt(lines)
end
@@ -324,9 +375,8 @@ local function next_of_type(head, id, args)
end
end
else
- --[[ Only LMTX has the built-in backwards traverser, so we need to do it
- ourselves here.
- ]]
+ -- Only LMTX has the built-in backwards traverser, so we need to do
+ -- it manually otherwise.
while head do
if head.id == id and
(head.subtype == args.subtype or args.subtype == nil)
@@ -346,33 +396,76 @@ local function next_of_type(head, id, args)
end
+--- Ensures that a paragraph is ready to be broken
+---
+--- Only applies to LuaMetaTeX
+---
+--- @param head node
+--- @return nil
+local function prepare_linebreak(head)
+ if not lmtx then
+ return
+ end
+
+ -- See how many of the par[left/right][init/fill]skips we have
+ local parfills = {}
+ local count = 0
+ for name, subid in pairs(parfill_subids) do
+ parfills[name] = next_of_type(head, glue_id, { subtype = subid })
+ if parfills[name] then
+ count = count + 1
+ end
+ end
+
+ if count == 0 then
+ -- Usual case
+ tex.preparelinebreak(head)
+ elseif count == 4 then
+ -- Already prepared for some reason, ignored
+ else
+ -- Uh oh
+ warning("Weird par(fill/init)skips found!")
+ tex.preparelinebreak(head) -- Try to fix it
+ end
+end
+
+
--- Breaks a paragraph one line longer than natural
---
--- @param head node The unbroken paragraph
+--- @param parfillskip table<number> The {width, stretch, shrink,
+--- stretch_order, shrink_order} to set
+--- for the \\parfillskip
--- @return node long_node The broken paragraph
--- @return table long_info An info table about the broken paragraph
-local function long_paragraph(head)
+local function long_paragraph(head, parfillskip)
-- We can't modify the original paragraph
head = copy_list(head)
- if lmtx then
- tex.preparelinebreak(head)
- end
-
- -- Prevent ultra-short last lines (\TeX{}Book p. 104), except with narrow columns
- -- Equivalent to \\parfillskip=0pt plus 0.8\\hsize
- local parfillskip = last(head)
+ prepare_linebreak(head)
- if tex.hsize > min_col_width then
- parfillskip[stretch_order] = 0
- parfillskip.stretch = 0.8 * tex.hsize -- Last line must be at least 20% long
- end
+ -- TODO node.setglue is broken in LMTX, so we have to do this manually
+ local n = last(head)
+ n.width = parfillskip[1]
+ n.stretch = parfillskip[2]
+ n.shrink = parfillskip[3]
+ n[stretch_order] = parfillskip[4]
+ n[shrink_order] = parfillskip[5]
-- Break the paragraph 1 line longer than natural
- return linebreak(head, {
+ local long_node, long_info = linebreak(head, {
looseness = 1,
emergencystretch = tex_dimen[emergencystretch],
})
+
+ -- Mark the last line for the costs display
+ set_attribute(
+ last(long_node),
+ paragraph_attribute,
+ -1 * (#paragraphs + 1 + (PAGE_MULTIPLE * pagenum))
+ )
+
+ return long_node, long_info
end
@@ -384,9 +477,7 @@ local function natural_paragraph(head)
-- We can't modify the original paragraph
head = copy_list(head)
- if lmtx then
- tex.preparelinebreak(head)
- end
+ prepare_linebreak(head)
-- Break the paragraph naturally to get \\prevgraf
local natural_node, natural_info = linebreak(head)
@@ -396,88 +487,50 @@ local function natural_paragraph(head)
end
-lwc.draft_mode = false
+local show_colours = false
--- Changes the text colour in a node list if draft mode is active
---
--- @param head node The first node to colour
---- @param colour table<number> A 3-tuple of RGB values
+--- @param colour string The name of a colour in `lwc.colours`
--- @return node head The coloured node
local function colour_list(head, colour)
- if not lwc.draft_mode then
+ if not show_colours then
return head
end
- -- Adapted from https://tex.stackexchange.com/a/372437
- -- \\pdfextension colorstack is ignored in LMTX
- local start_colour = new_node("whatsit", "pdf_colorstack")
- start_colour.stack = 0
- start_colour.command = 1
- start_colour.data = string.format("%.2f %.2f %.2f rg", table.unpack(colour))
-
- local end_colour = new_node("whatsit", "pdf_colorstack")
- end_colour.stack = 0
- end_colour.command = 2
-
- start_colour.next = head
- last(head).next = end_colour
-
- return start_colour
-end
+ local pdf_colour = str_format(
+ "%.2f %.2f %.2f rg",
+ table.unpack(lwc.colours[colour])
+ )
+ if optex and optex.set_node_color then
+ for n in node.traverse(head) do
+ optex.set_node_color(n, pdf_colour)
+ end
---- Generate an \\llap'ed box containing the provided string
----
---- @param str string The string to typeset
---- @return node head The box node
-local function llap_string(str)
- local first = new_node("glue")
- first.width = llap_offset
-
- local m = first
- for letter in str:gmatch(".") do
- local n = new_node("glyph")
- n.font = SMALL_FONT
- n.char = string.byte(letter)
-
- m.next = n
- m = n
+ return head
end
- local hss = new_node("glue")
- hss.stretch = 1
- hss[stretch_order] = 1
- hss.shrink = 1
- hss[shrink_order] = 1
- m.next = hss
-
- return node.hpack(first, 0, "exactly")
-end
-
-
---- Typesets the cost of a paragraph beside it in draft mode
----
---- @param paragraph node
---- @param cost number
---- @return nil
-local function show_cost(paragraph, cost)
- if not lwc.draft_mode then
- return
+ if context then
+ nodes.tracers.colors.setlist(head, "lwc_" .. colour)
+ return head
end
- local last_hlist_end = last(next_of_type(
- last(paragraph),
- hlist_id,
- { subtype = line_subid, reverse = true }
- ).list)
+ -- Adapted from https://tex.stackexchange.com/a/372437 and
+ -- https://github.com/zauguin/luametalatex/issues/8.
+ local start_colour = new_node("whatsit", subtype("pdf_colorstack"))
+ set_whatsit_field(start_colour, "stack", 0)
+ set_whatsit_field(start_colour, "command", 1)
+ set_whatsit_field(start_colour, "data", pdf_colour)
- local cost_str
- if cost < math.maxinteger then
- cost_str = string.format("%.0f", cost)
- else
- cost_str = "infinite"
- end
+ local end_colour = new_node("whatsit", subtype("pdf_colorstack"))
+ set_whatsit_field(end_colour, "stack", 0)
+ set_whatsit_field(end_colour, "command", 2)
+
+ start_colour.next = head
+ last(head).next = end_colour
- last_hlist_end.next = llap_string(cost_str)
+ return start_colour
end
@@ -485,10 +538,10 @@ end
---
--- Called by the `pre_linebreak_filter` callback
---
---- @param head node
---- @return node
+--- @param head node The pre-broken paragraph
+--- @return node head The unmodified `head` argument
function lwc.save_paragraphs(head)
- if (head.id ~= par_id and context) or -- Ensure that we were actually given a par
+ if (head.id ~= par_id and context) or -- Make sure that `head` is a paragraph
status.output_active or -- Don't run during the output routine
tex.nest.ptr > 1 -- Don't run inside boxes
then
@@ -504,10 +557,27 @@ function lwc.save_paragraphs(head)
lwc.callbacks.disable_box_warnings.enable()
end
- long_node, long_info = long_paragraph(head)
-
natural_info = natural_paragraph(head)
+ -- Prevent ultra-short last lines (\TeX{}book p. 104). Equivalent to
+ -- \\parfillskip=0.75\\hsize plus 0.05\\hsize minus 0.75\\hsize.
+ -- From http://petr.olsak.net/ftp/olsak/tbn/tbn.pdf p. 234 (via Jan Sustek)
+ long_node, long_info = long_paragraph(
+ head,
+ {0.75 * tex.hsize, 0.05 * tex.hsize, 0.75 * tex.hsize, 0, 0}
+ )
+
+ if long_info.prevgraf ~= natural_info.prevgraf + 1 then
+ -- The \\parfillskip settings with \\looseness=1 can sometimes
+ -- lengthen paragraphs by two lines instead of one. If this happens,
+ -- we fall back to a slightly-worse \\parfillskip setting.
+ free_list(long_node)
+ long_node, long_info = long_paragraph(
+ head,
+ {0, 0.8 * tex.hsize, false, 0, false}
+ )
+ end
+
if renable_box_warnings then
lwc.callbacks.disable_box_warnings.disable()
end
@@ -519,7 +589,13 @@ function lwc.save_paragraphs(head)
last(long_node).next = prevdepth
end
- local long_cost = lwc.paragraph_cost(long_info.demerits, long_info.prevgraf)
+ local long_cost = lwc.paragraph_cost(
+ long_info.demerits,
+ long_info.prevgraf,
+ natural_info.demerits,
+ natural_info.prevgraf,
+ long_node
+ )
if long_info.prevgraf ~= natural_info.prevgraf + 1 or
long_cost < 10 -- Any paragraph that is "free" to expand is suspicious
@@ -528,11 +604,12 @@ function lwc.save_paragraphs(head)
long_cost = math.maxinteger
end
+ -- The initial glue can disappear in ConTeXt's grid mode, so we
+ -- save starting at the first hlsit
local saved_node = next_of_type(long_node, hlist_id, { subtype = line_subid })
- show_cost(saved_node, long_cost)
for n in traverse_id(hlist_id, saved_node) do
- n.list = colour_list(n.list, lwc.colours.expanded)
+ n.list = colour_list(n.list, "expanded")
end
table.insert(paragraphs, {
@@ -542,20 +619,24 @@ function lwc.save_paragraphs(head)
free_list(long_node)
+ costs[#paragraphs + (PAGE_MULTIPLE * pagenum)] = long_cost
+
-- Print some debugging information
- get_chars(head)
- debug(get_location(), "nat lines " .. natural_info.prevgraf)
- debug(
- get_location(),
- "nat cost " ..
- lwc.paragraph_cost(natural_info.demerits, natural_info.prevgraf)
- )
- debug(get_location(), "long lines " .. long_info.prevgraf)
- debug(
- get_location(),
- "long cost " ..
- lwc.paragraph_cost(long_info.demerits, long_info.prevgraf)
- )
+ if lwc.debug then
+ get_chars(head)
+ debug(get_location(), "nat lines " .. natural_info.prevgraf)
+ debug(
+ get_location(),
+ "nat cost " ..
+ lwc.paragraph_cost(natural_info.demerits, natural_info.prevgraf)
+ )
+ debug(get_location(), "long lines " .. long_info.prevgraf)
+ debug(
+ get_location(),
+ "long cost " ..
+ lwc.paragraph_cost(long_info.demerits, long_info.prevgraf)
+ )
+ end
-- \ConTeXt{} crashes if we return `true`
return head
@@ -572,39 +653,40 @@ end
--- @return nil
local function mark_paragraphs(head)
-- Tag the paragraphs
- if not status.output_active then -- Don't run during the output routine
- -- Get the start and end of the paragraph
- local top = next_of_type(head, hlist_id, { subtype = line_subid })
- local bottom = last(head)
+ if status.output_active then
+ -- Don't run during the output routine
+ return
+ end
- while bottom.id == insert_id do
- bottom = bottom.prev
- end
+ -- Get the start and end of the paragraph
+ local top = next_of_type(head, hlist_id, { subtype = line_subid })
+ local bottom = last(head)
- if top ~= bottom then
- set_attribute(
- top,
- paragraph_attribute,
- #paragraphs + (PAGE_MULTIPLE * pagenum())
- )
- set_attribute(
- bottom,
- paragraph_attribute,
- -1 * (#paragraphs + (PAGE_MULTIPLE * pagenum()))
- )
- else
- -- We need a special tag for a 1-line paragraph since the node can only
- -- have a single attribute value
- set_attribute(
- top,
- paragraph_attribute,
- #paragraphs + (PAGE_MULTIPLE * pagenum()) + SINGLE_LINE
- )
- end
+ -- The inserts disappear before `pre_output_routine`, so we shouldn't
+ -- mark them.
+ while bottom.id == insert_id do
+ bottom = bottom.prev
+ end
- if #paragraphs > 0 then
- show_cost(head, paragraphs[#paragraphs].cost)
- end
+ if top ~= bottom then
+ set_attribute(
+ top,
+ paragraph_attribute,
+ #paragraphs + (PAGE_MULTIPLE * pagenum)
+ )
+ set_attribute(
+ bottom,
+ paragraph_attribute,
+ -1 * (#paragraphs + (PAGE_MULTIPLE * pagenum))
+ )
+ else
+ -- We need a special tag for a 1-line paragraph since the node can only
+ -- have a single attribute value
+ set_attribute(
+ top,
+ paragraph_attribute,
+ #paragraphs + (PAGE_MULTIPLE * pagenum) + SINGLE_LINE
+ )
end
end
@@ -625,14 +707,15 @@ local function mark_inserts(head)
-- Tag the insert's content so that we can find it later
set_attribute(insert.list, insert_attribute, #inserts)
+ -- We need to tag all lines---not just the start and the end---since
+ -- \TeX{} can split the insert between pages at any point.
for n in traverse(insert.list.next) do
set_attribute(n, insert_attribute, -1 * #inserts)
end
- --[[ Each hlist/line can have multiple inserts, but so we can't just tag
- the hlist as we go. Instead, we need save up all of their indices,
- then tag the hlist with the first and last indices.
- ]]
+ -- Each hlist/line can have multiple inserts, but so we can't just tag
+ -- the hlist as we go. Instead, we need save up all of their indices,
+ -- then tag the hlist with the first and last indices.
insert_indices[#insert_indices+1] = #inserts
if not insert.next or
@@ -642,18 +725,18 @@ local function mark_inserts(head)
local insert_class
if lmtx then
- -- FIXME: temporarily hardcode the main "footnote" class
- insert_class = 4 -- insert.index
+ insert_class = insert.index
else
insert_class = insert.subtype
end
- --[[ We tag the first element of the hlist/line with an integer
- that holds the insert class and the first and last indices
- of the inserts contained in the line. This won't work if
- the line has multiple classes of inserts, but I don't think
- that happens in real-world documents.
- ]]
+ -- We tag the first element of the hlist/line with an integer
+ -- that holds the insert class and the first and last indices
+ -- of the inserts contained in the line. This won't work if
+ -- the line has multiple classes of inserts, but I don't think
+ -- that happens in real-world documents. If this does turn out
+ -- to be an issue, we can get the insert's class from it's copy
+ -- at `pre_output_filter` instead of saving it now.
set_attribute(
hlist_before.list,
insert_attribute,
@@ -672,8 +755,8 @@ end
--- Saves the inserts and tags a typeset paragraph. Called by the
--- `post_linebreak_filter` callback.
---
---- @param head node
---- @return node
+--- @param head node The head of the broken paragraph
+--- @return node head The unmodified `head` parameter
function lwc.mark_paragraphs(head)
mark_paragraphs(head)
mark_inserts(head)
@@ -686,7 +769,7 @@ end
---
--- @param penalty number
--- @return boolean
-function is_matching_penalty(penalty)
+local function is_matching_penalty(penalty)
local widowpenalty = tex.widowpenalty
local clubpenalty = tex.clubpenalty
local displaywidowpenalty = tex.displaywidowpenalty
@@ -694,7 +777,9 @@ function is_matching_penalty(penalty)
penalty = penalty - tex.interlinepenalty
- -- https://tug.org/TUGboat/tb39-3/tb123mitt-widows-code.pdf#subsection.0.2.1
+ -- Adapted from https://tug.org/TUGboat/tb39-3/tb123mitt-widows-code.pdf.
+ -- This only takes into account the original \TeX{} penalties, not the
+ -- "new" \eTeX{} \\(club/widow/broken)penalties commands.
return penalty ~= 0 and
penalty < INFINITY and (
penalty == widowpenalty or
@@ -712,8 +797,24 @@ function is_matching_penalty(penalty)
end
+--- Determines if we should "activate" \lwc/ for the current page/column.
+---
+--- Users can redefine this if they wish.
+---
+--- @param penalty number The \\outputpenalty for the current page/column
+--- @param paragraphs table<table<string, node|number>> The `paragraphs` table
+--- @param head node The head of the current page/column
+--- @return boolean activate True if \lwc/ should move the last line on this page
+function lwc.should_remove_widows(penalty, paragraphs, head)
+ return is_matching_penalty(penalty)
+end
+
--- Reset any state saved between pages
---
+--- This function is *vital* to ensure that we don't leak any nodes.
+--- If we do leak nodes, then very large documents will slow down and
+--- eventually fail to compile.
+---
--- @return nil
local function reset_state()
for _, paragraph in ipairs(paragraphs) do
@@ -725,6 +826,8 @@ local function reset_state()
free(insert)
end
inserts = {}
+
+ pagenum = pagenum + 1
end
@@ -732,15 +835,15 @@ end
---
--- @return nil
local function remove_widows_fail()
- warning("Widow/Orphan/broken hyphen NOT removed on page " .. pagenum())
+ warning("Widow/Orphan/Broken Hyphen NOT removed on page " .. pagenum)
local last_line = next_of_type(
- last(tex_lists.page_head),
+ last(tex_lists[page_head]),
hlist_id,
{ subtype = line_subid, reverse = true }
)
if last_line then
- last_line.list = colour_list(last_line.list, lwc.colours.failure)
+ last_line.list = colour_list(last_line.list, "failure")
end
local next_first_line = next_of_type(
@@ -749,7 +852,7 @@ local function remove_widows_fail()
{ subtype = line_subid }
)
if next_first_line then
- next_first_line.list = colour_list(next_first_line.list, lwc.colours.failure)
+ next_first_line.list = colour_list(next_first_line.list, "failure")
end
reset_state()
@@ -780,10 +883,12 @@ local function first_last_paragraphs(head)
-- Find the first paragraph on the page, from the top
local first_val, first_head = find_attribute(head, paragraph_attribute)
- while abs(first_val) // PAGE_MULTIPLE == pagenum() - 1 do
- --[[ If the first complete paragraph on the page was initially broken on the
- previous page, then we can't expand it here so we need to skip it.
- ]]
+ while abs(first_val) // PAGE_MULTIPLE == pagenum - 1 do
+ -- If the first complete paragraph on the page was initially broken on the
+ -- previous page, then we can't expand it here. Why can't we expand it?
+ -- Well, expanding it will nearly always change how the first few lines
+ -- are printed, but we can't modify those since they've already been
+ -- shipped out. So, we need to skip these paragraphs.
first_val, first_head = find_attribute(
first_head.next,
paragraph_attribute
@@ -826,11 +931,11 @@ local function best_paragraph(head)
debug(
"selected para",
- pagenum() .. "/" .. best_index .. " (" .. best_cost .. ")"
+ pagenum .. "/" .. best_index .. " (" .. best_cost .. ")"
)
if best_cost > tex_count[max_cost] or
- best_index == last_paragraph_index or
+ best_index == last_paragraph_index or -- Shouldn't happen
best_cost == math.maxinteger
then
return nil
@@ -858,7 +963,8 @@ local function get_inserts(last_line)
-- Demux the insert values
local class = line_value // INSERT_CLASS_MULTIPLE
- local first_index = (line_value % INSERT_CLASS_MULTIPLE) // INSERT_FIRST_MULTIPLE
+ local first_index = (line_value % INSERT_CLASS_MULTIPLE)
+ // INSERT_FIRST_MULTIPLE
local last_index = line_value % INSERT_FIRST_MULTIPLE
-- Get the output box containing the insert boxes
@@ -866,23 +972,37 @@ local function get_inserts(last_line)
if lmtx then
insert_box = tex.getinsertcontent(class)
+ -- `getinsertcontent` resets the insert box, so we need to
+ -- re-set it.
+ tex.setinsertcontent(class, insert_box)
else
insert_box = tex_box[class]
end
- -- Get any portions of the insert held over until the next page
- local split_insert = next_of_type(
- tex_lists[hold_head],
- insert_id,
- { subtype = class }
- )
+ -- Get any portions of the insert "held over" until the next page
+ local split_insert
+ if lmtx then
+ split_insert = next_of_type(
+ tex_lists[hold_head],
+ insert_id,
+ { index = class }
+ )
+ else
+ split_insert = next_of_type(
+ tex_lists[hold_head],
+ insert_id,
+ { subtype = class }
+ )
+ end
+ -- We use the same procedure for this page and the next page
for i, insert in ipairs { insert_box, split_insert } do
local m = insert and insert.list
while m do -- Iterate through the insert box
local box_value
box_value, m = find_attribute(m, insert_attribute)
+ local next = m.next
if not m then
break
@@ -898,13 +1018,15 @@ local function get_inserts(last_line)
table.insert(selected_inserts, copy(inserts[box_value]))
end
- m = free(m)
- else
- m = m.next
+ free(m)
end
+
+ m = next
end
end
+ -- If the box has no contents, then void it so that any \\ifvoid
+ -- tests work correctly in the output routine.
if not insert_box.list then
tex_box[class] = nil
end
@@ -917,7 +1039,11 @@ local function get_inserts(last_line)
end
if #selected_inserts ~= 0 then
- info("Moving footnotes on page " .. pagenum())
+ -- This is a somewhat-risky process, so we print an info
+ -- message just in case something goes wrong. We can probably
+ -- remove this in the future if we're sure that everything
+ -- is working correctly.
+ info("Moving footnotes on page " .. pagenum)
end
return selected_inserts
@@ -932,7 +1058,7 @@ lwc.nobreak_behaviour = "keep"
--- some low-level node shuffling.
---
--- @param head node The node representing the start of the page
---- @return boolean success
+--- @return boolean success Set to false if something went wrong
local function move_last_line(head)
-- Start of final paragraph
debug("remove_widows", "moving last line")
@@ -977,19 +1103,19 @@ local function move_last_line(head)
potential_penalty.subtype == linebreakpenalty_subid and
is_matching_penalty(potential_penalty.penalty)
then
- warning("Making a new widow/orphan/broken hyphen on page " .. pagenum())
+ warning("Making a new widow/orphan/broken hyphen on page " .. pagenum)
local second_last_line = next_of_type(
potential_penalty,
hlist_id,
{ subtype = line_subid, reverse = true }
)
- second_last_line.list = colour_list(second_last_line.list, lwc.colours.failure)
+ second_last_line.list = colour_list(second_last_line.list, "failure")
end
last_line = copy_list(n)
- last_line.list = colour_list(last_line.list, lwc.colours.moved)
+ last_line.list = colour_list(last_line.list, "moved")
-- Reinsert any inserts originally present in this moved line
local selected_inserts = get_inserts(last_line)
@@ -1019,13 +1145,29 @@ end
--- @param head node
--- @param paragraph_index number
local function replace_paragraph(head, paragraph_index)
- local target_node = copy_list(paragraphs[paragraph_index].node)
+ -- Remove any inserts. They are completely ignored after
+ -- the page has been broken, but they can upset LuaMetaLaTeX if
+ -- they're found somewhere unexpected.
+ local target_node, last_target_node
+ for n in traverse(paragraphs[paragraph_index].node) do
+ -- Just removing the inserts from the list doesn't work properly,
+ -- so we instead copy over everything that isn't an insert.
+ if n.id ~= insert_id then
+ if not target_node then
+ target_node = copy(n)
+ last_target_node = target_node
+ else
+ last_target_node.next = copy(n)
+ last_target_node = last_target_node.next
+ end
+ end
+ end
local start_found = false
local end_found = false
local free_nodes_begin
- -- Loop through all of the nodes on the page with the lwc attribute
+ -- Loop through all of the nodes on the page with the \lwc/ attribute
local n = head
while n do
local value
@@ -1038,8 +1180,8 @@ local function replace_paragraph(head, paragraph_index)
debug("remove_widows", "found " .. value)
-- Insert the start of the replacement paragraph
- if value == paragraph_index + (PAGE_MULTIPLE * pagenum()) or
- value == paragraph_index + (PAGE_MULTIPLE * pagenum()) + SINGLE_LINE
+ if value == paragraph_index + (PAGE_MULTIPLE * pagenum) or
+ value == paragraph_index + (PAGE_MULTIPLE * pagenum) + SINGLE_LINE
then
debug("remove_widows", "replacement start")
start_found = true
@@ -1047,7 +1189,9 @@ local function replace_paragraph(head, paragraph_index)
-- Fix the `\\baselineskip` glue between paragraphs
height_difference = (
next_of_type(n, hlist_id, { subtype = line_subid }).height -
- next_of_type(target_node, hlist_id, { subtype = line_subid }).height
+ next_of_type(
+ target_node, hlist_id, { subtype = line_subid }
+ ).height
)
local prev_bls = next_of_type(
@@ -1065,8 +1209,8 @@ local function replace_paragraph(head, paragraph_index)
end
-- Insert the end of the replacement paragraph
- if value == -1 * (paragraph_index + (PAGE_MULTIPLE * pagenum())) or
- value == paragraph_index + (PAGE_MULTIPLE * pagenum()) + SINGLE_LINE
+ if value == -(paragraph_index + (PAGE_MULTIPLE * pagenum)) or
+ value == paragraph_index + (PAGE_MULTIPLE * pagenum) + SINGLE_LINE
then
debug("remove_widows", "replacement end")
end_found = true
@@ -1095,7 +1239,7 @@ local function replace_paragraph(head, paragraph_index)
if start_found and end_found then
free_list(free_nodes_begin)
else
- warning("Paragraph NOT expanded on page " .. pagenum())
+ warning("Paragraph NOT expanded on page " .. pagenum)
end
end
@@ -1114,7 +1258,7 @@ function lwc.remove_widows(head)
debug("outputpenalty", tex.outputpenalty .. " " .. #paragraphs)
-- See if there is a widow/orphan for us to remove
- if not is_matching_penalty(tex.outputpenalty) then
+ if not lwc.should_remove_widows(tex.outputpenalty, paragraphs, head) then
reset_state()
return head
end
@@ -1154,10 +1298,9 @@ function lwc.remove_widows(head)
-- Replace the chosen paragraph with its expanded version
replace_paragraph(head, paragraph_index)
- --[[ The final \\box255 needs to be exactly \\vsize tall to avoid
- over/underfull box warnings, so we correct any discrepancies
- here.
- ]]
+ -- The final \\box255 needs to be exactly \\vsize tall to avoid
+ -- over/underfull box warnings, so we correct any discrepancies
+ -- here.
local new_vpack = vpack(head)
local new_height_diff = new_vpack.height - vsize
new_vpack.list = nil
@@ -1169,7 +1312,8 @@ function lwc.remove_widows(head)
free(bls)
if abs(net_height_diff) > 0 and
- -- A difference larger than 0.25\\baselineskip is probably not from \lwc/
+ -- A difference larger than 0.25\\baselineskip is probably not from
+ -- \lwc/, so we let those warnings surface
abs(net_height_diff) < bls_width / 4
then
local bottom_glue = new_node("glue")
@@ -1181,7 +1325,7 @@ function lwc.remove_widows(head)
"Widow/orphan/broken hyphen successfully removed at paragraph "
.. paragraph_index
.. " on page "
- .. pagenum()
+ .. pagenum
)
reset_state()
@@ -1190,6 +1334,143 @@ function lwc.remove_widows(head)
end
+local show_costs = false
+--- Add the paragraph to the list of paragraphs on the page.
+---
+--- Called immediately before the page is shipped out so that we can get
+--- the costs on the correct side in multi-column layouts.
+---
+--- To evenly align all of the costs in the margins, we need to know the
+--- the exact position of the start and end of the paragraph on the page.
+--- This is surprisingly complicated.
+---
+--- @param head node The box to be shipped out
+--- @return true
+function lwc.show_costs (head)
+ if not show_costs then
+ return true
+ end
+
+ local pagewidth = tex.pagewidth or layouts.getpagedimensions()
+
+ --- Loop over each sublist, add up the total width, and show the costs.
+ ---
+ --- @param n node The node to loop over
+ --- @param width number The accumulated width so far
+ --- @param parent node The parent node of the current list
+ --- @return nil
+ local function recurse(n, width, parent)
+ for m in traverse(n) do
+ -- Anything with an \\hbox parent and a width is actual width.
+ -- (If it had a \\vbox parent, then n.width would actually be height.)
+ local self_width = 0
+ if m.id == glue_id and parent.id == hlist_id then
+ self_width = effective_glue(m, parent)
+ elseif m.width and parent.id == hlist_id then
+ self_width = m.width
+ end
+
+ -- A node's "shift" attribute is horizontal only if the parent is
+ -- a \\vbox. This corresponds to the primatives \\moveleft and
+ -- \\moveright. (If the parent is a \\hbox, then n.shift is
+ -- vertical and corresponds to \\raise.)
+ local shift = 0
+ if m.shift and
+ (parent.id == vlist_id or
+ not is_node(parent))
+ then
+ shift = m.shift
+ end
+
+ width = width + self_width
+
+ local attr = get_attribute(m, paragraph_attribute)
+ if attr and abs(attr) % PAGE_MULTIPLE >= SINGLE_LINE then
+ attr = -1 * (abs(attr) - SINGLE_LINE)
+ end
+
+ local cost = costs[abs(attr or 0)]
+
+ if attr and attr < 0 and cost and m.list then
+ -- We've found the end of a marked paragraph!
+
+ -- Generate the \\hbox containing the formatted cost
+ local cost_str
+ if not cost then
+ return
+ elseif cost < math.maxinteger then
+ cost_str = str_format("%.0f", cost)
+ else
+ cost_str = "infinite"
+ end
+
+ local prev, first
+ for letter in cost_str:gmatch(".") do
+ local curr = new_node("glyph")
+ curr.font = SMALL_FONT
+ curr.char = str_byte(letter)
+
+ if not first then
+ first = curr
+ else
+ prev.next = curr
+ end
+ prev = curr
+ end
+
+ local text = hpack(colour_list(first, "cost"))
+
+ -- Make an \\hss to make sure that our `\\hbox`es aren't overfull
+ local hss = new_node("glue")
+ hss.stretch = 1
+ hss[stretch_order] = 1
+ hss.shrink = 1
+ hss[shrink_order] = 1
+
+ local hbox
+ local offset = new_node("glue")
+
+ if (width >= pagewidth / 2) or
+ (m.width >= 0.4 * pagewidth)
+ then -- Right column or single-column
+ -- Costs in the right margin
+ offset.width = (
+ pagewidth -
+ width -
+ m.width -
+ shift -
+ tex_dimen[draft_offset]
+ )
+ text.next = hss
+ hbox = hpack(text, 0, "exactly")
+ else -- Left column
+ -- Costs in the left margin
+ offset.width = (
+ tex_dimen[draft_offset] -
+ m.width -
+ width -
+ shift
+ )
+ hss.next = text
+ hbox = hpack(hss, 0, "exactly")
+ end
+
+ last(m.list).next = offset
+ offset.next = hbox
+ elseif m.list then
+ recurse(m.list, width - self_width + shift, m)
+ end
+ end
+ end
+
+ -- Start at the root of the page
+ recurse(head.list, (tex.hoffset or 0) + horigin, {})
+
+ -- LaTeX requires us to always return true here
+ return true
+end
+
+
--- Create a table of functions to enable or disable a given callback
---
--- @param t table Parameters of the callback to create
@@ -1198,8 +1479,8 @@ end
--- name: string = The name/ID of the callback
--- category: string = The category for a \ConTeXt{} "Action"
--- position: string = The "position" for a \ConTeXt{} "Action"
---- lowlevel: boolean = If we should use a lowlevel \LuaTeX{} callback instead of a
---- \ConTeXt{} "Action"
+--- lowlevel: boolean = If we should use a lowlevel \LuaTeX{} callback
+--- instead of a \ConTeXt{} "Action"
--- @return table t Enablers/Disablers for the callback
--- enable: function = Enable the callback
--- disable: function = Disable the callback
@@ -1215,9 +1496,8 @@ local function register_callback(t)
}
elseif context and not t.lowlevel then
return {
- --[[ Register the callback when the table is created,
- but activate it when `enable()` is called.
- ]]
+ -- Register the callback when the table is created,
+ -- but activate it when `enable()` is called.
enable = nodes.tasks.appendaction(t.category, t.position, "lwc." .. t.name)
or function()
nodes.tasks.enableaction(t.category, "lwc." .. t.name)
@@ -1227,18 +1507,18 @@ local function register_callback(t)
end,
}
elseif context and t.lowlevel then
- --[[ Some of the callbacks in \ConTeXt{} have no associated "actions". Unlike
- with \LuaTeX{}base, \ConTeXt{} leaves some \LuaTeX{} callbacks unregistered
- and unfrozen. Because of this, we need to register some callbacks at the
- engine level. This is fragile though, because a future \ConTeXt{} update
- may decide to register one of these functions, in which case
- \lwc/ will crash with a cryptic error message.
- ]]
+ -- Some of the callbacks in \ConTeXt{} have no associated "actions".
+ -- Unlike with \LuaTeX{}base, \ConTeXt{} leaves some \LuaTeX{} callbacks
+ -- unregistered and unfrozen. Because of this, we need to register some
+ -- callbacks at the engine level. This is fragile though, because a
+ -- future \ConTeXt{} update may decide to register one of these
+ -- functions, in which case \lwc/ will crash with a cryptic error
+ -- message.
return {
enable = function() callback.register(t.callback, t.func) end,
disable = function() callback.register(t.callback, nil) end,
}
- elseif optex then -- Op\TeX{} is very similar to luatexbase
+ elseif optex then -- Op\TeX{} is luckily very similar to luatexbase
return {
enable = function()
callback.add_to_callback(t.callback, t.func, t.name)
@@ -1279,6 +1559,13 @@ lwc.callbacks = {
category = "finalizers",
position = "after",
}),
+ show_costs = register_callback({
+ callback = "pre_shipout_filter",
+ func = lwc.show_costs,
+ name = "show_costs",
+ category = "shipouts",
+ position = "finishers",
+ }),
}
@@ -1303,11 +1590,10 @@ function lwc.disable_callbacks()
if lwc_enabled then
lwc.callbacks.save_paragraphs.disable()
lwc.callbacks.mark_paragraphs.disable()
- --[[ We do \emph{not} disable `remove_widows` callback, since we still want
- to expand any of the previously-saved paragraphs if we hit an orphan
- or a widow.
- ]]
+ -- We do *not* disable `remove_widows` callback, since we still want
+ -- to expand any of the previously-saved paragraphs if we hit an orphan
+ -- or a widow.
lwc_enabled = false
else
info("Already disabled")
@@ -1358,6 +1644,8 @@ local function register_tex_cmd(name, func, args)
scanners[#scanners+1] = token['scan_' .. arg]
end
+ -- An intermediate function that properly "scans" for its arguments
+ -- in the \TeX{} side.
scanning_func = function()
local values = {}
for _, scanner in ipairs(scanners) do
@@ -1385,7 +1673,8 @@ local function register_tex_cmd(name, func, args)
end
end
-
+--[[ Make all of the \lwc/ Lua commands available from \TeX{}
+ ]]
register_tex_cmd("if_enabled", lwc.if_lwc_enabled, {})
register_tex_cmd("enable", lwc.enable_callbacks, {})
register_tex_cmd("disable", lwc.disable_callbacks, {})
@@ -1404,12 +1693,26 @@ register_tex_cmd(
{ "string" }
)
register_tex_cmd(
- "draft",
+ "show_costs",
+ function(str)
+ show_costs = str ~= "0" and str ~= "false" and str ~= "stop"
+ end,
+ { "string" }
+)
+register_tex_cmd(
+ "show_colours",
function(str)
- lwc.draft_mode = str ~= "0" and str ~= "false" and str ~= "stop"
+ show_colours = str ~= "0" and str ~= "false" and str ~= "stop"
end,
{ "string" }
)
+register_tex_cmd(
+ "pre_shipout",
+ function(box)
+ luatexbase.call_callback('pre_shipout_filter', tex_box[box])
+ end,
+ { "int" }
+)
--- Silence the luatexbase "Enabling/Removing <callback>" info messages
---
@@ -1424,8 +1727,8 @@ register_tex_cmd(
--- with useless error messages, so we disable it here.
---
--- This uses the Lua `debug` library to internally modify the log upvalue in the
---- `add_to_callback` function. This is almost certainly a terrible idea, but I don't
---- know of a better way.
+--- `add_to_callback` function. This is almost certainly a terrible idea, but I
+--- don't know of a better way to do it.
---
--- @return nil
local function silence_luatexbase()
@@ -1451,17 +1754,28 @@ local function silence_luatexbase()
end
---[[ Call `silence_luatexbase` in Plain and LaTeX, unless the undocmented global
- `LWC_NO_DEBUG` is set. We provide this opt-out in case something goes awry
- with the `debug` library calls.
- ]]
+-- Call `silence_luatexbase` in Plain and LaTeX, unless the undocmented global
+-- `LWC_NO_DEBUG` is set. We provide this opt-out in case something goes awry
+-- with the `debug` library calls.
if (plain or latex) and
not LWC_NO_DEBUG --- @diagnostic disable-line
then
silence_luatexbase()
end
+-- Register colours for ConTeXt
+if context then
+ for colour, values in pairs(lwc.colours) do
+ attributes.colors.defineprocesscolor(
+ "lwc_" .. colour,
+ str_format("r=%.2f, g=%.2f, b=%.2f", table.unpack(values))
+ )
+ end
+end
+
-- Activate \lwc/
lwc.callbacks.remove_widows.enable()
+lwc.callbacks.show_costs.enable()
return lwc
+
diff --git a/Master/texmf-dist/tex/luatex/lua-widow-control/lua-widow-control.tex b/Master/texmf-dist/tex/luatex/lua-widow-control/lua-widow-control.tex
index 9c26719eb6f..e25342ee4b0 100644
--- a/Master/texmf-dist/tex/luatex/lua-widow-control/lua-widow-control.tex
+++ b/Master/texmf-dist/tex/luatex/lua-widow-control/lua-widow-control.tex
@@ -3,7 +3,7 @@
% SPDX-License-Identifier: MPL-2.0+
% SPDX-FileCopyrightText: 2022 Max Chernoff
-\wlog{lua-widow-control v2.2.2} %%version
+\wlog{lua-widow-control v3.0.0} %%version
\ifx\directlua\undefined
\errmessage{%
@@ -14,6 +14,9 @@
\catcode`@=11
+% We need to change some code if we're using LuaMetaTeX
+\def\lwc@iflmtx{\ifnum\luatexversion>200\relax}
+
\input ltluatex % \LuaTeX{}Base
\clubpenalty=1
@@ -24,6 +27,9 @@
\newdimen\lwcemergencystretch
\lwcemergencystretch=3em
+\newdimen\lwcdraftoffset
+\lwcdraftoffset=1in
+
\newcount\lwcmaxcost
\lwcmaxcost=2147483647
@@ -32,12 +38,20 @@
% Here, we enable font expansion/contraction. It isn't strictly necessary for
% \lwc/'s functionality; however, it is required for the
% lengthened paragraphs to not have terrible spacing.
-\expandglyphsinfont\the\font 20 20 5
-\adjustspacing=2
+\lwc@iflmtx\else
+ \expandglyphsinfont\the\font 20 20 5
+ \adjustspacing=2
+\fi
% Enable \lwc/ by default when the package is loaded.
\lwc@enable
+% Fix some strange LMTX bugs
+\lwc@iflmtx
+ \normalizelinemode=\numexpression\normalizelinemode bor 2\relax
+\fi
+
+
% Expansion of some parts of the document, such as section headings, is quite
% undesirable, so we'll disable \lwc/ for certain commands.
@@ -80,7 +94,9 @@
}
\begingroup
- \suppressoutererror=1
+ \lwc@iflmtx\else
+ \suppressoutererror=1
+ \fi
\lwcdisablecmd{\beginsection} % Sectioning
\endgroup
@@ -88,9 +104,15 @@
\let\lwcenable=\lwc@enable
\let\lwcdisable=\lwc@disable
\let\lwcdebug=\lwc@debug
-\let\lwcdraft=\lwc@draft
+\def\lwcdraft#1{%
+ \lwc@show@costs{#1}%
+ \lwc@show@colours{#1}%
+}
+\let\lwcshowcosts=\lwc@show@costs
+\let\lwcshowcolours=\lwc@show@colours
\let\iflwc=\lwc@if@enabled
\let\lwcnobreak=\lwc@nobreak
+\let\lwcpreshipout=\lwc@pre@shipout
\catcode`@=12
diff --git a/Master/texmf-dist/tex/optex/lua-widow-control/lua-widow-control.opm b/Master/texmf-dist/tex/optex/lua-widow-control/lua-widow-control.opm
index 6d3ce3ea54e..fa2be778636 100644
--- a/Master/texmf-dist/tex/optex/lua-widow-control/lua-widow-control.opm
+++ b/Master/texmf-dist/tex/optex/lua-widow-control/lua-widow-control.opm
@@ -3,7 +3,7 @@
% SPDX-License-Identifier: MPL-2.0+
% SPDX-FileCopyrightText: 2022 Max Chernoff
-\_codedecl\lwcenable{lua-widow-control <v2.2.2>} %%version
+\_codedecl\lwcenable{lua-widow-control <v3.0.0>} %%version
\_namespace{lwc}
\_clubpenalty=1
@@ -14,6 +14,9 @@
\_newdimen\lwcemergencystretch
\lwcemergencystretch=3em
+\_newdimen\lwcdraftoffset
+\lwcdraftoffset=1in
+
\_newcount\lwcmaxcost
\lwcmaxcost=2147483647
@@ -72,7 +75,12 @@
\_let\lwcdisable=\.disable
\_let\lwcdisablecmd=\.disable_cmd
\_let\lwcdebug=\.debug
-\_let\lwcdraft=\.draft
+\_def\lwcdraft#1{%
+ \.show_costs{#1}%
+ \.show_colours{#1}%
+}
+\_let\lwcshowcosts=\.show_costs
+\_let\lwcshowcolours=\.show_colours
\_let\iflwc=\.if_enabled
\_let\lwcnobreak=\.nobreak