3

enter image description here

I am trying to draw a below given diagram for myself. I have been able to draw the rectangles using \foreach however I am unable to write numbers below the boxes as shown. Could somebody help me in drawing such a diagram.

Here is my code, it is IEEE Tran's code. Also, please see that I have manually added numbers from 1 to n, I want to do it using \foreach command. Kindly do not give negative voting because I am beginner with Tikz and this forum.

Please answer one more question- is there a way I can initialize x and y coordinates as x = 1 and y = 1 in the starting of the Tikz figure and then draw all the blocks/rectangles using reference coordinates?

\documentclass[conference]{IEEEtran}

\ifCLASSINFOpdf

\else
\fi
\usepackage[usenames,dvipsnames]{color}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\usetikzlibrary{shapes.multipart}
\usepackage[caption=false]{subfig}
\usepackage{tabularx,tikz}


\hyphenation{op-tical net-works semi-conduc-tor}


\begin{document}

\title{Bare Demo of IEEEtran.cls for Conferences}

\maketitle

\IEEEpeerreviewmaketitle

\section{Introduction}
I wish you the best of success.

\begin{tikzpicture}
%Can I give a reference coordinate such as x = 0 and y = 0 and then draw figures
%based on that reference points through out the picture?

\draw [rounded corners](0,0) rectangle +(9,6);

\draw [rounded corners](0.15,0.5) node[above right, font =\small,align=center,text width = 2cm]{YYYY} rectangle +(2.25,5);

\foreach \x in {2.5}
    \foreach \y in {0.5,3,4.5}
    {
    \draw [rounded corners] (\x,\y) rectangle +(6.35,1);

    }

\foreach \x in {2.75,4.25,5.75,7.25}
    \foreach \y in {0.9,3.4,4.9}
    {
    \draw [rounded corners] (\x, \y) node[above right, font=\scriptsize,text width = 1.8cm] {XXX} rectangle +(1.2,0.5);
    }
\node at (2.75,4.5) [above right, font=\scriptsize,text width = 1.8cm] {1} ;
\node at (2.75,3) [above right, font=\scriptsize,text width = 1.8cm] {2} ;
\node at (2.75,0.5) [above right, font=\scriptsize,text width = 1.8cm] {n} ;

\node at (4.25,4.5) [above right, font=\scriptsize,text width = 1.8cm] {n+1} ;
\node at (4.25,3) [above right, font=\scriptsize,text width = 1.8cm] {n+2} ;
\node at (4.25,0.5) [above right, font=\scriptsize,text width = 1.8cm] {2n} ;

\node at (5.75,4.5) [above right, font=\scriptsize,text width = 1.8cm] {2n+1} ;
\node at (5.75,3) [above right, font=\scriptsize,text width = 1.8cm] {2n+2} ;
\node at (5.75,0.5) [above right, font=\scriptsize,text width = 1.8cm] {3n} ;

\node at (7.25,4.5) [above right, font=\scriptsize,text width = 1.8cm] {3n+1} ;
\node at (7.25,3) [above right, font=\scriptsize,text width = 1.8cm] {3n+2} ;
\node at (7.25,0.5) [above right, font=\scriptsize,text width = 1.8cm] {4n} ;

\end{tikzpicture}

\end{document}
  • 5
    You should help us adding the code you have right now (from \documentclass to \end{document}). That way you make much more easily our way to help you. – Manuel Jun 28 '14 at 19:28
  • 2
    Welcome to TeX.SX! As Manuel says, please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to help when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – cfr Jun 28 '14 at 20:12
  • What are you actually trying to illustrate? – Thruston Jun 28 '14 at 20:58
  • 2
    Please remove any code that is not directly related to your question; see minimal working example (MWE). – jub0bs Jun 28 '14 at 20:59
  • I just did and hopefully it will easy for anyone to answer my questions. Thanks once again! – New User Jun 28 '14 at 21:16
5

Here is an example using two nested \foreach loops and \ifthenelse from the ifthen package:

\documentclass[tikz,border=1cm]{standalone}

\usepackage{ifthen}

\begin{document}

\begin{tikzpicture}[
    rect/.style={rectangle,rounded corners,fill=green,draw=black,text width=.5cm,text height=.25cm},
    none/.style={rectangle,rounded corners,fill=green!10,draw=gray!20,text width=.5cm,text height=.25cm},
    nmbr/.style={font=\scriptsize,yshift=-.25cm,anchor=north}
]

\newcounter{n}

\foreach \y in {1,...,3} {
  \setcounter{n}{0}
  \foreach \x in {1,...,4} {
    \ifthenelse{\y<3}{
      \node[rect] at (\x,-\y) {};
      \node[nmbr] at (\x,-\y) {\ifthenelse{\x=1}{\y} {
        \ifthenelse{\arabic{n}=1}{}{\arabic{n}}n+\y}};
      \stepcounter{n}
    }{
      \stepcounter{n}
      \node[none] at (\x,-\y) {};
      \node[rect] at (\x,-\y-1) {};
      \node[nmbr] at (\x,-\y-1) {\ifthenelse{\x=1}{n}{\arabic{n}n}}; 
    }
  }
}

\end{tikzpicture}

\end{document} 

enter image description here

  • Thanks a lot , this is so cool!!! I am following the Tikz manual, is there any other reference material that I could follow ? How can I learn many other things about \foreach, \ifthenelse ? Please let me know. – New User Jun 28 '14 at 21:40
  • @user56277 You can find a lot of really cool examples at texample.net. – sergej Jun 28 '14 at 21:48
  • I would put all of those in math mode. – Manuel Jun 28 '14 at 22:03
2

A PSTricks solution:

\documentclass{article}

\usepackage[margin = 4cm]{geometry} % to avoid `Overfull \hbox' warning
\usepackage{multido}
\usepackage{pstricks}
\usepackage{xfp}

\newcommand*\vertDistOuter{\fpeval{\distVertOuter+\heightOuter}}
\newcommand*\vertDistOuterTwo[1]{\fpeval{\heightOuter/2+#1*(\heightOuter+\distVertOuter)}}
\newcommand*\horiDistInner{\fpeval{\distHoriInner+\widthInner}}
\newcommand*\horiDistInnerTwo[1]{\fpeval{\distHoriInnerStart+\widthInner/2+#1*(\widthInner+\distHoriInner)}}
\newcommand*\vertDistInner{\fpeval{(\heightOuter-\heightInner)/2}}
\newcommand*\widthTotal{\fpeval{2*\distHoriInnerStart+\boxesInEachRow*\widthInner+(\boxesInEachRow-1)*\distHoriInner} }
\newcommand*\heightTotal{\fpeval{3*\heightOuter+2*\distVertOuter}}

%%% parameters %%%
% spacing
\def\distVertOuter{0.5}
\def\distHoriInnerStart{0.3}
\def\distHoriInner{0.7}
% size of boxes
\def\heightOuter{3}
\def\widthInner{2.5}
\def\heightInner{2}
% number of boxes in each row
\def\boxesInEachRow{4}

\begin{document}

\begin{pspicture}(\widthTotal,\heightTotal)
  \multido{\r = 0+\vertDistOuter}{3}{%
    \psframe[
      fillstyle = solid,
      fillcolor = blue!60
    ](0,\r)(\widthTotal,\fpeval{\r+\heightOuter})}
  \multido{\rA = \distHoriInnerStart+\horiDistInner}{\boxesInEachRow}{%
    \multido{\rB = \vertDistInner+\vertDistOuter}{3}{%
      \psframe[
        fillstyle = solid,
        fillcolor = green!70!black
      ](\rA,\rB)(\fpeval{\rA+\widthInner},\fpeval{\rB+\heightInner})}}
  \rput(\horiDistInnerTwo{0},\vertDistOuterTwo{2}){$1$}
  \rput(\horiDistInnerTwo{1},\vertDistOuterTwo{2}){$n+1$}
  \multido{\i = 2+1, \r = \horiDistInnerTwo{2}+\horiDistInner}{\fpeval{\boxesInEachRow-2}}{\rput(\r,\vertDistOuterTwo{2}){$\i n+1$}}
  \rput(\horiDistInnerTwo{0},\vertDistOuterTwo{1}){$2$}
  \rput(\horiDistInnerTwo{1},\vertDistOuterTwo{1}){$n+2$}
  \multido{\i = 2+1, \r = \horiDistInnerTwo{2}+\horiDistInner}{\fpeval{\boxesInEachRow-2}}{\rput(\r,\vertDistOuterTwo{1}){$\i n+2$}}
  \rput(\horiDistInnerTwo{0},\vertDistOuterTwo{0}){$n$}
  \multido{\i = 2+1, \r = \horiDistInnerTwo{1}+\horiDistInner}{\fpeval{\boxesInEachRow-1}}{\rput(\r,\vertDistOuterTwo{0}){$\i n$}}
\end{pspicture}

\end{document}

output

All you have to do is choose the values of the parameters and the drawing will automatically adjust.

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.