3

I use the following code to draw a tikz object.

\begin{tikzpicture}

    % Axis
    \draw (-0.5,0) -- (5.0,0);
    \draw (0,-0.5) -- (0,2.5);

    \node (rect) at (1.5,2) [draw,minimum height=0.65cm,minimum width=2cm,fill=gray!50] {};
    \node (rect) at (2.5,1) [draw,minimum height=0.65cm,minimum width=3cm,fill=gray!50] {};

\end{tikzpicture}

and the object looks like -

enter image description here

What I would like to have is grid of such objects. So that in the grid at every location (1,1), (1,2)... (n,n) this object should be drawn.

Of course I can copy paste this code using scope and use xshift, yshift in order to place many of them. What I need to see how 10 x 10 grid or even 100 x 100 grid of such objects. However I would like to use a for loop to draw this grid of tikz nodes. I want to use the for loop mentioned in this question. However my tikz node consists of multiple nodes (2 lines and 2 rectangles).

Is it possible to draw the grid of objects using for loop?

  • 1
    You can take a look at matrix library, and the corresponding section of the manual. Of course you can also use a \foreach loop, wich can be more convenient of a large number of similar repeated nodes. – JLDiaz Oct 1 '14 at 17:19
7

It is indeed possible

\begin{tikzpicture}
  \foreach \x in {1,2,3}{
    \foreach \y in {1,2,3}{
    \begin{scope}[shift={(3*\x,2*\y)}]
      \draw (-0.25,0) -- (2.5,0);
      \draw (0,-0.25) -- (0,1.25);
      \node (rect) at (.75,1) [draw,minimum height=0.325cm,minimum width=1cm,fill=gray!50] {};
      \node (rect) at (1.25,.5) [draw,minimum height=0.325cm,minimum width=1.5cm,fill=gray!50] {};
    \end{scope}
    }
  }
\end{tikzpicture}

enter image description here

Open for comments or better solutions!

|improve this answer|||||
3

Just another solution with pics which can be used within foreach loops or matrix.

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

\tikzset{
    myobject/.pic={
      \draw (-0.25,0) -- (2.5,0);
      \draw (0,-0.25) -- (0,1.25);
      \node (rect) at (.75,1) [draw,minimum height=0.325cm,minimum width=1cm,fill=gray!50] {};
      \node (rect) at (1.25,.5) [draw,minimum height=0.325cm,minimum width=1.5cm,fill=gray!50] {};
        }
}

\begin{document}

\begin{tikzpicture}
  \foreach \x in {1,2,3}{
    \foreach \y in {1,2,3}{
    \begin{scope}[shift={(3*\x,2*\y)}]
      \pic{myobject};
    \end{scope}
    }
  }
\end{tikzpicture}

\begin{tikzpicture}
  \matrix [column sep=3mm, row sep=3mm] {
      \pic{myobject}; &  \pic{myobject}; &  \pic{myobject}; \\
      \pic{myobject}; &  \pic{myobject}; &  \pic{myobject}; \\
      \pic{myobject}; &  \pic{myobject}; &  \pic{myobject}; \\
    };
\end{tikzpicture}

\end{document}

enter image description here

|improve this answer|||||
  • At least here you don't have to calculate the shift in function of the pic size... – s__C Oct 2 '14 at 11:20

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.