2

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?

  • Welcome to TeX.SX! I'm a little bit confused.... do you meanc.x=b.x and c.y=a.y? – Bobyandbob May 27 '18 at 7:54
  • @Bobyandbob: yeah. you are right. sorry for the mistake – Ravi Joshi May 27 '18 at 7:59
4

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

  • 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 Joshi May 27 '18 at 8: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}
  • 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 Joshi May 27 '18 at 8: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 8:17
  • @RaviJoshi See also tex.stackexchange.com/questions/401425/… – 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 Joshi May 27 '18 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.