I want to number my cells in a Table in LaTeX. The result should look like this. enter image description here

Does somebody know to to do this?

  • 1
    Welcome to TeX.SX! Check this for future questions: tex.meta.stackexchange.com/q/228/120578
    – koleygr
    Mar 3, 2019 at 14:34
  • 3
    Welcome to TeX.SE. Please provide more information about your document setup and about the specific formatting objective. E.g,, are you planning on using a tabular or similar environment? And, will the cell contents always consist of a single, centered uppercase letter and a four-digit number placed in the upper-right hand corner of the cell? If not, what might cells look like in general?
    – Mico
    Mar 3, 2019 at 14:36

3 Answers 3


I defined a new environment, with one argument, the number of columns, which numbers automatically its cells:

\usepackage{array, siunitx, makebox}
\newenvironment{countcells}[1]{\setcounter{cellctr}{0}\setlength{\extrarowheight}{4pt}\tabular{|*{#1}{c<{\enspace \stepcounter{cellctr}\raisebox{1.5ex}{\scriptsize\makebox*{\scriptsize000}[l]{\num[minimum-integer-digits=4]{\thecellctr}}}}|}}}%


     A & B & C & D \\ \hline
E & F & G & H \\ \hline
\makebox*{H}{I} & J & K & L \\ \hline


enter image description here

  • @CarlaTeX: You're right, I hadn't noticed it.That's due to the letter I being less wide than the others. I've edited a work-around with \makebox*. Thanks for drawing my attention to this problem!
    – Bernard
    Mar 3, 2019 at 17:37

You could define a command with a tabular environment yo use it inside your tabular:


\newcommand{\mycell}[2]{\begin{tabular}{@{}c@{}c@{}}&\tiny #2\\\Large #1&\end{tabular}}
     \mycell{A}{0001}& \mycell{B}{0002} & \mycell{C}{0003}& \mycell{D}{0004}\\\hline

enter image description here

By using tikz package you may do something like this:

\def\myFontSize{\f@size pt}
\newcommand{\mycontent}[2]{\begin{tabular}{@{}c@{}c@{}}&\tiny #2\\\Large #1&\end{tabular}}
\foreach \item[count=\i from 1] in {#1}{\ifnum\i=1\xdef\mycolor{\item}\else\xdef\NoFLine{\item}\fi}
\begin{tikzpicture}[inner sep=0pt,outer sep=0pt]
\draw[-,thick,color={\mycolor}](0,0) --({\fsize*\BoxFactor},0)--({\fsize*\BoxFactor},{\fsize*\BoxFactor} )--(0,{\fsize*\BoxFactor}) coordinate (A);
\node at ({\fsize*\BoxFactor/2},{\fsize*\BoxFactor/2}) {\mycontent{#2}{#3}};


enter image description here

  • Is it possible to modify the first solution so that the numbers are always right-aligned with the cell, even if the text in the cells have different width?
    – Simon
    Mar 28, 2022 at 17:49

With a TikZ matrix and a \foreach:


   \matrix [
        matrix of nodes,
        column sep=-\pgflinewidth,
        every node/.style={
            text height=30pt,
            text depth=10pt,
            text width=40pt
        ] (mymatr) {
        A & B & C & D \\
    \foreach \i in {1, 2, ..., 4}
        {\node[anchor=north east, font=\scriptsize]
          at (mymatr-1-\i.north east) {\texttt{000\i}};}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .