# hyperref not detecting theorem counter/theorem number

Currently, my theorems are indexed as Theorem {chapter}{section}{number}, e.g. Theorem 1.5.15 is the 15th theorem in Chapter 1, Section 5. However, when referencing these using hyperref, the counter is dropped. To reference Theorem 1.5.15, the link is only 1.5.

Here is a minimum working example:

\documentclass[10pt]{report}
\usepackage{amsmath, amsthm}
\usepackage{hyperref}

\newtheorem{theorem}{Theorem}
\numberwithin{theorem}{section}

\begin{document}

\chapter{Chapter.}
\section{Section.}

\label{thm: Amazing.}\begin{theorem} Amazing!
\end{theorem}

\label{thm: Wow.}\begin{theorem} Wow!
\end{theorem}

By Theorem \ref{thm: Wow.} ...

\end{document}

• Spaces in label names are possible, but should not be used, however
– user31729
Apr 13 '18 at 3:21
• since you're using amsthm, a better way to indicate the numbering of theorems is to specify \newtheorem{theorem}{Theorem}[section]. (read the amsthm user's guide -- texdoc amsthdoc.) \numberwithin isn't as reliable in this situation. but the real problem, as identified in the answers, is the position of the \label. Apr 13 '18 at 14:05

## 1 Answer

The \label command for numbered environments should appear inside inside the environment in general, since \refstepcounter (increasing the relevant counter) sets \@currentlabel, which is only a macro defined by \protected@edef, so \@currentlabel gets lost after the group formed by the environment.

A \label following after \end{....} will grab the last global \@currentlabel, in this case the section value, i.e. 1.1.

Using the cleveref package, it would be possible to say \Cref{lem: Wow} and Theorem 1.1.2 would appear automatically, i.e. without writing Theorem explicitly.

Although possible, spaces in label names are not recommended.

\documentclass[10pt]{report}
\usepackage{amsmath, amsthm}
\usepackage{hyperref}

\newtheorem{theorem}{Theorem}
\numberwithin{theorem}{section}

\begin{document}

\chapter{Chapter.}
\section{Section.}

\begin{theorem} Amazing! \label{lem: Amazing.}
\end{theorem}

\begin{theorem} Wow! \label{lem: Wow.}
\end{theorem}

By Theorem \ref{lem: Wow.} ...

\end{document}


• Excellent, thank you! Does this hold for chapters, sections, etc.? That is, should I be writing this as \label{ch:ex}\chapter{Chapter.} or \chapter{Chapter.}\label{ch:ex}? Apr 13 '18 at 3:22
• @user160653: \chapter, then \label. \chapter etc. are not forming groups where \@currentlabel can be lost, but in general \label must appear after such macros, not before, in environment it should be inside, but again, not before.
– user31729
Apr 13 '18 at 3:23
• @ChristianHupfer -- see the recommendation in my comment to the question. Apr 13 '18 at 14:06
• @barbarabeeton: Yes, but the real issue is the wrong placement of the \label ...
– user31729
Apr 13 '18 at 14:59