TransWikia.com

regexpatch xpatchcmd fails in patching command begins with space

TeX - LaTeX Asked by muzimuzhi Z on February 4, 2021

The xpatchcmd provided by regexpatch fails in patching command which begins with space. The same xpatchcmd provided by xpatch works alright.

Some version info:

XeTeX      3.14159265-2.6-0.99999 
expl3      2018/06/14  
xparse     2018/05/12  
regexpatch 2018/05/02 v0.2d

Example:

documentclass{minimal}
usepackage{regexpatch}

deftest{source}
xpatchcmd{test}{source}{substituted}{}{ddt}

% only this one fails
deftest{ source}
xpatchcmd{test}{source}{substituted}{}{ddt}

deftest{source source}
xpatchcmd{test}{source}{substituted}{}{ddt}

begin{document}
abc
end{document}

One Answer

This problem is caused by line

tl_set:Nf l_xpatch_replacement_tl { cs_replacement_spec:N #1 }

inside the definition of xpatch_get_all:N, in regexpatch.sty.

Since f-type always gobbles a space char (see doc of f-type from sec. I.1, texdoc interface3 and this answer), the first line sets l_xpatch_replacement_tl to source (instead of <space>source). Using tl_set:Ne instead of tl_set:Nf may fix this problem.

The following redefinition of xpatch_get_all:N seems to work.

documentclass{article}
usepackage{regexpatch}

ExplSyntaxOn
cs_generate_variant:Nn tl_set:Nn {Ne}
cs_generate_variant:Nn tl_set_rescan:Nnn {Nne}

cs_set_protected:Npn xpatch_get_all:N #1
  {
    % use tl_set:Ne instead of tl_set:Nf
    tl_set:Ne l_xpatch_prefix_tl { cs_prefix_spec:N #1 }
    tl_set_rescan:Nnx l_xpatch_prefix_tl { } l_xpatch_prefix_tl
    tl_set:Ne l_xpatch_arg_tl { cs_argument_spec:N #1 }
    tl_set_rescan:Nnx l_xpatch_arg_tl 
      { char_set_catcode_space:n {` } } l_xpatch_arg_tl
    tl_set:Ne l_xpatch_replacement_tl { cs_replacement_spec:N #1 }
    tl_set_rescan:Nne l_xpatch_replacement_tl 
      { char_set_catcode_space:n {` } } l_xpatch_replacement_tl
  }
ExplSyntaxOff

begin{document}
deftest#1 #2{ source }

ttfamily
|meaningtest| par
xpatchcmd{test}{source}{substituted}{}{fail}
|meaningtest|
end{document}

I did not fully understand why regexpatch uses tl_set:Nf there yet. And I am not sure if the above solution will cause new problems.

Answered by muzimuzhi Z on February 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP