pgf-tikz / pgf

A Portable Graphic Format for TeX
https://pgf-tikz.github.io/
1.1k stars 105 forks source link

Document brace stripping in pgfkeys #912

Closed hmenke closed 3 years ago

hmenke commented 3 years ago

Version: 3.1.5b

pgfkeys will strip braces around single arguments, e.g.

\pgfset{
  /test/foo/.code=bar,
  /test/.style n args={1}{/test/.cd,#1},
  /test={foo},
}

will result in

! Package pgfkeys Error: I do not know the key '/test/f' and I am going to igno
re it. Perhaps you misspelled it.

This is totally expected when you know that /.style handlers are implemented in terms of /.code handlers and usual TeX argument brace stripping applies. However, this is not mentioned clearly in the manual.

muzimuzhi commented 3 years ago

With current implementation, to successfully pass foo to key /test, one has to use four extra pair of braces \pgfkeys{/test={{{{foo}}}}}, which is rather inconvenient.

Since this only happens to keys defined by <key>/.style n args={1}{...} (although this kind of usage is fully avoidable), how about specially treat #2 == 1 in definition of \pgfkeysdefnargs@? For example,

diff --git a/tex/generic/pgf/utilities/pgfkeys.code.tex b/tex/generic/pgf/utilities/pgfkeys.code.tex
index f2288175..6d1027db 100644
--- a/tex/generic/pgf/utilities/pgfkeys.code.tex
+++ b/tex/generic/pgf/utilities/pgfkeys.code.tex
@@ -717,7 +717,11 @@
   % (with expansion of '#1'):
   \edef\pgfkeys@tempargs{\noexpand\pgfkeysvalueof{#1/.@@body}}%
   \def\pgfkeys@temp{\pgfkeysdef{#1}}%
-  \expandafter\pgfkeys@temp\expandafter{\pgfkeys@tempargs##1}%
+  \ifnum#2=1\relax
+    \expandafter\pgfkeys@temp\expandafter{\pgfkeys@tempargs{##1}}%
+  \else
+    \expandafter\pgfkeys@temp\expandafter{\pgfkeys@tempargs##1}%
+  \fi
   #5{#1/.@body}{#3}%
 }

PS: In your example, it seems \pgfset should be \pgfkeys.

hmenke commented 3 years ago

That's a good idea. Can you submit that as a PR?