如何使用 for(或 foreach)代替硬编码

如何使用 for(或 foreach)代替硬编码

我是 Tikz 新手,我正在尝试绘制以下内容 在此处输入图片描述

到目前为止,我是手动完成的,但我想知道 foreach 循环如何工作。

\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}

注释掉的内容显示了我自己的尝试。此外,我还在努力适应命名约定以及如何有效地重命名变量。如果你能帮助我就太好了。提前谢谢!

答案1

这取决于您是否想在之后用内容填充节点。但在这种情况下,以下代码正在foreach循环运行。这绝对不是最好的选择,因为每行都不同。节点矩阵应该是更好的答案。

\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}

编辑 由于我建议使用节点矩阵,因此这里是我的解决方案:

\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}

节点矩阵

相关内容