1

I am trying to create two custom commands, that create subsections with labels and custom numbering. But when I use \nameref to get the title of the reference, it expands \decimal{reqCounter} again so that the numbering does not fit to the label anymore.

\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}

Compiled MWE

In this example, the text after "Covers:" should show "RQ01: Timer" and not "RQ02: Timer". This tells me, that tex is expanding that subsection title again, so that the current counter value is used, instead of just displaying the constructed subsection title. Can you tell me, what is the correct way of doing such a thing?

  • Welcome to TeX.SX! – Bobyandbob Mar 7 '18 at 9:07
  • 1
    Perhaps you should explain more what you are trying to do here. I do not understand your macros here. You use \section* and \refstepcounter after the label, so what exactly are you expecting req:timer to actually return. If I move the two \refstepcounter's up above the \section*'s and let the counter start from zero, I seem to get what you are look for? – daleif Mar 7 '18 at 9:20
  • 1
    And please document your macro arguments! #1 #2 and #3 do not in themselves give any indication what they are used for. – John Kormylo Mar 7 '18 at 15:27
  • @daleif: The shifting of the \refstepcounter does solve the numbering problem only for a short 'moment', since the content of \decimal{reqCounter} is written unexpanded to the .aux file, so having more than one those \requirement macros, \nameref will use the current counter value and not the one being valid at the time of \requirement usage. (the same holds for the testcase macro usage) – user31729 Mar 8 '18 at 7:46
  • @daleif: It was my first idea as well, then I saw your comment and tried it and it did not work in sense of 'providing the correct reference value'. I suspected the unexpanded storage then and checked the .aux file. The 4th argument of \newlabel has \decimal{reqCounter} inside, i.e. unexpanded – user31729 Mar 8 '18 at 7:52
2

There are two issues here:

  1. \refstepcounter is used too late, especially it's done after \label, which is useless
  2. \nameref uses the 4th argument of the \newlabel content written to the .aux file. However, this is an unexpanded \decimal{reqCounter}, so at the time of \nameref usage, it will grab the current value of reqCounter and not the expanded ('frozen') one.

A possible solution for both is to shift the \refstepcounter{...}\label{...} macros upward and explicitly define an expanded \@currentlabelname which is written to the .aux file instead of the unexpanded \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}

enter image description here

  • Even shorter, but possibly errorprone: \protected@edef\@currentlabelname{\@currentlabelname}\label{...} after the \subsection usage – user31729 Mar 8 '18 at 7:48
  • This does the trick. Thanks (I still need to learn a lot about the tex backend) – Florin Hillebrand Mar 9 '18 at 12:13
  • @FlorinHillebrand: You're welcome. Happy TeXing! – user31729 Mar 9 '18 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.