最近我读了很多关于使用键值接口和处理程序接口之间的区别的帖子pgfkeys
。据我所知,相同的值可以用两种方式存储和检索。例如:
\documentclass{article}
\usepackage{tikz}
\pgfkeys{
/testa/.initial,
/testb/.store in=\testbvalue,
}
\begin{document}
\def\foo{bar}
\pgfkeys{/testa=\foo,/testb=\foo}
\pgfkeysvalueof{/testa}
\testbvalue
\pgfkeysgetvalue{/testa}\testavalue
\meaning\testavalue
\meaning\testbvalue
\end{document}
\pgfkeysvalueof{/testa}
这里可以通过和得到相同的值\testbvalue
。事实上\testavalue
和的含义\testbvalue
是一样的(这并不奇怪)。但是,为什么下面的代码可以工作呢?
\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{overpic}
\pgfkeys{
/icon/.cd,
width/.initial,
overlay/.store in=\iconoverlay,
set defaults/.unknown/.code={\pgfkeys{/icon/\pgfkeyscurrentname/.default=#1}}
}
\newcommand\icondefaults[1]{%
\pgfkeys{/icon/set defaults/.cd, #1}%
}
\newcommand\ifkeyempty[3]{%
\pgfkeysgetvalue{#1}{\keyvalue}%
\ifempty{\keyvalue}{#2}{#3}%
}
\newcommand\ifempty[3]{%
\def\novalue{\pgfkeysnovalue}%
\def\empty{}%
\ifboolexpr{test {\ifdefequal{#1}{\empty}} or test {\ifdefequal{#1}{\novalue}}}{#2}{#3}%
}
\newcommand{\icon}[2][]{%
{%
\pgfkeys{/icon/.cd,width,overlay,#1}% Reset to default values
\def\options{}%
\ifkeyempty{/icon/width}{}{\edef\options{width=\pgfkeysvalueof{/icon/width},\options}}%
\ifempty{\iconoverlay}{%
\edef\graphic{\noexpand\includegraphics[\options]{#2}}%
}{%
\edef\overlay{\noexpand\begin{overpic}[\options]{#2}}%
\def\graphic{\overlay\put(0,0){\iconoverlay}\end{overpic}}%
}%
\graphic%
}%
}%
\begin{document}
\icondefaults{width=6cm}
\icon{example-image} % Image gets default width 6cm
\icon[width=2cm,overlay={\icon{example-image-overlay}}]{example-image} % Image gets width 2cm, while the overlaid image gets default width 6cm
\icon{example-image} % Image gets default width 6cm
\end{document}
而下一段代码不起作用。唯一的区别是/.store in
不用于键overlay
,因此\pgfkeysvalueof
用于访问键的值。这会导致错误TeX capacity exceeded
。
\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{overpic}
\pgfkeys{
/icon/.cd,
width/.initial,
overlay/.initial,
set defaults/.unknown/.code={\pgfkeys{/icon/\pgfkeyscurrentname/.default=#1}}
}
\newcommand\icondefaults[1]{%
\pgfkeys{/icon/set defaults/.cd, #1}%
}
\newcommand\ifkeyempty[3]{%
\pgfkeysgetvalue{#1}{\keyvalue}%
\ifempty{\keyvalue}{#2}{#3}%
}
\newcommand\ifempty[3]{%
\def\novalue{\pgfkeysnovalue}%
\def\empty{}%
\ifboolexpr{test {\ifdefequal{#1}{\empty}} or test {\ifdefequal{#1}{\novalue}}}{#2}{#3}%
}
\newcommand{\icon}[2][]{%
{%
\pgfkeys{/icon/.cd,width,overlay,#1}% Reset to default values
\def\options{}%
\ifkeyempty{/icon/width}{}{\edef\options{width=\pgfkeysvalueof{/icon/width},\options}}%
\ifkeyempty{/icon/overlay}{%
\edef\graphic{\noexpand\includegraphics[\options]{#2}}%
}{%
\edef\overlay{\noexpand\begin{overpic}[\options]{#2}}%
\def\graphic{\overlay\put(0,0){\pgfkeysvalueof{/icon/overlay}}\end{overpic}}%
}%
\graphic%
}%
}%
\begin{document}
\icondefaults{width=6cm}
\icon{example-image} % Image gets default width 6cm
\icon[width=2cm,overlay={\icon{example-image-overlay}}]{example-image} % Image gets width 2cm, while the overlaid image gets default width 6cm
\icon{example-image} % Image gets default width 6cm
\end{document}
有人能解释这种行为吗?我的理解是否错误,两种情况下存储和检索的值相同,只是方式不同?
答案1
它们并不等同。/.store in
处理程序只是执行\def
操作,并不保存值。但是设置键允许检索在该范围内设置的值。这就是为什么在第二种情况下必须设置键。
这就是第一个方法有效的原因,因为默认行为的设置对两个递归级别都有效。然而,第二个方法不会改变键的值。
为此,您必须明确将覆盖键设置为无。
\pgfkeys{/icon/.cd,width,overlay=,#1}% Notice the equal sign for the overlay key