2

In a previous thread, I asked about the possibility of defining a custom theorem environment whose QED symbol would be a boxed version of the theorem's name/number/label. There, I was graciously provided with the following solution:

\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}

The result of that code is as follows: enter image description here

Is it possible to modify the given code in such a way as to "detect nesting," i.e. so that the theorems/lemmas/propositions on the "top-level" (unnested) maintain their usual QED symbol while the theorems/lemmas/propositions on subsequent levels (those which are nested in the top-level ones) are designated as such with QED symbols which are the boxed names/numbers/labels of the theorems/lemmas/propositions?

Aside from the above code, I don't have a MWE because I have literally no idea how to go about coding such a thing. I have found a couple other forums on TeX.SX (here and here) which deal with formatting nested environments, but I was never able to adapt the code I have to the code provided therein.

Any help would be greatly appreciated.

1 Answer 1

3

The idea is to add a conditional that's true when a proof starts and change the code accordingly: if the conditional is false, we're not nesting, so we set \qed@current to \qed@empty, otherwise we use the same mechanism as in the other answer.

\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}

enter image description here

2
  • Thanks, @egreg! This is exactly what I had in mind. Follow-up question: I look at the code you provide and I have no idea how to come up with that. In particular, I'm familiar with using LaTeX as a typesetting environment (and with tweaking it slightly), but I'm completely ignorant regarding how to use it to "program" new environments; I'm unfamiliar with using if-then-else statements, etc. Can you recommend any resources (in English) to help me get familiarized with that aspect? Thanks again!
    – cstover
    Sep 26, 2014 at 14:01
  • 1
    @twigg1313 That's a very long way to go. You can start with TeX by Topic (it's in the documentation part of TeX distributions with texdoc texbytopic) for the basics about TeX's way of doing things; then study macros wherever you find them. In some time you'll begin to understand what they are doing.
    – egreg
    Sep 26, 2014 at 14:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .