我的目标是在文档末尾构建一个自动可追溯性矩阵。为此,我定义了所有需要首先验证的需求,以及之后在不同部分中测试的需求。
该代码基于这个问题但我想通过自动生成标签名称来简化这一过程,为此,我想使用计数器。
这是代码:
\documentclass{article}
\newcommand{\requirements}{}
\providecommand*\phantomsection{}
\makeatletter
\newcommand{\req}[1]{%
\textbf{R#1}%
\phantomsection
\def\@currentlabel{R#1}%
\label{req@#1}%
\g@addto@macro\requirements{{req@#1}}%
\global\@namedef{req@#1@ismetby}{}%
}
\newcommand{\meetsreq}[1]{%
\ref{req@#1}%
\expandafter\g@addto@macro\csname req@#1@ismetby\expandafter\endcsname
\expandafter {\expandafter{\@currentspec}}%
}
\newcommand{\specswithreq}[1]%
% The space before \ref below is intentional and will be swallowed by \xintApply
% It is not mandatory however, the thing works without it too.
{\xintListWithSep{, }{\xintApply { \ref}{\csname #1@ismetby\endcsname }}}
\newcommand{\spec}[1]{\label{spec@#1}\gdef\@currentspec{spec@#1}}
% (update Jan 5, to use \gdef rather than \def in \spec, allowing more flexible usage; has its pros and cons)
\makeatother
\usepackage{xinttools}
\usepackage{hyperref}% check if ok with hyperlinks
\hypersetup{colorlinks=true}
\newcounter{argcnt}\setcounter{argcnt}{0}
\begin{document}
\section{Requirement LIST}
[\req{R1}] requirement 1
[\req{R2}] requirement 2
[\req{R3}] requirement 3
[\req{R4}] requirement 4
[\req{R5}] requirement 5
[\req{R6}] requirement 6
\section{section1}
\setcounter{argcnt}{1}
\spec{1} Test 1 covert : \meetsreq{R1} \meetsreq{R2}
\section{section2}
\setcounter{argcnt}{2}
\spec{\theargcnt} Test 2 covert : \meetsreq{R4} \meetsreq{R2}
\section{section3}
\setcounter{argcnt}{3}
\spec{3} Test 3 covert : \meetsreq{R6}
\section{section4}
\setcounter{argcnt}{4}
\spec{4} Test 4 covert : \meetsreq{R2}, \meetsreq{R3}
\section{Tracability MAtrix}
\begin{table}[htbp]
\begin{tabular}{|l|l|}
\hline
Requirement & Specification \\
\hline
\xintFor* #1 in \requirements\do {\ref{#1}&\specswithreq{#1}\\
\hline }%
\end{tabular}
\end{table}
\end{document}
就像您在图片中看到的那样,计数器函数的生成不起作用。矩阵没有将正确的测试与良好的需求联系起来。
答案1
\newcommand{\spec}[1]{\label{spec@#1}\def\@currentspec{spec@#1}}
将使用参数#1
,在本例中为。这里的求值太晚了,你需要在应用的那一刻就得到\theargcount
的值(或展开) ,即使用。\theargcount
\spec
\xdef\@currentspec{spec@#1}
\documentclass{article}
\newcommand{\requirements}{}
\makeatletter
\newcommand{\req}[1]{%
\textbf{R#1}%
\phantomsection
\def\@currentlabel{R#1}%
\label{req@#1}%
\g@addto@macro\requirements{{req@#1}}%
\global\@namedef{req@#1@ismetby}{}%
}
\newcommand{\meetsreq}[1]{%
\ref{req@#1}%
\expandafter\g@addto@macro\csname req@#1@ismetby\expandafter\endcsname
\expandafter {\expandafter{\@currentspec}}%
}
\newcommand{\specswithreq}[1]%
% The space before \ref below is intentional and will be swallowed by \xintApply
% It is not mandatory however, the thing works without it too.
{\xintListWithSep{, }{\xintApply { \ref}{\csname #1@ismetby\endcsname }}}
\newcommand{\spec}[1]{\label{spec@#1}\xdef\@currentspec{spec@#1}}
% (update Jan 5, to use \gdef rather than \def in \spec, allowing more flexible usage; has its pros and cons)
\makeatother
\usepackage{xinttools}
\usepackage{hyperref}% check if ok with hyperlinks
\hypersetup{colorlinks=true}
\newcounter{argcnt}\setcounter{argcnt}{0}
\begin{document}
\section{Requirement LIST}
[\req{R1}] requirement 1
[\req{R2}] requirement 2
[\req{R3}] requirement 3
[\req{R4}] requirement 4
[\req{R5}] requirement 5
[\req{R6}] requirement 6
\section{section1}
\setcounter{argcnt}{1}
\spec{1} Test 1 covert : \meetsreq{R1} \meetsreq{R2}
\section{section2}
\setcounter{argcnt}{2}
\spec{\theargcnt} Test 2 covert : \meetsreq{R4} \meetsreq{R2}
\section{section3}
\setcounter{argcnt}{3}
\spec{3} Test 3 covert : \meetsreq{R6}
\section{section4}
\setcounter{argcnt}{4}
\spec{4} Test 4 covert : \meetsreq{R2}, \meetsreq{R3}
\clearpage
\section{Tracability MAtrix}
\begin{table}[htbp]
\begin{tabular}{|l|l|}
\hline
Requirement & Specification \\
\hline
\xintFor* #1 in \requirements\do {\ref{#1}&\specswithreq{#1}\\
\hline }%
\end{tabular}
\end{table}
\end{document}