56

Is there any way to get automatic row numbers in table?

For example, if I define table contents as

\begin{tabular} % black magic column definition
   \rownumber & foo\\
   \rownumber & bar\\
   \rownumber & baz\\
\end{tabular}

and get something like

1 | foo
2 | bar
3 | baz
David Carlisle
  • 757,742
miah
  • 663

4 Answers4

45
\documentclass{article}
\usepackage{array,etoolbox}
\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
\begin{document}

\begin{tabular}{@{\makebox[3em][r]{\rownumber\space}} | r}
  Something \\
  Other stuff \\
  MAGIC!
\end{tabular}

\end{document}

output

enter image description here
If you want to start with the second row use

\documentclass{article}
\usepackage{array,etoolbox}
\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}
\def\rownumber{}
\begin{document}

\begin{tabular}{@{\makebox[3em][r]{\rownumber\space}} | r}
  Something 
  \gdef\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}} \\
  Other stuff \\
  MAGIC!
\end{tabular}

\end{document}

output
enter image description here

If one wants a heading for the columns use:

\documentclass{article}
\usepackage{array,etoolbox}
\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
\begin{document}

\begin{tabular}{@{\makebox[3em][r]{\rownumber\space}} | r}
\multicolumn{1}{@{\makebox[3em][r]{ID~}} | r}{\emph{whatever}}\\    
        Something \\
        Other stuff \\
        MAGIC!
\end{tabular}

\end{document}

enter image description here

  • 1
    Is it possible to start numbering at the second line? i.e. have a header row? TIA. – Fabian Tamp Oct 10 '12 at 12:00
  • @Herbert Is it possible what Fabian asked? Starting at the second or third line? – Isai Jun 29 '13 at 05:22
  • 2
    edit the command \rownumber that it test the value:\ifnum\themagicrownumber>2\relax\arabic{magicrownumbers}\fi` then it puts only the number at the third row –  Jun 30 '13 at 07:08
  • 1
    @Herbert Do you mean replace \begin{tabular}{@{\makebox[3em][r]{\rownumber\space}} | r}

    with \begin{tabular}{@{\makebox[3em][r]{\ifnum\themagicrownumber>2\relax\arabic{magicrownumbers}\fi\space}} | r}

    ? When I do that, I get "Undefined control sequence" errors.

    – David Doria Jul 07 '14 at 14:21
  • @FabianTamp: for the first (column in the first) line use \multicolumn{1}{r}{stuff} and it should not be numbered. –  Jul 07 '14 at 14:40
  • @FabianTamp just add \addtocounter{magicrownumbers}{-1}before the line of \newcommand this will start the number on second row. – nellix1 Jun 30 '15 at 03:52
  • @Herbert, can you please show more explicitly how to make numbering begin with the second row? Perhaps an example with two columns also? – littleO Sep 03 '15 at 22:30
  • 1
    @littleO: see my edited answer –  Sep 05 '15 at 07:10
  • @Herbert How to do this for attendance sheet? see https://tex.stackexchange.com/questions/409618/latex-tables-vertical-line-not-shown – alhelal Jan 18 '18 at 15:51
  • Is it possible to add a header for the numbers as well? By header, I mean the first row --- in your example, 'Something'; I want something like 'ID' above the numbers. – bp99 Dec 07 '18 at 18:37
  • sure, use \multicolumn{1}{c}{ID} –  Dec 07 '18 at 20:37
  • @Herbert thank you, but how exactly? Forgive my ignorance, but where do I put the multicolumn? I have a tabularx with columns like id | col1 | col2. So far, using the counter I had to put \rowcount in each row for the first column. I though I could improve this by removing the first column and using that @{ ... } thing (macro before each row??), but that way I won't be able to put the word id above the first column. Am I missing something? – bp99 Dec 08 '18 at 05:46
  • 1
    see edited anwer (at the end) –  Dec 08 '18 at 10:36
43

You could just make it use a counter...

\documentclass{article}
\newcounter{magicrownumbers}
\newcommand\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
\begin{document}
\begin{tabular}{l|r}
  \rownumber & Something \\
  \rownumber & Other stuff \\
  \rownumber & MAGIC!
\end{tabular}
\end{document}

enter image description here

alhelal
  • 2,451
Seamus
  • 73,242
2

The environment {NiceTabular} of nicematrix has its own built-in counter for the rows called iRow.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{>{\arabic{iRow}}c|c} & Something \ & Other stuff \ & MAGIC! \end{NiceTabular}

\end{document}

Output of the above code

F. Pantigny
  • 40,250
0

A quick plain-format version as well for completenes:

\newcount\rowcount
\def\myhalign#1{\halign{\global\advance\rowcount by 1 \the\rowcount\quad##&#1}\rowcount=0}
% Yes, yes, next up in *sniping*: "One problem: you can't use repeating preamble"...
\myhalign{#\hfil\cr
  blah\cr
  foo\cr}
\halign{#\hfil\cr
  bar\cr
  baz\cr}
\myhalign{&#\hfil\cr
  asdf&asdf&asdf\cr
  sdfg&asdf&dfgh\cr}
\myhalign{#\hfil&&\hfil#\cr
  doesn't&work\cr
  boo&hoo\cr}
\bye
morbusg
  • 25,490
  • 4
  • 81
  • 162
  • Two problems. 1. Every subsequent \halign will advance the counter even if it doesn't need it. 2. If a row shouldn't be numbered (a header), omitting \the\rowcount would not avoid stepping the counter. The stepping should go in the table preamble. – egreg Jun 21 '11 at 14:48
  • @egreg I wasn't aiming for a be-all-end-all solution; just an answer to the question. – morbusg Jun 21 '11 at 14:51
  • @egreg: You don't need the @<username>-notation when you're addressing your comment to the answerer/questioner. – morbusg Jun 21 '11 at 14:57