
\node
の使用をテストするために次のコードを作成しましたtikz
。別のパス (この例では /a) で定義されたスタイルを使用すると問題が発生することがわかりました。詳細については MWE を参照してください。原因と対処方法について誰か教えてくれませんか?
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}
答え1
なぜ動作するのかと尋ねていますcircle
。この答えは実際には少し微妙で、次のコード ブロックに含まれています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%
}%
ご覧の通り、Tiけcircle
Z はさまざまなことを試します。 の代わりにshape=circle
やred
の代わりにと言うことができるので、ユーザーにとっては便利ですcolor=red
。
コードを で動作させることもできます/.try
。しかし、これはお勧めしません。むしろ、 Ti を与えることをお勧めします。けZ 不足している情報、つまりshape=circle
の代わりにcircle
、/pgf/minimum size
の代わりにと言う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}
これを推奨する理由は、/.try
などをやりすぎると、どのキーが優先されるのかがますますわかりにくくなるためです。