0

Simple use case, I have a counter for requirements, R1, R2, etc like the one below:

\newcounter{requirementcounter}
\newcommand{\requirement}{\refstepcounter{requirementcounter}R.\therequirementcounter}

However, when I refer to a counter instance (i.e. \requirement{req:a} by \ref{req:a}) then the \ref{} will only contain the counter value, rather than the custom label in the format of R.value

How can I format the counter reference label?

1
  • Welcome to TeX.SE. Does your code provide a \label instruction for req:a?
    – Mico
    Jun 25, 2020 at 12:24

2 Answers 2

3

It is sufficient to redefine macro \the<counter>. Then the \label/\ref system will pick it up automatically:

\newcounter{requirementcounter}
\renewcommand*{\therequirementcounter}{R\arabic{requirementcounter}}

...
\refstepcounter{requirementcounter}\therequirementcounter
\label{req:a}
...
\ref{req:a}
0

Here's a small snippet that can do this:

\newcounter{reqcount}
\newcommand{\reqitem}[1]{%
  R\ref*{#1} \refstepcounter{reqcount}\label{#1}
}
\newcommand{\descref}[1]{%
  \hyperref[#1]{R\ref*{#1}}%
}

Example usage:

Here is req: \reqitem{reqi:a}

Here is req: \reqitem{reqi:b}

Here is reference: \descref{reqi:a}, \descref{reqi:a}, \descref{reqi:b}

Result:

enter image description here

1
  • What are you trying to demonstrate with the duplicate \descref{reqi:a} directives?
    – Mico
    Jun 25, 2020 at 12:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .