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}
-
1IMHO you should explicit state that it needs the
calc
library. Alsopositioning
would be an alternative worth mentioning, even when they
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 thepositioning
library, 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
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.)
-
-
-
-
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
-
1This is a good solution @kgr. Note that if you want y-coordinate, use :
\draw (1, 52 |- A) node {B};
– JKHA Sep 6 '18 at 9:24
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}
-
7I 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 specifyingon grid
before thebelow
option. – Jake May 17 '11 at 9: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.
-
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 6: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}
-
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:
-|
(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