我正在使用etoolbox
。如何创建返回布尔值的新命令?例如,如果我创建以下命令:
\newcommand\ifkeyempty[1]{
\def\novalue{\pgfkeysnovalue}%
\def\empty{}%
\pgfkeysgetvalue{#1}{\value}%
\ifboolexpr{test {\ifdefequal{\value}{\empty}} or test {\ifdefequal{\value}{\novalue}}}{true}{false}%
}
我希望能够在这样的布尔表达式中使用它:
\ifboolexpr{test {\ifkeyempty{/keyname}} or ... }{expression is true}{expression is false}
这可能吗?
答案1
哇!其实很简单!我只是在命令定义中删除了表达式为真或为假时应执行的子句。现在,它既可以在测试中使用,也可以\ifboolexpr
直接使用\ifkeyempty{<key>}{<true>}{<false>}
。
\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\pgfkeys{
/foo/.initial,
/bar/.initial,
}
\newcommand\ifkeyempty[1]{
\def\novalue{\pgfkeysnovalue}%
\def\empty{}%
\pgfkeysgetvalue{#1}{\keyvalue}%
\ifboolexpr{test {\ifdefequal{\keyvalue}{\empty}} or test {\ifdefequal{\keyvalue}{\novalue}}}%
}
\begin{document}
\ifboolexpr{test {\ifkeyempty{/foo}} and test {\ifkeyempty{/bar}}}{expression is true}{expression is false}
\ifkeyempty{/foo}{foo is empty}{foo is not empty}
\pgfkeys{/foo=value1}
\ifboolexpr{test {\ifkeyempty{/foo}} and test {\ifkeyempty{/bar}}}{expression is true}{expression is false}
\ifkeyempty{/foo}{foo is empty}{foo is not empty}
\pgfkeys{/bar=value2}
\ifboolexpr{test {\ifkeyempty{/foo}} and test {\ifkeyempty{/bar}}}{expression is true}{expression is false}
\ifkeyempty{/foo}{foo is empty}{foo is not empty}
\pgfkeys{/foo=}
\ifboolexpr{test {\ifkeyempty{/foo}} and test {\ifkeyempty{/bar}}}{expression is true}{expression is false}
\ifkeyempty{/foo}{foo is empty}{foo is not empty}
\pgfkeys{/bar=}
\ifboolexpr{test {\ifkeyempty{/foo}} and test {\ifkeyempty{/bar}}}{expression is true}{expression is false}
\ifkeyempty{/foo}{foo is empty}{foo is not empty}
\end{document}