From 5983e00c03a741978b65fe1d550606afcca07b9a Mon Sep 17 00:00:00 2001 From: muzimuzhi Date: Tue, 11 Jan 2022 02:37:21 +0800 Subject: [PATCH 1/6] fix(pgfmath): Strip spaces around each function arg #1123 Signed-off-by: muzimuzhi --- doc/generic/pgf/CHANGELOG.md | 4 +++- tex/generic/pgf/math/pgfmathfunctions.code.tex | 12 +++++++++--- tex/generic/pgf/utilities/pgfutil-common.tex | 9 +++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/doc/generic/pgf/CHANGELOG.md b/doc/generic/pgf/CHANGELOG.md index 63068af68..9d6707bc9 100644 --- a/doc/generic/pgf/CHANGELOG.md +++ b/doc/generic/pgf/CHANGELOG.md @@ -57,7 +57,9 @@ lot of contributed changes. Thanks to everyone who volunteered their time! - Update Debian installation instructions - Suppress white space at line end when `datavisualization` reads from a file #1112 -- Form-only patterns have no specified color #1122 +- Form-only patterns have no specified color #1122 +- Functions defined by `/pgf/declare function` now allow spaces in their arg + list #1123 ### Changed diff --git a/tex/generic/pgf/math/pgfmathfunctions.code.tex b/tex/generic/pgf/math/pgfmathfunctions.code.tex index 6e5d079c6..047858f9d 100644 --- a/tex/generic/pgf/math/pgfmathfunctions.code.tex +++ b/tex/generic/pgf/math/pgfmathfunctions.code.tex @@ -184,7 +184,7 @@ \expandafter\pgfmath@toks\expandafter=\expandafter{\pgfmath@local@body}% \else% \pgfmath@toks={}% - \expandafter\pgfmath@local@function@@body\pgfmath@local@args,,% + \expandafter\pgfmath@local@function@@body@trimspaces\pgfmath@local@args,,% \fi% \xdef\pgfmath@local@temp{% \noexpand\pgfmathnotifynewdeclarefunction{\pgfmath@local@name}{\the\c@pgf@counta}% @@ -202,7 +202,13 @@ \pgfmathdeclarefunction{#1}{#2}{\pgfmathparse{#3}}% }% -\def\pgfmath@local@function@@body#1,{% +\def\pgfmath@local@function@@body@trimspaces#1,{% + % strip spaces on both sides of #1 in case it starts with a space, + % e.g., #1 is ` \y` which comes from `func(\x, \y)=...;` + \expandafter\pgfmath@local@function@@body\expanded{{\pgfutil@trimspaces@noexp{#1}}}% +} + +\def\pgfmath@local@function@@body#1{% \def\pgfmath@local@test{#1}% \ifx\pgfmath@local@test\pgfutil@empty% \let\pgfmath@local@next=\relax% @@ -215,7 +221,7 @@ \pgfmath@toks={}% \expandafter\pgfmath@local@function@@@body\pgfmath@local@body @% \edef\pgfmath@local@body{\the\pgfmath@toks}% - \let\pgfmath@local@next=\pgfmath@local@function@@body% + \let\pgfmath@local@next=\pgfmath@local@function@@body@trimspaces% \fi% \pgfmath@local@next% } diff --git a/tex/generic/pgf/utilities/pgfutil-common.tex b/tex/generic/pgf/utilities/pgfutil-common.tex index 952af70f5..2e11a0aca 100644 --- a/tex/generic/pgf/utilities/pgfutil-common.tex +++ b/tex/generic/pgf/utilities/pgfutil-common.tex @@ -49,6 +49,15 @@ \def\pgfutil@trimspaces@@#1Q#2{#1} \catcode`\Q=11 +% \pgfutil@trimspaces@noexp{} +% +% Variant of \pgfutil@trimspaces that after full expansion leaves stripped +% original text unexpanded in the input stream. Same as \trim@spaces@noexp +% in trimspaces.sty +\def\pgfutil@trimspaces@noexp#1{% + \unexpanded\expandafter\expandafter\expandafter{\pgfutil@trimspaces{#1}}% +} + % \pgfutil@ifx{}{}{}{} % % This macro is expandable. From 190342a19826f6ab485db81d28a8d624c90be32c Mon Sep 17 00:00:00 2001 From: muzimuzhi Date: Tue, 11 Jan 2022 03:27:46 +0800 Subject: [PATCH 2/6] refactor(pgfexpl): Initial macros which are drop-in replacements for expl3 functions Signed-off-by: muzimuzhi --- .../pgf/math/pgfmathfunctions.code.tex | 2 +- tex/generic/pgf/utilities/pgfutil-common.tex | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/tex/generic/pgf/math/pgfmathfunctions.code.tex b/tex/generic/pgf/math/pgfmathfunctions.code.tex index 047858f9d..714cab3f8 100644 --- a/tex/generic/pgf/math/pgfmathfunctions.code.tex +++ b/tex/generic/pgf/math/pgfmathfunctions.code.tex @@ -205,7 +205,7 @@ \def\pgfmath@local@function@@body@trimspaces#1,{% % strip spaces on both sides of #1 in case it starts with a space, % e.g., #1 is ` \y` which comes from `func(\x, \y)=...;` - \expandafter\pgfmath@local@function@@body\expanded{{\pgfutil@trimspaces@noexp{#1}}}% + \pgfexpl@exp@args@@Ne\pgfmath@local@function@@body{\pgfutil@trimspaces@noexp{#1}}% } \def\pgfmath@local@function@@body#1{% diff --git a/tex/generic/pgf/utilities/pgfutil-common.tex b/tex/generic/pgf/utilities/pgfutil-common.tex index 2e11a0aca..acca5cd9a 100644 --- a/tex/generic/pgf/utilities/pgfutil-common.tex +++ b/tex/generic/pgf/utilities/pgfutil-common.tex @@ -918,6 +918,77 @@ \pgfmath@smuggleone#1% \endgroup }% + +% +% Utility commands that are drop-in replacements for expl3 macros +% +% Naming conventions: +% pgf_cs = "pgfexpl@" + expl3_cs.replace("_", "@").replace(":", "@@") +% For example, +% expl3_cs: \exp_args:Ne, \tl_put_right:Nn +% pgf_cs: \pgfexpl@exp@args@@Ne, \pgfexpl@tl@put@right@@Nn + + +% +% l3basics package +% + +% \cs_meaning:c +\long\def\pgfexpl@cs@meaning@@c#1{% + \ifcsname #1\endcsname + \expandafter\pgfutil@firstoftwo + \else + \expandafter\pgfutil@secondoftwo + \fi + {\pgfexpl@exp@args@@Nc\meaning{#1}} + {\detokenize{undefined}}% +} + +% +% l3tl package +% + +% \tl_if_eq:NNTF +\long\def\pgfexpl@tl@if@eq@@NNTF#1#2{% + \ifx#1#2\pgfexpl@@@prg@TF@true@@w\fi\pgfutil@secondoftwo +} + +% \tl_if_eq:ccTF +\long\def\pgfexpl@tl@if@eq@@ccTF{% + \pgfexpl@exp@args@@Ncc\pgfexpl@tl@if@eq@@NNTF +} + +% \__prg_TF_true:w +\long\def\pgfexpl@@@prg@TF@true@@w\fi\pgfutil@secondoftwo{% + \fi\pgfutil@firstoftwo +} + +% +% l3expan package +% + +% \exp_args:Nc +\long\def\pgfexpl@exp@args@@Nc#1#2{% + \expandafter#1\csname #2\endcsname +} + +% \exp_args:No +\long\def\pgfexpl@exp@args@@No#1#2{% + \expandafter#1\expandafter{#2}% +} + +% \exp_args:Ne +\long\def\pgfexpl@exp@args@@Ne#1#2{% + \expandafter#1\expanded{{#2}}% +} + +% \exp_args:Ncc +\long\def\pgfexpl@exp@args@@Ncc#1#2#3{% + \expandafter#1\csname #2\expandafter\endcsname\csname #3\endcsname +} + +% End of pgfexpl staff + \input pgfutil-common-lists.tex \endinput From 1e9cf8487595118a4c87ac14bd9c27d4ebae5126 Mon Sep 17 00:00:00 2001 From: muzimuzhi Date: Tue, 11 Jan 2022 03:31:09 +0800 Subject: [PATCH 3/6] test: Add test Signed-off-by: muzimuzhi --- testfiles/pgfmath-gh1123.lvt | 33 +++++++++++++++++++++++++++++++++ testfiles/pgfmath-gh1123.tlg | 10 ++++++++++ 2 files changed, 43 insertions(+) create mode 100644 testfiles/pgfmath-gh1123.lvt create mode 100644 testfiles/pgfmath-gh1123.tlg diff --git a/testfiles/pgfmath-gh1123.lvt b/testfiles/pgfmath-gh1123.lvt new file mode 100644 index 000000000..0bb7f1f92 --- /dev/null +++ b/testfiles/pgfmath-gh1123.lvt @@ -0,0 +1,33 @@ +\documentclass{minimal} +\input{pgf-regression-test} + +\RequirePackage{pgfmath} + +\makeatletter +\csname protected\endcsname\long\def\ASSERTPGFMATHFUNCTIONS#1#2{% + \pgfexpl@tl@if@eq@@ccTF{pgfmath#1@}{pgfmath#2@}{% + \TYPE{PASSED}% + \TYPE{\pgfexpl@cs@meaning@@c{pgfmath#1@}}% + }{% + \TYPE{FAILED}% + \TYPE{\pgfexpl@cs@meaning@@c{pgfmath#1@}}% + \TYPE{\pgfexpl@cs@meaning@@c{pgfmath#2@}}% + }% +} +\makeatother + +\START + +\BEGINTEST{/pgf/declare function, space(s) in arg list} + \pgfkeys{ + /pgf/declare function={ + funcA( \x,\y) = sqrt(\x^2 + \y^2); + funcB( \x, \y) = sqrt(\x^2 + \y^2); + funcX(\x,\y) = sqrt(\x^2 + \y^2); + } + } + \ASSERTPGFMATHFUNCTIONS{funcA}{funcX} + \ASSERTPGFMATHFUNCTIONS{funcB}{funcX} +\ENDTEST + +\END diff --git a/testfiles/pgfmath-gh1123.tlg b/testfiles/pgfmath-gh1123.tlg new file mode 100644 index 000000000..ed5de0783 --- /dev/null +++ b/testfiles/pgfmath-gh1123.tlg @@ -0,0 +1,10 @@ +This is a generated file for the l3build validation system. +Don't change this file in any respect. +============================================================ +TEST 1: /pgf/declare function, space(s) in arg list +============================================================ +PASSED +macro:#1#2->\pgfmathparse {sqrt(#1^2+#2^2)} +PASSED +macro:#1#2->\pgfmathparse {sqrt(#1^2+#2^2)} +============================================================ From 169908a71614664bda253f115b736cbc9fff8518 Mon Sep 17 00:00:00 2001 From: muzimuzhi Date: Tue, 11 Jan 2022 04:53:41 +0800 Subject: [PATCH 4/6] test: add braces [ci skip] This commit should be ammended to existing ones when the PR is approved Signed-off-by: muzimuzhi --- testfiles/pgfmath-gh1123.lvt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testfiles/pgfmath-gh1123.lvt b/testfiles/pgfmath-gh1123.lvt index 0bb7f1f92..d0325fa0f 100644 --- a/testfiles/pgfmath-gh1123.lvt +++ b/testfiles/pgfmath-gh1123.lvt @@ -21,9 +21,9 @@ \BEGINTEST{/pgf/declare function, space(s) in arg list} \pgfkeys{ /pgf/declare function={ - funcA( \x,\y) = sqrt(\x^2 + \y^2); - funcB( \x, \y) = sqrt(\x^2 + \y^2); - funcX(\x,\y) = sqrt(\x^2 + \y^2); + funcA( \x,\y) = sqrt((\x)^2 + (\y)^2); + funcB( \x, \y) = sqrt((\x)^2 + (\y)^2); + funcX(\x,\y) = sqrt((\x)^2 + (\y)^2); } } \ASSERTPGFMATHFUNCTIONS{funcA}{funcX} From 79d0ec3a9df55110b5e28c2ecd9fe3d7c8a4755c Mon Sep 17 00:00:00 2001 From: muzimuzhi Date: Tue, 11 Jan 2022 05:36:15 +0800 Subject: [PATCH 5/6] test: update test file (`.tlg`) This commit should be ammended to existing ones when the PR is approved. Signed-off-by: muzimuzhi --- testfiles/pgfmath-gh1123.tlg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testfiles/pgfmath-gh1123.tlg b/testfiles/pgfmath-gh1123.tlg index ed5de0783..c3e01f552 100644 --- a/testfiles/pgfmath-gh1123.tlg +++ b/testfiles/pgfmath-gh1123.tlg @@ -4,7 +4,7 @@ Don't change this file in any respect. TEST 1: /pgf/declare function, space(s) in arg list ============================================================ PASSED -macro:#1#2->\pgfmathparse {sqrt(#1^2+#2^2)} +macro:#1#2->\pgfmathparse {sqrt((#1)^2+(#2)^2)} PASSED -macro:#1#2->\pgfmathparse {sqrt(#1^2+#2^2)} +macro:#1#2->\pgfmathparse {sqrt((#1)^2+(#2)^2)} ============================================================ From bc836b431fa6c0476afe8c26a502415634655b67 Mon Sep 17 00:00:00 2001 From: muzimuzhi Date: Tue, 11 Jan 2022 08:56:10 +0800 Subject: [PATCH 6/6] fix: Use wrapped `\pgfutil@unexpanded` and `\pgfutil@expanded` This commit should be ammended to existing ones when the PR is approved. Signed-off-by: muzimuzhi --- tex/generic/pgf/utilities/pgfutil-common.tex | 4 ++-- tex/generic/pgf/utilities/pgfutil-context.def | 3 ++- tex/generic/pgf/utilities/pgfutil-latex.def | 3 ++- tex/generic/pgf/utilities/pgfutil-plain.def | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tex/generic/pgf/utilities/pgfutil-common.tex b/tex/generic/pgf/utilities/pgfutil-common.tex index acca5cd9a..da94284cf 100644 --- a/tex/generic/pgf/utilities/pgfutil-common.tex +++ b/tex/generic/pgf/utilities/pgfutil-common.tex @@ -55,7 +55,7 @@ % original text unexpanded in the input stream. Same as \trim@spaces@noexp % in trimspaces.sty \def\pgfutil@trimspaces@noexp#1{% - \unexpanded\expandafter\expandafter\expandafter{\pgfutil@trimspaces{#1}}% + \pgfutil@unexpanded\expandafter\expandafter\expandafter{\pgfutil@trimspaces{#1}}% } % \pgfutil@ifx{}{}{}{} @@ -979,7 +979,7 @@ % \exp_args:Ne \long\def\pgfexpl@exp@args@@Ne#1#2{% - \expandafter#1\expanded{{#2}}% + \expandafter#1\pgfutil@expanded{{#2}}% } % \exp_args:Ncc diff --git a/tex/generic/pgf/utilities/pgfutil-context.def b/tex/generic/pgf/utilities/pgfutil-context.def index eddab09ab..75748531b 100644 --- a/tex/generic/pgf/utilities/pgfutil-context.def +++ b/tex/generic/pgf/utilities/pgfutil-context.def @@ -365,9 +365,10 @@ \def\pgfutil@translate#1{#1} % \translate works very different in ConTeXt -% e-TeX primitives +% e-TeX primitives and beyond \let\pgfutil@protected\normalprotected \let\pgfutil@unexpanded\normalunexpanded +\let\pgfutil@expanded\normalexpanded \endinput diff --git a/tex/generic/pgf/utilities/pgfutil-latex.def b/tex/generic/pgf/utilities/pgfutil-latex.def index e88cfb942..da59a8bf4 100644 --- a/tex/generic/pgf/utilities/pgfutil-latex.def +++ b/tex/generic/pgf/utilities/pgfutil-latex.def @@ -209,9 +209,10 @@ \def\pgfutil@translate#1{\translate{#1}} \fi -% e-TeX primitives +% e-TeX primitives and beyond \let\pgfutil@protected\protected \let\pgfutil@unexpanded\unexpanded +\let\pgfutil@expanded\expanded \endinput diff --git a/tex/generic/pgf/utilities/pgfutil-plain.def b/tex/generic/pgf/utilities/pgfutil-plain.def index 2d08c5303..9be4f3b07 100644 --- a/tex/generic/pgf/utilities/pgfutil-plain.def +++ b/tex/generic/pgf/utilities/pgfutil-plain.def @@ -335,9 +335,10 @@ \def\pgfutil@translate#1{#1} % is there a translator package for plain? -% e-TeX primitives +% e-TeX primitives and beyond \let\pgfutil@protected\protected \let\pgfutil@unexpanded\unexpanded +\let\pgfutil@expanded\expanded \endinput