Tikz: Connect chain of rectangles with lines












2














I want to be able to connect the white rectangles with lines as shown in the picture below (this example consists only of two rectangles, the original code consists of more than 10 of such rectangles). Right now I am working with coordinates. But this is a rather complicated approach and error-prone. It would be ideal if I could define a connecting point by "north-west, south-east" instead of using numeric coordinates. It would also be very helpful if I could additionally connect the lines coming from the left rectangle to the center of the right rectangle.



Is there an easier way to do it? Here is the code and the output it produces:



    documentclass[a5paper]{article}

usepackage{tikz}
usetikzlibrary{decorations.pathreplacing}
usetikzlibrary{fadings}

usepackage{etoolbox}

begin{document}

newcommand{sizein}{1.0}
newcommand{distin}{0.15}
newcommand{opacity}{0.8}
newcommand{xDist}{2.0}

begin{figure}[htb!]
centering
begin{tikzpicture}
foreach i[count=xi] in {2,...,0}{
draw [xshift=xDist*0cm, fill=gray, opacity=opacity, draw=black]
(distin*i,distin*i) rectangle (distin*i+sizein,distin*i+sizein);
}

foreach i[count=xi] in {2,...,0}{
draw [xshift=xDist*1cm, fill=gray, opacity=opacity, draw=black]
(distin*i,distin*i) rectangle (distin*i+sizein,distin*i+sizein);
}

draw [fill=white, draw=black] (0.1,0.1) rectangle (0.3,0.3);
draw [fill=white, draw=black] (2.5,0.4) rectangle (2.7,0.6);

draw (0.3,0.1) -- (2.7,0.4);
draw (0.3,0.3) -- (2.5,0.6);
end{tikzpicture}
end{figure}

end{document}


enter image description here










