
Ich erstelle den folgenden Code, um die Verwendung von \node
in zu testen tikz
. Ich habe festgestellt, dass die Verwendung von Stilen, die unter einem anderen Pfad definiert sind (in diesem Beispiel /a), Probleme verursacht. Weitere Einzelheiten finden Sie im MWE. Kann mir jemand bei der Ursache und der Lösung helfen?
MWE:
\documentclass[a4paper]{article}
\usepackage{tikz}
\begin{document}
\pgfkeys{/a/.search also={/tikz},
/a/.cd,
myshape/.style={fill=red,circle},
size/.style={minimum size=#1*0.5cm},
size/.default=1
}
% example No.1:
\tikz\node[fill=red,circle]{AAA}; %The option "circle" can be used dirctly in "\node".
% example No.2:
% Contrasted with example No.1, why can "circle" not be used in myshape/.style which leads to unsuccessful compile?
% \tikz[/tikz/.search also={/a}]\node[myshape]{AAA};
% example No.3:
% Why does the "size=3" not work?
\tikz[/tikz/.search also={/a}]\node[fill=red,size=3]{AAA};
\end{document}
Antwort1
Sie fragen, warum circle
das funktioniert. Die Antwort darauf ist eigentlich etwas heikel und im folgenden Codeblock enthalten tikz.code.tex
:
\pgfkeys{/tikz/.unknown/.code=%
% Is it a pgf key?
\let\tikz@key\pgfkeyscurrentname%
\pgfkeys{/pgf/\tikz@key/.try={#1}}%
\ifpgfkeyssuccess%
\else%
\expandafter\pgfutil@in@\expandafter!\expandafter{\tikz@key}%
\ifpgfutil@in@%
% this is a color!
\expandafter\tikz@addoption\expandafter{\expandafter\tikz@compat@color@set\expandafter{\tikz@key}}%
\edef\tikz@textcolor{\tikz@key}%
\else%
\pgfutil@doifcolorelse{\tikz@key}
{%
\expandafter\tikz@addoption\expandafter{\expandafter\tikz@compat@color@set\expandafter{\tikz@key}}%
\edef\tikz@textcolor{\tikz@key}%
}%
{%
% Ok, second chance: This might be an arrow specification:
\expandafter\pgfutil@in@\expandafter-\expandafter{\tikz@key}%
\ifpgfutil@in@%
% Ah, an arrow spec!
\expandafter\tikz@processarrows\expandafter{\tikz@key}%
\else%
% Ok, third chance: A shape!
\expandafter\ifx\csname pgf@sh@s@\tikz@key\endcsname\relax%
\pgfkeys{/errors/unknown key/.expand
once=\expandafter{\expandafter/\expandafter t\expandafter i\expandafter k\expandafter z\expandafter/\tikz@key}{#1}}%
\else%
\edef\tikz@shape{\tikz@key}%
\fi%
\fi%
}%
\fi%
\fi%
}%
Wie Sie sehen können, TikZ probiert allerlei Dinge aus. Das ist praktisch für den Benutzer, denn er kann circle
statt shape=circle
und red
statt sagen color=red
.
Sie könnten Ihren Code mit zum Laufen bringen /.try
. Ich empfehle dies jedoch nicht. Vielmehr empfehle ich, einfach Ti zu gebenkZ die fehlenden Angaben, also shape=circle
statt circle
und /pgf/minimum size
statt zu sagen minimum size
.
\documentclass[a4paper]{article}
\usepackage{tikz}
\begin{document}
\pgfkeys{/a/.search also={/tikz},
/a/.cd,
myshape/.style={fill=red,shape=circle},
size/.style={/pgf/minimum size=#1*0.5cm},
size/.default=1
}
% example No.1:
\tikz\node[fill=red,circle]{AAA}; %The option "circle" can be used dirctly in "\node".
% example No.2:
% Contrasted with example No.1, why can "circle" not be used in myshape/.style which leads to unsuccessful compile?
\tikz[/tikz/.search also={/a}]\node[myshape]{AAA};
% example No.3:
% Why does the "size=3" not work?
\tikz[/tikz/.search also={/a}]\node[fill=red,size=3]{AAA};
\end{document}
Der Grund, warum ich dies empfehle, besteht darin, dass, wenn man es mit dem /.try
usw. übertreibt, immer unklarer wird, welche Taste Vorrang hat.