這個例子比它真正需要的要長得多,但我現在沒有時間刪減它(抱歉!)。
這個例子中的一切都按預期工作除了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
您的問題與(由)無關my fill=blue
。你會得到同樣的錯誤fill=blue
。您的問題與 pgfkey 的巨集擴充功能有關: pgfkey 看到的是鍵,而不是帶有值的my flll=blue
鍵。my fill
blue
第一個解決方案:
代替 :
\fill[\@@ae@fill]...
經過:
\expandafter\fill\expandafter[\@@ae@fill]...
第二種解決方案:
代替:
\def\@@ae@fill{#3}
經過:
\tikzset{@@ae@fill/.style={#3}}
然後替換:
\fill[\@@ae@fill]...
經過:
\fill[@@ae@fill]...