# Use a nested enumeration as a column of a table

I am creating a list of requirements in a table in which the first column should be an enumeration, such that each requirement is identified by a unique number. Some requirements also have sub requirements, so I need nested enumeration.

An example:

+-----+-----------------------------+
| ID  |         Requirement         |
+-----+-----------------------------+
| 1   | Requirement a               |
| 2   | Requirement b               |
| 2.1 | Some requirement of b       |
| 2.2 | Some other requirement of b |
| 3   | Another requirement         |
+-----+-----------------------------+


The only solution I could come up with was the following (obviously not working, even without nesting):

\begin{tabularx}{\textwidth}{|X|X|}
\begin{enumerate}
\hline
\item & Requirement a \\ \hline
\item & Requirement b \\ \hline
\end{enumerate}
\end{tabularx}


Since this example without nesting doesn't work, I assume that a similar example with nesting wouldn't work as well. Other approaches for adding enumeration to a tabular which I found on SE such as this one don't extend to nested enumerations as well.

How can I achieve such an enumeration?

How can I achieve such a thing?

A somewhat involved way to do it:

\documentclass{report}

\usepackage{tabularx}

\newcounter{rowItemCount}%
\newcounter{subRowItemCount}%
\newcommand\rowItem{
\setcounter{subRowItemCount}{0}
\arabic{rowItemCount}}
\newcommand\subRowItem{
\arabic{rowItemCount}.\arabic{subRowItemCount}}

\begin{document}
\begin{tabularx}{\textwidth}{|X|X|}
\hline
\rowItem{} & Requirement a \\ \hline
\rowItem{} & Requirement b \\ \hline
\subRowItem{} & Requirement b.1 \\ \hline
\subRowItem{} & Requirement b.2 \\ \hline
\rowItem{} & Requirement c \\ \hline
\subRowItem{} & Requirement c.1 \\ \hline
\end{tabularx}
\end{document}


You could change the arabic to alph (or Alph) if you wanted letters instead of numbers. And would not extend to deeper nesting without adding extra commands.

Is there a particular reason that this needs to be in a table?

EDIT

with these added, the row numbers will be reset with each tabularx environment, as suggested below:

\newcommand\resetRowItemCounters{
\setcounter{rowItemCount}{0}}
\AtBeginEnvironment{tabularx}{\resetRowItemCounters}

• Thanks for your answer, I really like it! The reason that I need a table is that I have a few more columns than just the 2 described in the question. Since I have more than 2 columns I can't use a normal enumeration (as far as I know). Therefore it seems most logical to me to use a table structure. – Tiddo May 27 '13 at 18:28
• A few comments to clean up your code: You need the etoolbox package for \AtBeginEnvironment. Also, if you use \refstepcounter instead of \addtocounter you use \labels and then \ref to refer to them. It's also probably better to use \therowItemCount and \thesubRowItemCount (redefined as needed) rather than hard coding the numbering in the item commands. – Alan Munn May 27 '13 at 20:12
• @AlanMunn I can see the benefits of your solution. Good to learn new things. – Chris May 27 '13 at 21:13

Here's a version using a couple of counters (add more for more levels) and the array package. I've created two commands to nest and unnest the levels of the counters. There may be a more elegant way to do this, but this works quite intuitively.

\documentclass{article}

\usepackage{array}
\usepackage{tabularx}
\usepackage{etoolbox}
% two counters for the nesting level; add more as needed
% counters are of the form tabl<num> where <num> is roman
\newcounter{tabli}
\newcounter{tablii}[tabli]
% counter to keep track of the level
\newcounter{counterlevel}
% command to reset the counters for multiple tables
\newcommand{\resettablcounters}
{\setcounter{counterlevel}{1}
\setcounter{tabli}{0}
\setcounter{tablii}{0}}
% reset counters each tabularx environment
\AtBeginEnvironment{tabularx}{\resettablcounters}
% define the dependent counters display
\renewcommand{\thetablii}{\thetabli.\arabic{tablii}}
% two commands to nest and unnest the levels
% the \nest command must go at the beginning of the row that will
% have nested dependents (not on the first dependent row)
% the \unnest command goes as the beginning of the last dependent
% command to step and display the level to be executed at each row.
\newcommand{\displaylevel}
{\refstepcounter{tabl\roman{counterlevel}}\csname thetabl\roman{counterlevel}\endcsname}
% create an enumerated column type
\newcolumntype{E}{>{\displaylevel}l}

\begin{document}

\begin{tabularx}{\textwidth}{|E|X|}
\hline
\multicolumn{1}{|c|}{ID}  & Requirement \\
\hline
& This is a requirement. \\
\nest & This is a requirement. \\
& This is a sub requirement \\
\unnest & And yet another\\
\nest & This is no longer a sub requirement\\
\unnest & But this one is.\\
& And another requirement\\
\hline
\end{tabularx}

% another table to show that counters reset properly

\begin{tabularx}{\textwidth}{|E|X|}
\hline
\multicolumn{1}{|c|}{ID}  & Requirement \\
\hline
& This is a requirement. \\
\nest & This is a requirement. \\
& This is a sub requirement \\
\unnest & And yet another\\
\nest & This is no longer a sub requirement\\
\unnest & But this one is.\\
& And another requirement\\
\hline
\end{tabularx}

\end{document}


• Thanks for your answer! I really like this one as well! However, I can only chose one answer as accepted answer, and I like the solution of Chris just a little better so I'm going to accept his solution. Thanks anyway, it's a really great solution! – Tiddo May 27 '13 at 18:32
• Just be careful with Chris's solution, you'll need to reset the counters each table too if you use it multiple times. Also if you want to refer to any of the items with \ref and \label you'll need to use \refstepcounter instead of just \addtocounter. – Alan Munn May 27 '13 at 18:38