我如何设置布尔 pgfkeys?

我如何设置布尔 pgfkeys?

我正在制作一个名为的命令,seal它需要一个布尔key=value选项。由于我不知道如何执行此操作,因此我使用key=yeskey=no来定义我的,pgfkeys如下所示。

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

然后,我\seal大致定义如下:

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

我想知道如何重新定义它以获得如下代码:

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

如果传递了其他值(例如invert=blah),我希望\seal返回如下消息:

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

不过,最后这一点可能不是必需的。这取决于将非布尔值传递到布尔键时打印的内容。

在此先感谢您的帮助。

PS:我正在使用/需要的包是graphicxtikz pgfkeysifthen

答案1

我想你正在寻找.is choice关键。当然,我不知道如果任何一个布尔值为真或假,你想做什么,所以我只是用一些消息和排版替换了代码。你需要...替换

invert/true/.code=...

用您自己的代码。下面说明了其工作原理。代码还在注释中包含了一些其他解释。

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

在此处输入图片描述

鉴于您的评论,这里有一个使用/.is ifkey 的版本。与/.is choicekey 不同,这是一个布尔值,即只允许true和。false

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

在此处输入图片描述

您可以按如下方式混合它们:

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

在此处输入图片描述

相关内容