我正在表格中创建需求列表,其中第一列应为枚举,以便每个需求都由唯一编号标识。有些需求还有子需求,所以我需要嵌套枚举。
一个例子:
+-----+-----------------------------+
| ID | Requirement |
+-----+-----------------------------+
| 1 | Requirement a |
| 2 | Requirement b |
| 2.1 | Some requirement of b |
| 2.2 | Some other requirement of b |
| 3 | Another requirement |
+-----+-----------------------------+
我能想到的唯一解决方案如下(显然不起作用,即使没有嵌套):
\begin{tabularx}{\textwidth}{|X|X|}
\begin{enumerate}
\hline
\item & Requirement a \\ \hline
\item & Requirement b \\ \hline
\end{enumerate}
\end{tabularx}
由于这个没有嵌套的示例不起作用,我认为带有嵌套的类似示例也不会起作用。我在 SE 上找到了其他向表格添加枚举的方法,例如这个不要扩展到嵌套枚举。
我怎样才能实现这样的枚举?
我怎样才能实现这样的目标?
答案1
一种稍微复杂的方法:
\documentclass{report}
\usepackage{tabularx}
\newcounter{rowItemCount}%
\newcounter{subRowItemCount}%
\newcommand\rowItem{
\setcounter{subRowItemCount}{0}
\addtocounter{rowItemCount}{1}
\arabic{rowItemCount}}
\newcommand\subRowItem{
\addtocounter{subRowItemCount}{1}
\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}
如果您想要使用字母而不是数字,可以将 改为arabic
(alph
或)。如果不添加额外命令,则无法扩展到更深的嵌套。Alph
是否有特殊原因需要将其放在表格中?
编辑
添加这些内容后,行号将根据每个tabularx
环境重置,如下所示:
\newcommand\resetRowItemCounters{
\setcounter{rowItemCount}{0}}
\AtBeginEnvironment{tabularx}{\resetRowItemCounters}
答案2
这是使用几个计数器(添加更多计数器以增加更多级别)和array
包的版本。我创建了两个命令来嵌套和取消嵌套计数器的级别。可能有更优雅的方法来做到这一点,但这非常直观。
\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
\newcommand{\nest}{\addtocounter{counterlevel}{1}}
\newcommand{\unnest}{\addtocounter{counterlevel}{-1}}
% 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}