I should note that I am already familiar with TikZ and am I am looking for technical answers, as if TikZ weren't technical enough.
This question boils does to the following question, what is the difference between these things:
\def\pointA{\pgfpoint{0cm}{0cm}}
\coordinate (A) at (current page.north east);
\coordinate (A) at (1,1);
(current page.north east)
\node [anchor=east] {}
<-- an anchor? :-p (just demonstrating overlapping of terms)\pgfpointanchor
<-- but this must really be an anchor, or is it a point? :-p
I think my understanding is lacking, and it is interfering with my attempts shown below.
In mathematics
A point is defined as:
A point is an exact position or location on a plane surface. It is important to understand that a point is not a thing, but a place.
A coordinate is defined as:
Coordinates are an ordered set of numbers that define the position of a point.
An anchor is:
I would like to clarify this with practical examples.
Let's define a coordinate:
\coordinate (rightmid) at ($ (current page.north east) + (0,-18cm) $); % use tikz calc library
There are many implicit coordinates (should they be called coordinates, points, or anchors or something else?) such as:
- (current page.north west)
- (current page.north)
- (current page.north east)
- (current page.east)
- (current page.south east)
- (current page.south)
- (current page.south west)
- (current page.center)
Let's say I want to calculate the vertical distance between two things (I am choosing the word "things" on purpose) AND store this variable for use outside of the current scope.
e.g. the two things are (current page.north east) and (rightmid)
To do arithmetic I know of the following options (is that all of them?):
I could use
\pgfmathparse
to evaluate the arithmeticI could use the
\path let \p1=
syntax.
Using option 1, I can set \pgfxa
, \pgfya
to \pgfx
and \pgfy
for the calculations and acquire the result with \pgfmathresult
.
I can then define a unique length (global) e.g. \setlength{\mylength}{2cm}
. I can then use \mylength
to retrieve it. This will not allow me to write everything in a self-contained macro, because \setlength
may only be defined once. An alternative is to use \global\let\mylength{2cm}
, which seems to me better for the job (correct me if I am wrong. Does this have implications on the value+unit/dimension)?
Putting it all together
- Is it possible to use the width of one TikZ node to calculate the width of another?
- How can I convert a coordinate to a pgfpoint, and viceversa?
- Coordinates A, B: compute |B-A| and angle between +x and (B-A)
- pgf macro that returns a node
Here is an experiment that does not compile based on a couple of answers:
\documentclass{article}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{calc}
% A4 Paper
\pdfpagewidth=210mm \pdfpageheight=297mm % for PDF output
\paperwidth=210mm \paperheight=297mm % for TikZ
\newcommand{\getdistance}[3]{
\makeatletter
% Syntax: <length variable/macro> <coordinate1> <coordinate2>
\pgfpointdiff{\pgfpointanchor{#2}{center}} % https://tex.stackexchange.com/a/39325/13552
{\pgfpointanchor{#3}{center}} % https://tex.stackexchange.com/a/39325/13552
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
\pgfmathparse{veclen(\pgf@xa,\pgf@ya)}
\global\let#1\pgfmathresult % <-- I want the pgfmathresult to be a dimension like 510 pt
\makeatother
}
\begin{document}
\begin{tikzpicture}[overlay]
\coordinate (rightmidtest) at ($ (current page.north east) + (0,-10cm) $); % use tikz calc library
\coordinate (rightmid) at ($ (current page.north east) + (0,-18cm) $); % use tikz calc library
\end{tikzpicture}
\getdistance{\globalresult}{rightmidtest}{rightmid}
\getdistance{\globalresult}{current page.north east}{rightmid}
\getdistance % <-- Should expand to last distance measured
\end{document}
Updated Code 2016-02-25 Working
After analysis and newfound understanding from percusse's answer, I came up with something else.
Remember:
(current page)
is implicit- anchor .center is implicit in TikZ but not in PGF
\pgfpointanchor{<ref>}{<anchor e.g. center>}
I used xparse
syntax for the macro because it is easier to read.
\documentclass{article}
\usepackage{fontspec}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xparse}
\makeatletter
\NewDocumentCommand{\getdistance}{ m m O{center} m O{center} }{%
%Syntax: {\<macro>} {<ref1>} [<anchor>] {<ref2>} [<anchor>]
\pgfpointdiff{\pgfpointanchor{#2}{#3}}% https://tex.stackexchange.com/a/39325/13552
{\pgfpointanchor{#4}{#5}}% https://tex.stackexchange.com/a/39325/13552
\pgf@xa=\pgf@x
\pgf@ya=\pgf@y
\pgfmathparse{veclen(\pgf@xa,\pgf@ya)} % result as number (floating point)
%\pgfmathveclen{\pgf@xa}{\pgf@ya} % alternate syntax to pgfmathparse: result as number (floating point)
%\global\let#1\pgfmathresult pt % does not recognize pt.
\global\edef#1{\pgfmathresult\ pt}
%\pgfmathprintnumber{\pgfmathresult}%
}
\makeatother
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
% (current page) is implicit
% .center is implicit in TikZ but not in PGF \pgfpointanchor{<ref>}{<anchor e.g. center>}
\coordinate (referral1) at ($ (current page.north east) + (0,-18cm) $); % use tikz calc library
\node [minimum height=2cm] (referral2) {};
\end{tikzpicture}
\begin{tikzpicture}[remember picture,overlay]
\getdistance{\globalresultA}{current page}{referral1}
\getdistance{\globalresultB}{current page}[north east]{referral1}
\getdistance{\globalresultC}{current page}[north east]{referral2}[north]
\getdistance{\globalresultD}{current page}[north east]{referral2}[south]
\end{tikzpicture}
\globalresultA
\globalresultB
\globalresultC
\globalresultD
\end{document}
Undefined control sequence. ...nce{\globalresult}{rightmidtest}{rightmid}
.\makeatletter\def\getdistance#1#2#3{\pgfpointdiff{\tikz@scan@one@point\pgf@process(#2)}{\tikz@scan@one@point\pgf@process(#3)}\pgfmathveclen{\the\pgf@x}{\the\pgf@y}\edef#1{\pgfmathresult pt}}