1

Using the following code (from the answer to this question) to draw boxes using 2 \foreach loops; and defining the starting positions of both of them (yshift=.4cm and yshift=4.4cm). I want the blue boxes to automatically begin where the red boxes end.

Is it possible to combine them into one loop.

I mean using one \foreach loop to draw the lower 4 red boxes then continue with the upper 2 blue boxes.

\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}

enter image description here

  • It is possible by performing a test, but what is the point of doing that? – AndréC Aug 17 '19 at 5:55
  • To avoid using 2 loops and manually adding spacing between the red boxes and blue ones; which have to be adjusted if the boxes dimensions have to be changed. – Hany Aug 17 '19 at 5:56
1

With partial loop only and based on my answer the solution is simple:

\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}

enter image description here

|improve this answer|||||
  • @ZarkoThank you very much for your answer. Just one comment. Can the red boxes dimensions be different from the blue boxes. I mean separate their definitions. – Hany Aug 17 '19 at 15:00
  • 1
    @Hany as I already mentioned, please limit your scope of your question, please dont add constraints on-the-fly. – Raaja Aug 17 '19 at 16:01
  • @Hany, of course it is possible.You only need to define two styles for boxes. However, from your question I'm not able to figure out the context of image, i.e. what you like to present. Structure of your code is (to my opinion) u necessary complex and unclear. – Zarko Aug 17 '19 at 16:17
  • @ZarkoThank you very much. I defined 2 styles for the boxes, and it works fine. This is what I need. – Hany Aug 18 '19 at 5:05
2

Added Dunno's recommendation to make the solution as per the OP's requirements

You can simply loop the variables over:

\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}

to get:

enter image description here

PS: I made the code in such a manner that only one yshift is necessary, thereby, making the yshift common for both the blue and red boxes.

|improve this answer|||||
  • @RaajaThank you. But this does not solve the problem of manually adding spacing between the red boxes and blue ones; which have to be adjusted if the boxes dimensions have to be changed; by defining yshift=.4cm and yshift=4.4cm – Hany Aug 17 '19 at 6:38
  • @Hany based on your question, I understood that you wanted to make it into a single loop. Could you make it explicit in your question. The information you just provided is missing. – Raaja Aug 17 '19 at 6:40
  • @RaajaPlease refer to my edited question. I want to avoid defining the starting position of the blue boxes, in case I have to change their dimensions. I want the blue boxes to automatically begin where the red boxes end. – Hany Aug 17 '19 at 6:43
  • @Hany Let me know whether this new answer would suit you. – Raaja Aug 17 '19 at 6:46
  • @RaajaThank you for your time; but if you change the dimensions of the red boxes, the starting position of the blue boxes is not adjusted accordingly. – Hany Aug 17 '19 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.