![如何为规范表中的每一行提供唯一的ID?](https://linux22.com/image/455046/%E5%A6%82%E4%BD%95%E4%B8%BA%E8%A7%84%E8%8C%83%E8%A1%A8%E4%B8%AD%E7%9A%84%E6%AF%8F%E4%B8%80%E8%A1%8C%E6%8F%90%E4%BE%9B%E5%94%AF%E4%B8%80%E7%9A%84ID%EF%BC%9F.png)
我想找到一种简单的方法来为表格中的每个规范行创建唯一的引用。引用的形式如下:
- 类别的字母
- 当前表格参考编号
- 每行增加一个值
以下示例有效,但很麻烦。我必须创建一个新的计数器并在每行上提及当前表的标题。相反,我希望有类似的东西:
\begin{table}
\stdcategory{S}
\caption{Description \label{label}}
\begin{tabular}{ll}
\stdref & Foo \\
\stdref & Bar \\
\end{tabular}
\end{table}
这个例子:
Figure \ref{specifications-directives} shows the hierarchy to be used with the identifiers. The plain arrow means “belongs
to” and the blank arrow means “refers to”. A deliverable is based on a task. Requirements and
specifications belong to a deliverable.
\begin{table}[H]
\centering
\newcounter{tablecounter}
\setcounter{tablecounter}{1}
\caption{Specification Directives \label{specifications-directives}}
\renewcommand*{\arraystretch}{1.8}
\begin{tabularx}{\textwidth}{|l|X|}\hline
\textbf{ID} & \textbf{Directive} \\ \hline
S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter & The requirements \textbf{shall} list “what” the system have to do \\ \hline
S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter & Each requirement specification \textbf{shall} have a unique identifier \\ \hline
S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter & Each unique identifier \textbf{shall} be considered once the final document is released \\ \hline
S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter & A document is released when it is formally approved by all involved parties \\ \hline
S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter & Entries are numbered with a prefix following table \ref{directives} \\ \hline
S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter & Only the verbal forms listed in table \ref{verbal-forms} \textbf{can} be used in requirements/specifications \\ \hline
S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter & A table \textbf{shall} contain only one prefix type entry \\ \hline
S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter & A table entry \textbf{shall} not exceed one line unless for sake of readability \\ \hline
S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter & Requirements and specifications \textbf{shall} always be given using a table of entries \\ \hline
S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter & Additional explanation \textbf{may} be added as standard text referring the entry number \\ \hline
\end{tabularx}
\end{table}
答案1
这里有一个解决方案,通过定义一个新的列类型(x
如下所示)来实现,它以前缀标签(比如说S
)作为其参数并将规范数字显示为<prefix label><table number>-<row number>
。
每次执行与表相关的指令时,跟踪行号的计数器(directcntr
在下面的代码中命名)都会自动重置;因此,请将语句放在\caption
\caption
顶部表的。如果您希望前缀为S
,则使用x{S}
。如果您希望前缀标签为 ,则在环境的列规范列表中Y
使用。x{Y}
tabularx
\label
指令可以使用标准 LaTeX -机制通过其花哨的行号进行交叉引用。下面的代码还提供了通过用户宏(例如)\ref
启用交叉引用所需的一些代码,\cref
聪明人包裹。
\documentclass{article}
\counterwithin{table}{section} % optional
%% Define a new counter and bind it to the 'table' counter:
\newcounter{directcntr}[table]
\usepackage{array} % for '\newcolumntype' macro
\usepackage{tabularx} % for 'tabularx' env. and 'X' col. type
\usepackage{ragged2e} % for '\RaggedRight' macro
\newcolumntype{L}{>{\RaggedRight}X}
%% Now define a new column type
\newcolumntype{x}[1]{%
>{\renewcommand\thedirectcntr{#1\thetable-\arabic{directcntr}}%
\refstepcounter{directcntr}\thedirectcntr} l }
%% Optional: Load cleveref package and inform it how to create
%% cross-referencing call-outs to directive-type items
\usepackage[noabbrev]{cleveref}
\crefname{directcntr}{directive}{directives}
\newcommand{\creflastconjunction}{, and~}
\begin{document}
\renewcommand\familydefault\sfdefault % optional
\sffamily % optional
\setcounter{section}{2} % just for this example
\setcounter{table}{2}
\begin{table}[h]
\setlength\extrarowheight{2pt} % for a less-cramped look
\caption{Specification Directives\strut}
\label{specifications-directives}
\begin{tabularx}{\textwidth}{| x{S} | L |} % <-- note 'x{S}'
\hline
\multicolumn{1}{|l|}{\textbf{ID}} & \textbf{Directive} \\
\hline
& The requirements \textbf{shall} list “what” the system have to do \\ \hline
& Each requirement specification \textbf{shall} have a unique identifier \\ \hline
\label{directive:unique}
& Each unique identifier \textbf{shall} be considered once the final document is released \\ \hline
& A document is released when it is formally approved by all involved parties \\ \hline
\label{directive:prefix}
& Entries are numbered with a prefix following table \ref{directives} \\ \hline
& Only the verbal forms listed in table \ref{verbal-forms} \textbf{can} be used in requirements\slash specifications \\ \hline
& A table \textbf{shall} contain only one prefix type entry \\ \hline
\label{directive:not_exceed}
& A table entry \textbf{shall} not exceed one line unless for sake of readability \\ \hline
& Requirements and specifications \textbf{shall} always be given using a table of entries \\ \hline
& Additional explanation \textbf{may} be added as standard text referring the entry number \\
\hline
\end{tabularx}
\end{table}
\noindent
Cross-references to \cref{directive:prefix,directive:not_exceed,directive:unique}.
\begin{table}[h]
\setlength\extrarowheight{2pt}
\caption{Yellow directives\strut}
\label{tbl:next}
\begin{tabularx}{\textwidth}{| x{Y} | L |} % <-- note 'x{Y}'
\hline
\multicolumn{1}{|l|}{\textbf{ID}} & \textbf{Directive} \\
\hline
\label{directive:y1} & \dots \\ \hline
\label{directive:y2} & \dots \\ \hline
\label{directive:y3} & \dots \\
\hline
\end{tabularx}
\end{table}
\noindent
Cross-references to \cref{directive:y3,directive:y2}.
\end{document}
答案2
以下定义了特殊环境directives
,它接受一个可选参数(key=value,其中键为start
和max
,start
指定第一个指令索引并max
定义具有最大宽度的索引),后跟两个强制参数(单字母前缀和\caption
)。环境主体应该是以 结尾的指令\\
。您可以\label
在指令描述中使用它来创建对此指令的引用。
我还使用了booktabs
规则并删除了垂直规则以获得更漂亮的结果。
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\makeatletter
\DeclareKeys[directives]
{%
max .store = \directives@max
,start .store = \directives@start
}
\SetKeys[directives]{max = 99, start = 1}
\newcounter{directivesrow}
\newdimen\directives@wd
\newcommand\directives@line
{%
\stepcounter{directivesrow}%
\makebox[\directives@wd][l]{\thedirectivesrow}%
\addtocounter{directivesrow}{-1}%
}
\NewDocumentEnvironment{directives} {O{} m m}
{%
\SetKeys[directives]{#1}%
\begin{table}
\caption{#3}%
\edef\thedirectivesrow{#2\thetable-\noexpand\arabic{directivesrow}}%
\setcounter{directivesrow}{\directives@max}%
\settowidth\directives@wd{\thedirectivesrow}%
\setcounter{directivesrow}{\numexpr\directives@start-1\relax}%
\begin{tabular}
{
@{\hskip\tabcolsep\directives@line\hskip2\tabcolsep}
>{\leavevmode\refstepcounter{directivesrow}}
p{\dimexpr\linewidth-4\tabcolsep-\directives@wd\relax}
}
\toprule
\multicolumn{1}{l}
{\makebox[\directives@wd][l]{ID}\hskip2\tabcolsep Directive}\\
\midrule
}
{%
\crcr
\bottomrule
\end{tabular}%
\end{table}
}
% to get the counter formatting of your example
\counterwithin{table}{section}
\setcounter{section}{2}
\setcounter{table}{2}
\begin{document}
\begin{directives}[start=2]{S}{Specification Directives\label{directives}}
\label{directive:shall}The requirements \textbf{shall} list “what” the system
have to do \\
Each requirement specification \textbf{shall} have a unique identifier \\
Each unique identifier \textbf{shall} be considered once the final document is
released \\
A document is released when it is formally approved by all involved parties \\
Entries are numbered with a prefix following table \ref{directives} \\
Only the verbal forms listed in table \ref{verbal-forms} \textbf{can} be used
in requirements/specifications \\
A table \textbf{shall} contain only one prefix type entry \\
A table entry \textbf{shall} not exceed one line unless for sake of
readability \\
Requirements and specifications \textbf{shall} always be given using a table
of entries \\
Additional explanation \textbf{may} be added as standard text referring the
entry number \\
\end{directives}
\begin{directives}[max=9]{V}{Verbal Directives\label{verbal-forms}}
random \\
things
\end{directives}
You can even reference directive~\ref{directive:shall}!
\end{document}
答案3
\documentclass{article}
\usepackage{tabularray}
\newcounter{tablecounter}
\setcounter{tablecounter}{1}
\newcommand{\mynum}{S\ref{specifications-directives}-\stepcounter{tablecounter}\thetablecounter}
\begin{document}
\figurename~\ref{specifications-directives} shows the hierarchy to be used with the identifiers.
The plain arrow means ``belongs to'' and the blank arrow means ``refers to''.
A deliverable is based on a task. Requirements and specifications belong to a deliverable.
\begin{table}
\caption{Specification Directives}
\label{specifications-directives}
\centering
\begin{tblr}{width=\textwidth,colspec={Q[l,m]X[l,m]},vlines,hlines,row{1}={font=\bfseries},row{1}={c},cell{2-Z}{1}={cmd=\mynum}}
ID & Directive \\
& The requirements \textbf{shall} list “what” the system have to do \\
& Each requirement specification \textbf{shall} have a unique identifier \\
& Each unique identifier \textbf{shall} be considered once the final document is released \\
& A document is released when it is formally approved by all involved parties \\
& Entries are numbered with a prefix following table \ref{directives} \\
& Only the verbal forms listed in table \ref{verbal-forms} \textbf{can} be used in requirements/specifications \\
& A table \textbf{shall} contain only one prefix type entry \\
& A table entry \textbf{shall} not exceed one line unless for sake of readability \\
& Requirements and specifications \textbf{shall} always be given using a table of entries \\
& Additional explanation \textbf{may} be added as standard text referring the entry number \\
\end{tblr}
\end{table}
\end{document}