TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is possible to create a table (for example using tabular environment or tikz)in latex, but which under every cell has an index, which describes order of this cells? Something like this: enter image description here

share|improve this question
up vote 7 down vote accepted

Tikz solution, generalized to any queue length:


% #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) {};
  \foreach \i in {#3,...,\last} {
    \node[shaded cell] (back) at (\back,0) {};
  \node[below] at (cell#3.south) {front};
  \node[below] at (back.south) {back};

  cell/.style = {draw, minimum width=1cm, minimum height=0.8cm},
  shaded cell/.style = {cell, fill=black!30},







Answering a question in a comment by the OP: How to add text to some cells?

It is easy given the code above, because that code defines a series of node names (cell0), (cell1), etc which you can use as coordinates to put any text, as for example:

\node at (cell1) {132};

If you want to put numbers in all cells, this can be easily automated via a \foreach loop, as in the following example:

\foreach [count=\i from 0] \number in {120, 32, 187, 299, 8, 14, 53, 78}
   \node at (cell\i) {\number};

Which gives:


share|improve this answer
I like this answer! – skpblack Jul 2 '14 at 13:36
@JLDiaz Thank you for your answer! It is very helpful. – Ziva Jul 2 '14 at 15:57
@JLDiaz I have one more question: if I want now to insert some number value to a cell, for example to first cell value 2, to second cell value 100, how should I modify the definition? – Ziva Jul 2 '14 at 16:59
@Ziva see my updated answer – JLDiaz Jul 2 '14 at 19:19
@JLDiaz It looks so nice and clearly, Thank you! – Ziva Jul 2 '14 at 20:28

With simple tabular:

    0 & 1 & 2 & 3 & 4 & 5 \\\hline
    \multicolumn{1}{|r|}{\cellcolor{gray!40}} & \multicolumn{1}{r|}{\cellcolor{gray!40}} &
    \multicolumn{1}{r|}{\cellcolor{gray!40}}& \multicolumn{1}{r|}{}& \multicolumn{1}{r|}{}& \multicolumn{1}{r|}{} 
    front & \hphantom{front}  & \hphantom{front}\makebox[0pt][r]{back} & \hphantom{front} & \hphantom{front} &
       \hphantom{front} \\[1ex]
    0 & 1 & 2 & 3 & 4 & 5 \\\hline
    \multicolumn{1}{|r|}{} & \multicolumn{1}{r|}{} &  \multicolumn{1}{r|}{}& 
     \multicolumn{1}{r|}{\cellcolor{gray!40}}& \multicolumn{1}{r|}{\cellcolor{gray!40}}& 
    \hphantom{front} & \hphantom{front} & \hphantom{front} & front & \hphantom{front}  &
      \hphantom{front}\makebox[0pt][r]{back}      \\[1ex]
    0 & 1 & 2 & 3 & 4 & 5 \\\hline
    \multicolumn{1}{|r|}{\cellcolor{gray!40}} & \multicolumn{1}{r|}{} &  \multicolumn{1}{r|}{}&
     \multicolumn{1}{r|}{}& \multicolumn{1}{r|}{\cellcolor{gray!40}}&
    \hphantom{front}\makebox[0pt][r]{back} & \hphantom{front} & \hphantom{front} & \hphantom{front} & front   &
      \hphantom{front}   \\

enter image description here

share|improve this answer

One can either use \nextblock[under text]{color} or else the shorthands \grayblk[under text] for gray and \nullblk[under text] for white. Then, just repeat the invocations as needed. Use a \resetblock when complete.




{\sffamily Some configurations of three elements in a queue}


enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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