我正在尝试使用pgfkeys
来定义一些宏的 API,但在检测某个键是否为“空”时遇到了麻烦。基于这个答案,我得到了以下代码:
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{etoolbox}
\newcommand{\specialsave}[1]{
\pgfkeys{
/specialstore/place/.cd,
a/.initial,
c/.initial,#1, % there cannot be a newline before #1
c/.get=\cvalA, % this must come after #1, otherwise it won't get the values that are set
}
\edef\cvalB{\pgfkeysvalueof{/specialstore/place/c}}
\def\compareval{\pgfkeysnovalue}
\ifdefequal{\cvalA}{\compareval}{c is empty by comparison A}{c is non-empty by comparison A}
\ifdefequal{\cvalB}{\compareval}{c is empty by comparison B}{c is non-empty by comparison B}
}
\begin{document}
\specialsave{
a=the values of a,
}
\\
\specialsave{
a=the values of a,
c
}
\\
\specialsave{
a=the values of a,
c=
}
\\
\specialsave{
a=the values of a,
c={}
}
\end{document}
生成结果:
c is empty by comparison A
c is non-empty by comparison B
c is empty by comparison A
c is non-empty by comparison B
c is non-empty by comparison A
c is non-empty by comparison B
c is non-empty by comparison A
c is non-empty by comparison B
我的问题是
- 如何检测
\cvalB
、 和中的空值 c=
我如何检测案例和中的值为c={}
“空”?
答案1
我只添加\edef\compareval{\pgfkeysnovalue}
并获得了
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{etoolbox}
\newcommand{\specialsave}[1]{
\pgfkeys{
/specialstore/place/.cd,
a/.initial,
c/.initial,#1, % there cannot be a newline before #1
c/.get=\cvalA, % this must come after #1, otherwise it won't get the values that are set
}
\edef\cvalB{\pgfkeysvalueof{/specialstore/place/c}}
\def\compareval{\pgfkeysnovalue}
\ifdefequal{\cvalA}{\compareval}{c is empty by comparison A}{c is non-empty by comparison A}
\edef\compareval{\pgfkeysnovalue} %<-
\ifdefequal{\cvalB}{\compareval}{c is empty by comparison B}{c is non-empty by comparison B}
}
\begin{document}
\specialsave{
a=the values of a,
}
\\
\specialsave{
a=the values of a,
c
}
\\
\specialsave{
a=the values of a,
c=
}
\\
\specialsave{
a=the values of a,
c={}
}
\end{document}
这意味着第二次比较似乎完成了您想要做的事情。
\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{etoolbox}
\newcommand{\specialsave}[1]{
\pgfkeys{
/specialstore/place/.cd,
a/.initial,
c/.initial,#1, % there cannot be a newline before #1
}
\edef\cvalB{\pgfkeysvalueof{/specialstore/place/c}}
\edef\compareval{\pgfkeysnovalue} %<-
\ifdefequal{\cvalB}{\compareval}{c is empty}{c is non-empty}
}
\begin{document}
\specialsave{
a=the values of a,
}
\specialsave{
a=the values of a,
c
}
\specialsave{
a=the values of a,
c=
}
\specialsave{
a=the values of a,
c={}
}
\specialsave{
a=the values of a,
c={pft}
}
\end{document}