# Making a table with numbered cells

I would like to make a table with numbers at the top left corner of each cell. In particular, how do I make this table?

This answer almost does what I want, but I want to make the numbers left aligned and the main entry in the cell centered in each box.

Thank you!

• Welcome to TeX.SE. Should the small numbers be generated automatically or by hand? Please advise.
– Mico
Mar 28, 2022 at 23:52
• Preferably generated by hand. Mar 28, 2022 at 23:53
• Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
– Community Bot
Mar 29, 2022 at 0:00

By use of the tabularray package and separate narrow columns for numbers:

\documentclass{article}
\usepackage{geometry}
\usepackage{amssymb, mathtools}
%\usepackage{newtxtext, newtxmath}
\usepackage{tabularray}

\begin{document}
\begin{table}[!th]
\begin{tblr}{hlines,
colspec = {|X[1,l] | X[1,l] |
*{3}{Q[r,h, font=\bfseries] X[2,c,m,mode=dmath] |}},
colsep  = 3pt,
rowsep  = 3pt,
row{1}  = {font=\small\bfseries, c, m}
}
Elements of N
&   Elements of X
& \SetCell[c=2]{c}  Any $f$
&   & \SetCell[c=2]{c}  Injective $f$
&   & \SetCell[c=2]{c}  Surjective $f$
&       \\
dist.
& dist.
&1. & x^n
&2. & (x)_n
& 3. & x!S(n.x)
\\
indist.
& dist.
&4. & \left(\binom{x}{n}\right)
&5. & \binom{x}{n}
& 6. & \left(\binom{x}{n-x}\right)
\\
dist.
& indist.
&7. & \begin{multlined}
S(n,0) + S(n,1)\\
+\dots + S(n,x)
\end{multlined}
&8. & \begin{aligned}
1 & \quad \mathrm{if}\ n\leqslant x   \\
0 & \quad \mathrm{if}\ n > x
\end{aligned}
& 9. & S(n,x)
\\
indist.
& indist.
&10.& \begin{multlined}
p_0(n) + p_1(n)\\
+\dots + p_x(x)
\end{multlined}
&11.& \begin{aligned}
1 & \quad \mathrm{if}\ n\leqslant x   \\
0 & \quad \mathrm{if}\ n > x
\end{aligned}
&12. & p_x(n)
\\
\end{tblr}
\end{table}
\end{document}


Addendum: A possible wee bit different table design where first two column are narrower and other (with math expressions) wider:

For it the following changes in tblr preamble and column headers are done:

% document preamble
% ...
\begin{tblr}{hlines,
colspec = {| l | l |   % <---
*{3}{Q[r,h, font=\bfseries] X[c,m,mode=dmath] |},% <---
colsep  = 3pt,
rowsep  = 4pt,
row{1,2}= {font=\small\bfseries, c, m, rowsep=1pt}     % <---
}
\SetCell[c=2]{c}    Elements of:    % <--- new
&   & \SetCell[c=2, r=2]{c}  Any $f$    % <---
&   & \SetCell[c=2,r=2]{c}  Injective $f$   % <---
&   & \SetCell[c=2,r=2]{c}  Surjective $f$  % <---
&       \\
X   & Y &   &   &   &   &   &       \\  % <--- new
%% table body

• @F. Pantigny, thank you for edit! Mar 29, 2022 at 7:48

With {NiceTabular} of nicematrix, you can can put the numbers in the cells after the construction of the main tabular with Tikz.

\documentclass{article}
\usepackage{geometry}
\usepackage{amssymb}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{table}[!th]
\begin{NiceTabular}{ccX[c]X[c]X[c]}[width=\linewidth,hvlines,cell-space-limits=3pt]
\RowStyle[bold,cell-space-limits=5pt]{}
\Block{}{Elements\\ of N}
& \Block{}{Elements\\ of X}
& Any $f$
& Injective $f$
& Surjective $f$ \\
dist.
& dist.
& $x^n$
& $(x)_n$
& $x!S(n,x)$ \\
indist.
& dist.
& $\left(\binom{x}{n}\right)$
& $\binom{x}{n}$
& $\left(\binom{x}{n-x}\right)$ \\
dist.
& indist.
& \begin{aligned} S(n,0) + S(n,1)\\ +\dots + S(n,x) \end{aligned}
& \begin{aligned} 1 & \quad \mathrm{if}\ n\leqslant x \\ 0 & \quad \mathrm{if}\ n > x \end{aligned}
& $S(n,x)$ \\
indist.
& indist.
& \begin{aligned} p_0(n) + p_1(n)\\ +\dots + p_x(x) \end{aligned}
& \begin{aligned} 1 & \quad \mathrm{if}\ n\leqslant x \\ 0 & \quad \mathrm{if}\ n > x \end{aligned}
& $p_x(n)$ \\
\CodeAfter
\newcounter{cell}
\begin{tikzpicture}
\foreach \i in {2,3,4,5}
\foreach \j in {3,4,5}
{
\stepcounter{cell}
\node [below right] at (\i-|\j) { \bfseries \small \thecell } ;
} ;
\end{tikzpicture}
\end{NiceTabular}
\end{table}

\end{document}


You need several compilations because of the PGF/Tikz nodes created by nicematrix.