share|improve this question



























    2














    I want to be able to connect the white rectangles with lines as shown in the picture below (this example consists only of two rectangles, the original code consists of more than 10 of such rectangles). Right now I am working with coordinates. But this is a rather complicated approach and error-prone. It would be ideal if I could define a connecting point by "north-west, south-east" instead of using numeric coordinates. It would also be very helpful if I could additionally connect the lines coming from the left rectangle to the center of the right rectangle.



    Is there an easier way to do it? Here is the code and the output it produces:



        documentclass[a5paper]{article}

    usepackage{tikz}
    usetikzlibrary{decorations.pathreplacing}
    usetikzlibrary{fadings}

    usepackage{etoolbox}

    begin{document}

    newcommand{sizein}{1.0}
    newcommand{distin}{0.15}
    newcommand{opacity}{0.8}
    newcommand{xDist}{2.0}

    begin{figure}[htb!]
    centering
    begin{tikzpicture}
    foreach i[count=xi] in {2,...,0}{
    draw [xshift=xDist*0cm, fill=gray, opacity=opacity, draw=black]
    (distin*i,distin*i) rectangle (distin*i+sizein,distin*i+sizein);
    }

    foreach i[count=xi] in {2,...,0}{
    draw [xshift=xDist*1cm, fill=gray, opacity=opacity, draw=black]
    (distin*i,distin*i) rectangle (distin*i+sizein,distin*i+sizein);
    }

    draw [fill=white, draw=black] (0.1,0.1) rectangle (0.3,0.3);
    draw [fill=white, draw=black] (2.5,0.4) rectangle (2.7,0.6);

    draw (0.3,0.1) -- (2.7,0.4);
    draw (0.3,0.3) -- (2.5,0.6);
    end{tikzpicture}
    end{figure}

    end{document}


    enter image description here










    share|improve this question

























      2












      2








      2


      2





      I want to be able to connect the white rectangles with lines as shown in the picture below (this example consists only of two rectangles, the original code consists of more than 10 of such rectangles). Right now I am working with coordinates. But this is a rather complicated approach and error-prone. It would be ideal if I could define a connecting point by "north-west, south-east" instead of using numeric coordinates. It would also be very helpful if I could additionally connect the lines coming from the left rectangle to the center of the right rectangle.



      Is there an easier way to do it? Here is the code and the output it produces:



          documentclass[a5paper]{article}

      usepackage{tikz}
      usetikzlibrary{decorations.pathreplacing}
      usetikzlibrary{fadings}

      usepackage{etoolbox}

      begin{document}

      newcommand{sizein}{1.0}
      newcommand{distin}{0.15}
      newcommand{opacity}{0.8}
      newcommand{xDist}{2.0}

      begin{figure}[htb!]
      centering
      begin{tikzpicture}
      foreach i[count=xi] in {2,...,0}{
      draw [xshift=xDist*0cm, fill=gray, opacity=opacity, draw=black]
      (distin*i,distin*i) rectangle (distin*i+sizein,distin*i+sizein);
      }

      foreach i[count=xi] in {2,...,0}{
      draw [xshift=xDist*1cm, fill=gray, opacity=opacity, draw=black]
      (distin*i,distin*i) rectangle (distin*i+sizein,distin*i+sizein);
      }

      draw [fill=white, draw=black] (0.1,0.1) rectangle (0.3,0.3);
      draw [fill=white, draw=black] (2.5,0.4) rectangle (2.7,0.6);

      draw (0.3,0.1) -- (2.7,0.4);
      draw (0.3,0.3) -- (2.5,0.6);
      end{tikzpicture}
      end{figure}

      end{document}


      enter image description here










      share|improve this question













      I want to be able to connect the white rectangles with lines as shown in the picture below (this example consists only of two rectangles, the original code consists of more than 10 of such rectangles). Right now I am working with coordinates. But this is a rather complicated approach and error-prone. It would be ideal if I could define a connecting point by "north-west, south-east" instead of using numeric coordinates. It would also be very helpful if I could additionally connect the lines coming from the left rectangle to the center of the right rectangle.



      Is there an easier way to do it? Here is the code and the output it produces:



          documentclass[a5paper]{article}

      usepackage{tikz}
      usetikzlibrary{decorations.pathreplacing}
      usetikzlibrary{fadings}

      usepackage{etoolbox}

      begin{document}

      newcommand{sizein}{1.0}
      newcommand{distin}{0.15}
      newcommand{opacity}{0.8}
      newcommand{xDist}{2.0}

      begin{figure}[htb!]
      centering
      begin{tikzpicture}
      foreach i[count=xi] in {2,...,0}{
      draw [xshift=xDist*0cm, fill=gray, opacity=opacity, draw=black]
      (distin*i,distin*i) rectangle (distin*i+sizein,distin*i+sizein);
      }

      foreach i[count=xi] in {2,...,0}{
      draw [xshift=xDist*1cm, fill=gray, opacity=opacity, draw=black]
      (distin*i,distin*i) rectangle (distin*i+sizein,distin*i+sizein);
      }

      draw [fill=white, draw=black] (0.1,0.1) rectangle (0.3,0.3);
      draw [fill=white, draw=black] (2.5,0.4) rectangle (2.7,0.6);

      draw (0.3,0.1) -- (2.7,0.4);
      draw (0.3,0.3) -- (2.5,0.6);
      end{tikzpicture}
      end{figure}

      end{document}


      enter image description here







      tikz-pgf






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      Samuel

      410211




      410211






















          1 Answer
          1






          active

          oldest

          votes


















          2














          instead of rectangles use nodes with reactangle shape:



          documentclass[a5paper]{article}
          usepackage{tikz}

          begin{document}
          newcommand{sizein}{1.0}
          newcommand{distin}{0.15}
          newcommand{opacity}{0.8}
          newcommand{xDist}{2.0}

          begin{tikzpicture}[
          box/.style = {draw, fill=gray, opacity=opacity, minimum size=sizein cm},
          wbx/.style = {draw, fill=white, minimum size=distin cm, outer sep=0pt}
          ]
          foreach i in {2,1,0}%
          {
          node [box,above right] at (i*distin,i*distin) {};
          node [box,above right] at (xDist+i*distin,i*distin) {};
          }
          node (w1) [wbx] at (0.3,0.3) {};
          node (w2) [wbx] at (xDist+0.5,0.5) {};
          draw[very thin] (w1.north west) -- (w2.north west)
          (w1.south east) -- (w2.south east);
          end{tikzpicture}
          end{document}


          enter image description here



          actual size and position of white boxes you can change according to your needs.






          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "85"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f467886%2ftikz-connect-chain-of-rectangles-with-lines%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            instead of rectangles use nodes with reactangle shape:



            documentclass[a5paper]{article}
            usepackage{tikz}

            begin{document}
            newcommand{sizein}{1.0}
            newcommand{distin}{0.15}
            newcommand{opacity}{0.8}
            newcommand{xDist}{2.0}

            begin{tikzpicture}[
            box/.style = {draw, fill=gray, opacity=opacity, minimum size=sizein cm},
            wbx/.style = {draw, fill=white, minimum size=distin cm, outer sep=0pt}
            ]
            foreach i in {2,1,0}%
            {
            node [box,above right] at (i*distin,i*distin) {};
            node [box,above right] at (xDist+i*distin,i*distin) {};
            }
            node (w1) [wbx] at (0.3,0.3) {};
            node (w2) [wbx] at (xDist+0.5,0.5) {};
            draw[very thin] (w1.north west) -- (w2.north west)
            (w1.south east) -- (w2.south east);
            end{tikzpicture}
            end{document}


            enter image description here



            actual size and position of white boxes you can change according to your needs.






            share|improve this answer


























              2














              instead of rectangles use nodes with reactangle shape:



              documentclass[a5paper]{article}
              usepackage{tikz}

              begin{document}
              newcommand{sizein}{1.0}
              newcommand{distin}{0.15}
              newcommand{opacity}{0.8}
              newcommand{xDist}{2.0}

              begin{tikzpicture}[
              box/.style = {draw, fill=gray, opacity=opacity, minimum size=sizein cm},
              wbx/.style = {draw, fill=white, minimum size=distin cm, outer sep=0pt}
              ]
              foreach i in {2,1,0}%
              {
              node [box,above right] at (i*distin,i*distin) {};
              node [box,above right] at (xDist+i*distin,i*distin) {};
              }
              node (w1) [wbx] at (0.3,0.3) {};
              node (w2) [wbx] at (xDist+0.5,0.5) {};
              draw[very thin] (w1.north west) -- (w2.north west)
              (w1.south east) -- (w2.south east);
              end{tikzpicture}
              end{document}


              enter image description here



              actual size and position of white boxes you can change according to your needs.






              share|improve this answer
























                2












                2








                2






                instead of rectangles use nodes with reactangle shape:



                documentclass[a5paper]{article}
                usepackage{tikz}

                begin{document}
                newcommand{sizein}{1.0}
                newcommand{distin}{0.15}
                newcommand{opacity}{0.8}
                newcommand{xDist}{2.0}

                begin{tikzpicture}[
                box/.style = {draw, fill=gray, opacity=opacity, minimum size=sizein cm},
                wbx/.style = {draw, fill=white, minimum size=distin cm, outer sep=0pt}
                ]
                foreach i in {2,1,0}%
                {
                node [box,above right] at (i*distin,i*distin) {};
                node [box,above right] at (xDist+i*distin,i*distin) {};
                }
                node (w1) [wbx] at (0.3,0.3) {};
                node (w2) [wbx] at (xDist+0.5,0.5) {};
                draw[very thin] (w1.north west) -- (w2.north west)
                (w1.south east) -- (w2.south east);
                end{tikzpicture}
                end{document}


                enter image description here



                actual size and position of white boxes you can change according to your needs.






                share|improve this answer












                instead of rectangles use nodes with reactangle shape:



                documentclass[a5paper]{article}
                usepackage{tikz}

                begin{document}
                newcommand{sizein}{1.0}
                newcommand{distin}{0.15}
                newcommand{opacity}{0.8}
                newcommand{xDist}{2.0}

                begin{tikzpicture}[
                box/.style = {draw, fill=gray, opacity=opacity, minimum size=sizein cm},
                wbx/.style = {draw, fill=white, minimum size=distin cm, outer sep=0pt}
                ]
                foreach i in {2,1,0}%
                {
                node [box,above right] at (i*distin,i*distin) {};
                node [box,above right] at (xDist+i*distin,i*distin) {};
                }
                node (w1) [wbx] at (0.3,0.3) {};
                node (w2) [wbx] at (xDist+0.5,0.5) {};
                draw[very thin] (w1.north west) -- (w2.north west)
                (w1.south east) -- (w2.south east);
                end{tikzpicture}
                end{document}


                enter image description here



                actual size and position of white boxes you can change according to your needs.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                Zarko

                121k865156




                121k865156






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f467886%2ftikz-connect-chain-of-rectangles-with-lines%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Plaza Victoria

                    Puebla de Zaragoza

                    Musa