0

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?

2
  • 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

5

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

\documentclass[12pt,a4paper]{report}
\usepackage{array, siunitx, makebox}
\newcounter{cellctr}
\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}}}}|}}}%
{\endtabular\setcounter{cellctr}{0}}

\begin{document}

\begin{countcells}{4}\hline%
     A & B & C & D \\ \hline
E & F & G & H \\ \hline
\makebox*{H}{I} & J & K & L \\ \hline
\end{countcells}

\end{document}

enter image description here

1
  • @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
2

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

\documentclass[12pt,a4paper]{report}

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

enter image description here

By using tikz package you may do something like this:

\documentclass[12pt,a4paper]{report}
\usepackage{tikz}
\makeatletter
\def\myFontSize{\f@size pt}
\makeatother
\def\BoxFactor{2.5}
\def\fsize{\myFontSize}
\newcommand{\mycontent}[2]{\begin{tabular}{@{}c@{}c@{}}&\tiny #2\\\Large #1&\end{tabular}}
\newcommand{\mycell}[3][black,1]{%
\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);
\ifnum\NoFLine=1
\relax
\else
\draw[-,thick,color={\mycolor}](A)--(0,0);
\fi
\node at ({\fsize*\BoxFactor/2},{\fsize*\BoxFactor/2}) {\mycontent{#2}{#3}};
\end{tikzpicture}
}

\begin{document}
\noindent\(\mycell[blue,0]{A}{0001}\!\mycell{B}{0002}\mycell{C}{0003}\mycell{D}{0004}\)
\end{document}

enter image description here

1
  • 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
2

With a TikZ matrix and a \foreach:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
   \begin{tikzpicture}
   \matrix [
        matrix of nodes,
        column sep=-\pgflinewidth,
        every node/.style={
            draw,
            align=center,
            font=\bfseries\Huge,  
            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}};}
    \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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