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?

5
  • 3
    A minimal example is needed
    – egreg
    Nov 2, 2015 at 18:18
  • ItsmeMimi: Do you have loaded cleveref as the last package?
    – user31729
    Nov 2, 2015 at 22:01
  • @ChristianHupfer It doesn't change anything when I do.
    – mimuller
    Nov 2, 2015 at 22:21
  • 1
    @ItsmeMimi: Alright, I have a look on the code
    – user31729
    Nov 2, 2015 at 22:23
  • 1
    The error is the \setcounter{#1}{0} command -- this leads to non-unique counter settings and thereby to wrong anchors
    – user31729
    Nov 2, 2015 at 22:34

1 Answer 1

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}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.