Theorem environment identation error with hyperref

So I want to indent my theorem text like on this post but when I use \usepackage{hyperref}, the theorem name gets shifted out of the document frame. E.g.

\documentclass[
toc=chapterentrydotfill,
numbers=endperiod
]
{scrreprt}
\usepackage{hyperref}

\usepackage{amsthm}
\usepackage{etoolbox}
\usepackage{thmtools}
\usepackage{lipsum}
\swapnumbers

\makeatletter
\patchcmd\@thm
{\trivlist}
{\list{}{\leftmargin2.5em\itemindent-15em}}
{}{}
\newcommand{\xdeclaretheorem}[2][]{%
\declaretheorem[#1]{#2}%
\expandafter\patchcmd\csname thmt@original@end#2\endcsname
{\endtrivlist}{\endlist}{}{}%
}

\declaretheoremstyle[
notebraces = {[}{]},
bodyfont = \normalfont,
spacebelow = 5pt,
spaceabove = 5pt]{thms}

\xdeclaretheorem[style = thms, name = Theorem, numberwithin = chapter]{thm}
\xdeclaretheorem[style = thms, name = Lemma, sibling = thm]{lem}

\begin{document}
\chapter{Chapter}
\begin{lem}
\lipsum[1]
\end{lem}
\end{document}


outputs

The second picture is the same code as above, but with the hyperref package removed. How do I fix this?

• Welcome on SE, @HannaBloom! In the above MWE there is no hyperref command. Could you include it? Because the position of this command within the document is relevant. – tanGIS Jun 19 '20 at 20:13
• @tanGIS thanks! it was all the way at the top – HannahBloom Jun 19 '20 at 20:36

The hyperref package does its own patching to \@thm, so we need to delay ours after it.

\documentclass[
toc=chapterentrydotfill,
numbers=endperiod
]{scrreprt}

\usepackage{etoolbox}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{hyperref}

\usepackage{lipsum}
\swapnumbers

\makeatletter
\AtBeginDocument{% we need to wait after hyperref has done its own patching
\patchcmd\@thm
{\trivlist}
{\list{}{\leftmargin2.5em\itemindent-15em}}
{}{}%
}
\newcommand{\xdeclaretheorem}[2][]{%
\declaretheorem[#1]{#2}%
\expandafter\patchcmd\csname thmt@original@end#2\endcsname
{\endtrivlist}{\endlist}{}{}%
}

\declaretheoremstyle[
notebraces = {[}{]},
bodyfont = \normalfont,
spacebelow = 5pt,
spaceabove = 5pt]{thms}

\xdeclaretheorem[style = thms, name = Theorem, numberwithin = chapter]{thm}
\xdeclaretheorem[style = thms, name = Lemma, sibling = thm]{lem}

\begin{document}
\chapter{Chapter}

\begin{lem}\label{test}
\lipsum[1][1-4]
\end{lem}

\ref{test}

\end{document}


• that's awesome, thanks! – HannahBloom Jun 19 '20 at 20:04