有没有一种(或多或少自动的)方法来通过名称而不是编号来引用定理?让我们解释一下:
\usepackage{amsmath}
\usepackage[thref]{ntheorem}
\newtheorem{theorem}{Theorem}
\begin{document}
\begin{theorem}[stupid theorem]
\label{thm:stupid}
asdf
\end{theorem}
\begin{theorem}[important theorem]
\label{thm:important}
asdf
\end{theorem}
\thref{thm:stupid}, \thref{thm:important}
\end{document}
输出为
我希望,当我提到“重要定理”时,将其引用为“重要定理”而不是“定理 2”。
答案1
下面我重新定义\label
以实际设置两个标签。第一个是常规标签,而是一个捕获标签\theoremname
- 包含可选参数中包含的定理名称的宏。然后它允许您使用\nameref{<label>}
它来提取:
\documentclass{article}
\usepackage[thref]{ntheorem}
\let\oldlabel\label
\makeatletter
\renewcommand{\label}[1]{% \label{<label>}
\oldlabel{#1}% Set regular \label
\ifcsname theoremname\endcsname% Check for existence of \theoremname
\edef\@currentlabel{\theoremname}% Update reference
\oldlabel{#1-name}% Set reference
\fi
}
\makeatother
\newcommand{\nameref}[1]{\ref{#1-name}}% Wrapper for reference of theorem name
\newtheorem{theorem}{Theorem}
\begin{document}
\begin{theorem}[stupid theorem]
\label{thm:stupid}
asdf
\end{theorem}
\begin{theorem}[important theorem]
\label{thm:important}
asdf
\end{theorem}
\thref{thm:stupid}, \nameref{thm:important}
\end{document}
尚未测试其兼容性hyperref
。