对自定义列表中项目的引用未引用列表项

对自定义列表中项目的引用未引用列表项

我正在尝试定义一个自定义环境来显示需求。需求应有一个计数器,因此会自动计数 R1、R2、R2...(这有效)。我还想用标签引用每个需求(这不起作用,而是引用指向该部分)。

代码:

\newcounter{reqcount}
%\renewcommand\p@reqcount{R}
\renewcommand{\thereqcount}{\arabic{reqcount}}
\newcommand*{\labelreqcount}{\thereqcount}
\newenvironment{requirements}
 {
  \begin{list}
    {\refstepcounter{reqcount}\textbf{R\thereqcount}}
    {\setlength{\rightmargin}{\leftmargin}}}
 {\end{list}}

\begin{document}

\begin{requirements}
    \item test
    \item long text that needs two lines long text that needs two lines long text that needs two lines long text that needs two lines long text that needs two lines long text that needs two lines long text that needs two lines \label{test2}
\end{requirements}

\ref{test2}

\end{document}

实际结果

在预期结果中,5.3.1 应该是 R2(计数器)。

根据 Bernards 的回答进行编辑:子需求定义:

\newcounter{subreqcount}[reqcount]
\renewcommand\p@subreqcount{R}
\renewcommand\thesubreqcount{\thereqcount.\arabic{subreqcount}}
\newenvironment{subrequirements}%
  {
  \begin{list}
    {\refstepcounter{subreqcount}\textbf{R{\thesubreqcount}}}%
    {\setlength{\rightmargin}{\leftmargin}}%
    \setlength{\labelsep}{10pt}\setlength{\leftmargin}{35pt}%
    \setlength{\labelwidth}{40pt}\setlength{\listparindent}{0pt}}%
  {\end{list}} 

更新,因为对标签的引用不起作用

  • 一级列表中的引用工作正常
  • 二级列表中对要求的引用不起作用,始终引用 R1

示例(运行,但显示错误行为)

\documentclass{article}
\usepackage[utf8]{inputenc}


\usepackage{enumitem}
\newlist{requirements}{enumerate}{2}
\setlist[requirements,1]{wide=0pt, label=R\arabic*, widest=\textbf{R9}, font=\bfseries, leftmargin=*, ref=\textbf{R\arabic*}}
\setlist[requirements,2]{label*=.\arabic*, widest=\textbf{R.9.9}, font=\bfseries, ref=\textbf{R\arabic*}}


\begin{document}

\section{section 1}
\begin{requirements}
    \item test 
\end{requirements}

Some textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome textSome text

\section{section 2}
\begin{requirements}[resume]
    \item The system shall offer a User Interface for the management console. 
    \begin{requirements}
        \item The user interface may be customizable.\label{req-2.1}
        \item The user interface shall show relevant and  specific information in a clear way.
    \end{requirements}
    \item The management console shall be easy-to-use.
    \item All reporting functionalities shall be easy-to-use.
\end{requirements}


\section{section 3}
\begin{requirements}[resume]
    \item The system shall offer a User Interface for the management console.
    \item The management console shall be easy-to-use.
    \item All reporting functionalities shall be easy-to-use. \ref{req-2.1}
\end{requirements}

\end{document}

答案1

我建议使用此解决方案enumitem以及软件包cleveref来简化交叉引用的 yping:

\documentclass[11pt]{report}
\usepackage{enumitem}
\newlist{requirements}{enumerate}{1}
\setlist[requirements,1]{wide=0pt, label=R\arabic*, widest=\textbf{R9}, font=\bfseries, leftmargin=*, ref=\textbf{R\arabic*}}
\usepackage{cleveref}
\crefname{requirementsi}{requirement}{requirements}
\Crefname{requirementsi}{Requirement}{Requirements}

\begin{document}
\setcounter{section}{3}

\begin{requirements}
    \item A minimalistic requirement. \label{test1}
    \item Long text that needs two lines long text that needs two lines, long text that needs two lines, long text that needs two lines. \label{test2}
\end{requirements}

\Cref{test2,test1}

\end{document}

在此处输入图片描述

相关内容