0

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}

enter image description here

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}

enter image description here

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

  1. 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.
  2. The width for all cells from the column displaying the row numbering must be the same.
  3. (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.
1
  • 1
    why don't you simply use a column rather than an @{} then you can use l c r as usual and get automatic width setting? – David Carlisle Jun 8 '20 at 20:35
3

You can put the numbering into a column rather than @-expression, then automatic width is standard and you can use any table column alignment. It also has the benefit that you can use \label in the first column to label a row for use with \ref

enter image description here

\documentclass{article}

\usepackage{array}

\newcounter{rowcount}

\begin{document}
\begin{tabular}{>{\refstepcounter{rowcount}\therowcount.)}rll}
\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}
1

You also can do that with blkarray:

\documentclass{article}
\usepackage{blkarray}

\begin{document}

\begin{blockarray}{|@{\hskip -1.3\tabcolsep}\BAenum!{)\hskip\labelsep\vrule\hskip\tabcolsep}l|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{blockarray}

\end{document} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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