[Tex/LaTex] Newcommand returning a boolean value

conditionalsetoolboxmacros

I'm using etoolbox. How can I create a new command that returns a boolean value? For example if I create the following command:

\newcommand\ifkeyempty[1]{
    \def\novalue{\pgfkeysnovalue}%
    \def\empty{}%
    \pgfkeysgetvalue{#1}{\value}%
    \ifboolexpr{test {\ifdefequal{\value}{\empty}} or test {\ifdefequal{\value}{\novalue}}}{true}{false}%
}

I would like to be able to use it in a boolean expression like this:

    \ifboolexpr{test {\ifkeyempty{/keyname}} or ... }{expression is true}{expression is false}

Is this possible?

Best Answer

Wow! It was actually easy! I just removed the clauses that should be performed if the expression is true or false in the definition of the command. It now works both in tests like \ifboolexpr and by using it directly like \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}
Related Question