自定义命令再次扩展计数器

自定义命令再次扩展计数器

我正在尝试创建两个自定义命令,它们创建带有标签和自定义编号的子节。但是当我使用它们\nameref获取参考文献的标题时,它又会扩展,\decimal{reqCounter}以至于编号不再适合标签。

\documentclass{memoir}

\usepackage{fmtcount}
\usepackage{hyperref}

% Define requirement command
\newcounter{reqCounter}
\setcounter{reqCounter}{1}
\newcommand{\requirement}[3] {
  \subsection*{RQ\padzeroes[2]{\decimal{reqCounter}}: #2}
  \label{req:#1}
  \refstepcounter{reqCounter}
#3
}

% Define testcase command
\newcounter{tcCounter}
\setcounter{tcCounter}{1}
\newcommand{\testcase}[5] {
  \subsection*{TC\padzeroes[2]{\decimal{tcCounter}}: #3}
  \label{tc:#1}
  \refstepcounter{tcCounter}
#4

#5

Covers: \nameref{#2}
}

\begin{document}


\requirement{timer}
  {Timer}
  {
    It shall be possible to use the system timer for task scheduling purposes.
  }

\testcase{timer-simple}{req:timer}
  {Timer simple}
  {
    Enable the system timer and enable/disable a LED in a 60Hz interval.
  }
  {
    Verify the frequency with an oscilloscope or a digital signal analyser.
  }

\end{document}

编译的 MWE

在此示例中,“Covers:”后面的文本应显示“RQ01: Timer”,而不是“RQ02: Timer”。这告诉我,tex 正在再次扩展该子节标题,以便使用当前计数器值,而不是仅显示构造的子节标题。你能告诉我,做这样的事情的正确方法是什么吗?

答案1

这里有两个问题:

  1. \refstepcounter使用得太晚,尤其是之后\label,这是没用的
  2. \nameref使用写入文件4th的内容作为参数。但是,这是一个未展开的,因此在使用时,它将获取 的当前值,而不是展开的(“冻结”)值。\newlabel.aux\decimal{reqCounter}\namerefreqCounter

对于两者来说,一个可能的解决方案是将\refstepcounter{...}\label{...}宏向上移动,并明确定义一个扩展的\@currentlabelname,将其写入.aux文件而不是未扩展的\decimal{reqCounter}


\documentclass{memoir}
\usepackage{fmtcount}
\usepackage{hyperref}

% Define requirement command
\newcounter{reqCounter}
\newcounter{tcCounter}

\makeatletter
\newcommand{\requirement}[3]{%
  \begingroup
  \refstepcounter{reqCounter}%
  \protected@edef\@currentlabelname{RQ\padzeroes[2]{\decimal{reqCounter}}: #2}
  \label{req:#1}
  \subsection*{RQ\padzeroes[2]{\decimal{reqCounter}}: #2}%
  \endgroup
#3
}

% Define testcase command
\newcommand{\testcase}[5]{%
  \refstepcounter{tcCounter}%
  \protected@edef\@currentlabelname{TC\padzeroes[2]{\decimal{tcCounter}}: #3}
  \label{tc:#1}
  \subsection*{TC\padzeroes[2]{\decimal{tcCounter}}: #3}
  #4%

  #5%

  Covers: \nameref{#2}%
}
\makeatother


\begin{document}


\requirement{timer}
  {Timer}
  {
    It shall be possible to use the system timer for task scheduling purposes. See \nameref{tc:timer-simple}
  }


\requirement{othertimer}
  {Other Timer}
  {
    It shall be possible to use the system timer for task scheduling purposes. See \nameref{tc:othertimer-simple}
  }



\testcase{timer-simple}{req:timer}
{Timer simple}
{
  Enable the system timer and enable/disable a LED in a 60Hz interval.
}
{
  Verify the frequency with an oscilloscope or a digital signal analyser.
}

\testcase{othertimer-simple}{req:othertimer}
{Other timer simple}
{
  Enable the system timer and enable/disable a LED in a 60Hz interval.
}
{
  Verify the frequency with an oscilloscope or a digital signal analyser.
}


\end{document}

在此处输入图片描述

相关内容