# Latex Counter custom reference formatting

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?

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

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}


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:

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