Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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
\usepackage{hyperref}   % hyperlinks        

\newcounter{problem}
\newcounter{subproblem}
\newenvironment{problem}{\setcounter{subproblem}{0}\refstepcounter{problem}{\bfseries Problem \theproblem}}{}
\newenvironment{subproblem}{%
    \stepcounter{subproblem}%
    \begin{enumerate}[label=\protect\hyperlink{newpage}{\theproblem.\thesubproblem},leftmargin=*]
     \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

\hypertarget{newpage}{We'll link to this}

\end{document}
share|improve this question
    
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
add comment

1 Answer 1

up vote 6 down vote accepted

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}%
      [label=\protect\hyperlink{newpage}{\theproblem.\thesubproblem},% label
       ref=\theproblem.\thesubproblem,% reference
       leftmargin=*]% left margin
    

\documentclass{article}

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

\newcounter{problem}
\newcounter{subproblem}[problem]
\newenvironment{problem}{\refstepcounter{problem}{\bfseries Problem~\theproblem}}{}
\newenvironment{subproblem}{%
    \refstepcounter{subproblem}%
    \begin{enumerate}[label=\protect\hyperlink{newpage}{\theproblem.\thesubproblem},ref=\theproblem.\thesubproblem,leftmargin=*]
     \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

\hypertarget{newpage}{We'll link to this}

\end{document}
share|improve this answer
    
Brilliant! As always, thanks not only for the answer, but for the detailed explanation. –  cmhughes Nov 14 '11 at 5:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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