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 the following code:

\usepackage{hyperref}
\newtheorem{thm}{Theorem}

\begin{thm} \label{mythm}
   \begin{enumerate}
      \item 
   \end{enumerate}
\end{thm}

\ref{mythm}

The ref prints the right number. However, when I click on it, it will lead me to a wrong page (basically, to the beginning of the document). pdflatex also has some warning. If I add $ $ between \begin{thm} and \begin{enumerate}, the problem disappears, but now, the enumeration starts a new line. Is there any way to fix this?

share|improve this question

3 Answers 3

up vote 10 down vote accepted

You can use the \phantomsection command from hyperref:

\documentclass{book}
\usepackage{amsthm}
\usepackage{hyperref}

\newtheorem{thm}{Theorem}

\begin{document}

\begin{thm}\phantomsection \label{mythm}
   \begin{enumerate}
      \item 
   \end{enumerate}
\end{thm}

\ref{mythm}

\end{document}

Another option would be to use the ntheorem package instead of amsthm:

\documentclass{book}
\usepackage{ntheorem}
\usepackage{hyperref}

\newtheorem{thm}{Theorem}

\begin{document}
\mbox{}\newpage
\begin{thm}
\label{mythm}
   \begin{enumerate}
      \item
   \end{enumerate}
\end{thm}

\newpage
\ref{mythm}

\end{document}
share|improve this answer
    
For ntheorem, do I need to also define the proof environment? –  brian Mar 21 '11 at 21:27
1  
@brian: if you load the package with the standard option, then some predefined theorem-like environments become available, proof amongst them (you'll also need the thmmarks package option to activate the endmark). If you don't load those options, you'll have to define your own proof environment. –  Gonzalo Medina Mar 21 '11 at 21:39

MWE:

\documentclass[12pt]{article}

\usepackage{amsthm}    
\usepackage[colorlinks]{hyperref}

\newtheorem{thm}{Theorem}

\begin{document}

\begin{thm} \label{thm:test}
\begin{enumerate}
\item First thing.
\item Second thing.
\item Third thing.
\end{enumerate}
\end{thm}

Reference to Theorem~\ref{thm:test}.

\end{document}

The theorem environment of amsthm delays the anchor setting to the start of the next paragraph via \everypar. However the first \item of environment enumerate clears it by \everypar{}. Thus the anchor is lost for the \label{thm:test}.

A normal paragraph start would set the anchor. The answer of John Kormylo start the paragraph with \strut, an invisible rule with a height and depth. The following example uses \noindent:

\documentclass[12pt]{article}

\usepackage{amsthm}
\usepackage[colorlinks]{hyperref}

\newtheorem{thm}{Theorem}

\begin{document}

\begin{thm}
\noindent\label{thm:test}
\begin{enumerate}
\item First thing.
\item Second thing.
\item Third thing.
\end{enumerate}
\end{thm}

Reference to Theorem~\ref{thm:test}.

\end{document}

Result \noindent

Then the first item moves from the theorem title line to the next line, the number aligned with the other numbers of the enumeration. Therefore I would call it a feature.

Otherwise \phantomsection instead of \noindent can be used, as shown in the answer of Gonzalo Medina:

\documentclass[12pt]{article}

\usepackage{amsthm}
\usepackage[colorlinks]{hyperref}

\newtheorem{thm}{Theorem}
\def\thmautorefname{Theorem}

\begin{document}

\begin{thm}
\phantomsection\label{thm:test}
\begin{enumerate}
\item First thing.
\item Second thing.
\item Third thing.
\end{enumerate}
\end{thm}

Theorem~\ref{thm:test} and \autoref{thm:test}.

\end{document}

Result \phantomsection

\phantomsection does not start a new paragraph, therefore the original anchor for the theorem label is still lost. But it creates a new anchor, that is then used by the following \label.

However, \phantomsection does not create a theorem anchor, but an anchor with the internal name of a section. Therefore \autoref will refer to the theorem as "section".

As workaround, the correct anchor can be set via \hyper@anchor:

\documentclass[12pt]{article}

\usepackage{amsthm}
\usepackage[colorlinks]{hyperref}

\newtheorem{thm}{Theorem}
\def\thmautorefname{Theorem}

\begin{document}

\begin{thm}
\makeatletter
\hyper@anchor{\@currentHref}%
\makeatother
\label{thm:test}
\begin{enumerate}
\item First thing.
\item Second thing.
\item Third thing.
\end{enumerate}
\end{thm}

Theorem~\ref{thm:test} and \autoref{thm:test}.

\end{document}

Result \hyper@anchor

share|improve this answer
    
A better command than \phantomsection would be welcome. ;-) What about \makeanchor? –  egreg Jul 24 at 19:50
    
@egreg: Is not this easy. Also a unique anchor name is needed. I doubt, a normal user would know to use \@currentHref or thm.\theHthm here. –  Heiko Oberdiek Jul 24 at 19:54

Even simpler...

\documentclass{book}
\usepackage{amsthm}
\usepackage{hyperref}

\newtheorem{thm}{Theorem}

\begin{document}

\begin{thm}\label{mythm}\strut% ******************
   \begin{enumerate}
      \item 
\end{enumerate}
\end{thm}

\ref{mythm}

\end{document}
share|improve this answer
    
I suspect that this clever solution may be more clever and less a solution to the problem at hand. In particular, I suspect that it might break down if and when either amsthm or hyperref are updated at some point in the future. However, this is no more than a suspicion for just now. I'd be happy to be shown wrong. –  Mico Jun 28 at 15:36
    
@Mico - One would hope than any updates will actually fix the problem in the first place. –  John Kormylo Jun 28 at 15:38
    
One should always hope... Nothing wrong with hope! :-) –  Mico Jun 28 at 15:40

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.