我尝试使用 pgfkeys 来提供带有周围空格标记的键值:
\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}
不幸的是,pgfmanual 的第 87.3 节“设置键”说:
\pgfkeys{⟨key list⟩}
这⟨关键列表⟩应为以逗号分隔的键值对列表。键值对可以采用以下两种形式:⟨钥匙=价值⟩要不就⟨钥匙⟩。周围的任何空间⟨钥匙⟩或周围⟨价值⟩被删除。允许同时围绕⟨钥匙⟩或者⟨价值⟩在花括号中,这些花括号也被删除了。** 尤其是把⟨价值⟩在花括号中需要经常这样做,即每当⟨价值⟩包含等号或逗号。
似乎周围的空间⟨价值⟩即使⟨价值⟩放在花括号内。
你能做些什么来对抗这种空间消除呢?
除此之外,任何空间删除的内容似乎不正确:
从上面的 MWE 可以看出,如果我付出额外的努力来破解值之前和之后的连续空格标记,那么在值之前和之后,只有其中一个会被删除。
答案1
简单的答案:您无法可靠地做到这一点,因为pgfkeys
有时会重新解析内容(这取决于所涉及的键处理程序的内部结构)。但是,对于用 定义的键/.initial
,您可以在等号后放置一个空格。这将阻止内部发生第一次括号剥离,而这应该只发生在后外部空间被剥离。
\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}
生成: