EngineeringSoftwareDesign
Queuepictures

Description

This is a LaTeX code that generates a diagram using TikZ package.

First, the document class is set to "standalone", which means that the output will be a single, cropped image.

Then, the TikZ and "calc" libraries are imported.

A new command called "\cells" is defined that takes three parameters: the total number of cells, the number of grey cells, and the index for the "front" cell. The command creates a row of cells, with each cell labeled with a number. The grey cells are filled with a dark color to distinguish them from the other cells. The "front" and "back" cells are labeled with the text "front" and "back", respectively.

Two styles are defined using the "\tikzset" command: "cell" and "shaded cell". The "cell" style sets the minimum width and height of the cell and the "shaded cell" style inherits from the "cell" style and fills the cell with a dark color.

Finally, a TikZ picture is created using the "\begin{tikzpicture}" command. The "\cells" command is called with the parameters 8, 7, and 0, which generates a row of 8 cells with 7 shaded cells starting at index 0. Each cell is then labeled with a number using a loop that uses the "\node" command to add a label to each cell. The resulting diagram shows a row of 8 cells, with 7 shaded cells and numbers labeled in each cell.

Keywords

latex, tikz, picture, plot, array, matrix, nodes, styles, axes.

Source Code

EngineeringSoftwareDesign

\documentclass{standalone}
 
\usepackage{tikz}
\usetikzlibrary{calc}
 
\def\cells#1#2#3{%
% #1 = total number of cells
% #2 = number of grey cells
% #3 = index for "front" ("back" is mod(#3+#2-1, #1))
  \foreach [count=\i from 0] \j  in {1,...,#1} {
    % \node[cell,label=above:\i] (cell\i) at (\i,0) {};
    \node[cell] (cell\i) at (\i,0) {};
  }
  \pgfmathsetmacro{\last}{#3+#2-1}
  \foreach \i in {#3,...,\last} {
    \pgfmathsetmacro{\back}{mod(\i,#1)}
    \node[shaded cell] (back) at (\back,0) {};
  }
  \node[below] at (cell#3.south) {front};
  \node[below] at (back.south) {back};
}
 
\tikzset{
  cell/.style = {draw, minimum width=1cm, minimum height=0.8cm},
  shaded cell/.style = {cell, fill=black!30},
}
 
\begin{document}
%\begin{tikzpicture}
%\cells{6}{3}{0}
%\end{tikzpicture} \\
%
%\begin{tikzpicture}
%\cells{6}{3}{3}
%\end{tikzpicture} \\
%
%\begin{tikzpicture}
%\cells{6}{3}{4}
%\end{tikzpicture} \\
 
\begin{tikzpicture}
\cells{8}{7}{0}
\foreach [count=\i from 0] \number in {$P_1[0]$, $P_2[0]$, $P_3[0]$, 299, 8, 14, 53, 78}
\node at (cell\i) {\number};
\end{tikzpicture}
 
\end{document}
Deploy to Overleaf