3

I have a table (the one here is just an example) whose rows need to be numbered alphabetically, i.e., using a), b), c) etc instead of with arabic numerals. I tried every way of adding \alph to the code but nothing works.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}

\title{Example}

\newcounter{rowcount}
\setcounter{rowcount}{0}

\begin{document}
\maketitle

\begin{table}[h!]
\begin{tabular}{@{\stepcounter{rowcount}\therowcount.)\hspace*{\tabcolsep}}c|c|c|c}
First Name & Last Name & Education & Occupation \\
April & Kelly & Bachelor & Mathematician \\
Miller & Martin & Upper secondary & Meteorologist \\
Jenna & Hawkins & Upper secondary & Programmer \\
Brad & Wells & Doctoral & Lawyer \\
Julian & Fowler & Master & Programmer
\end{tabular}
\end{table}

\end{document}

enter image description here

4
  • 4
    Use \alph{rowcount} instead of \therowcount.. – user228539 Nov 15 '20 at 16:48
  • @abcdefg That's an answer, not a comment. – Alenanno Nov 15 '20 at 19:25
  • You don't want to number the first row, do you? – egreg Nov 15 '20 at 21:04
  • Just for the records: if you are interested in a stable solution that really accomplishes what the currently accepted answer promises to achieve, have a look at the nicematrix package, which offers these things right away. I am not at all interested in reputation points, but I want to spare other users from the disappointment they will run into if they copy this code. – user228539 Nov 16 '20 at 22:30
2

If you want to represent the rowcount counter with alphabet letters, redefine \therowcounter.

I propose a slightly different way, that has the inconvenience of requiring a leading & in each row, but easily allows to keep the alignment of the columns also if a row is not numbered. I chose \* to represent “no number in this row” because it's unlikely you need its standard meaning in the body of the table.

Note that, apart from adding the leading & and \* in the row that shouldn't be numbered, it's all automatic, including the resetting of the counter.

Using a dummy column also has the benefit that you can add a \label in it for referring to the letter, see the examples below. (Thanks to azetina for suggesting the improvement.)

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}

\newcounter{rowcount}
\renewcommand{\therowcount}{\alph{rowcount}}
\newcolumntype{\ROWCOUNT}{>{\refstepcounter{rowcount}\therowcount.)}r}

\newenvironment{counttabular}[2][c]{%
  \setcounter{rowcount}{0}% reset the counter
  \def\*{\multicolumn{1}{c}{}}% for a nonnumbered row
  \begin{tabular}[#1]{@{}\ROWCOUNT #2@{}}
}{\end{tabular}}


\begin{document}

\begin{counttabular}{c|c|c|c}
\* & First Name & Last Name & Education       & Occupation \\
\hline
\label{AK}
   & April      & Kelly     & Bachelor        & Mathematician \\
   & Miller     & Martin    & Upper secondary & Meteorologist \\
   & Jenna      & Hawkins   & Upper secondary & Programmer \\
   & Brad       & Wells     & Doctoral        & Lawyer \\
   & Julian     & Fowler    & Master          & Programmer
\end{counttabular}

\bigskip

April Kelly is on line \ref{AK}

\bigskip

\begin{counttabular}{cccc}
\toprule
\* & First Name & Last Name & Education       & Occupation \\
\midrule
   & April      & Kelly     & Bachelor        & Mathematician \\
   & Miller     & Martin    & Upper secondary & Meteorologist \\
   & Jenna      & Hawkins   & Upper secondary & Programmer \\
\label{BW}
   & Brad       & Wells     & Doctoral        & Lawyer \\
   & Julian     & Fowler    & Master          & Programmer \\
\bottomrule
\end{counttabular}

\bigskip

The lawyer is on line \ref{BW}

\end{document}

enter image description here

It goes without saying that I prefer the latter realization to the former.

2
  • Maybe add \refstepcounter just in case he wants to refer to the lines in the table? – azetina Nov 15 '20 at 21:29
  • 1
    @azetina That's a good idea! I implemented it. – egreg Nov 15 '20 at 22:59
2

\alph does work if you use \alph{rowcount}.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}

\title{Example}

\newcounter{rowcount}
\setcounter{rowcount}{0}

\begin{document}
\maketitle

\begin{table}[h!]
\begin{tabular}{@{\stepcounter{rowcount}\alph{rowcount})\hspace*{\tabcolsep}}c|c|c|c}
First Name & Last Name & Education & Occupation \\
April & Kelly & Bachelor & Mathematician \\
Miller & Martin & Upper secondary & Meteorologist \\
Jenna & Hawkins & Upper secondary & Programmer \\
Brad & Wells & Doctoral & Lawyer \\
Julian & Fowler & Master & Programmer
\end{tabular}
\end{table}

\end{document}

BTW, one could argue that using \therowcount is actually not necessarily as "clean" as it could be, it just happens to yield an Arabic number, but you should really think of it as "the counter" and not a representation of the counter. So the arguably cleaner way of getting an Arabic number is to use \arabic{rowcount}. In these conventions it is easier to see that one can use \alph{rowcount} and so on to dial different representations of the counter.

1
  • I am not interested in reputation points at all, but loath to see that shaky proposals get advertised, upvoted, and even accepted. Another reason not to sign up on this site. If one was interested in really nice and stable solutions that do what counttabular promises to do, one should definitely look at the nicematrix, which does offer automatic numbering out of the box, and has many other nice options. – user228539 Nov 16 '20 at 22:26

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.