Este ejemplo es mucho más largo de lo que realmente necesita ser, pero no tengo tiempo para podarlo ahora (¡disculpas!).
Todo en este ejemplo funciona como se esperaba.exceptola my fill
clave que se supone que debe definirse como .is choice
clave. Sin embargo, cuando intento compilar, LaTeX se queja de que
! 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.
Si reemplazo cada aparición de my fill=<color>
con my <color>
, el código se compila correctamente.
%-*-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}%%'
¿Alguien podría explicarme cómo puedo hacer que mi clave de elección funcione correctamente?
Cuando funciona correctamente y cuando my colors
se selecciona la clave, el resultado debería verse como:
Respuesta1
Su problema no está relacionado con .is choice
(utilizado por my fill=blue
). Obtienes el mismo error con fill=blue
. Su problema está relacionado con la expansión de la macro mediante pgfkey: pgfkey ve la clave my flll=blue
y no la clave my fill
con el valor blue
.
Primera solución:
Reemplazar :
\fill[\@@ae@fill]...
por:
\expandafter\fill\expandafter[\@@ae@fill]...
Segunda solución:
Reemplazar:
\def\@@ae@fill{#3}
por:
\tikzset{@@ae@fill/.style={#3}}
Luego reemplaza:
\fill[\@@ae@fill]...
por:
\fill[@@ae@fill]...