使用 amsthm 和 thmtools 在节名称内进行 autoref

使用 amsthm 和 thmtools 在节名称内进行 autoref

下面的例子效果很好:

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem[parent=section]{theorem}
\usepackage{hyperref}


\begin{document}
\section{Section}
\begin{theorem}\label{true}
  True.
\end{theorem}
According to \autoref{true}, this is true.

\subsection{Proof of Theorem \ref{true}}
Proof.
\end{document}

但下面的例子却失败了:

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem[parent=section]{theorem}
\usepackage{hyperref}


\begin{document}
\section{Section}
\begin{theorem}\label{true}
  True.
\end{theorem}
According to \autoref{true}, this is true.

\subsection{Proof of \autoref{true}}
Proof.
\end{document}

出现错误:

Argument of \@sect has an extra } \subsection{Proof of \autoref{true}}
Paragraph ended before \@sect was complete \subsection{Proof of \autoref{true}}
Paragraph ended before \HyPsd@@ProtectSpacesFi was complete \subsection{Proof of \autoref{true}}
Extra }, or forgotten \endgroup \subsection{Proof of \autoref{true}}
Use of \HyPsd@RemoveMask doesn't match its definition \subsection{Proof of \autoref{true}}
Undefined control sequence \subsection{Proof of \autoref{true}}
Paragraph ended before \HyPsd@RemoveBraces was complete \subsection{Proof of \autoref{true}}
Extra }, or forgotten \endgroup \subsection{Proof of \autoref{true}}
Argument of \HyPsd@CheckCatcodes has an extra } \subsection{Proof of \autoref{true}}
Paragraph ended before \HyPsd@CheckCatcodes was complete \subsection{Proof of \autoref{true}}
File ended while scanning use of \HyPsd@RemoveMask

知道原因是什么吗,或者如何克服这个问题?

答案1

\autoref处理用于制作书签的文本时会阻塞。使用\texorpdfstring

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem[parent=section]{theorem}
\usepackage{hyperref}

\begin{document}
\section{Section}
\begin{theorem}\label{true}
  True.
\end{theorem}
According to \autoref{true}, this is true.

\subsection{Proof of \texorpdfstring{\autoref{true}}{"true"}}
Proof.
\end{document}

在此处输入图片描述

或者,使用cleveref

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem[parent=section]{theorem}
\usepackage{hyperref}
\usepackage[capitalize]{cleveref}


% remove a spurious warning
\makeatletter
\pdfstringdefDisableCommands{\let\cref\@firstofone}
\makeatother

\begin{document}
\section{Section}
\begin{theorem}\label{true}
  True.
\end{theorem}
According to \cref{true}, this is true.

\subsection{Proof of \cref{true}}
Proof.
\end{document}

答案2

奇怪的是,以下解决方案似乎也有效:添加title={Theorem}到定理声明中。

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}
\declaretheorem[parent=section,title={Theorem}]{theorem} % added title={Theorem}
\usepackage{hyperref}


\begin{document}
\section{Section}
\begin{theorem}\label{true}
  True.
\end{theorem}
According to \autoref{true}, this is true.

\subsection{Proof of \autoref{true}}
Proof.
\end{document}

我不知道为什么这样做有效;我偶然发现了这个解决方案。大概有问题的代码是在自动标题生成中的某个地方?

如果由于某种原因您无法(或不愿意)使用该包,则手动为每种定理样式设置标题可能是一种替代解决方案cleveref。到目前为止,这似乎是唯一可以生成正确 PDF 书签的解决方案(尽管对某些人来说这可能并不重要)。

相关内容