使用pgfkeys
,当遇到未知密钥时,我会自动构建一些子密钥。请参阅下面的 MWE。
我希望避免在访问子键之前设置test
键(即触发处理.unknown
程序),即使其隐式。这可能吗?
\documentclass{article}
\usepackage{pgfkeys}
\usepackage{etoolbox}
\newcommand\buildkeys[1]{%
\pgfkeys{%
/mypath/#1/a/.initial=N/A,
/mypath/#1/b/.initial=N/A,
}
}
\pgfkeys{%
/mypath/.unknown/.code={%
\expandafter\buildkeys\expandafter{\pgfkeyscurrentname}%
}
}
\pgfkeys{%
/mypath/.cd,
test, % Needed to fire the .unknown handler. I would like to avoid this
test/a=test a,
test/b=test b,
}
\begin{document}
should be "test a": \pgfkeysvalueof{/mypath/test/a}
should be "test b": \pgfkeysvalueof{/mypath/test/b}
\end{document}
答案1
根据@HenriMenke 的说法这是不可能的,所以我可能会使用这个解决方案(但我仍然愿意接受更好的方法!)
\documentclass{article}
\usepackage{pgfkeys}
\newcommand\mykeys[2]{%
\pgfkeys{%
/mypath/#1/.cd,
a/.initial=N/A,
b/.initial=N/A,
#2,
}%
}
\mykeys{test}{%
a=test a,
b=test b,
}
\begin{document}
should be "test a": \pgfkeysvalueof{/mypath/test/a}
should be "test b": \pgfkeysvalueof{/mypath/test/b}
\end{document}