3

I am composing a tikz drawing consisting of two nodes a and b. I want to insert node c such that c.x=b.x and c.y=a.y. See below the code snippet-

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
  \draw[style=help lines] (0, 0) grid (5, 5);
  \node (a) [rectangle, draw, fill=red!20]   at (1, 1) {Rectangle A};
  \node (b) [rectangle, draw, fill=green!20] at (4, 4) {Rectangle B};
  \node (c) [rectangle, draw, fill=blue!20]  at (4, 1) {Rectangle C};
\end{tikzpicture}
\end{document}

Below is the generated diagram-

genered diagram

Notice that I have manually mentioned the coordinates of node c.

How to use let or any other command from calc to provide the coordinates such that c.x=b.x and c.y=a.y?

ravi
  • 1,618

2 Answers2

5

if I understand correctly it must answer your question

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc}

\begin{document}
\begin{tikzpicture}
  \draw[style=help lines] (0, 0) grid (5, 5);
  \node (a) [rectangle, draw, fill=red!20]   at (1, 1) {Rectangle A};
  \node (b) [rectangle, draw, fill=green!20] at (4, 4) {Rectangle B};
  \node (c) [rectangle, draw, fill=blue!20]  at (b|-a) {Rectangle C};

  \node[rectangle, draw, fill=blue!20](d) at ($(a)!0.3!(b)$) {rect D};


  \node (e) [rectangle, draw, fill=blue!20]  at (a|-b) {Rectangle E};
\end{tikzpicture}
\end{document}

enter image description here

rpapa
  • 12,350
  • Thank you very much... I have used |- and -| for positioning lines such as turning by 90 degrees... Can you please provide a brief explanation of the code you provided? I would like to know about !0.3! supported by calc. Previously I have used !0.5! for midpoint calculation but this seems more powerful – ravi May 27 '18 at 08:13
  • 1
    ($ (a)! xx! (b) $) designates the point at xx% between (a) and (b), xx can take values between 0 and 1 as well as greater than 1, we will go beyond (b) or less than 0 and then the point will be before (a) – rpapa May 27 '18 at 10:41
4
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
  \draw[style=help lines] (0, 0) grid (5, 5);
  \node (a) [rectangle, draw, fill=red!20]   at (1, 1) {Rectangle A};
  \node (b) [rectangle, draw, fill=green!20] at (4, 4) {Rectangle B};
  \node (c) [rectangle, draw, fill=blue!20]  at (b|-a) {Rectangle C};
\end{tikzpicture}
\end{document}
Skillmon
  • 60,462
  • Thank you very much... The operator |- used with nodes is new to me. thought, I have used it for drawing lines in the past! Can you please provide a brief explanation of the code? – ravi May 27 '18 at 08:15
  • 1
    @RaviJoshi it is described on page 38 of the current pgfmanual (section 2.15 Specifying Coordinates). The first listing using it is on page 44 (section 2.21 Adding Text). – Skillmon May 27 '18 at 08:17
  • @RaviJoshi See also https://tex.stackexchange.com/questions/401425/tikz-what-exactly-does-the-the-notation-for-arrows-do/401429#401429 – Torbjørn T. May 27 '18 at 13:03
  • @TorbjørnT.: Thank you very much. I have used |- and -|with draw however this was the first time, I got to know that it can be used with In a coordinate specification for a node. – ravi May 27 '18 at 13:06