1

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?

  • 3
    You shouldn't use \value to begin with, because it's already an important command in LaTeX. – egreg Mar 5 '14 at 11:20
  • I suppose, that you have to use a boolean variable or ('flag') which is used globally, but this is not a very nice approach: \providebool{MyKeyFlag} and \boolfalse{MyKeyFlag} or \booltrue{MyKeyFlag} – user31729 Mar 5 '14 at 11:20
  • Please add an MWE also – user31729 Mar 5 '14 at 11:26
2

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.