在上一個線程,我詢問了定義自訂定理環境的可能性,其 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
這個想法是在證明開始時添加一個為 true 的條件,並相應地更改程式碼:如果條件為 false,則我們不會嵌套,因此我們設定\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}