在先前的讨论帖,我询问是否可以定义一个自定义定理环境,其 QED 符号将是定理名称/编号/标签的盒装版本。在那里,我慷慨地获得了以下解决方案:
\documentclass{book}
\usepackage{amsthm,xpatch}
\makeatletter
\let\qed@empty\openbox % <--- change here, if desired
\def\@begintheorem#1#2[#3]{%
\deferred@thm@head{%
\the\thm@headfont\thm@indent
\@ifempty{#1}
{\let\thmname\@gobble}
{\let\thmname\@iden}%
\@ifempty{#2}
{\let\thmnumber\@gobble\global\let\qed@current\qed@empty}
{\let\thmnumber\@iden\xdef\qed@current{#2}}%
\@ifempty{#3}
{\let\thmnote\@gobble}
{\let\thmnote\@iden}%
\thm@swap\swappedhead
\thmhead{#1}{#2}{#3}%
\the\thm@headpunct\thmheadnl\hskip\thm@headsep
}\ignorespaces
}
\renewcommand{\qedsymbol}{%
\ifx\qed@thiscurrent\qed@empty
\qed@empty
\else
\fbox{\scriptsize\qed@thiscurrent}%
\fi
}
\renewcommand{\proofname}{%
Proof%
\ifx\qed@thiscurrent\qed@empty
\else
\ of \qed@thiscurrent
\fi
}
\xpretocmd{\proof}{\let\qed@thiscurrent\qed@current}{}{}
\newenvironment{proof*}[1]
{\def\qed@thiscurrent{\ref{#1}}\proof}
{\endproof}
\makeatother
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}[thm]{Lemma}
\newtheorem*{nthm}{Theorem}
\begin{document}
\chapter{Title}
\section{Title}
\begin{thm}
Pigs can fly.
\end{thm}
\begin{proof}
Would you doubt it?
\end{proof}
\begin{nthm}
Unnumbered.
\end{nthm}
\begin{proof}
What should we say?
\end{proof}
The following theorem will be proved later.
\begin{thm}\label{thm:later}
$P=NP$.
\end{thm}
Long text here.
\begin{proof*}{thm:later}
Oh, well! Should I really do it? We'll use the following lemma.
\begin{lem}
Something surely can fly.
\end{lem}
\begin{proof}
Clear.
\end{proof}
Now use the lemma and apply the well known identity
\[
1=0.\qedhere
\]
\end{proof*}
\end{document}
该代码的结果如下:
是否可以以“检测嵌套”的方式修改给定的代码,即使得“顶层”(非嵌套)上的定理/引理/命题保持其通常的 QED 符号,而后续级别(嵌套在顶层中的定理/引理/命题)上的定理/引理/命题用 QED 符号指定,这些符号是定理/引理/命题的盒装名称/数字/标签?
除了上面的代码,我没有 MWE,因为我根本不知道如何编写这样的代码。我在 TeX.SX 上找到了几个其他论坛(这里和这里) 处理嵌套环境的格式化,但我无法使我拥有的代码适应其中提供的代码。
任何帮助将不胜感激。
答案1
这个想法是添加一个在证明开始时为真的条件并相应地更改代码:如果条件为假,我们就不会嵌套,因此我们设置\qed@current
为\qed@empty
,否则我们使用与其他答案相同的机制。
\documentclass{book}
\usepackage{amsthm,xpatch}
\makeatletter
\let\qed@empty\openbox % <--- change here, if desired
\def\@begintheorem#1#2[#3]{%
\deferred@thm@head{%
\the\thm@headfont\thm@indent
\@ifempty{#1}
{\let\thmname\@gobble}
{\let\thmname\@iden}%
\@ifempty{#2}
{\let\thmnumber\@gobble\global\let\qed@current\qed@empty}
{\let\thmnumber\@iden
\ifqed@inproof
\xdef\qed@current{#2}%
\else
\global\let\qed@current\qed@empty
\fi}%
\@ifempty{#3}
{\let\thmnote\@gobble}
{\let\thmnote\@iden}%
\thm@swap\swappedhead
\thmhead{#1}{#2}{#3}%
\the\thm@headpunct\thmheadnl\hskip\thm@headsep
}\ignorespaces
}
\renewcommand{\qedsymbol}{%
\ifx\qed@thiscurrent\qed@empty
\qed@empty
\else
\fbox{\scriptsize\protect\qed@thiscurrent}%
\fi
}
\renewcommand{\proofname}{%
Proof%
\ifx\qed@thiscurrent\qed@empty
\else
\ of \qed@thiscurrent
\fi
}
\newif\ifqed@inproof
\xpretocmd{\proof}{\let\qed@thiscurrent\qed@current\qed@inprooftrue}{}{}
\newenvironment{proof*}[1]
{\def\qed@thiscurrent{\ref{#1}}\proof}
{\endproof}
\def\debug{\show\qed@thiscurrent}
\makeatother
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}[thm]{Lemma}
\newtheorem*{nthm}{Theorem}
\begin{document}
\chapter{Title}
\section{Title}
\begin{thm}
Pigs can fly.
\end{thm}
\begin{proof}
Would you doubt it?
\end{proof}
\begin{nthm}
Unnumbered.
\end{nthm}
\begin{proof}
What should we say?
\end{proof}
The following theorem will be proved later.
\begin{thm}\label{thm:later}
$P=NP$.
\end{thm}
Long text here.
\begin{proof*}{thm:later}
Oh, well! Should I really do it? We'll use the following lemma.
\begin{lem}
Something surely can fly.
\end{lem}
\begin{proof}
Clear.
\end{proof}
Now use the lemma and apply the well known identity
\[
1=0.\qedhere
\]
\end{proof*}
\end{document}