1

I'm trying to use the label/ref mechanism to get a numbering chapter.nb of my theorems. I get the numbering correct, but the hyperref link goes to the wrong address: it forgets the "chapter" part, so "2.1" goes to (thm number) "1" and the first such instance is "1.1". I kept fancyhdr and mdframed in the ECM in case it influences a solution.

Many thanks for your time and advice! Best, Olivier

\documentclass{book}
\usepackage{fancyhdr, mdframed, hyperref}
\newcounter{mythmcnt}
\setcounter{mythmcnt}{0}
\renewcommand*\themythmcnt{\thechapter.\arabic{mythmcnt}}
\newmdenv{ispecialthm}
\newenvironment{thmenvN}[2][]{\refstepcounter{mythmcnt}%
\begingroup\begin{ispecialthm}[frametitle={#2\ \themythmcnt}]}{\end{ispecialthm}\endgroup}

\newenvironment{thmN}[1][]{\begingroup\begin{thmenvN}[#1]{Theorem}}{\end{thmenvN}\endgroup}

\begin{document}
\chapter{First}
\setcounter{mythmcnt}{0}

\begin{thmN}
  \label{thm1} A first theorem.
\end{thmN}

Here are Theorem~\ref{thm1} and Theorem~\ref{thm2}.

\chapter{Second}
\setcounter{mythmcnt}{0}

\begin{thmN}
  \label{thm2} A second theorem.
\end{thmN}

Here are Theorem~\ref{thm1} and Theorem~\ref{thm2}.
\end{document}
3
  • Check the log file. It already mentions that the hyperref target is no uniq.
    – daleif
    Commented Sep 12, 2022 at 8:39
  • Try \renewcommand\theHmythmcnt{\arabic{mythmcnt}.\thepage} on order to make the target hyperref uses more uniq
    – daleif
    Commented Sep 12, 2022 at 8:42
  • Many thanks, that works like a charm! Commented Sep 12, 2022 at 9:20

1 Answer 1

1

Just define mythmcnt to be subordinate to chapter.

I also removed redundant grouping. However, there are parts that get unused.

\documentclass{book}
\usepackage{fancyhdr, mdframed, hyperref}

\newcounter{mythmcnt}[chapter]
\renewcommand*\themythmcnt{\thechapter.\arabic{mythmcnt}}

\newmdenv{ispecialthm}

\newenvironment{thmenvN}[2][]
  {%
   \refstepcounter{mythmcnt}%
   \ispecialthm[frametitle={#2\ \themythmcnt}]%
  }
  {\endispecialthm}

\newenvironment{thmN}[1][]
  {\thmenvN[#1]{Theorem}}
  {\endthmenvN}

\begin{document}

\chapter{First}

\begin{thmN}[abc]
  \label{thm1} A first theorem.
\end{thmN}

Here are Theorem~\ref{thm1} and Theorem~\ref{thm2}.

\chapter{Second}

\begin{thmN}
  \label{thm2} A second theorem.
\end{thmN}

Here are Theorem~\ref{thm1} and Theorem~\ref{thm2}.

\end{document}
2
  • Many thanks for that! And for the cleaning of my 'code':) Can you explain a bit why you define \printifnotempty ? Olivier Commented Sep 12, 2022 at 10:50
  • @OlivierRamaré That was for the second argument, but I realized you don't really use it like I thought. Remove.
    – egreg
    Commented Sep 12, 2022 at 12:13

You must log in to answer this question.

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