將 2 個 \foreach 迴圈合併為一個

將 2 個 \foreach 迴圈合併為一個

使用以下程式碼(來自答案這個問題) 使用 2 個 \foreach 迴圈繪製方框;並定義它們兩個的起始位置(yshift=.4cm 和 yshift=4.4cm)。我希望藍色框自動從紅色框結束的地方開始。

是否可以將它們組合成一個循環。

我的意思是使用一個 \foreach 循環繪製下面的 4 個紅色框,然後繼續繪製上面的 2 個藍色框。

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\newsavebox{\recAr}
\savebox{\recAr}{%
\begin{tikzpicture}[scale=.6]
\draw [thick, black, fill=red] (0,0) rectangle +(1,1.24);
\end{tikzpicture}}
\newsavebox{\recAb}
\savebox{\recAb}{%
\begin{tikzpicture}[scale=.6]
\draw [thick, black, fill=blue!60!white] (0,0) rectangle +(1,1.24);
\end{tikzpicture}}
\begin{tikzpicture}[scale=.8, transform shape]
\draw [line width=.4mm, black, dashed] (0,5.8) -- +(0:11) (0,0) -- +(0:11) node [pos=.68] (A) {};
\foreach \X in {0,1,2,3}
{\node[yshift=.4cm] (y-\X) at (A|-0,\X){\usebox{\recAr}};}
\foreach \X in {0,1}
{\node[yshift=4.4cm] (y-\X) at (A|-0,\X){\usebox{\recAb}};}
\end{tikzpicture}
\end{frame}
\end{document}

在此輸入影像描述

答案1

僅部分循環並基於我的答案解決方案很簡單:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{chains,
                positioning}

\begin{document}
\begin{frame}[t, fragile]
\frametitle{}
    \begin{tikzpicture}[
node distance = 1mm and 55mm,
  start chain = going below,
   box/.style = {draw,  thick, fill=#1,
                 minimum width=6mm, minimum height=12mm,
                 inner sep=0pt, outer sep=0mm,
                 on chain}
                        ] 
\node (n1) [box=blue] {};
\node (n2) [box=blue] {};
    \node (n3) [below=1ex of n2, % when you need additional space, otherwide omit this option
                box=red] {}; %
\foreach \i in {4,5,6}
    \node (n\i) [box=red] {};
\coordinate[left=of n1.north] (a); % for shift node to the right
\draw [line width=.4mm, dashed]
    (a) -- ++ (11,0)  
    (a |- n6.south) -- ++ (11,0);
\end{tikzpicture}
\end{frame}
\end{document}

在此輸入影像描述

答案2

增加了Dunno's根據 OP 的要求制定解決方案的建議

您可以簡單地循環變數:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\begin{document}
\begin{frame}[t]
\frametitle{}
\newsavebox{\recAr}
\savebox{\recAr}{%
\begin{tikzpicture}[scale=.6]
\draw [thick, black, fill=red] (0,0) rectangle +(1,1.24);
\end{tikzpicture}}
\newsavebox{\recAb}
\savebox{\recAb}{%
\begin{tikzpicture}[scale=.6]
\draw [thick, black, fill=blue!60!white] (0,0) rectangle +(1,1.24);
\end{tikzpicture}}
\begin{tikzpicture}[scale=.8, transform shape]
\draw [line width=.4mm, black, dashed] (0,5.8) -- +(0:11) (0,0) -- +(0:11) coordinate[pos=.68,alias=y-6] (A) {}; \path foreach \X [remember=\X as \LastX (initially 6)] in {5,4,...,0}{ node[above=1.5mm of y-\LastX,outer sep=0pt,inner sep=0pt] (y-\X) {\ifnum \X>3 \usebox{\recAb} \else \usebox{\recAr} \fi}};
\end{tikzpicture}
\end{frame}
\end{document}

要得到:

在此輸入影像描述

PS:我以這樣的方式編寫程式碼,只yshift需要一個,從而使yshift藍色和紅色框通用。

相關內容