I tried to use pgfkeys for providing keys' values that have surrounding space-tokens:
\documentclass{article}
\usepackage{pgfkeys}
\pgfkeys{/WeirdKeys/weird/.initial=weird}%
\newcommand\Weird[1]{\pgfkeys{/WeirdKeys/.cd,#1}}%
\begin{document}
\ttfamily
Here I don't want spaces around weird: |\Weird{weird}|.
\Weird{weird={ weird }}
Here I do want spaces around weird: |\Weird{weird}|.
But I don't get them.
It should look like this: | weird |.
Extra effort:
\def\surroundwithspaces#1#2{#1#2#1}
\expandafter\Weird
\expandafter{%
\expandafter w%
\expandafter e%
\expandafter i%
\expandafter r%
\expandafter d%
\expandafter =%
\surroundwithspaces{ }{weird}}
Here I do not get spaces around weird: |\Weird{weird}|.
\def\surroundwithspaces#1#2{#1#1#2#1#1}
\expandafter\Weird
\expandafter{%
\expandafter w%
\expandafter e%
\expandafter i%
\expandafter r%
\expandafter d%
\expandafter =%
\surroundwithspaces{ }{weird}}
Here I do get spaces around weird: |\Weird{weird}|.
\def\surroundwithspaces#1#2{#1#1#1#2#1#1#1}
\expandafter\Weird
\expandafter{%
\expandafter w%
\expandafter e%
\expandafter i%
\expandafter r%
\expandafter d%
\expandafter =%
\surroundwithspaces{ }{weird}}
Here I do get more spaces around weird: |\Weird{weird}|.
\end{document}
Unfortunately, section 87.3 Setting Keys of the pgfmanual says:
\pgfkeys{⟨key list⟩}
The ⟨key list⟩ should be a list of key–value pairs, separated by commas. A key–value pair can have the following two forms: ⟨key=value⟩ or just ⟨key⟩. Any spaces around the ⟨key⟩ or around the ⟨value⟩ are removed. It is permissible to surround both the ⟨key⟩ or the ⟨value⟩ in curly braces, which are also removed.** Especially putting the ⟨value⟩ in curly braces needs to be done quite often, namely whenever the ⟨value⟩ contains an equal-sign or a comma.
Seems spaces surrounding the ⟨value⟩ are removed even if the ⟨value⟩ is put in curly braces.
What can you do against this space-removal?
Besides this, the statement of any space around things being removed seems not correct:
As can be seen in the MWE above, if I put extra effort in hacking consecutive space-tokens before and behind the value, before and behind the value only one of them is removed.
pgfkeys
like many other key=value implementations only strips at most one space from the start and end of a key or value. I think the only key=value parser which reliably strips all spaces around the key or value before stripping braces is the one provided byl3keys
. There are other parsers which get the brace-stripping only after space stripping right, though.expkv
you don't use the\tl_trim_spaces:n
approach as it's a bit more 'expensive' than the classical 'drop a space at either end and hope tokenization did the rest' plan\tl_trim_spaces:n
, it is a great piece of code, but one has to either have erroneous spaces in a definition or try really hard (see the code in this question) to end up with multiple spaces. So my take on it is: If you end up with more than a single space, that's what you want.