Cleveref 与定理子编号的交互

Cleveref 与定理子编号的交互

我根据这个问题的建议,列入了子编号定理:如何获得定理(定理 1.A.、定理 1.B.、定理 2.)的子编号?

这效果很好,并且它在视觉上与包\cref{}的命令很好地结合在一起cleveref。但是,它不知何故弄乱了生成的超链接的目标。具体来说,我生成了定理2.A2.B,但任何指向的链接都会2.A跳转到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}

有什么办法可以修复它吗?

答案1

该计数器是一个当其发生变化时thm应该在其内重置的计数器,但在其外可能受到其他重置计数器的影响。subtheoremparentnumber

使用chngcntr包及其\counterwithin*命令,\counterwithout*可以thm在重置列表中设置计数器parentnumber,并在环境结束时将其删除。(此处使用带星号的版本以防止\thethm格式更改)

cleveref与此问题无关,因为\ref会跳转到相同的错误锚点。

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

相关内容