2

I am trying to calculate a new coordinate in relation to two other cordinates. How can I use the x component of one coordinate and the y component of the other? Thanks in advance.

Minimal example:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
  \begin{tikzpicture}
    \node at (0,0) (a){a};
    \node at (1,1) (b){b};
    \node at ($ (*xcomponent of a*-1,*ycomponent of b*+1) $)
  \end{tikzpicture} 
\end{document}
moku89
  • 31
  • i hoped there was a something like /getxy(variable){x component} {y component} but if there is no such command think i will use \path(a) and\pgfgetlastxy{\Xa}{\Ya} – moku89 Jul 28 '16 at 08:08

2 Answers2

1

You can extract the coordinates via Extract x, y coordinate of an arbitrary point in TikZ and then perform the coordainte calculations:

enter image description here

Notes:

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

% https://tex.stackexchange.com/questions/33703/extract-x-y-coordinate-of-an-arbitrary-point-in-tikz \newdimen\XCoord \newdimen\YCoord \newcommand{\ExtractCoordinateX}[2]{\path (#2); \pgfgetlastxy{\XCoord}{\YCoord}; \xdef#1{\XCoord}}% \newcommand{\ExtractCoordinateY}[2]{\path (#2); \pgfgetlastxy{\XCoord}{\YCoord}; \xdef#1{\YCoord}}%

\begin{document} \begin{tikzpicture} \node at (0,0) (a){a}; \node at (1,1) (b){b};

\ExtractCoordinateX{\NewX}{a}
\ExtractCoordinateY{\NewY}{b}
\coordinate (X) at (\NewX,\NewY);

\node [draw=red, shape=circle] at ($ (X)+ (-1,1) $) {X};

\end{tikzpicture} \end{document}

Peter Grill
  • 223,288
1

You may use the |- operation:

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
  \begin{tikzpicture}
    \node at (0,0) (a){a};
    \node at (1,1) (b){b};
    \node at ($(a |- b) + (-1,1) $) {c};
  \end{tikzpicture} 
\end{document}

Or you may use the let operation (same result):

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
  \begin{tikzpicture}
    \node at (0,0) (a){a};
    \node at (1,1) (b){b};
    \path let \p1=(a), \p2=(b) in
    node at (\x1 - 1cm, \y2 + 1cm) {c};
  \end{tikzpicture} 
\end{document}
Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
  • In my dokument I tried |- and it worked after changenging a|-b to b|-a but I do not know how and why it works. – moku89 Jul 28 '16 at 07:56
  • @moku89 With b|-a, you get the x coordinate of b and the y coordinate of a: you get a coordinate vertically (|) aligned with b and horizontally (-) aligned with a. – Paul Gaborit Jul 28 '16 at 08:40