Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: is coordinate defined #646

Closed
daleif opened this issue Apr 5, 2019 · 8 comments
Closed

Feature request: is coordinate defined #646

daleif opened this issue Apr 5, 2019 · 8 comments

Comments

@daleif
Copy link

daleif commented Apr 5, 2019

Often when I do trace images for people, I make a lot of helper coordinates. It is useful to be able to mark them. But I do not want to maintain a list of my defined coordinates, nor do I want to number my coordinates sequentially. At one point I came up with the MWE below. It works for normal coordinates, but not for things like node positions.

So I'm requesting a feature one can use to determine whether a coordinate/.. is defined.

\documentclass[tikz]{standalone}
\makeatletter
% handy macro, then the loop does not need to be over only defined nodes
\def\ifcoordinatedefined#1#2#3{%
  \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\makeatother

\newcommand\ShowPoints[1][0,1,...,200]{
  \begingroup
  \foreach\n in{#1}{
    \ifcoordinatedefined{T\n}{
      \fill[green,opacity=0.75] (T\n) circle (1pt) node[below,red] {\tiny\n};
    }{}
  }
  \endgroup
}

\begin{document}

\begin{tikzpicture}
  \draw (0,0) circle (1);
  \coordinate (T0) at (0,0);
  \coordinate (T1) at (1,0);
  \node[draw,anchor=west] (A) at (T1) {Test};

  % this works
  \ShowPoints

  % this does not work
  \ifcoordinatedefined{A.north east}{
    \fill[magenta] (A.north east) circle (2pt);
  }{}
  
  
\end{tikzpicture}

\end{document}
@hmenke
Copy link
Member

hmenke commented Apr 5, 2019

A.north east is not a coordinate but a node anchor.

@daleif
Copy link
Author

daleif commented Apr 5, 2019

Thanks, I did not know the proper terminology for A.north east

@hmenke
Copy link
Member

hmenke commented Apr 5, 2019

You can use this bit to check for the existence of an anchor.

% Invoke an anchor
\def\pgf@sh@reanchor#1#2{%
\pgfutil@ifundefined{pgf@anchor@#1@#2}%
{%
\pgfutil@ifundefined{pgf@anchor@generic@#2}{%
\pgfmathsetcounter{pgf@counta}{#2}%
\csname pgf@anchor@#1@border\endcsname{\pgfqpointpolar{\the\c@pgf@counta}{1pt}}%
}{%
\csname pgf@anchor@generic@#2\endcsname{#1}%
}%
}%
{\csname pgf@anchor@#1@#2\endcsname}%
}%

The problem is that A.180 is a valid anchor, so if you try to reference a non-existent anchor, like A.weest, PGF will try to evaluate weest as a math function which will fail.

\pgfmathsetcounter{pgf@counta}{#2}%

There is no way to check whether something is a valid math expression or not prior to evaluation.

@daleif
Copy link
Author

daleif commented Apr 5, 2019

Then it seems best to at least just check for existence on coordinates. Then users will just have to make aliases for anchors if they want to use the test.

Wouldn't that be doable?

(I have yet to actually need to test on an anchor)

@hmenke
Copy link
Member

hmenke commented Apr 5, 2019

I have a possible patch ready here hmenke@98b9d6b but I'm wondering whether this is so useful.

@daleif
Copy link
Author

daleif commented Apr 5, 2019

can that test coordinates as well? I don't quite understand it.

@hmenke
Copy link
Member

hmenke commented Apr 6, 2019

This is just the same definition as you had for \ifcoordinatedefined but renamed to \pgfifnamedshapeundefined and with true and false branch flipped to match the new name.

@hmenke
Copy link
Member

hmenke commented Apr 17, 2019

I figured that this is not really worth providing in the PGF core, because it ca be implemented by a user in literally a single line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants