0

I am writing a book in which theorems are numbered "Theorem 1", "Theorem 2", etc. in every chapter; the resetting of the Theorem counter is done using an explicit \setcounter command. When I intend to point at Theorem 1 of any chapter using hyperref, the link created points to Theorem 1 of Chapter 1 instead (see MWE below).

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}[First]\label{thm1}
The first.
\end{theorem}

\newpage

\setcounter{theorem}{0}

\begin{theorem}[Second]\label{thm2}
The second.
\end{theorem}

Reference to the second occurrence of Theorem 1 : \ref{thm2}.
\end{document}

I know I could certainly fix this issue by numbering theorems including a chapter number (like "Theorem 1.1"), but I like the simpler numbering better. Is there a way to convince hyperref to distinguish between the two theorems above (and therefore to point properly to the second one) even if the numbering displayed is the same?

Thank you in advance!

Piotr

1
  • 3
    ensure that \theHtheorem is unique for every theorem, e.g. \renewcommand\theHtheorem{\thechapter.\thetheorem} Feb 23, 2022 at 23:36

1 Answer 1

1

I don't think it's nice to your readers having to see “as proved in theorem 8 of chapter 11” instead of the simpler “as proved in theorem 11.8”.

Anyway, you don't need to manually reset the counter and you can change the H-representation of the counter to get a unique anchor.

\documentclass{book}
\usepackage{hyperref}

\newtheorem{theorem}{Theorem}
\counterwithin*{theorem}{chapter}
\renewcommand{\theHtheorem}{\thechapter.\arabic{theorem}}

\begin{document}

\chapter{First}

\begin{theorem}[First]\label{thm1}
The first.
\end{theorem}

\chapter{Second}

\begin{theorem}[Second]\label{thm2}
The second.
\end{theorem}

Reference to the first occurrence of Theorem 1: \ref{thm1}.

Reference to the second occurrence of Theorem 1: \ref{thm2}.

\end{document}

You'll see that the first link points to chapter 1 and the second link to chapter 2.

enter image description here

5
  • Thank you very much egreg! I avoid referring to "Theorem X from Chapter Y" as much as possible, but I do make references to "Proposition X" (from the same chapter) in proofs; that was why I needed the link to be correct. Also, I do some (rare) references to "Exercise X from Chapter Y", but I indeed feel forced to add a page number to make the browsing easier. I might switch to a more precise formatting in this case.
    – Piotr
    Feb 24, 2022 at 16:56
  • It works fine now, many thanks. I just had to add \usepackage{chngcntr} to my preamble in order to be able to use \counterwithin.
    – Piotr
    Feb 24, 2022 at 17:09
  • 2
    @Piotr If you need that, you've got a rather old and outdated TeX distribution.
    – egreg
    Feb 24, 2022 at 17:11
  • It very possibly dates back to 2016 indeed... Yet I'm terrified with the idea of updating it and finding out that my code, some parts of which are truly ancient, doesn't compile anymore. Is such a thing possible?
    – Piotr
    Feb 24, 2022 at 17:36
  • 1
    @Piotr I wouldn't worry too much. If you don't rely on distributions provided by Debian or similar systems, but just the standard vanilla TeX Live, you can have as many releases as your disk allows. I have all collections from 2012 on.
    – egreg
    Feb 24, 2022 at 17:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .