# How can I finish this about LTE diagram using filldraw rectangle

This is an picture from 3GPP "LTE Resource Guide" called {LTE Uplink Subframe with Normal Cyclic Prefix}

I have done

\documentclass{standalone}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage{scalefnt}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}
\filldraw[fill=blue] (4.5,6)rectangle (1,0.5);%PUCCH-ACK/NACK/SRI
\filldraw[fill=orange] (3.5,6) rectangle (0.5,0.5);%PUCCH-CQI
\filldraw[fill=white!50!blue](3,6) rectangle(0.5,0.5);%Demodulation RS
\filldraw[fill=orange] (2.5,6) rectangle (1,0.5);%PUCCH-CQI
\filldraw[fill=orange] (2,6) rectangle (0.5,0.5);%PUCCH-CQI
\filldraw[fill=white!50!blue] (1,6) rectangle (0.5,0.5);%Demodulation RS
\filldraw[fill=white!50!blue] (4,0.5) rectangle (4,0.5);%Demodulation RS
\filldraw[fill=white!50!blue] (0,6) rectangle (0.5,0.5);
\filldraw[fill=orange] (0,6) rectangle (0.5,0.5);%PUCCH-CQI
\filldraw[fill=orange] (0,6) rectangle (0.5,0.5);%PUCCH-CQI
\filldraw[fill=black!30!green] (2,0.5) rectangle (5,5.5);%PUSCH-Data
\filldraw[fill=black!30!green] (0,0.5) rectangle (1.5,5.5);%PUSCH-Data
\filldraw[fill=white!50!blue] (2.5,0) rectangle (1,0.5);%Demodulation RS
\filldraw[fill=blue] (3,0) rectangle (1,0.5);%PUCCH-ACK/NACK/SRI
\filldraw[fill=blue] (0,0) rectangle (1,0.5);%PUCCH-ACK/NACK/SRI
\filldraw[fill=white!50!blue] (0,1) rectangle (1.5,5);%Demodulation RS
\draw[step=0.5,thick] (0,0) grid (8,6);%resource grid
\end{tikzpicture}
\end{document}


now my problem is how to add little grid on the right side of this figure. maybe you probably have a question about why not use this figure. my answer is my teacher just accepts painting own and I just know LaTeX can accept the file eps format

-
I just know LaTeX can accept the file eps format? Not at all! –  Claudio Fiandrino Jun 13 '14 at 7:48
'...you probably have a question about why not use this figure. my answer is my teacher just accepts painting own...'. So is this an assignment for a class? –  cfr Jul 7 '14 at 2:47

I would go for a more "semantic" implementation. Instead of giving apparently arbitrary coordinates and colors, define some styles with names related to your problem, for example:

\tikzset{
cell/.style = {draw=green!20, very thick},
Data/.style = {cell, fill=black!50!green},
CQI/.style  = {cell, fill=orange},
RS/.style   = {cell, fill=blue!50},
ACK/.style  = {cell, fill=blue!70!black},
}


Then, you can give "shorthand" single-char-names to some of these styles, for example:

\tikzset{
!/.style = {Data},
C/.style = {CQI},
R/.style = {RS},
A/.style = {ACK},
}


This will allow you to create a \foreach loop to draw all your grid, which is then specified as a "matrix" of characters, in which each char represents the style in which the cell has to be drawn:

  C,R,C,C,C,R,C,A,A,R,R,R,A,A,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
A,A,R,R,R,A,A,C,R,C,C,C,R,C


This is the complete code which uses these ideas:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\begin{document}
\tikzset{
cell/.style = {draw=green!20, very thick},
border/.style = {draw=black!50!green, very thick},
Data/.style = {cell, fill=black!50!green},
CQI/.style  = {cell, fill=orange},
RS/.style   = {cell, fill=blue!50},
ACK/.style  = {cell, fill=blue!70!black},
% Shorthands
!/.style = {Data},
C/.style = {CQI},
R/.style = {RS},
A/.style = {ACK},
}

\begin{tikzpicture}
\foreach \cell [count=\i from 0] in {
C,R,C,C,C,R,C,A,A,R,R,R,A,A,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
A,A,R,R,R,A,A,C,R,C,C,C,R,C} {
\pgfmathsetmacro{\x}{mod(\i, 14)}  % 14 is the number of cells per row
\pgfmathsetmacro{\y}{floor(\i/14)}
\filldraw[\cell] (\x,-\y) rectangle (\x+1,-\y-1);
}

\draw[border]
($(0,0)+(-\pgflinewidth,\pgflinewidth)$) rectangle
($(14,-8) + (\pgflinewidth, -\pgflinewidth)$);
\end{tikzpicture}
\end{document}


and produces:

Now for the legend to the right. First, define an appropiate style for the text:

\tikzset{
legend text/.style = {anchor=center, black!50, font=\sffamily},
}


Then, you can use positioning library to define a coordinate where the first legend will appear. After the above grid was drawn, the node named current bounding box represents the rectangle containing that grid, so we can position the leged 2cm to its right with:

\coordinate[right=2cm of current bounding box.north east] (legend);


And with a loop we draw each little square, put the text legend below, and redefine the coordinate legend to be 2cm below of the previous one:

\foreach \style/\text in {
C/{PUCCH-CQI},
A/{PUCCH-ACK/NACK/SRI},
!/{PUSCH-Data},
R/{Demodulation RS}
} {
\filldraw[border,\style] (legend) rectangle +(1,-1);
\node[below=1.3cm of legend, xshift=0.5cm, legend text] {\text};
\coordinate[below=2cm of legend] (legend);
}


This is the result:

and this is the complete code:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\begin{document}
\tikzset{
cell/.style = {draw=green!20, very thick},
border/.style = {draw=black!50!green, very thick},
Data/.style = {cell, fill=black!50!green},
CQI/.style  = {cell, fill=orange},
RS/.style   = {cell, fill=blue!50},
ACK/.style  = {cell, fill=blue!70!black},
legend text/.style = {anchor=center, black!50, font=\sffamily},
% Shorthands
!/.style = {Data},
C/.style = {CQI},
R/.style = {RS},
A/.style = {ACK},
}

\begin{tikzpicture}

\foreach \cell [count=\i from 0] in {
C,R,C,C,C,R,C,A,A,R,R,R,A,A,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
!,!,!,R,!,!,!,!,!,!,R,!,!,!,
A,A,R,R,R,A,A,C,R,C,C,C,R,C} {
\pgfmathsetmacro{\x}{mod(\i, 14)}
\pgfmathsetmacro{\y}{floor(\i/14)}
\filldraw[\cell] (\x,-\y) rectangle (\x+1,-\y-1);
}

\draw[border]
($(0,0)+(-\pgflinewidth,\pgflinewidth)$) rectangle
($(14,-8) + (\pgflinewidth, -\pgflinewidth)$);

\coordinate[right=2cm of current bounding box.north east] (legend);

\foreach \style/\text in {
C/{PUCCH-CQI},
A/{PUCCH-ACK/NACK/SRI},
!/{PUSCH-Data},
R/{Demodulation RS}
} {
\filldraw[border,\style] (legend) rectangle +(1,-1);
\node[below=1.3cm of legend, xshift=0.5cm, legend text] {\text};
\coordinate[below=2cm of legend] (legend);
}
\end{tikzpicture}
\end{document}

-
'...you probably have a question about why not use this figure. my answer is my teacher just accepts painting own...'. So is this an assignment for a class? –  cfr Jul 7 '14 at 2:46