3

How could I check in tikz (during a path creation) if a node has a given anchor (say west)? Notably, I may want to draw different path depending on the type of node that I get, like \tikztostart.west if there is a west anchor:

mystyle/.style={to path={ (\tikztostart.west) -- (\tikztotarget) \tikztonodes}}

Related: I also want to check if \tikztostart is a node (like A) or a pgfpoint (like A.center)... If you know how to do then you may answer my question there Tikz: `\tikztostart`, how to differentiate nodes from coordinates, and obtain coordinate at angle X of node's border

EDIT

Note that at least, if a node does not have a given anchor, A.west fails gracefully (I think to the point origin). But my initial question is still open.

I also think that one can use this code to test, namely \pgfutil@ifundefined{pgf@anchor@#1@#2}, where #1 seems to be {\csname pgf@sh@ns@\tikztostart\endcsname} and #2 is the name of the anchor, as used here. But this is not documented, so it is strange that tikz does not provide a more accessible function...

7
  • 1
    Do you know that any TikZ's node ( a generalization of point) has infinitely many anchors, including .west, . 180, .north, .90 ?
    – Black Mild
    Commented Oct 15, 2021 at 7:17
  • Oh, good to know that there is a .90 anchor. That said, it's still interesting to know sometimes if a node has a given anchor or not, for instance to do different paths depending on the shape of the node (oh, btw, is it possible to check the shape of a node?).
    – tobiasBora
    Commented Oct 15, 2021 at 7:38
  • All that stuff is in pgfmanual. Please read it before saying something is not documented!
    – Black Mild
    Commented Oct 15, 2021 at 7:50
  • The .90 is surely yes, but \pgfutil@ifundefined{pgf@anchor#1#2} is definitely not documented (or my pdf's search function is broken...). I've been spending two days on this manual and in the tikz code... and it's 1321 pages, so I don't plan to know it perfectly any time soon. Concerning a function which takes as input a node and outputs the shape, I can't find any (I've been notably reading "Nodes and shapes" (p. 1135), notably "Declaring new shapes" starting page 1143). If I missed it, then could you please point me to it?
    – tobiasBora
    Commented Oct 15, 2021 at 8:11
  • Sometimes we are confusing between reading and understanding. From pgfmanual, page 165, it said that the macro \tikztotarget is set to a coordinate. Unlike a node, by definition a coordinate has only one anchor - itself. Therefore, it has no meaning when writing \tikztostart.west. I will not discuss more.
    – Black Mild
    Commented Oct 15, 2021 at 8:32

1 Answer 1

1

After some time I managed to figure it out. I can't find a more "official" solution, I needed to dig into tikz code... Hope it won't break later!

\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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .