我编写了以下代码来测试\node
in的使用tikz
。我发现使用在另一个路径(本例中为 /a)下定义的样式会导致问题 - 请参阅 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钾Z 尝试了各种各样的方法。这对用户来说很方便,因为他们可以说circle
而不是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
诸如此类的东西,哪个键具有优先权就会变得越来越不清楚。