假设我有一个定理\label{thm}
,并且我想创建一个与它相同类型的定理,我希望它的理想行为如下:
\documentclass{article}
\usepackage{lipsum}
\usepackage{amsthm}
\usepackage{cleveref}
\newtheorem{theorem}{Theorem}
\begin{document}
\begin{theorem}\label{thm}
\lipsum[1][1-2]
\end{theorem}
% Want to create a theorem of the same type
\begin{\namecref{thm}}
\lipsum[1][3-4]
\end{\namecref{thm}}
% Ideally it would be like this:
\begin{theorem}
\lipsum[1][3-4]
\end{theorem}
\end{document}
但这会导致错误Missing \endcsname
。Environment \namecref{thm} undefined
有解决方法吗?
答案1
\namecref
不可扩展且不能用作字符串(即使可以:如果您不提供后备,在第一次编译时可能会出现错误)。
您可以将环境存储在具有属性的另一个标签中。这需要当前的 LaTeX:
\documentclass{article}
\usepackage{lipsum}
\usepackage{amsthm}
\usepackage{cleveref}
\newtheorem{theorem}{Theorem}
\makeatletter
\NewProperty{currenvir}{now}{unknown}{\@currenvir}
\makeatother
\newenvironment{unknown}{\textbf{UNKNOWN ENV}}{}
\begin{document}
\begin{theorem}\label{thm}\RecordProperties{envthm}{currenvir}
\lipsum[1][1-2]
\end{theorem}
% Want to create a theorem of the same type
\begin{\RefProperty{envthm}{currenvir}}
\lipsum[1][3-4]
\end{\RefProperty{envthm}{currenvir}}
% Ideally it would be like this:
\begin{theorem}
\lipsum[1][3-4]
\end{theorem}
\end{document}
答案2
cleveref
您需要从aux 文件内的注释中提取名称。
\namecref
请注意不是返回theorem
,而是“定理”。
\documentclass{article}
\usepackage{lipsum}
\usepackage{amsthm}
\usepackage{cleveref}
\newtheorem{theorem}{Theorem}
\newtheorem{??????}{??????} % for when a ref is undefined
\makeatletter
\newenvironment{reftheorem}[1]{%
\ifcsname r@#1@cref\endcsname
\edef\reftheorem@type{\reftheorem@extract{#1}}%
\else
\def\reftheorem@type{??????}%
\fi
\begin{\reftheorem@type}%
}{\end{\reftheorem@type}}
\newcommand\reftheorem@extract[1]{%
\expandafter\expandafter\expandafter\reftheorem@@extract
\csname r@#1@cref\endcsname
}
\newcommand\reftheorem@@extract[2]{\reftheorem@@@extract#1\@nil}
\def\reftheorem@@@extract[#1]#2\@nil{#1}
\makeatother
\begin{document}
\begin{theorem}\label{thm}
\lipsum[1][1-2]
\end{theorem}
\begin{reftheorem}{thm}
\lipsum[1][3-4]
\end{reftheorem}
% Ideally it would be like this:
\begin{theorem}
\lipsum[1][3-4]
\end{theorem}
\end{document}
第一次运行时你会得到“?????? 1”。