# TikZ: Node at same x-coordinate as another node, but specified y-coordinate?

In TikZ, if I have a node (A), how do I create a node (B) with the same x-coordinate as (A), but a given y-coordinate?

-

You can use the let syntax, that allows you to save a node coordinate into a macro \p<number> and then access its components using \x<number> and \y<number>. This requires the calc library to be loaded.

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

\begin{document}

\begin{tikzpicture}
\draw [help lines] (0,0) grid (4,4);
\node (A) at (2,1) {A};
\path let \p1 = (A) in node  at (\x1,3) {B};
\end{tikzpicture}

\end{document}


-
IMHO you should explicit state that it needs the calc library. Also positioning would be an alternative worth mentioning, even when the y coordinate can only be given relatively. –  Martin Scharrer May 16 '11 at 21:50
@Martin: I've added the bit about the need for the calc library. I guess you should write an answer using the positioning library, it is a worthwhile alternative indeed. –  Jake May 16 '11 at 22:05
Thanks for the edit. And sure I should post it as my own answer. However, I'm already on the rep-limit for today and didn't wanted to take easy earned rep points away from other people :-) –  Martin Scharrer May 16 '11 at 22:15
It should be noted that the let...in syntax only works locally; you can't use it if you need the coordinate in another path. –  Liam Dec 31 '13 at 16:44

Even simpler than the let syntax, I've lately become a fan of intersection coordinate systems; this example is technically a perpendicular intersection system (see 13.3 of the 2.10 manual). (Code modified from Jake's answer.)

\begin{tikzpicture}
\draw [help lines] (0,0) grid (4,4);
\node (A) at (2,1) {A};
%\path let \p1 = (A) in node  at (\x1,3) {B};
\draw (A |- 52,3) node {B};
\end{tikzpicture}


The first coordinate supplies the x value, the second the y value. (So it is perhaps a little counterintuitive in this particular use that "52" above is just discarded.)

-
Does the 52 have any effect on the bounding box? –  Matthew Leingang May 17 '11 at 4:25
Nice approach. @Matthew: I don't think it does. –  Jake May 17 '11 at 5:53
@Matthew it doesn't, but I couldn't find anything in the documentation. –  kgr May 17 '11 at 14:02
The 2.10 manual I have (TeXLive) does not have a section 12.2.4. The closest I found was section 13.3 Coordinates at Intersections. –  Martin Scharrer May 17 '11 at 14:15
@Martin you are right, looks like I had the old manual for 2.0 on this computer. –  kgr May 17 '11 at 14:22

Another option, you can place nodes using shift. Here you have some examples.

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw [help lines] (0,0) grid (4,4);
\node (A) at (2,1) {A};
\path ([yshift=2cm]A) node {B};
\node (C) at ([yshift=1cm]A) {C};
\node (D) at ([shift=({1cm,1cm})]A) {D};
\end{tikzpicture}

\end{document}

-

The let syntax already shown in Jakes answer is the most flexible one IMHO. However if you want to simple place a node above or below another (or left or right of it) you can use the above of=<node> or below of=<node> options. The positioning library gives you even more options like above=<opt.length> of <node> etc.

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

\begin{document}

\begin{tikzpicture}
\node (A) at (1,3) {A};
\node [below=2cm of A] {B};
\end{tikzpicture}

\end{document}

-
I guess it is worth pointing out that below=<distance> of will make the gap between the nodes equal <distance>, unlike the other solutions suggested so far, which make the distance between the centres equal a specified distance. This behaviour can be achieved with the positioning library by specifying on grid before the below option. –  Jake May 17 '11 at 9:18

\documentclass{article}
\usepackage{tikz}
\makeatletter
\newcommand{\gettikzxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother

%
\begin{document}
%
\begin{tikzpicture}
%Define some point A
\coordinate (A) at (1,1);
%
%Get x and y coordinates of point A
\gettikzxy{(A)}{\ax}{\ay}
%
%Using x coordinate of point A, define point B
\coordinate (B) at (\ax,4);
\fill[blue] (A) circle (2pt) node [black,right] {A};
\fill[red] (B) circle (2pt) node [black,right] {B};;
%
\end{tikzpicture}
%
\end{document}


Red dot is the new point with same x coord of blue dot and some y coord.

-

Besides Jake's suggestion, you could also use chains (requires the chains library), or relative positioning:

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

\begin{document}

\begin{tikzpicture}%[every node/.style={draw}]
\draw [help lines] (0,-2) grid (4,2);
% using explicit coordinates to place the nodes
\node (A) at (0,0) {A};
\node (B) at (0,2) {B};
% using relative positioning
\node[below=of A] (C) {C};
% using chains
\begin{scope}[xshift=3cm,start chain=going below,node distance=5mm]
\node[on chain] {D};
\node[on chain] {E};
\end{scope}
\end{tikzpicture}

\end{document}


-
Thanks for your comment! Interesting approach, I didn't know about that library. Accepted the other answer, though, as it was a shorter solution. –  Jamie Vicary May 16 '11 at 21:56