0

i use nowidow package and \setnowidow, but some case it work not fine: enter image description here

for example, i hope paragraph can do page break at red place, but it doesn't.

Then I test, if I change nowidow.sty, make \widowpenalties 2 10000 0 to \widowpenalties 3 10000 -1 0, it can work fine.

\newcommand{\setnowidow}[1][\nowidow@defaultlines]{% \mathchardef\nowidowmax#1\relax % \widowpenalties #1 \nowidow@X{1} 0\par <-- before it's \widowpenalties \numexpr(#1)+1 \nowidow@X{1} -1 0\par % <-- after i change } 

so I try many ways to patch it (not change the nowidow.sty directly), but all failed.

Way1: I use \xpatchcmd, not success:

\xpatchcmd\setnowidow{\widowpenalties #1 \nowidow@X{1} 0\par}{\widowpenalties \numexpr(#1)+1 \nowidow@X{1} -1 0\par}{}{} 

Q1: why it can't success?


Way2: I try \renewcommand, it also failed: MWE: my main tex test.tex:

\documentclass{book} \usepackage[paper=a5]{zztj} \begin{document} abc \end{document} 

my customed package zztj.sty:

\NeedsTeXFormat{LaTeX2e} \RequirePackage{expl3} \ProvidesExplPackage{zztj}{}{}{} \RequirePackage{l3keys2e} %--- \RequirePackage{nowidow} \renewcommand{\setnowidow}[1][\nowidow@defaultlines]{% \mathchardef\nowidowmax#1\relax \widowpenalties \numexpr(#1)+1 \nowidow@X{1} -1 0\par } \setnowidow % --------my other options-------- \tl_new:N\g__zztj_paper_tl \keys_define:nn{zztj/option} { paper.choices:nn= {a4, a5} {\tl_gset:Nn\g__zztj_paper_tl{#1}}, } \ProcessKeysOptions{zztj/option} 

build error:

! Missing number, treated as zero. <to be read again> \tex_par:D l.15 \tl_new:N \g__zztj_paper_tl ? 

Q2: why have this error?


Way3: so I copy the source from nowidow.sty, and change it, but can't success also, because I use l3keys2e package also: MWE: my customed package zztj.sty:

\NeedsTeXFormat{LaTeX2e} \RequirePackage{expl3} \ProvidesExplPackage{zztj}{}{}{} \RequirePackage{l3keys2e} % --------start copy from nowidow-------- \RequirePackage{kvoptions} \SetupKeyvalOptions{ family=nowidow, prefix=nowidow@, } \DeclareStringOption[2]{defaultlines} \DeclareBoolOption{all} \ProcessKeyvalOptions* \def\nowidow@X#1{% \ifnum#1<\nowidowmax 10000 \expandafter\nowidow@X\expandafter{% \the\numexpr(#1)+1\expandafter\relax\expandafter}% \fi } \newcommand{\setnowidow}[1][\nowidow@defaultlines]{% \mathchardef\nowidowmax#1\relax \widowpenalties \numexpr(#1)+1 \nowidow@X{1} -1 0\par } \ifnowidow@all \setnowidow \fi \newcommand{\nowidow}[1][\nowidow@defaultlines]{% \begingroup \setnowidow[#1] \endgroup } \newcommand{\setnoclub}[1][\nowidow@defaultlines]{% \mathchardef\nowidowmax#1\relax \clubpenalties \numexpr(#1)+1 \nowidow@X{1} -1 0\par } \ifnowidow@all \setnoclub \fi \newcommand{\noclub}[1][\nowidow@defaultlines]{% \begingroup \setnoclub[#1] \endgroup } % --------end copy from nowidow-------- \setnowidow % --------my other options-------- \tl_new:N\g__zztj_paper_tl \keys_define:nn{zztj/option} { paper.choices:nn= {a4, a5} {\tl_gset:Nn\g__zztj_paper_tl{#1}}, } \ProcessKeysOptions{zztj/option} 

build error:

! Package keyval Error: paper undefined. See the keyval package documentation for explanation. Type H <return> for immediate help. ... l.14 \ProcessKeyvalOptions* 

Q3: why have this error?


Way4: I input \widowpenalties 3 10000 -1 0 directly, It still can't success: my customed package zztj.sty:

\NeedsTeXFormat{LaTeX2e} \RequirePackage{expl3} \ProvidesExplPackage{zztj}{}{}{} \RequirePackage{l3keys2e} %--- \widowpenalties 3 10000 -1 0 % --------my other options-------- \tl_new:N\g__zztj_paper_tl \keys_define:nn{zztj/option} { paper.choices:nn= {a4, a5} {\tl_gset:Nn\g__zztj_paper_tl{#1}}, } \ProcessKeysOptions{zztj/option} 

build error:

! Missing number, treated as zero. <to be read again> \tex_global:D l.9 \tl_new:N\g__zztj_paper_tl 

Q4: why \widowpenalties still fail?


Q5: what's the good way to patch the \setnowidow command but don't change the nowidow.sty directly?

5
  • I don't get any error from using \usepackage{nowidow}\usepackage{xpatch} \makeatletter\xpatchcmd\setnowidow{\widowpenalties #1 \nowidow@X{1} 0\par}{\widowpenalties \numexpr(#1)+1\relax \nowidow@X{1} -1 0\par}{}{\GenericError{}{failed}{}{}}\makeatother Commented Feb 20, 2023 at 17:06
  • Can you please create a complete minimal example of your first approach in which it fails for you? Commented Feb 20, 2023 at 17:06
  • yes, no error, but the patch not effective. I'll try to build the problem MWE and post it. Commented Feb 21, 2023 at 3:21
  • @Skillmon Please check my new question why \widowpenalties work not fine? with MWE. Commented Mar 1, 2023 at 13:23
  • The problem is the Latex3 ignore spaces in \widowpenalties 3 10000 -1 0. Please check the answer in this question Why can't \widowpenalties be put in the preamble?. Commented Mar 1, 2023 at 13:33

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.