为什么我的 /.is 选择键不能正常工作?

为什么我的 /.is 选择键不能正常工作?

这个例子比它实际需要的要长得多,但是我现在没有时间将其删减(抱歉!)。

此示例中的所有内容均按预期运行除了my fill应该定义为密钥的密钥。.is choice但是,当我尝试编译时,LaTeX 抱怨说

! Package pgfkeys Error: I do not know the key '/tikz/my fill=blue' and I am go
ing to ignore it. Perhaps you misspelled it.

如果我将每个出现的 替换my fill=<color>my <color>,代码就可以正确编译。

%-*-mode:LaTeX-*-
\documentclass[border=8]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
\usepackage{etoolbox}
\let\x\expandafter

\makeatletter

%-@-(1)---------------------------------------------------------------------
\long\def\if@node@defined(#1)#2#3{%%                                        
  \@ifundefined{pgf@sh@ns@#1}%%                                             
    {#3}%% FALSE: "yes" it's undefined                                      
    {#2}}% TRUE:  "no"  it's not undefined                                  
\def\ifsquarenodedefined#1{%%
  \x\let\x\ae@tmp\csname ae@#1@node\endcsname
  \x\if@node@defined\x(\ae@tmp){\booltrue{#1.bool}}{\boolfalse{#1.bool}}}

\providebool{nw.bool}
\providebool{ne.bool}
\providebool{sw.bool}
\providebool{se.bool}

%-@-(2)---------------------------------------------------------------------
%% First call the function to read the "optional" arguments, which really   
%% aren't so optional.  The optional arguments should be passed as          
%% [#1;#2;#3] where #1 is actually required.                                
\newcommand\aesquare{\ae@square}

%% #1=nw,ne,se,sw ==> set the right angle for the triangle to be colored    
%% #2=sets the optional arguments to be passed to the draw command.         
%% #3=sets the optional arguments to be passed to the fill command.         
\def\ae@square[#1;#2;#3]{%%
  \def\@@ae@corner{#1}%%
  \def\@@ae@draw{#2}%%
  \def\@@ae@fill{#3}
  \@ae@square}
%% This next macro is the meat of the business.  It will only draw and fill
%% the triangle defined by the passed nodes if, in fact, the nodes are     
%% already defined.                                                        
\def\@ae@square(#1,#2,#3){%%
  %%
  \pgfmathsetmacro\ae@node@nx{int(#2+1)}%%
  \pgfmathsetmacro\ae@node@ny{int(#3-1)}%%
  %% set the macros that `\ifsquarenodedefined` expects to already be
  %% defined.
  \edef\ae@nw@node{A#2#3}%%
  \edef\ae@ne@node{A\ae@node@nx#3}%%
  \edef\ae@se@node{A\ae@node@nx\ae@node@ny}%%
  \edef\ae@sw@node{A#2\ae@node@ny}%%
  %% set the booleans that will be tested next.
  \ifsquarenodedefined{nw}%%
  \ifsquarenodedefined{ne}%%
  \ifsquarenodedefined{se}%%
  \ifsquarenodedefined{sw}%%
  %% only draw and fill a triangle if the corners are all properly defined.
  \ifboolexpr{
      bool {nw.bool} and
      bool {ne.bool} and
      bool {se.bool} and
      bool {sw.bool} }
  {
    \x\ifstrequal\x{\@@ae@corner}{nw}{\ae@set{a}{sw}\ae@set{b}{nw}\ae@set{c}{ne}}{}
    \x\ifstrequal\x{\@@ae@corner}{ne}{\ae@set{a}{nw}\ae@set{b}{ne}\ae@set{c}{se}}{}
    \x\ifstrequal\x{\@@ae@corner}{se}{\ae@set{a}{ne}\ae@set{b}{se}\ae@set{c}{sw}}{}
    \x\ifstrequal\x{\@@ae@corner}{sw}{\ae@set{a}{se}\ae@set{b}{sw}\ae@set{c}{nw}}{}
    \draw[\@@ae@draw] (\ae@a) -- (\ae@b) -- (\ae@c) -- cycle;
    \fill[\@@ae@fill] (\ae@a) -- (\ae@b) -- (\ae@c) -- cycle;
  }{}}

\def\ae@set#1#2{%%
  \x\edef\csname ae@#1\endcsname{\csname ae@#2@node\endcsname}}

\makeatother

\begin{document}%%'

\begin{tikzpicture}[my dot/.style={fill,circle,inner sep=1.5pt},>={To[scale=2]},
                    my colors/.style={
                      my gray/.style={gray},
                      my blue/.style={blue!30},
                      my red/.style={red},
                      my green/.style={green},
                    },
                    my same colors/.style={              
                      my gray/.style={gray},
                      my blue/.style={gray},
                      my red/.style={gray},
                      my green/.style={gray},
                    },
                    my fill/.is choice,
                    my fill/blue/.style={my blue},
                    my fill/gray/.style={my gray},
                    my fill/red/.style={my red},
                    my fill/green/.style={my green},
                    my draw/.style={line width=0.8pt},
                    my colors,
                    x=1.5cm,y=1.5cm
  ]
  \coordinate (A11) at (0,0);
  \foreach \myx in {0,...,6}
  {
    \foreach \myy in {0,...,6}
    {
      \coordinate (A\myx\myy) at (\myx,\myy);
    }
  }

  \foreach \myx in {0,...,6}
  {
    \foreach \myy in {0,...,6}
    {
      \pgfmathsetmacro\aeinta{int(\myx)}%%
      \pgfmathsetmacro\aeintb{int(\myy)}%%
      \ifnumcomp{\aeinta}{<}{3}
        { 
          \ifnumcomp{\aeintb}{<}{4}
          {
            \aesquare[ne;my draw;my fill=blue](A,\myx,\myy)
          }
          {
            \aesquare[se;my draw;my fill=red](A,\myx,\myy)
          }
        }
        {
          \ifnumcomp{\aeintb}{<}{4}
          {
            \aesquare[nw;my draw;my fill=green](A,\myx,\myy)
          }
          {
            \aesquare[sw;my draw;my fill=gray](A,\myx,\myy)
          }
        }
    }
  }

  \draw[my draw] (A00) rectangle (A66);

\end{tikzpicture}

\end{document}%%'

有人可以解释一下如何才能让我的选择键正常工作吗?

当正常工作并选择键时my colors,结果应如下所示:

在此处输入图片描述

答案1

.is choice您的问题与(used by )无关my fill=blue。使用 时会遇到同样的错误fill=blue。您的问题与 pgfkey 的宏扩展有关:pgfkey 看到的是键,而不是值为 的my flll=blue键。my fillblue

第一个解决方案:

代替 :

\fill[\@@ae@fill]...

经过:

\expandafter\fill\expandafter[\@@ae@fill]...

第二种解决方案:

代替:

\def\@@ae@fill{#3}

经过:

\tikzset{@@ae@fill/.style={#3}}

然后替换:

\fill[\@@ae@fill]...

经过:

\fill[@@ae@fill]...

相关内容