# Have reference in section with custom counter

I have some "items" that I want to count and reference throughout the document. These items are usually equations. I also have different types of "items" that I want to differentiate via a prefix on the labels.

E.g. for "foo" items I can choose an "F" prefix on the corresponding label.

I want to count them in two ways:

• in custom enumerate environments, where the counter is automatically increased by the \item command.

• in sections, or other similar places, where I add a reference to the item besides the section's text

In both use cases I want to have a label defined for cross-reference. Also, the labels should have a prefix to help understand which types of items are referenced.

Here is an MVE

\documentclass[en,12pt]{article}
\usepackage{ifthen}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{prettyref}

% Define a new set of "things" to be counted
\newcommand{\DefEnumStuff}[3]{%
\newcounter{count#2}%
\setcounter{count#2}{0}%
% add a new enumerate command
\newenvironment{#1}%
{\let\olditem\item%
\begin{enumerate}[label={#3\arabic*}., ref={#3\arabic*}]%
\setcounter{enumi}{\value{count#2}}%
\renewcommand{\item}[1][]{%
\refstepcounter{count#2} \olditem%
\ifthenelse{ \equal{####1}{} }{}{\label{####1}}%
}%
}%
{\end{enumerate}}%
% add command to increase the counter outside the enumerate environment and define a label
\expandafter\newcommand\csname refstep#2\endcsname[1] {{\refstepcounter{count#2}\label{##1}}}%
}%
\newrefformat{foo}{F\ref{#1}}%

% define "foo" items to be counted.
% Have enumerate-like environment named "foo"
% and command "\refstepfoo" for use cases 1 and 2
\DefEnumStuff{foo}{foo}{F}

\begin{document}

\begin{foo}
\item[foo:A] foo A
\item[foo:B] foo B
\end{foo}

\begin{foo}
\item C
\end{foo}
As seen in \ref{foo:A}

\refstepfoo{foo:D}
\section*{Example (\ref{foo:D})} % expected Example F4
\end{document}


On my system I get the following result

It seems to me it works, except that I'd like the reference in the "Example" section to read as "Example (F4)" instead of "Example (4)".

Could you help me achieve this? Further suggestions on the overall idea are welcome!

TIA

The issue here is that you are not using prettyref properly. When you define a \newrefformat, you need to use \prettyref to actually use it. \ref continues to work as usual -- it is not overridden. That's why you only see 4 instead of F4 in your section.

But when you replace \ref with \prettyref everywhere, you would notice that the reference F1 would now be printed as FF1 since \prettyref adds an additional F in front of the F prefix you already set within the enumerate's ref field. So you need to remove that.

EDIT: Additionally, if you want to hyperlink F4 instead of just 4 being hyperlinked, then you would have to manually invoke \hyperref:

\newrefformat{foo}{\hyperref[{#1}]{F\ref*{#1}}}


I use \ref* to suppress just 4 being hyperlinked, and then uses \hyperref to hyperlink F4.

The final working example is:
(I also simplified having to type the foo:X labels for items to just typing X, since you are already inside foo environment.)

\documentclass[en,12pt]{article}

\usepackage{ifthen}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{prettyref}

% Define a new set of "things" to be counted
\newcommand{\DefEnumStuff}[3]{%
\newcounter{count#2}%
\setcounter{count#2}{0}%
% add a new enumerate command
\newenvironment{#1}%
{\let\olditem\item%
\begin{enumerate}[label={#3\arabic*}., ref={\arabic*}]%
\setcounter{enumi}{\value{count#2}}%
\renewcommand{\item}[1][]{%
\refstepcounter{count#2}\olditem%
\ifthenelse{ \equal{####1}{} }{}{\label{#2:####1}}%
}%
}%
{\end{enumerate}}%
% add command to increase the counter outside the enumerate environment and define a label
\expandafter\newcommand\csname refstep#2\endcsname[1] {%
{\refstepcounter{count#2}\label{#2:##1}}%
}%
}
\newrefformat{foo}{\hyperref[{#1}]{F\ref*{#1}}}

% define "foo" items to be counted.
% Have enumerate-like environment named "foo"
% and command "\refstepfoo" for use cases 1 and 2
\DefEnumStuff{foo}{foo}{F}

\begin{document}

\begin{foo}
\item[A] foo A
\item[B] foo B
\end{foo}

\begin{foo}
\item C
\end{foo}
As seen in \prettyref{foo:A}

\refstepfoo{D}

\section*{Example (\prettyref{foo:D})} % expected Example F4

\end{document}


• It's a 1920x1200 image. You just need to click on it :-) Sep 9, 2019 at 22:05
• Thanks! Is there a way to extend the link to the prefix, e.g. have F4 in the red square instead of just 4? Sep 10, 2019 at 8:04
• Yes, it's a very simple tweak. Updated my answer. Sep 10, 2019 at 9:09