4

I am making a command called seal that needs a boolean key=value option. Since I don't know how to do this, I am using key=yes and key=no for defining my pgfkeys as follows.

\pgfkeys{
/seal/.is family, /seal,
% "seal" options
invert/.estore in = \sealInvert,
other/.estore in = \sealOther,}

An then, I define \seal roughly as follows:

\newcommand{\seal}[2][]{%
\pgfkeys{/seal,invert=no,other=value,#1}%
    %
    % IF invert=no
    \ifthenelse{\equal{\storeInvert}{no}}{%
        Code...}{%
    % ELSEIF invert=yes
    \ifthenelse{\equal{\storeInvert}{yes}}{%
        Code...}{%
    % ELSE
    \PackageWarning{seal}{Unknown option invert=\storeInvert.}}}%
}

I'd like to know how to redefine this in order to have a code like this:

\newcommand{\seal}[2][]{%
    \pgfkeys{/seal,invert=false,other=value,#1}%
    %
    % IF invert=false
    \ifthenelse{\NOT\storeInvert}{%
        Code...}{%
    % ELSE (invert=true)
        Code...}%
}

In case other value is passed (e.g. invert=blah), I'd like \seal to return a message like:

Unknown option `invert=blah`: `blah` is not boolean.

This last may not be necessary, though. It would depend on what is printed when a non boolean value is passed into a boolean key.

Thanks in advance for any help.

PS: The packages I am using/requiring are graphicx, tikz pgfkeys, and ifthen.

2
  • 2
    You are looking for ‘.is choice’, I think.
    – user194703
    Commented Aug 31, 2019 at 1:40
  • Thanks, I'll check info about it.
    – lfba
    Commented Aug 31, 2019 at 1:47

1 Answer 1

8

I think you are looking for the .is choice key. Of course, I do not know what you want to do if any of the Booleans is true or false, so I just replaced the code by some message and typeset. You need to replace the right-hand sides ... of

invert/true/.code=...

by your own codes. The following illustrates how this works. The code also contains some more explanations in the comments.

\documentclass{article}
\usepackage{pgf}
\pgfkeys{/seal/.is family, /seal/.cd, %<-added /.cd 
invert/.is choice,
invert/.default=false,
invert/true/.code={\message{Set /seal/invert to true.^^J}Set /seal/invert to true.}, %<-replace by your own
invert/false/.code={\message{Set /seal/invert to false.^^J}Set /seal/invert to false.}, %<-replace by your own
other/.is choice,
other/.default=false,
other/true/.code={\message{Set /seal/other to true.^^J}Set /seal/other to true.}, %<-replace by your own
other/false/.code={\message{Set /seal/other to false.^^J}Set /seal/other to false.}, %<-replace by your own
}
\begin{document}
\pgfkeys{/seal/invert=true}\par
\pgfkeys{/seal/other=false}
\end{document}

enter image description here

In view of your comment, here is a version using the /.is if key. Unlike the /.is choice key, this is a Boolean, i.e. only true and false are allowed.

\documentclass{article}
\usepackage{pgf}
\newif\ifsealinvert
\newif\ifsealother
\pgfkeys{/seal/.is family, /seal/.cd, %<-added /.cd 
invert/.is if=sealinvert,
other/.is if=sealother,
}
\newcommand{\seal}[2][]{%
\pgfkeys{/seal,invert=false,other=true,#1}%
    %
    % IF invert=no
    \ifsealinvert
     \message{seal invert is true.^^J}%
     seal invert is true.
    \else 
       \message{seal invert is false.^^J}%
     seal invert is false.
    \fi 
    \ifsealother
     \message{seal other is true.^^J}%
     seal other is true.
    \else 
       \message{seal other is false.^^J}%
     seal other is false.
    \fi 
}
\begin{document}
\seal{}\par
\seal[invert=true]{}\par
\seal[other=false]{}\par
\seal[invert=true,other=false]{}\par
\end{document}

enter image description here

And you can mix these e.g. as follows:

\documentclass{article}
\usepackage{pgf}
\newif\ifsealinvert
\newif\ifsealother
\pgfkeys{/seal/.is family, /seal/.cd, %<-added /.cd 
invert/.is choice,
invert/yes/.code=\sealinverttrue,
invert/no/.code=\sealinvertfalse,
other/.is if=sealother,
}
\newcommand{\seal}[2][]{%
\pgfkeys{/seal,invert=no,other=true,#1}%
    %
     \ifsealinvert
     \message{seal invert is yes.^^J}%
     seal invert is yes.
    \else 
       \message{seal invert is no.^^J}%
     seal invert is no.
    \fi 
    \ifsealother
     \message{seal other is true.^^J}%
     seal other is true.
    \else 
       \message{seal other is false.^^J}%
     seal other is false.
    \fi 
}
\begin{document}
\seal{}\par
\seal[invert=yes]{}\par
\seal[other=false]{}\par
\seal[invert=yes,other=false]{}\par
\end{document}

enter image description here

7
  • Thanks again! I'll study your answer.
    – lfba
    Commented Aug 31, 2019 at 3:50
  • 1
    @LuisBartolo For the case of an unknown key look at section 87.3.6 Keys That Are Unknown of pgfmanual v3.1.4.
    – user194703
    Commented Aug 31, 2019 at 4:02
  • Thanks, I won't need the .unknown for this command. I'd rather like to store the boolean value of invert in a variable \invertVar, so that I can use it in ifthenelse statements. For example, \ifthenelse{\invertVar}{Code if invert=true...}{Code if invert=false...}. I'm not sure how to do it with your first answer, but it's still helpful.
    – lfba
    Commented Aug 31, 2019 at 4:08
  • 1
    @LuisBartolo You can define e.g. invert/true/.code={\def\invertVar{true}} but to me this looks unnecessarily complicated. You may use the /.is if key for that, see the example on top of p. 974 of pgfmanual v3.1.4.
    – user194703
    Commented Aug 31, 2019 at 4:12
  • 1
    @LuisBartolo Yes, you won't need the ifthenelse package. I added an example for the /.is if key. Of course, you can mix these by introducing the \newifs in the first example and doing something like invert/yes/.code={\sealinverttrue}.
    – user194703
    Commented Aug 31, 2019 at 4:25

You must log in to answer this question.

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