2

I am trying to use pgfkeys to define an API for some macros, and I am having trouble detecting when a key is "empty". Based on this answer, I have come up with the following code:

\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}

which produces:

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

My question is

  1. How can I detect empty values in \cvalB, and
  2. How can I detect that the values in cases c= and c={} are "empty"?
1

I only added \edef\compareval{\pgfkeysnovalue} and obtained

\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}

enter image description here

This means that the second comparison seems to do what you wanted to do.

\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}

enter image description here

  • That's great! It seems to break the comparison with \cvalA, though... Can you explain why it works? – Jason Siefken May 26 at 15:50
  • @JasonSiefken Could you please explain what is meant by "It seems to break the comparison with \cvalA, though."? The reason why this works is that I expand both \edef\cvalB{\pgfkeysvalueof{/specialstore/place/c}} and \edef\compareval{\pgfkeysnovalue}, and compare the expanded values. In your code, only the first one is expanded, so you compare an expanded value with an unexpanded one. – user121799 May 26 at 17:32
  • Using the \edef\cmpareval{\pgfkeysnovalue} version, \ifdefequal{\cvalA}{\compareval}{c is empty}{c is non-empty} reports c is non-empty in the first two cases. – Jason Siefken May 26 at 18:18
  • @JasonSiefken From the title of your question "Detect empty key in pgfkeys without .get code" I was under the impression that you only want the B check to work. (I just realized that the above code really did not work because I again lost again the editor of this site. I fixed this. The lower code does not use .get and works.) – user121799 May 26 at 18:23

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.