# hyperlink an \item and \ref

I have a custom environment which is just an enumerate with the \item written for me. I would like the enumeration label to be a hyperlink- in the MWE below, it simply links to a page at the end of the document.

When referencing the environment using \ref the printed version will look fine. However, you'll notice that the hyperlink created by \ref{testref} goes to the same link that the enumeration label goes to, and not to the environment that it references.

What needs to be changed to get the hyperlinks to behave as expected?

\documentclass{article}

\usepackage{lipsum}     % sample text
\usepackage{enumitem}   % enumerations

\newcounter{problem}
\newcounter{subproblem}
\newenvironment{problem}{\setcounter{subproblem}{0}\refstepcounter{problem}{\bfseries Problem \theproblem}}{}
\newenvironment{subproblem}{%
\stepcounter{subproblem}%
\item}{\end{enumerate}}

\begin{document}

\begin{problem}
\begin{subproblem}
\lipsum[1]
\end{subproblem}
\begin{subproblem}\label{testref}
\lipsum[1]
\end{subproblem}
\end{problem}

Test reference: \ref{testref}

\begin{problem}
\begin{subproblem}
\lipsum[1]
\end{subproblem}
\begin{subproblem}
\lipsum[1]
\end{subproblem}
\end{problem}

\newpage

\end{document}

-
Have you tried using \refstepcounter instead of \stepcounter? –  Mico Nov 14 '11 at 4:40
@Mico Tried that already, no difference. I'm not convinced that the environment is setup correctly- I think it may be more appropriate to use the resume feature from enumitem, but I haven't been able to get it working correctly. –  cmhughes Nov 14 '11 at 4:42

Here are some corrections incorporated in your MWE. They include:

• Using a master counter to reset your subproblem at every increment or problem:

\newcounter{subproblem}[problem]


This removed the requirement for setting the subproblem counter to 0 every time (\setcounter{subproblem}{0}) at the start of the problem environment.

• Adding a referenced stepping of the subproblem counter using

\refstepcounter{subproblem}

• Explicitly stating the reference included with the enumerate environment, even though it is similar to the label to avoid the label from being considered the same as the ref. This corrected the incorrect referencing to elements:

\begin{enumerate}%
ref=\theproblem.\thesubproblem,% reference
leftmargin=*]% left margin


\documentclass{article}

\usepackage{lipsum}     % sample text
\usepackage{enumitem}   % enumerations

\newcounter{problem}
\newcounter{subproblem}[problem]
\newenvironment{problem}{\refstepcounter{problem}{\bfseries Problem~\theproblem}}{}
\newenvironment{subproblem}{%
\refstepcounter{subproblem}%
\item}{\end{enumerate}}

\begin{document}

\begin{problem}
\begin{subproblem}
\lipsum[1]
\end{subproblem}
\begin{subproblem}\label{testref}
\lipsum[1]
\end{subproblem}
\end{problem}

Test reference: \ref{testref}

\begin{problem}
\begin{subproblem}
\lipsum[1]
\end{subproblem}
\begin{subproblem}
\lipsum[1]
\end{subproblem}
\end{problem}

\newpage