![Combinando 2 loops \foreach em um](https://rvso.com/image/400390/Combinando%202%20loops%20%5Cforeach%20em%20um.png)
Usando o seguinte código (da resposta paraessa questão) para desenhar caixas usando 2 loops \foreach; e definir as posições iniciais de ambos (yshift=0,4cm e yshift=4,4cm). Quero que as caixas azuis comecem automaticamente onde as caixas vermelhas terminam.
É possível combiná-los em um loop.
Quero dizer, usar um loop \foreach para desenhar as 4 caixas vermelhas inferiores e depois continuar com as 2 caixas azuis superiores.
\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}
Responder1
Somente com loop parcial e baseado emminha respostaa solução é simples:
\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}
Responder2
Adicionada Dunno's
recomendação para fazer a solução de acordo com os requisitos do OP
Você pode simplesmente repetir as variáveis:
\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}
obter:
PS: Fiz o código de forma que apenas um yshift
seja necessário, tornando assim o yshift
comum para as caixas azul e vermelha.