目录和 \hyperref 指向 newtcbtheorem 环境的不同位置

目录和 \hyperref 指向 newtcbtheorem 环境的不同位置

我有一个用户定义的newtcbtheorem环境变体,如下所示,您可以看到,在其中我有一个addcontentsline用于将定理编号和标题添加到目录的。我已添加一个\phantomsection,以便目录中的引用指向标题上方,而不是标题下方(正如所发生的那样)。

但是,即使这样可行,当\hyperref我在文档中使用它链接到相同的定理时,它指向定理标题下方,即

在此处输入图片描述

我怎样才能让它模仿指向该定理的 ToC 链接的指针?

\documentclass[12pt]{report}
\usepackage[dvipsnames]{xcolor}
\usepackage[parfill]{parskip}
\usepackage[margin=1in]{geometry}
\usepackage{empheq}
\usepackage[most]{tcolorbox}
\usepackage{amsmath , amsthm , amssymb, mathtools}
\usepackage{fullpage}  
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{xpatch}
\usepackage{hyperref}
\usepackage[all]{hypcap}
\hypersetup{
    colorlinks,
    linkcolor={blue!80!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}

\ExplSyntaxOn

\NewDocumentCommand{\betternewtcbtheorem}{O{}mmmm}
{
    \newtcbtheorem[#1]{#2inner}{#3}{#4}{#5}
    \NewDocumentEnvironment{#2}{O{}}
    {
        \keys_set:nn { hushus/tcb } { ##1 }
        %Add to ToC
        \phantomsection
        \addcontentsline{toc}{section}{#3\hspace{0.2em} \thechapter.{ \the\numexpr\value{thmcounter}+1\relax}  {
                \if\l__hushus_tcb_title_tl\empty
                \else   
                :\hspace{0.4em} \l__hushus_tcb_title_tl
                \fi} }
        %Finish adding to ToC
        \hushus_tcb_begin:nVV {#2inner} \l__hushus_tcb_title_tl \l__hushus_tcb_label_tl
    }
    {
    \end{#2inner}
}
\cs_if_exist:cF { c@#5} { \newcounter{#5} }
}

\cs_new_protected:Nn \hushus_tcb_begin:nnn
{
\begin{#1}{#2}{#3}
}
\cs_generate_variant:Nn \hushus_tcb_begin:nnn { nVV }
\keys_define:nn { hushus/tcb }
{
    title .tl_set:N = \l__hushus_tcb_title_tl,
    label .tl_set:N = \l__hushus_tcb_label_tl,
}

\ExplSyntaxOff

\newcounter{thmcounter}[chapter]
\betternewtcbtheorem[number within = chapter,use counter=thmcounter]{thm}{Theorem}%
{
    enhanced,
    colback=green!10,
    colframe=green!35!black,
    fonttitle=\bfseries,
    top=3mm,
    attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
    boxed title style = {colback=green!35!black}
}{thm}

\begin{document}
    \tableofcontents
    \newpage
    \begin{thm}[title = The Amazing Theorem, label= 1.1]
        This is the most amazing theorem
    \end{thm}
    Here I link to the theorem \hyperref[thm:1.1]{Corollary 1.1}
\end{document}

答案1

\ref 命令指向由 tcolorbox 设置的目的地,而不是指向您的 \phantomsection。

tcolorbox 用于\Hy@raisedlink提升链接,因此您可以通过本地更改用于提升链接的默认长度来调整目标的位置。默认值是\baselineskip,我留给您检查是否需要稍后在某处重置它...

\def\HyperRaiseLinkDefault{1cm}%
\documentclass[12pt]{report}
\usepackage[dvipsnames]{xcolor}
\usepackage[parfill]{parskip}
\usepackage[margin=1in]{geometry}
\usepackage{empheq}
\usepackage[most]{tcolorbox}
\usepackage{amsmath , amsthm , amssymb, mathtools}
\usepackage{fullpage}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{xpatch}
\usepackage{hyperref}
\usepackage[all]{hypcap}
\hypersetup{
    colorlinks,
    linkcolor={blue!80!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}
}

\ExplSyntaxOn

\NewDocumentCommand{\betternewtcbtheorem}{O{}mmmm}
{
    \newtcbtheorem[#1]{#2inner}{#3}{#4}{#5}
    \NewDocumentEnvironment{#2}{O{}}
    {   
        \keys_set:nn { hushus/tcb } { ##1 }
        %Add to ToC
        \phantomsection
        \addcontentsline{toc}{section}{#3\hspace{0.2em} \thechapter.{ \the\numexpr\value{thmcounter}+1\relax}  {
                \if\l__hushus_tcb_title_tl\empty
                \else
                :\hspace{0.4em} \l__hushus_tcb_title_tl
                \fi} }
        %Finish adding to ToC
        \def\HyperRaiseLinkDefault{1cm}%
        \hushus_tcb_begin:nVV {#2inner} \l__hushus_tcb_title_tl \l__hushus_tcb_label_tl
    }
    {
    \end{#2inner}
}
\cs_if_exist:cF { c@#5} { \newcounter{#5} }
}

\cs_new_protected:Nn \hushus_tcb_begin:nnn
{
\begin{#1}{#2}{#3}
}
\cs_generate_variant:Nn \hushus_tcb_begin:nnn { nVV }
\keys_define:nn { hushus/tcb }
{
    title .tl_set:N = \l__hushus_tcb_title_tl,
    label .tl_set:N = \l__hushus_tcb_label_tl,
}

\ExplSyntaxOff

\newcounter{thmcounter}[chapter]
\betternewtcbtheorem[number within = chapter,use counter=thmcounter]{thm}{Theorem}%
{
    enhanced,
    colback=green!10,
    colframe=green!35!black,
    fonttitle=\bfseries,
    top=3mm,
    attach boxed title to top left={xshift = 5mm, yshift=-1.5mm},
    boxed title style = {colback=green!35!black}
}{thm}

\begin{document}
    \tableofcontents
    \newpage
    \begin{thm}[title = The Amazing Theorem, label= 1.1]
        This is the most amazing theorem
    \end{thm}
    Here I link to the theorem \hyperref[thm:1.1]{Corollary 1.1}
\end{document}

相关内容