41

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
28
\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
  • 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 – user2478 Jun 30 '13 at 7: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 7 '14 at 14:21
  • 1
    @littleO: see my edited answer – user2478 Sep 5 '15 at 7:10
  • 1
    see edited anwer (at the end) – user2478 Dec 8 '18 at 10:36
31

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

  • Is there any frendly way to reset the counter between tables? – miah Jun 21 '11 at 11:56
  • 4
    @miah: Load etoolbox and say \preto\tabular{\setcounter{magicrownumbers}{0}} – egreg Jun 21 '11 at 11:58
  • 5
    @egreg I put that before my second table but the counter did not reset. I had to use simply \setcounter{magicrownumbers}{0} instead – David Doria Jul 7 '14 at 14:27
  • @DavidDoria That code should go in the preamble. – egreg Jul 7 '14 at 14:29
  • @egreg Hm, even with that in the preamble, the row numbers do not reset for me unless I clear the counter manually as I explained. – David Doria Jul 7 '14 at 15:05
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
  • 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

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.