2

I am new to Tikz and I am trying to draw the following enter image description here

So far I did this manually but I was wondering how a foreach-Loop would work.

\begin{document}
\begin{tikzpicture}[
%   \def\compOneOne{5}
%   \def\nodeName{C1}
    node distance = 0pt,
    square/.style = {draw=blue!60, fill=blue!5, very thick, 
        minimum height=3em, minimum width=3em, % <---
        outer sep=0pt},
    square2/.style = {draw=purple!60, fill=blue!5, very thick, 
            minimum height=3em, minimum width=3em, % <---
            outer sep=0pt},
    ]


    %Nodes
    \node[square]   (a1) {};
    \node[square, right=of a1] (a2) {};
    \node[square, right=of a2] (a3) {};
    \node[square, right=of a3] (a4) {};
    \node[square, below=of a1] (b1) {};
    \node[square, right=of b1] (b2) {};
    \node[square, right=of b2] (b3) {};
    \node[square2, below=of b1] (c1) {};
    \node[square2, right=of c1] (c2) {};
    \node[square2, right=of c2] (c3) {};
    \node[square2, right=of c3] (c4) {};
    \node[square2, below=of c1] (A1) {};
    \node[square2, right=of A1] (A2) {};
    \node[square2, right=of A2] (A3) {};
    \node[square2, right=of A3] (A4) {};
    \node[square2, right=of A4] (A5) {};
    \node[square2, right=of A5] (A6) {};
    \node[square2, right=of A6] (A7) {};
    \node[square2, right=of A7] (A8) {};
    \node[square2, below=of A1] (B1) {};
    \node[square2, right=of B1] (B2) {};

% \node[square2, below=of B1] (C1) {}; % \foreach \number in {1,...,\compOneOne}{ % \node[square2, right=of \nodeName] (N-\nodeName) {}; % \nodeName = N-\nodeName; %
% }

\end{tikzpicture}

\end{document}

The stuff that's commented out shows my own attempts on it. Furthermore I was struggeling with naming conventions and how to rename my variable efficiently. It would be very nice of you to help me. Thanks in advance!

sorry
  • 417
  • 1
    You can try out this answer here. But it is without the use of for or foreach but it you should be able to draw this graph. – Roland Feb 13 '21 at 14:21

1 Answers1

5

It depends if you want to fill your nodes with content afterwards. But at this state, the following code is working with foreach loops. It is definitely NOT the best option here, since each line is different. A matrix of nodes should be a better answer.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[
    node distance = 0pt,
    square/.style = {draw=blue!60, fill=blue!5, very thick, 
        minimum height=3em, minimum width=3em, % <---
        outer sep=0pt},
    square2/.style = {draw=purple!60, fill=blue!5, very thick, 
            minimum height=3em, minimum width=3em, % <---
            outer sep=0pt},
    ]

    \node[square]   (a1) {};
    \foreach \j in {2,3,4}
        {
        \pgfmathsetmacro\i{\j-1}
        \node[square, right=of a\i] (a\j) {};
        }

    \node[square, below=of a1] (b1) {};
    \foreach \j in {2,3}
        {
        \pgfmathsetmacro\i{\j-1}
        \node[square, right=of b\i] (b\j) {};
        }

    \node[square2, below=of b1] (c1) {};
    \foreach \j in {2,3,4}
        {
        \pgfmathsetmacro\i{\j-1}
        \node[square2, right=of c\i] (c\j) {};
        }

    \node[square2, below=of c1] (A1) {};
    \foreach \j in {2,...,8}
        {
        \pgfmathsetmacro\i{\j-1}
        \node[square2, right=of A\i] (A\j) {};
        }

    \node[square2, below=of A1] (B1) {};
    \node[square2, right=of B1] (B2) {};       

\end{tikzpicture}

\end{document}

EDIT Since I proposed using a matrix of nodes, here's my solution with it:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\def\wdth{1pt} % line width of your nodes
\tikzset{ 
table/.style={
  matrix of nodes,
  row sep=-\wdth,
  column sep=-\wdth,
  nodes={line width=\wdth,rectangle,minimum size=1cm,align=center}
  }
}

\newcommand{\bs}{|[fill=blue,fill opacity=0.2,draw=blue]|}
\newcommand{\rs}{|[fill=red,fill opacity=0.2,draw=red]|}

\begin{tikzpicture}
    \matrix (mat) [table]
    {
    \bs & \bs  & \bs & \bs  &   &   &   &   \\
    \bs & \bs  & \bs &      &   &   &   &   \\
    \rs & \rs  & \rs & \rs  &   &   &   &   \\
    \rs & \rs  & \rs & \rs  &   \rs & \rs  & \rs & \rs  \\
    \rs & \rs  &    &   &   &   &   &   \\
    };
\end{tikzpicture}

\end{document}

Matrix of nodes

SebGlav
  • 19,186