使用 \foreach 进行重复结构

使用 \foreach 进行重复结构
documentclass[border={5mm}]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=1,
    default rectangle/.style={rectangle, draw=black,thin, minimum size=1cm},    
    ]
%----------------------------------------------------------------------------
% fuel Assembly
%----------------------------------------------------------------------------
% Water around the assembly

 \foreach \x in {-10,-5}{
    \foreach \y in {-1,4}{
      \draw[black,very thin, fill=blue!50!white] (\x,\y) rectangle (-10,4);

% side plate left

       \foreach \x in {-9.9,-9.4}{
         \foreach \y in {-0.9,3.9}{      
           \draw[black,very thin, fill=gray!20!white] (\x,\y) rectangle (-9.4,3.9);

% side plate right

           \foreach \x in {-5.6,-5.1}{
            \foreach \y in {-0.9,3.9}{      
              \draw[black,very thin, fill=gray!20!white] (\x,\y) rectangle (-5.1,3.9);

% 19 fuel plates 

               \foreach \position in {(-5.6,-0.8)}{
                 \draw[black,very thin, fill=gray!50!white] \position rectangle (-9.4,-0.7);


                  }
                }
              }
             }
           }
         }
       }

 \end{tikzpicture}

\end{document}

我正在尝试重现下面的燃料组件图片。我的问题是使用循环(即 \foreach)重复组件的 19 个板。有没有办法避免对所有 19 个板重复使用 \foreach 循环?基本上,我需要构建一次板,然后像附图中那样重复 19 次。提前感谢您的帮助。在此处输入图片描述

答案1

Tikz 默认包含 calc 包。这意味着您可以使用 + 和 - 等常规操作。要绘制包含的图片,我会使用类似这样的操作。

\documentclass[border={5mm}]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=1,
  default rectangle/.style={rectangle, draw=black,thin, minimum size=1cm}]
  % ----------------------------------------------------------------------------
  % fuel Assembly
  % ----------------------------------------------------------------------------
  % Water around the assembly
  \def\numRep{19}
  \def\Marg{0.1}
  \draw[blue,fill=blue](0-\Marg,0-\Marg) rectangle (15+\Marg,\numRep+\Marg);
  \foreach \Y in {1,2,...,\numRep}{
    \draw[blue,fill=green] (0,\Y-1) rectangle (15,\Y);
    \draw[blue,fill=blue] (1,\Y-1) rectangle (14,\Y-0.65);
    \draw[blue,fill=blue] (1,\Y-0.35) rectangle (14,\Y);
    \draw[blue,fill=red] (1.5,\Y-0.55) rectangle (13.5,\Y-0.45);
    %% Lines horisontal
    \foreach \Yline in {0.35,0.45,0.55,0.65} \draw[blue] (1,\Y-\Yline) -- +(13,0);
  };
  %% Lines vertical
  \foreach \Xline in {0,1,...,6} \draw[blue] (2*\Xline+1.5,0) -- +(0,\numRep);
  \draw[blue] (1,0) -- +(0,\numRep);
  \draw[blue] (14,0) -- +(0,\numRep);
\end{tikzpicture}
\end{document}

相关内容