1

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}

enter image description here

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.

3
  • 2
    Yes, 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 by l3keys. There are other parsers which get the brace-stripping only after space stripping right, though.
    – Skillmon
    Commented Jul 26, 2021 at 20:07
  • @Skillmon Indeed: I guess in 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
    – Joseph Wright
    Commented Jul 26, 2021 at 20:24
  • @JosephWright correct :) I like \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.
    – Skillmon
    Commented Jul 26, 2021 at 20:35

1 Answer 1

2

Simple answer: You can't reliably, as pgfkeys sometimes reparses things (this depends on the internals of the key handlers involved). However, for the keys defined with /.initial, you can put a single space after the equals sign. That will prevent the first brace-stripping happening internally that should only take place after outer spaces are stripped.

 \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}|.

\end{document}

Produces:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .