147

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?

1
  • 6
    Use the orthogonal coordinate system identifiers -| (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
    Commented Oct 15, 2018 at 21:22

7 Answers 7

105

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}

pgf let syntax

4
  • 1
    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. Commented May 16, 2011 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
    Commented May 16, 2011 at 22:05
  • 1
    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 :-) Commented May 16, 2011 at 22:15
  • 3
    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
    Commented Dec 31, 2013 at 16:44
123

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.)

8
  • 1
    Does the 52 have any effect on the bounding box? Commented May 17, 2011 at 4:25
  • Nice approach. @Matthew: I don't think it does.
    – Jake
    Commented May 17, 2011 at 5:53
  • @Matthew it doesn't, but I couldn't find anything in the documentation.
    – kgr
    Commented May 17, 2011 at 14:02
  • 7
    This is a good solution @kgr. Note that if you want y-coordinate, use : \draw (1, 52 |- A) node {B};
    – JKHA
    Commented Sep 6, 2018 at 9:24
  • 2
    Since 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
    Commented Dec 20, 2023 at 20:36
35

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}
1
  • The ([shift=({1cm,1cm})]A) is best and is the only one I could understand of all the other answers given.
    – Nasser
    Commented Nov 28, 2022 at 5:56
27

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}
1
  • 8
    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
    Commented May 17, 2011 at 9:18
27

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}  

enter image description here

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

1
  • 1
    This 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. Commented Aug 26, 2016 at 6:08
15

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}

1
  • Thanks for your comment! Interesting approach, I didn't know about that library. Accepted the other answer, though, as it was a shorter solution. Commented May 16, 2011 at 21:56
7

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:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .