4

I have included sub-numbered theorems according to the suggestion for this question: How do I get sub-numbering for theorems (Theorem 1.A., Theorem 1.B., Theorem 2.)?

This works great, and it visually incorporates fine with the \cref{} command of the cleveref package. However, it somehow messes up the target for the hyperlinks that are generated. Specifically, I generated Theorems 2.A and 2.B, but any link to 2.A jumps to Theorem 1.

    \documentclass{article}
    \usepackage[colorlinks=true]{hyperref}
    \usepackage{cleveref}

    \newtheorem{thm}{Theorem}
    \crefname{thm}{Theorem}{Theorems} 

    \makeatletter \newenvironment{subtheorem}[1]{%
      \def\subtheoremcounter{#1}%
      \refstepcounter{#1}%
      \protected@edef\theparentnumber{\csname the#1\endcsname}%
      \setcounter{parentnumber}{\value{#1}}%
      \setcounter{#1}{0}%
      \expandafter\def\csname the#1\endcsname{\theparentnumber.\Alph{#1}}%
      \ignorespaces
    }{%
      \setcounter{\subtheoremcounter}{\value{parentnumber}}%
      \ignorespacesafterend
    }
    \makeatother
    \newcounter{parentnumber}

    \begin{document}

    \begin{thm}\label{thm1}
        ...
    \end{thm}

    \begin{subtheorem}{thm}
    \begin{thm}
        Part A\label{thm3a}
    \end{thm}
    \begin{thm}
        Part B
    \end{thm}
    \end{subtheorem}

    This link is correctly labeled as \cref{thm3a} but actually jumps back to \cref{thm1}.

    \end{document}

Any idea how to fix that?

mimuller
  • 800

1 Answers1

5

The thm counter is a counter that should be reset within subtheorem when parentnumber is changed, but outside it's subject to a possible other resetting counter.

Using chngcntr package and its \counterwithin* and \counterwithout* commands it's possible to set the thm counter on reset list of parentnumber and remove it at the end of the environment. (The starred version is used here to prevent a \thethm format change)

cleveref is not connected to this problem, since \ref would jump to the same false anchors.

\documentclass{article}

\usepackage{chngcntr}
\newtheorem{thm}{Theorem}

\newcounter{parentnumber}



\makeatletter 
\newenvironment{subtheorem}[1]{%
  \counterwithin*{thm}{parentnumber}
  \def\subtheoremcounter{#1}%
  \refstepcounter{#1}%
  \protected@edef\theparentnumber{\csname the#1\endcsname}%
  \setcounter{parentnumber}{\value{#1}}%
  \setcounter{#1}{0}%
  \expandafter\def\csname the#1\endcsname{\theparentnumber.\Alph{#1}}%
  \ignorespaces
}{%
  \setcounter{\subtheoremcounter}{\value{parentnumber}}%
  \counterwithout*{thm}{parentnumber} % kick it from the reset list
  \ignorespacesafterend
}
\makeatother
\usepackage[colorlinks=true]{hyperref}
\usepackage{cleveref}
\crefname{thm}{Theorem}{Theorems} 


\begin{document}

\begin{thm}\label{thm1}
  ...
\end{thm}

\clearpage

\begin{subtheorem}{thm}
  \begin{thm}
    Part A\label{thm3a}
  \end{thm}
  \clearpage
  \begin{thm}
    Part B
  \end{thm}
\end{subtheorem}

 \clearpage

 This link is correctly labeled as \cref{thm3a} but actually jumps back to \cref{thm1}.
 \ref{thm3a}  
 \end{document}