我怎样才能使用填充绘制矩形来完成关于 LTE 图表

我怎样才能使用填充绘制矩形来完成关于 LTE 图表

这是来自 3GPP“LTE 资源指南”的图片,名为{具有正常循环前缀的 LTE 上行链路子帧}

在此处输入图片描述

我已经做好了

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

现在我的问题是如何在这个图形的右侧添加小网格。也许你可能有一个问题,为什么不使用这个图形。我的回答是我的老师只接受自己绘画,我只知道 LaTeX 可以接受文件 eps 格式

答案1

我会选择更“语义化”的实现。不要给出明显任意的坐标和颜色,而是定义一些与问题相关的名称的样式,例如:

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

然后,您可以为其中一些样式提供“简写”单字符名称,例如:

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

这将允许您创建一个\foreach循环来绘制所有网格,然后将其指定为字符的“矩阵”,其中每个字符代表必须绘制单元格的样式:

  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

这是使用这些想法的完整代码:

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

并产生:

结果

现在来看看右边的图例。首先,为文本定义一个适当的样式:

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

然后,您可以使用positioning库来定义第一个图例出现的坐标。绘制完上述网格后,名为的节点current bounding box代表包含该网格的矩形,因此我们可以将图例定位在其右侧 2 厘米处:

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

然后我们用循环绘制每个小方块,将文本图例放在下面,并将坐标重新定义legend为前一个坐标下方 2 厘米:

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

结果如下:

结果

完整代码如下:

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

相关内容