我需要定义一个包含在宏中的样式,而无需扩展这个宏。
问题是,如果宏包含多个键,例如或将值设置为某个键,如下所示:
\def\myconfiguration{text={The value of my great counter is \themycounter}}
它失败了,因为 pgfkey 尝试首先评估宏,然后检查键是否名为text={The value ...}
,这当然是错误的。
那么,在宏中定义 pgfkey 样式的好方法是什么,并确保该宏在定义时不会扩展?
梅威瑟:
\documentclass{article}
\usepackage{pgfkeys}
\newcounter{mycounter}
\def\myconfiguration{text={The value of my great counter is \themycounter}}
\pgfkeys{
/prAtEnd/.cd,
% Text
text/.code={\def\sayhello{#1}},
configuration options/.style={
text={The counter value is \themycounter},
\myconfiguration %% This lines fails!
},
}
\begin{document}
\pgfkeys{/prAtEnd/.cd, configuration options}
\sayhello
\stepcounter{mycounter}
\sayhello
\stepcounter{mycounter}
\sayhello
\end{document}
-- 编辑 -- 我希望最后得到的结果与\myconfiguration
用其定义替换时的结果相同,即text={The value of my great counter is \number\value{mycounter}}
。因此输出必须是:
答案1
\myconfiguration
不是一种风格。下面是可行的方法,但如果您告诉我们您真正的想法,那么可能会有更好的建议。
\documentclass{article}
\usepackage{pgfkeys}
\newcounter{mycounter}
\def\myconfiguration{text={The value of my great counter is \number\value{mycounter}}}
\pgfkeys{/prAtEnd/.cd,
% Text
text/.code={\def\sayhello{#1}},
configuration options/.style={
text={The counter value is \number\value{mycounter}}},
configuration options/.style/.expand once=\myconfiguration
}
\begin{document}
\pgfkeys{/prAtEnd/.cd, configuration options}
\sayhello
\stepcounter{mycounter}
\sayhello
\stepcounter{mycounter}
\sayhello
\end{document}
答案2
受到启发后这,我想出了这个解决方案(编辑:.expand once
更好):
\documentclass{article}
\usepackage{pgfkeys}
\newcounter{mycounter}
\def\myconfiguration{text={The value of my greatt counter is \number\value{mycounter}}}
\show\myconfiguration
\pgfkeys{
/prAtEnd/.cd,
% Text
text/.code={\def\sayhello{#1}},
second text/.code={\def\saybye{#1}},
configuration options/.style={
text={The counter value is \number\value{mycounter}},
},
}
\edef\mydefinepgfkeys{\noexpand\pgfkeys{%
/prAtEnd/.cd,%
configuration options/.append style={%
\unexpanded\expandafter{\myconfiguration}%
},%
}}\mydefinepgfkeys
\begin{document}
\pgfkeys{/prAtEnd/.cd, configuration options}
\sayhello
% \saybye
\stepcounter{mycounter}
\sayhello
\stepcounter{mycounter}
\sayhello
\end{document}