我如何在 tikz 中(在创建路径期间)检查节点是否具有给定的锚点(例如west
)?值得注意的是,我可能希望根据获得的节点类型绘制不同的路径,例如\tikztostart.west
如果存在west
锚点:
mystyle/.style={to path={ (\tikztostart.west) -- (\tikztotarget) \tikztonodes}}
相关:我还想检查是否\tikztostart
是一个节点(如A
)或一个 pgfpoint(如A.center
)...如果你知道怎么做那么你可以回答我的问题Tikz:“\tikztostart”,如何区分节点和坐标,并获取节点边界 X 角的坐标
编辑
请注意,至少,如果节点没有给定的锚点,A.west
则会正常失败(我认为是指向原点)。但我最初的问题仍然悬而未决。
我也认为可以使用此代码测试,即\pgfutil@ifundefined{pgf@anchor@#1@#2}
,其中#1
似乎是{\csname pgf@sh@ns@\tikztostart\endcsname}
并且#2
是锚点的名称,如使用这里.但这是没有记录,因此奇怪的是 tikz 没有提供更易于访问的功能......
答案1
经过一段时间,我终于搞清楚了。我找不到更“官方”的解决方案,我需要深入研究 tikz 代码……希望它以后不会崩溃!
\makeatletter
%%% Checks if a function is a point or a node.
%%% Not sure if best solution (needed to dig into source of TeX), but can't find anything better in manual
%%% https://tex.stackexchange.com/questions/6189553
%%% #1 is the name of the node, for instance \tikztostart in path, or \tikz@fig@name in node.
%%% #2 is code to run if point,
%%% #3 is code to run if node.
\def\ifPgfpointOrNode#1#2#3{%
\pgfutil@ifundefined{pgf@sh@ns@#1}{%
#2%
}{%
#3%
}%
}
% This function is needed in the next function (doing \pgfutil@ifundefined{pgf@anchor@\shapenode{}@#2} fails).
% I guess it has to do with the way macro are expanded...
%%% #1: name of the shape (e.g. "rounded rectangle")
%%% #2: name of the anchor
%%% #3: condition if anchor exists
%%% #4 condition if anchor does not exist
\def\ifAnchorExistsFromShape#1#2#3#4{
\pgfutil@ifundefined{pgf@anchor@#1@#2}{%
#4%
}{%
#3%
}%
}
%%% Check if an anchor exists given a node.
%%% #1: name of the node (e.g. \tikztostart)
%%% #2: name of the anchor
%%% #3: condition if anchor exists
%%% #4 condition if anchor does not exist
\def\ifAnchorExists#1#2#3#4{%
%%% First we extract the shape of the node:
\edef\pgf@node@name{#1}%
\edef\shapenode{\csname pgf@sh@ns@\pgf@node@name\endcsname}%
\ifAnchorExistsFromShape{\shapenode}{#2}{#3}{#4}
}
\makeatother