2

I used this method to draw 2 rectangles:

\draw[fill=blue](0,0) rectangle (1,1) node[midway,align=center]{my text};

I don't know how to use anchors to connect these rectangles. Do i have to draw them using \node method got the idea from here.

I need a method that takes the two corners of the rectangle, as i will use this information to draw new shapes.

  • You have only one rectangle in which you put node with text "my text". – Zarko Nov 16 '15 at 9:52
2

One more answer ...

As said @Ignasi in his answer, only nodes has anchors. You can predefine node shapes with option of its width and height, which you determined when use it. Text in it is centered, their starting coordinate can be defined as in above MWE with help of tikzlibrary `positioning:

\documentclass[border=3mm,tikz]{standalone}
    \usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}[
    node distance=0mm,
     box/.style args = {#1/#2}{shape=rectangle,
            text width=#1mm, minimum height=#2mm,
            draw, thick, inner sep=0pt, outer sep=0pt, 
            align=center, text=black}
                    ]
\draw[thin, gray!25] (0,0) grid (8,8);
%
 \node[box=20/10,blue,above right=of {(0,1)}] (a) {my text};%
 \node[box=30/40,red, above right=of {(5,4)}] (b) {my very very very very long text}; %
%
\draw[thick, red] (a) -- (b);
\draw[thick, blue] (a) |- (b);
\draw[thick, green] (a.north east) -- (b.south west);
%
\draw[<-] (b.south west) -- + (0,-1) node[below] {coordinate (5,4)};
\draw[very thin,densely dashed] (a.center) -- (b.center);
    \end{tikzpicture}
\end{document}

or directly:

\documentclass[border=3mm,tikz]{standalone}

\begin{document}
    \begin{tikzpicture}[
    node distance=0mm,
     box/.style args = {#1/#2}{shape=rectangle,
            text width=#1mm, minimum height=#2mm,
            draw, thick, inner sep=0pt, outer sep=0pt, 
            above right, align=center, text=black}
                    ]
\draw[thin, gray!25] (0,0) grid (8,8);
%
 \node[box=20/10,blue] (a) at (0,1) {my text};%
 \node[box=30/40,red ] (b) at (5,4) {my very very very very long text}; %
%
\draw[thick, red] (a) -- (b);
\draw[thick, blue] (a) |- (b);
\draw[thick, green] (a.north east) -- (b.south west);
%
\draw[<-] (b.south west) -- + (0,-1) node[below] {coordinate (5,4)};
\draw[very thin,densely dashed] (a.center) -- (b.center);
    \end{tikzpicture}
\end{document}

In both cases we get the same result:

enter image description here

3

It's not possible to connect two rectangles unless you know which particular coordinates you want to join. Rectangles doesn't have anchors, nodes have them.

Although I'm sure this has been already answered here, you can draw a rectangular node with anchors and fixed size with fit library. Being nodes, you can connect these particular rectangles using anchors.

An example:

\documentclass[tikz,border=2mm]{standalone}

\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}

     \draw[thin, gray] (0,0) grid (8,8);

     \node[thick, blue, fit={(0,1) (3,3)}, inner sep=0pt, draw] (a) {};
     \node[thick, red, fit={(5,4) (8,8)}, inner sep=0pt, draw] (b) {};

    \draw[thick, red] (a) -- (b);
    \draw[thick, blue] (a) |- (b);
    \draw[thick, green,] (a.east) to[bend right] (b.south);

\end{tikzpicture}

\end{document}

enter image description here

  • I used this method but the text inside the rectangle is not vertically aligned in the center. – Misaki Nov 16 '15 at 10:44
  • @Misaki: With 'fit' nodes you better use option label=center:text you want instead of regular {text you want} to vertically align text. This solution is explained if fit library section. – Ignasi Nov 16 '15 at 13:31
1

You can connect the nodes in the rectangles on background layer so they appear as if you connected the rectangles themselves. Limitation here is that you cannot add arrow heads, etc.

\documentclass[tikz, border = 2pt]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}

\begin{tikzpicture}
\draw[fill=blue!20](0,0) rectangle (2,2) node[midway,align=center](m1){text 1};
\draw[fill=blue!20](4,0) rectangle (6,2) node[midway,align=center](m2){text 2};
\draw[fill=blue!20](2,3) rectangle (4,5) node[midway,align=center](m3){text 3};

\begin{scope}[on background layer]
\draw (m1) -- (m2) (m1) -- (m3);
\end{scope}
\end{tikzpicture}

\end{document}

enter image description here

0

I do not know a way to use or define anchors using the rectangle function as stated in this answer. Therefore, I would use nodes for example like this

\documentclass[tikz, border = 2pt]{standalone}

     \begin{document}
\begin{tikzpicture}
  % use a node, fill it blue, optional set the border color
  % the distance of the boxed need to be set in this formation with right of
  \node[fill=blue, draw=black, node distance = 2cm] (first) {my text};
  \node[fill=blue, draw=black, right of = first, node distance = 2cm,] (second) {my text};

  % there are different anchors present
  \draw[->] (first) -- (second);
  \draw[red, ->] (first.north east) -- (second.north west);
\end{tikzpicture}
    \end{document}

A drawback is that you cannot limit the box dimensions in an easy way.

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.