The context
I want to create a table whose first column of each row displays the row numbering. I've found some approaches but neither of them have suited my needs
Approach no. 1
According to the accepted answer from this question. I can accomplish this task with this minimal working example
\documentclass{article}
\usepackage{array}
\newcounter{rowcount}
\setcounter{rowcount}{0}
\begin{document}
\begin{tabular}{@{\stepcounter{rowcount}\therowcount.)\hspace*{\tabcolsep}}ll}
\hline my row 1 & 1
\\ \hline my row 2 & 2
\\ \hline my row 3 & 3
\\ \hline my row 4 & 4
\\ \hline my row 5 & 5
\\ \hline my row 6 & 6
\\ \hline my row 7 & 7
\\ \hline my row 8 & 8
\\ \hline my row 9 & 9
\\ \hline my row 10 & 10
\\ \hline
\end{tabular}
\end{document}
The reason I'm not satisfied with this approach is that the first column is displayed with different widths. That is, if my table had more than 99 rows, the width given to the first column for rows whose numbering is a three-digit number would be larger than the width given for a column whose row numbering is a two-digit number.
Approach no. 2
According to the accepted answer from this question. I can accomplish this task with this minimal working example
\documentclass{article}
\usepackage{array,etoolbox}
\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
\begin{document}
\begin{tabular}{|@{\makebox[1em][r]{\rownumber\space}}|c|c|}
\hline my row 1 & 1
\\ \hline my row 2 & 2
\\ \hline my row 3 & 3
\\ \hline my row 4 & 4
\\ \hline my row 5 & 5
\\ \hline my row 6 & 6
\\ \hline my row 7 & 7
\\ \hline my row 8 & 8
\\ \hline my row 9 & 9
\\ \hline my row 10 & 10
\\ \hline
\end{tabular}
\end{document}
The reason I'm not satisfied with this approach is that I need to hardcode the width for the first column in order for the row numbering to be displayed within the cell which might yield to undesired results when working with tables with more than 999 rows because I would have one-digit, two-digit, three-digit and four-digit numbers in the first column.
I know that I can compute that width by making sure that all row numberings are fully displayed within their corresponding cells but this would be a repetitive task that can be better performed by computers.
The question
How can I create a table whose first column displays the row numbering taking into consideration the following conditions
- The row numbering can be a number with any number of digits. That is, don't assume that it will be a one-digit or a two-digit number.
- The width for all cells from the column displaying the row numbering must be the same.
- (This is not required but would really help me) The user can change the alignment of the column displaying the row numbering just by setting the corresponding built-in column type
l
,r
,c
for that column.
@{}
then you can use l c r as usual and get automatic width setting?