30

Possible Duplicate:
TikZ: Node at same x-coordinate as another node, but specified y-coordinate?
Extract x, y coordinate of an arbitrary point in TikZ

How to use x coordinate of a point and y coordinate of other point? I need similar to

\coordinate (A) at (2,1);
\coordinate (B) at (3,0);
\coordinate (C) at (4,3);
\coordinate (D) at (5,-1);

\draw (xA,yB) -- (xC,yD);
41

You can use the orthogonal coordinate system identifiers -| and |-. They indicate horizontal component of the first and vertical component of the second for -| and vice versa for |-.

I have converted the coordinates to nodes to show the locations.

\begin{tikzpicture}
\node (A) at (2,1) {a};
\node (B) at (3,0) {b};
\node (C) at (4,3) {c};
\node (D) at (5,-1) {d};

\draw (A |- B) -- (C |- D);
\end{tikzpicture}

enter image description here

| improve this answer | |
18

Here's a less convenient solution than the solution proposed by Percusse but closer to the initial demand.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
    \node (A) at (2,1) {a};
    \node (B) at (3,0) {b};
    \node (C) at (4,3) {c};
    \node (D) at (5,-1) {d};

    \draw let \p{A}=(A), \p{B}=(B), \p{C}=(C), \p{D}=(D) in
    (\x{A},\y{B}) -- (\x{C},\y{D});
\end{tikzpicture}
\end{document}

The result is exactly the same...

| improve this answer | |
  • This should be the accepted answer since it is valid even when you are not using \draw. – Antonio Sesto Nov 10 '17 at 8:14
  • @AntonioSesto The other answer is also valid without \draw... – Paul Gaborit Apr 24 '18 at 14:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.