I want to automatically enumerate the text rows of a table. Is there an option within tabular, or another package? (Of course I do not want the rules to be enumerated.)
Asked
Active
Viewed 8,818 times
38
-
Duplicate question: Automatic table row numbers – koppor Oct 20 '15 at 13:02
1 Answers
40
First define your own counter -- in this case rowcount. Now you can use the the declaration by @{} to print out the counter:
Here the code:
\documentclass{article}
\usepackage{array}
\newcounter{rowcount}
\setcounter{rowcount}{0}
\begin{document}
\begin{tabular}{@{\stepcounter{rowcount}\therowcount.)\hspace*{\tabcolsep}}ll}
foo & bar\\
foo & bar\\
foo & bar\\
foo & bar\\
foo & bar\\
foo & bar\\
\end{tabular}
\end{document}

EDIT
The alignment of the row numbers can be down by adding a \makebox with a specific with and adjustment.
\makebox[3em][r]{\therowcount.)}
In this case the number rowcount will be printed in a box with a width of 3em and right aligned.

Colored Background
To get a colored background you can use packages like tcolorbox, framed, mdframed or adjustbox
Here an example with adjustbox. Unfortunately there is no fontcolor option.
In the newest of of adjustbox the author Martin Scharrer provided a new key fgcolor to setup the font color of the environment.
https://bitbucket.org/martin_scharrer/adjustbox/changeset/943f7cb95271
\documentclass{article}
\usepackage{array}
\usepackage{adjustbox}
\newcounter{rowcount}
\setcounter{rowcount}{0}
\usepackage[framemethod=tikz]{mdframed}
\begin{document}
\adjustbox{bgcolor=black, tabular=@{\stepcounter{rowcount}\makebox[3em][r]{\color{white}\therowcount.)}\hspace*{\tabcolsep}}>{\color{white}}l>{\color{white}}l}{%
foo & bar\\
foo & bar\\
foo & bar\\
foo & bar\\
foo & bar\\
foo & bar\\
}
\end{document}

David Carlisle
- 757,742
Marco Daniel
- 95,681
-
-
-
1
-
1@Forgiver: Therefor you can use the package
totcount. See also: http://tex.stackexchange.com/questions/58221/amsthm-theorems-lemmas-numbered-backwards/58240#58240 – Marco Daniel Jun 03 '12 at 13:41 -
How can you skip a row count so that it starts, lets say in the third row? – azetina Jul 02 '12 at 14:28
-
-
How can we set the enumerations to count in Roman numerals (e.g., i, ii, ...) instead of digits (e.g., 1, 2, ...)? – Chernoff Oct 03 '12 at 23:59
-
How to use \setcounter to skip the first row? I tried \setcounter{1} but it can only change the number of the first row to 2. – Romstar Nov 18 '12 at 16:39
-
2@user21916: Put
\multicolumn{1}{>{\makebox[3em][r]{}}l}{foo} & bar\\in the first row. – Nov 18 '12 at 23:52 -
1@Chernoff you can use
\roman{\therowcount}to get lowercase roman numerals. Other options are\Roman{}for upper case roman numeral (I, II, ...),\alph{}for lower case letters (a, b, ...) and\Alph{}. Default is\arabic{}. – mythealias Dec 08 '12 at 19:52 -
2What if there is a header, and I only want to show the number as of the next line, and in header show the column title, like # ?! – Ivan Machado Oct 31 '13 at 21:20
-
1@MarcoDaniel Daniel I have a \hline between all of my rows. This also gets a number using this method. Is there a way to make the numbering skip rows like this? – David Doria Jul 07 '14 at 14:11
-
1To be able referencing a row, one needs to use
\refstepcounterinstead of\stepcounter. See Reference table rows by automatic counter. – koppor Oct 20 '15 at 14:03