1

I need to be able to check if the value of a pgfkey is empty or not. A solution I have tried (using package etoolbox):

\edef\temp{\pgfkeysvalueof{/path/key}}%
\expandafter\ifstrequal\expandafter{\temp}{}{key is empty}{key is not empty}%

But this does not work if the value of the key expands to something empty or even worse: the value of the key cannot be expanded! How can I check whether a key is empty or not without expanding it? I just want to check if it's string value is equal to "" or not.

Edit: Added a full example

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{overpic}

\newcommand{\strcomp}{\expandafter\ifstrequal\expandafter}%

\pgfkeys{
    /icon/.cd,
    width/.initial,
    overlay/.initial,
}

\newcommand{\icon}[2][]{%
    {%
        \pgfkeys{/icon/.cd,width,overlay,#1}%
        \def\options{}%
        \edef\temp{\pgfkeysvalueof{/icon/width}}%
        \strcomp{\temp}{}{}{\edef\options{width=\pgfkeysvalueof{/icon/width},\options}}%
        \edef\temp{\pgfkeysvalueof{/icon/overlay}}%
        \strcomp{\temp}{}{%
            \edef\graphic{\noexpand\includegraphics[\options]{#2}}%
        }{%
            \edef\temp{\noexpand\begin{overpic}[\options]{#2}}%
            \def\graphic{\temp\put(0,0){\pgfkeysvalueof{/icon/overlay}}\end{overpic}}%
        }%
        \graphic
    }%
}%

\begin{document}

\icon{example-image} % Works!
\icon[width=3cm]{example-image} % Works :)
\icon[width=3cm,overlay=jei]{example-image} % Also works :)
\icon[width=3cm,overlay={\includegraphics{overlay-image}}]{example-image} % Breaks down when trying to expand in \edef\temp{\pgfkeysvalueof{/icon/overlay}}

\end{document}
2
  • Could you make a complete example?
    – egreg
    Commented Mar 4, 2014 at 12:03
  • I've now added a complete example, @egreg Thanks for looking :) Commented Mar 4, 2014 at 12:18

1 Answer 1

3

When a key is not set the value is \pgfkeysemptyvalue so you need to compare the value with this. One way is with \ifdefequal:

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\usepackage{overpic}


\pgfkeys{
    /icon/.cd,
    width/.initial,
    overlay/.initial,
}

\newcommand{\icon}[2][]{%
    {%
        \pgfkeys{/icon/.cd,width,overlay,#1,width/.get=\temp}%
        \def\tempa{\pgfkeysnovalue}
        \def\options{}%
        \ifdefequal{\temp}{\tempa}{}{\edef\options{width=\temp,\options}}%
        \pgfkeysgetvalue{/icon/overlay}{\temp}%
        \ifdefequal{\temp}{\tempa}{%
            \edef\graphic{\noexpand\includegraphics[\options]{#2}}%
        }{%
            \edef\temp{\noexpand\begin{overpic}[\options]{#2}}%
            \def\graphic{\temp\put(0,0){\pgfkeysvalueof{/icon/overlay}}\end{overpic}}%
        }%
        \graphic
    }%
}%

\begin{document}

\icon{example-image}

\icon[width=3cm]{example-image}

\icon[width=3cm,overlay=jei]{example-image}

\icon[width=3cm,overlay={\includegraphics{example-image-a}}]{example-image}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .