# Referencing labels created for theorems inside a subtheorem environment links to the theorem before

I am currently trying to create subtheorems that are linkable. I referenced the code put forth by another post, here: How do I get sub-numbering for theorems (Theorem 1.A., Theorem 1.B., Theorem 2.)?

I have tried to adapt it to numbering using the following code but it seemst o link to a theorem before the one inside the subtheorem environment. I am wondering if there is perhaps a fix to this. For example, running the code below and linking to Theorem 2A leads to Theorem 1 instead. Any help would be greatly appreciated. Thank you.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{amsfonts}
\usepackage{amsthm}

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

% - - environments
\newtheorem{thm}{Theorem}

\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}

\begin{document}

\begin{thm}\label{thm:one}
One
\end{thm}

\begin{subtheorem}{thm}\label{thm:two}
\begin{thm}\label{thm:twoA}
Two, first part
\end{thm}

\begin{thm}\label{thm:twoB}
Two, second part
\end{thm}
\end{subtheorem}

\begin{thm}\label{thm:three}
Three
\end{thm}

\ref{thm:one}, \ref{thm:two}, \ref{thm:twoA}, \ref{thm:twoB}, \ref{thm:three}.

\end{document}


Two things:

1. You should load hyperef as the last package
2. The code you are using is a modification of the subequations code from amsmath.sty, so you also need a mimic the code hyperef has specific to subequations

\documentclass{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amsthm}

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

\newtheorem{thm}{Theorem}

\begin{document}

\begin{thm}\label{thm:one}
One
\end{thm}

Text

text

text

\begin{subtheorem}{thm}\label{thm:two}
\begin{thm}\label{thm:twoA}
Two, first part
\end{thm}

Text

text

text

\begin{thm}\label{thm:twoB}
Two, second part
\end{thm}
\end{subtheorem}

Text

text

text

\begin{thm}\label{thm:three}
Three
\end{thm}

\ref{thm:one}, \ref{thm:two}, \ref{thm:twoA}, \ref{thm:twoB}, \ref{thm:three}.

\end{document}


Here is what is written it the .aux file

\newlabel{thm:one}{{1}{1}{}{thm.1}{}}
\newlabel{thm:two}{{2}{1}{}{thm.2}{}}
\newlabel{thm:twoA}{{2.A}{1}{}{thm.2.A}{}}
\newlabel{thm:twoB}{{2.B}{1}{}{thm.2.B}{}}
\newlabel{thm:three}{{3}{1}{}{thm.3}{}}


which shows that the extra hyperref arguments are correctly set matching the counters. Your original code generated

\newlabel{thm:twoA}{{2.A}{1}{}{thm.1}{}}


which was not what you wanted.

The position of the links for thm:twoA and thm:twoB are now fine. However, note that thm:two points to where subtheorem is issued. The subsequent \begin{thm} adds some vertical space, so the thm:two link is not on the line of the heading of the theorem. There is really no way around this with your current set-up.

• Thank you, very great explanation I was scratching my head for hours over! – user321627 Jun 15 at 20:12