diff --git a/source/lua-widow-control.sty b/source/lua-widow-control.sty index 1118001..e8afe64 100644 --- a/source/lua-widow-control.sty +++ b/source/lua-widow-control.sty @@ -27,35 +27,45 @@ {v3.0.1} %%version {Use Lua to remove widows and orphans} +% Variants +\cs_generate_variant:Nn \hook_gput_code:nnn { nVn } +\cs_generate_variant:Nn \keys_define:nn { Vn } +\cs_generate_variant:Nn \keys_set:nn { Vn } +\cs_generate_variant:Nn \msg_critical:nn { Vn } +\cs_generate_variant:Nn \msg_error:nnnn { Vnee } +\cs_generate_variant:Nn \msg_error:nnnnn { Vnnnn } +\cs_generate_variant:Nn \msg_new:nnn { Vnn } +\cs_generate_variant:Nn \msg_warning:nn { Vn } + % Message and String Constants \str_const:Nn \c__lwc_name_str { lua-widow-control } -\msg_new:nnn - { \c__lwc_name_str } +\msg_new:Vnn + \c__lwc_name_str { no-luatex } { LuaTeX~ is~ REQUIRED! \\ Make~ sure~ to~ compile~ your~ document~ with~ `lualatex'. } -\msg_new:nnn - { \c__lwc_name_str } +\msg_new:Vnn + \c__lwc_name_str { patch-failed } { Patching~ \c_backslash_str #1~ failed. \\ Please~ ensure~ that~ \c_backslash_str #1~ exists. } -\msg_new:nnn - { \c__lwc_name_str } +\msg_new:Vnn + \c__lwc_name_str { old-format-patch } { Patching~ not~ supported~ with~ old~ LaTeX. \\ Please~ use~ a~ LaTeX~ format~ >=~ 2021/06/01. } -\msg_new:nnn - { \c__lwc_name_str } +\msg_new:Vnn + \c__lwc_name_str { old-command } { \c_backslash_str #1~ has~ been~ REMOVED! \\ @@ -63,8 +73,8 @@ \c_right_brace_str\ instead. } -\msg_new:nnn - { \c__lwc_name_str } +\msg_new:Vnn + \c__lwc_name_str { dubious-emergencystretch } { Questionable~ `emergencystretch'~ value. \\ @@ -75,11 +85,10 @@ % Don't let the user proceed unless they are using \LuaTeX{}. \sys_if_engine_luatex:F { - \msg_critical:nn { \c__lwc_name_str } { no-luatex } + \msg_critical:Vn \c__lwc_name_str { no-luatex } } % Define (most of) the keys -\cs_generate_variant:Nn \keys_define:nn { Vn } \dim_new:N \g__lwc_emergencystretch_dim \keys_define:Vn { \c__lwc_name_str } { @@ -87,7 +96,7 @@ \dim_gset:Nn \g__lwc_emergencystretch_dim { #1 } \dim_compare:nNnT { #1 } > { \dim_max:nn { \textwidth } { 6in } } { - \msg_warning:nn { \c__lwc_name_str } { dubious-emergencystretch } + \msg_warning:Vn \c__lwc_name_str { dubious-emergencystretch } } }, emergencystretch .value_required:n = true, @@ -120,15 +129,17 @@ microtype .initial:n = true, microtype .usage:n = preamble, - disablecmds .clist_gset:N = \g__lwc_disablecmds_cl, + disablecmds .clist_gset:N = \g__lwc_disablecmds_clist, disablecmds .value_required:n = false, - disablecmds .initial:n = { \@sect, % LaTeX default - \@ssect, % LaTeX starred - \M@sect, % Memoir - \@mem@old@ssect, % Memoir Starred - \ttl@straight@ii, % titlesec normal - \ttl@top@ii, % titlesec top - \ttl@part@ii, % titlesec part + disablecmds .initial:n = { \@sect, % LaTeX default + \@ssect, % LaTeX starred + \@makechapterhead, % LaTeX chapter + \M@sect, % Memoir + \@mem@old@ssect, % Memoir Starred + \ttl@straight@ii, % titlesec normal + \ttl@top@ii, % titlesec top + \ttl@part@ii, % titlesec part + \ttl@mkchap, % titlesec chapter }, disablecmds .usage:n = preamble, } @@ -176,7 +187,7 @@ % 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. -\hook_gput_code:nnn { begindocument / before } { \c__lwc_name_str } { +\hook_gput_code:nVn { begindocument / before } \c__lwc_name_str { \bool_if:NT \g__lwc_use_microtype_bool { \@ifpackageloaded { microtype } {} { \RequirePackage[ @@ -228,16 +239,14 @@ \cs_new:Npn \__lwc_patch_cmd:c #1 { \IfFormatAtLeastTF { 2021/06/01 } { - \hook_gput_code:nnn { cmd / #1 / before } { \c__lwc_name_str } { + \hook_gput_code:nVn { cmd / #1 / before } \c__lwc_name_str { \__lwc_patch_pre: } - \hook_gput_code:nnn { cmd / #1 / after } { \c__lwc_name_str } { + \hook_gput_code:nVn { cmd / #1 / after } \c__lwc_name_str { \__lwc_patch_post: } } { - \msg_warning:nn - { \c__lwc_name_str } - { old-format-patch } + \msg_warning:Vn \c__lwc_name_str { old-format-patch } } } @@ -255,8 +264,14 @@ } } -\hook_gput_code:nnn { begindocument / before } { \c__lwc_name_str } { - \clist_map_function:NN \g__lwc_disablecmds_cl \__lwc_patch_cmd:n +\hook_gput_code:nVn { begindocument / before } \c__lwc_name_str { + % Remove the @makechapterhead hooks with titlesec + \@ifpackageloaded { titlesec } { + \clist_remove_all:Nn \g__lwc_disablecmds_clist { \@makechapterhead } + } {} + + % Patch the commands that we want to disable + \clist_map_function:NN \g__lwc_disablecmds_clist \__lwc_patch_cmd:n } \__lwc_if_lmtx:T { @@ -353,23 +368,22 @@ \exp_args:NV \ProcessKeysOptions { \c__lwc_name_str } } -\cs_generate_variant:Nn \keys_set:nn { Vn } \NewDocumentCommand \lwcsetup {m} { \keys_set:Vn { \c__lwc_name_str }{ #1 } } % Legacy Commands \NewDocumentCommand \lwcemergencystretch { } { - \msg_error:nnnnn - { \c__lwc_name_str } + \msg_error:Vnnnn + \c__lwc_name_str { old-command } { lwcemergencystretch } { emergencystretch=XXXpt } } \NewDocumentCommand \lwcdisablecmd { m } { - \msg_error:nnxx - { \c__lwc_name_str } + \msg_error:Vnee + \c__lwc_name_str { old-command } { lwcdisablecmd } { disablecmds={\c_backslash_str aaa,~ \c_backslash_str bbb} } diff --git a/tests/latex/chapter-headings.lvtext b/tests/latex/chapter-headings.lvtext new file mode 100644 index 0000000..1b4d512 --- /dev/null +++ b/tests/latex/chapter-headings.lvtext @@ -0,0 +1,39 @@ +% lua-widow-control +% https://github.com/gucci-on-fleek/lua-widow-control +% SPDX-License-Identifier: MPL-2.0+ +% SPDX-FileCopyrightText: 2024 Max Chernoff + +% See https://tex.stackexchange.com/q/721804 + +\documentclass{book} +\raggedbottom +\widowpenalty=1 +\clubpenalty=1 + +\usepackage[draft]{lua-widow-control} + +\def\test{ + \chapter{Test} + + \newcount\n \n=0 + \loop\ifnum\n<30 + \advance\n by 1\relax + \the\n~One line one line one line one line one line one line. + \par + \repeat + + 1~Three lines three lines three lines three lines three lines three lines + three 2~lines. Three lines three lines three lines three lines three lines + three lines three 3~lines. Three lines three lines three lines three lines + three lines three lines. + + \clearpage +} + +\begin{document} + \lwcsetup{disable} + \test + + \lwcsetup{enable} + \test +\end{document} diff --git a/tests/latex/chapter-headings.tltext b/tests/latex/chapter-headings.tltext new file mode 100644 index 0000000..3e63d80 --- /dev/null +++ b/tests/latex/chapter-headings.tltext @@ -0,0 +1,111 @@ +106.9 209.3 185.1 214.5 Chapter 1 +105.9 157.5 230.2 265.5 Test Test +106.4 339.4 298.9 313.1 1 line. +121.8 354.8 310.9 325.0 2 line. +121.8 354.8 322.9 337.0 3 line. +121.8 354.8 334.8 348.9 4 line. +121.8 354.8 346.8 360.9 5 line. +121.8 354.8 358.7 372.8 6 line. +121.8 354.8 370.7 384.8 7 line. +121.8 354.8 382.6 396.8 8 line. +121.8 354.8 394.6 408.7 9 line. +121.8 359.8 406.5 420.7 10 line. +121.8 359.8 418.5 432.6 11 line. +121.8 359.8 430.5 444.6 12 line. +121.8 359.8 442.4 456.5 13 line. +121.8 359.8 454.4 468.5 14 line. +121.8 359.8 466.3 480.4 15 line. +121.8 359.8 478.3 492.4 16 line. +121.8 359.8 490.2 504.3 17 line. +121.8 359.8 502.2 516.3 18 line. +121.8 359.8 514.1 528.3 19 line. +121.8 359.8 526.1 540.2 20 line. +121.8 359.8 538.1 552.2 21 line. +121.8 359.8 550.0 564.1 22 line. +121.8 359.8 562.0 576.1 23 line. +121.8 359.8 573.9 588.0 24 line. +121.8 359.8 585.9 600.0 25 line. +121.8 359.8 597.8 611.9 26 line. +121.8 359.8 609.8 623.9 27 line. +121.8 359.8 621.7 635.9 28 line. +121.8 359.8 633.7 647.8 29 line. +121.8 359.8 645.6 659.8 30 line. +121.8 450.6 657.6 671.7 1 three +276.2 281.2 685.8 699.9 1 1 + +160.7 165.6 094.6 108.8 2 2 +408.6 504.4 094.6 108.8 CHAPTER TEST +160.4 504.4 122.6 136.7 2 three +160.4 487.5 134.6 148.7 3 lines. + +106.9 209.3 185.1 214.5 Chapter 2 +105.9 157.5 230.2 265.5 Test Test +106.4 339.4 298.9 313.1 1 line. +121.8 354.8 310.9 325.0 2 line. +121.8 354.8 322.9 337.0 3 line. +121.8 354.8 334.8 348.9 4 line. +121.8 354.8 346.8 360.9 5 line. +121.8 354.8 358.7 372.8 6 line. +121.8 354.8 370.7 384.8 7 line. +121.8 354.8 382.6 396.8 8 line. +121.8 354.8 394.6 408.7 9 line. +121.8 359.8 406.5 420.7 10 line. +121.8 359.8 418.5 432.6 11 line. +121.8 359.8 430.5 444.6 12 line. +121.8 359.8 442.4 456.5 13 line. +121.8 359.8 454.4 468.5 14 line. +121.8 359.8 466.3 480.4 15 line. +121.8 359.8 478.3 492.4 16 line. +121.8 359.8 490.2 504.3 17 line. +121.8 359.8 502.2 516.3 18 line. +121.8 359.8 514.1 528.3 19 line. +121.8 359.8 526.1 540.2 20 line. +121.8 359.8 538.1 552.2 21 line. +121.8 359.8 550.0 564.1 22 line. +121.8 359.8 562.0 576.1 23 line. +121.8 359.8 573.9 588.0 24 line. +121.8 359.8 585.9 600.0 25 line. +121.8 359.8 597.8 611.9 26 line. +121.8 359.8 609.8 623.9 27 line. +121.8 359.8 621.7 635.9 28 line. +121.8 359.8 633.7 647.8 29 line. +121.8 359.8 645.6 659.8 30 line. +121.8 450.6 657.6 671.7 1 three +276.2 281.2 685.8 699.9 3 3 +540.0 561.4 306.7 311.1 infinite infinite +540.0 561.4 318.7 323.1 infinite infinite +540.0 561.4 330.6 335.1 infinite infinite +540.0 561.4 342.6 347.0 infinite infinite +540.0 561.4 354.5 359.0 infinite infinite +540.0 561.4 366.5 370.9 infinite infinite +540.0 561.4 378.4 382.9 infinite infinite +540.0 561.4 390.4 394.8 infinite infinite +540.0 561.4 402.4 406.8 infinite infinite +540.0 561.4 414.3 418.7 infinite infinite +540.0 561.4 426.3 430.7 infinite infinite +540.0 561.4 438.2 442.6 infinite infinite +540.0 561.4 450.2 454.6 infinite infinite +540.0 561.4 462.1 466.6 infinite infinite +540.0 561.4 474.1 478.5 infinite infinite +540.0 561.4 486.0 490.5 infinite infinite +540.0 561.4 498.0 502.4 infinite infinite +540.0 561.4 510.0 514.4 infinite infinite +540.0 561.4 521.9 526.3 infinite infinite +540.0 561.4 533.9 538.3 infinite infinite +540.0 561.4 545.8 550.2 infinite infinite +540.0 561.4 557.8 562.2 infinite infinite +540.0 561.4 569.7 574.2 infinite infinite +540.0 561.4 581.7 586.1 infinite infinite +540.0 561.4 593.6 598.1 infinite infinite +540.0 561.4 605.6 610.0 infinite infinite +540.0 561.4 617.6 622.0 infinite infinite +540.0 561.4 629.5 633.9 infinite infinite +540.0 561.4 641.5 645.9 infinite infinite +540.0 561.4 653.4 657.8 infinite infinite + +160.7 165.6 094.6 108.8 4 4 +408.6 504.4 094.6 108.8 CHAPTER TEST +160.4 504.4 122.6 136.7 2 three +160.4 487.5 134.6 148.7 3 lines. +540.0 553.6 142.3 146.8 1345 1345 +