
Mit dem folgenden Code (aus der Antwort aufdiese Frage), um Kästen mit 2 \foreach-Schleifen zu zeichnen und die Startpositionen beider zu definieren (yshift=.4cm und yshift=4.4cm). Ich möchte, dass die blauen Kästen automatisch dort beginnen, wo die roten Kästen enden.
Ist es möglich, sie zu einer Schleife zu kombinieren?
Ich meine, eine \foreach-Schleife zu verwenden, um die unteren vier roten Kästen zu zeichnen, und dann mit den oberen beiden blauen Kästen fortzufahren.
\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}
Antwort1
Mit nur partieller Schleife und basierend aufmeine AntwortDie Lösung ist einfach:
\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}
Antwort2
Empfehlung hinzugefügt Dunno's
, die Lösung gemäß den Anforderungen des OP zu erstellen
Sie können die Variablen einfach in einer Schleife durchlaufen:
\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}
zu bekommen:
PS: Ich habe den Code so geschrieben, dass nur einer yshift
erforderlich ist, und so yshift
den blauen und roten Kästchen gemeinsam ist.