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?
7 Answers
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.)
- 2,251
-
1
-
-
-
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
-
7This is a good solution @kgr. Note that if you want y-coordinate, use :
\draw (1, 52 |- A) node {B};– JKHA Sep 06 '18 at 09:24 -
One can use a command to abstract this a bit if it seems unintuitive:
\newcommand{\combineXY}[2]{#1 |- #2}. – geometrian Apr 02 '19 at 03:40 -
1Since the x-value of the y-giving coordinate is arbitrary, perhaps it is better to just omit it. This works:
\draw (A |- , 3) node {B};. – Lorents Dec 20 '23 at 20:36
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}

- 232,450
-
1IMHO you should explicit state that it needs the
calclibrary. Alsopositioningwould be an alternative worth mentioning, even when theycoordinate can only be given relatively. – Martin Scharrer May 16 '11 at 21:50 -
@Martin: I've added the bit about the need for the
calclibrary. I guess you should write an answer using thepositioninglibrary, it is a worthwhile alternative indeed. – Jake May 16 '11 at 22:05 -
1Thanks 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
-
3It 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
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}
- 136,588
-
The
([shift=({1cm,1cm})]A)is best and is the only one I could understand of all the other answers given. – Nasser Nov 28 '22 at 05:56
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}
- 262,582
-
8I guess it is worth pointing out that
below=<distance> ofwill 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 specifyingon gridbefore thebelowoption. – Jake May 17 '11 at 09:18
Another answer
\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.
- 7,950
-
1This looks like a nice and easy solution, but on Kubuntu 14.04 (with pgf 2.10-1) I get the error message that \pgfutil is an undefined control sequence. – Supernormal Aug 26 '16 at 06:08
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}

- 54,637
- 505,128
-
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
Another way based on \usetikzlibrary{calc} (alternative to Jake's syntax):
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw [help lines] (0,0) grid (4,4);
\node (A) at (2,1) {A};
\node (B) at ($(A)+(0,2)$) {B};
\end{tikzpicture}
\end{document}
Same output:
- 2,923

-|(vertical component of the first and horizontal component of the second) and|-(horizontal component of the first and vertical component of the second) . - Given y-coordinate=9:\node (B) at (A|-9) {B};– Bobyandbob Oct 15 '18 at 21:22