0

In Tikz: how can I place a node A relative to the position of node B but with one coordinate fixed? Or put differently, how can one make two nodes share either the x or the y coordinate? For example, I would like to have box2 above box1 but not 0.3cm above box1, but let's say at y=3cm, here:

\documentclass[tikz, margin=1pt]{standalone}
\usetikzlibrary{3d}
\tikzset{box/.style={draw, rectangle}}

\begin{document}
\begin{tikzpicture}

\node (box1) [box] {};
\node (box2) [box, above of = box1, node distance = .3cm] {};

\end{tikzpicture}
\end{document}

enter image description here

AKG
  • 197
  • 1
    \node (box2) [box] at (box1|-0,3) {}; See https://tex.stackexchange.com/a/401429/121799 for more details on the |- and -| syntax. –  Apr 24 '19 at 14:39
  • Just tried both answers - both work. Thank you! – AKG Apr 24 '19 at 14:43
  • No, my approach doesn't generally work. @marmot's approach works in every case. –  Apr 24 '19 at 14:52
  • In both approaches it is to be noted that the first node is placed at (0,0), so @JouleV does work, too. Please notice that in your MWE you are using deprecated syntax. You will be much better off if you load \usetikzlibrary{positioning} and say something like \node (box2) [box, above=.3cm of box1] {};. (The 3d library is not used here.) –  Apr 24 '19 at 15:04

0 Answers0