我想在稿件中重述几个结果。目前我在环境中使用ntheorem
和,但我对源代码的外观感到烦恼:thm-restate
restatable
\begin{restatable}{theorem}{mytheoremnameaboutfoo}
\label{thm:my-theorem-name-about-foo}
...
\end{restatable}
...
\mytheoremnameaboutfoo*
我希望它看起来像这样:
\begin{theorem}
\label{thm:my-theorem-name-about-foo}
...
\end{theorem}
...
\restate{thm:my-theorem-name-about-foo}
编写此类宏的最佳方法是什么?我不介意我的所有环境都是可重载的,除非它会使编译速度非常慢。
答案1
为了使以下代码正常工作,您需要遵守纪律:\label
用于可重述定理的必须是\label
主体中的第一个命令。
对于您计划重述的定理,请使用\newrestatabletheorem
,其语法与相同\newtheorem
。
\documentclass{article}
\usepackage{amsthm} % but ntheorem might work as well
\usepackage{thm-restate}
\ExplSyntaxOn
\NewDocumentCommand{\newrestatabletheorem}{momo}
{
\IfNoValueTF{#2}
{
\IfNoValueTF{#4}
{
\newtheorem{#1inner}{#3}
}
{
\newtheorem{#1inner}{#3}[#4]
}
}
{
\newtheorem{#1}[#2]{#3}
}
\NewDocumentEnvironment{#1}{o +b}
{
\paleka_restate:nnn { #1 } { ##1 } { ##2 }
}
{}
}
\NewDocumentCommand{\restate}{m}
{
\use:c { #1 }*
}
\seq_new:N \l_paleka_restate_labels_seq
\tl_new:N \l_paleka_restate_label_tl
\cs_new_protected:Nn \paleka_restate:nnn
{
\regex_extract_once:nnNTF { \c{label}\{(.*?)\} } { #3 } \l_paleka_restate_labels_seq
{% found a label, make restatable
\__paleka_restate_yes:nnn { #1 } { #2 } { #3 }
}
{% no label found
\tl_if_novalue:nTF { #2 }
{
\begin{#1inner}#3\end{#1inner}
}
{
\begin{#1inner}[#2]#3\end{#1inner}
}
}
}
\cs_new_protected:Nn \__paleka_restate_yes:nnn
{
\tl_if_novalue:nTF { #2 }
{
\__paleka_restate_noopt:nne { #1 } { #3 } { \seq_item:Nn \l_paleka_restate_labels_seq { 2 } }
}
{
\__paleka_restate_opt:nnne { #1 } { #2 } { #3 } { \seq_item:Nn \l_paleka_restate_labels_seq { 2 } }
}
}
\cs_new_protected:Nn \__paleka_restate_noopt:nnn
{
\begin{restatable}{#1inner}{#3}
#2
\end{restatable}
}
\cs_generate_variant:Nn \__paleka_restate_noopt:nnn { nne }
\cs_new_protected:Nn \__paleka_restate_opt:nnnn
{
\begin{restatable}[#2]{#1inner}{#4}
#3
\end{restatable}
}
\cs_generate_variant:Nn \__paleka_restate_opt:nnnn { nnne }
\ExplSyntaxOff
% now we can define the restatable theorems
\newrestatabletheorem{theorem}{Theorem}[section]
\newrestatabletheorem{lemma}{Lemma}
\begin{document}
\section{Theorems}
\begin{lemma}
\label{thm:my-theorem-name-about-foo}
Pigs can fly.
\end{lemma}
\begin{theorem}[Negation]
\label{thm:my-theorem-name-about-foo-negate}
Pigs cannot fly.
\end{theorem}
\section{Restatements}
\restate{thm:my-theorem-name-about-foo}
\restate{thm:my-theorem-name-about-foo-negate}
\end{document}
代码检查环境主体并提取第一个\label
命令,使用其参数作为重述定理的键。如果没有\label
找到,它只会排版环境。