
Используя следующий код (из ответа наэтот вопрос) для рисования ящиков с использованием 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
рекомендация по принятию решения в соответствии с требованиями ОП.
Вы можете просто перебрать переменные:
\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
общим для синего и красного ящиков.