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

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


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


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}

Tags:

Tikz Pgf