15

I'm wondering if there's a relatively simple way to achieve a result similar to the following:

enter image description here

In particular, I'd like to be able to assign names and/or numbers to theorems/lemmas/propositions and to then replace the standard QED symbol of that theorem/lemma/proposition with an outlined version of the theorem/lemma/proposition name/number.

I don't have a minimum working example here because, unfortunately, my knowledge of modifying theorem environments is sufficiently low that I haven't the slightest clue how to start. Any suggestions would be hugely appreciated.

8

This does what you want, though I find it cumbersome and not really informative.

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

If a proof is delayed, use the proof* environment, which wants as argument the label used in the relative theorem.

As you see, proofs can be nested. I only showed a nested proof inside a “delayed” proof, but you can check it works also with the standard proof environment.

enter image description here

Some words of explanation.

First I modify the definition of \@begintheorem to add a couple of settings. If the theorem is unnumbered, I set (globally, because we're already in an environment) \qed@current to \qed@empty (which is defined to be the standard QED symbol, we'll see later why); if the theorem is numbered, I do \xdef\qed@current{#2}, because #2 contains the theorem number (but not in explicit form, so the need to fully expand it).

In case the theorem numbers contain formatting instructions or when different number systems are used (Greek numerals with babel, for instance), this should be

\protected@edef\@tempa{#2}\global\let\qed@current\@tempa

in order to avoid problems. In standard English settings this shouldn't be needed.

Other than these two changes, \@begintheorem is the same as the original.

Then I redefine \qedsymbol. It compares \qed@thiscurrent with \qed@empty; if it's the same, it typesets the standard symbol, otherwise it typesets

\fbox{\scriptsize\qed@thiscurrent}

because, as we'll see, \qed@thiscurrent contains the number of the theorem currently being proved.

Also \proofname is redefined to add “of <number>” using \qed@thiscurrent if the last stated theorem is numbered.

The proof environment is modified to set (locally) \qed@thiscurrent to \qed@current; finally proof* is defined to do like proof but retrieving the number with \ref.

The case of a simple proof following a theorem is easy: \qed@thiscurrent will contain the theorem number. The same for proof*.

If a proof environment is inside another proof, the statement will globally reset \qed@current, but this will not influence \qed@thiscurrent at the end of the main proof, because \qed@thiscurrent for the nested proof is set locally and \end{proof} will revert the local \qed@thiscurrent to the previous value.

  • This is a good solution, though the Don't nest proofs. bit does throw a wrench. I didn't mention this explicitly in the question (perhaps I should edit it to add this?) but one of the big things I want to do is to use this while nesting: For example, if I have \begin{proof}[Proof of Theorem]We need a lemma.\begin{lemma}Blah.\end{lemma}\begin{proof}[Proof of Lemma]...\end{proof}And now the proof of the theorem follows.\end{proof}, I'd like to use this as a mechanism to differentiate between the \qed tied to the Lemma and the one tied to the Theorem. If that makes sense. – cstover Sep 24 '14 at 13:50
  • @egreg: I would rather prove pigs can fly because P = NP… – Bernard Sep 24 '14 at 13:58
  • @twigg1313 In my opinion it doesn't make sense; it just adds to confusion. Nesting proofs is against clarity; if used, one should simply make clear with some words that the subproof ends (before the relative tombstone). However, I added the possibility to nest proofs. – egreg Sep 24 '14 at 14:28
  • @egreg: I agree completely, and while nesting proofs isn't something that I personally tend to do (I'm sure I have done it, but it's something I frown upon, personally), this whole idea was more an exercise in possibility rather than practicality. For some context: Dave Gabai's first Foliations... paper from JDG back in '83 has a ridiculously obscene number of nested proofs to the point that a lot of the results are too cumbersome to digest. After I saw the above example (the photo I included in my OP), I wondered if such a thing would be possible for rewrites of such papers. – cstover Sep 24 '14 at 17:10
  • @egreg: Also, FYI: Your answer is this best, so I've marked it as such. I surely do appreciate you taking the time to edit your answer (and to provide such a good answer anyway). Thanks for drawing a pleasant close to my first inquiry on TeX.SE. :) – cstover Sep 24 '14 at 17:10
7

If we use the amsthm package then we can do this by hijacking the \qedsymbol command and hacking the way that the theorem environments are constructed internally. This comes down to adding some code to \@begintheorem to overwrite \qedsymbol so that it becomes a boxed version of the last theorem number.

enter image description here

There are two issues with the code below. The first is that the \qedsymbol has been lost - but you can use \realqedsymbol instead.

The second issue is that if you state one theorem, say, and then prove another lemma in the middle of the proof before returning to the main result then the last boxed number will be wrong. The simplest fix for this is probably to define a command like

\newcommand\QedSymbol[1]{\gdef\qedsymbol{\fbox{\ref{#1}}}}

for manually setting \qedsymbol equal to a boxed \ref command. To use this you would just have to write \label{MyWondrousTheorem} to add a reference to your (wonderful) theorem and then use \Qedsymbol{MyWondrousTheorem} before the end of the proof.

If the theorem-like environment does not have a theorem number then \qedsymbol reverts to \realqedsymbol, the real \qedsymbol.

I haven't tested this much so it is possible that this breaks something or that there are other corner cases where this does not work.

Here's the code.

\documentclass{article}
\usepackage{amsmath,amsthm}
\makeatletter% the hack to change the qedsymbol automatically
  \let\@@begintheorem=\@begintheorem% save real AMS theorem environment
  \let\realqedsymbol\qedsymbol
  \def\@begintheorem#1#2[#3]{%
    \@@begintheorem{#1}{#2}[#3]% start the theorem
    \@ifempty{#2}{\let\qedsymbol\realqedsymbol}{\gdef\qedsymbol{\fbox{#2}}}
  }
\makeatother

\swapnumbers\numberwithin{equation}{section}
\newtheorem{Proposition}[equation]{Proposition}
\newtheorem{Lemma}[equation]{Lemma}

\begin{document}
  \section{Important facts}
  \begin{Lemma}
    $1+1=2$
  \end{Lemma}

  \begin{proof}Count.
  \end{proof}

  \begin{Proposition}
    $1+3=4$
  \end{Proposition}

  \begin{proof}Count more carefully.
  \end{proof}

  \begin{Proposition}
    $2+2=4$
  \end{Proposition}

  \begin{proof}Count more carefully.
  \end{proof}
\end{document}


\documentclass{article}
\usepackage{amsmath,amsthm}
\makeatletter
  \let\@@begintheorem=\@begintheorem% save real AMS theorem environment
  \let\@qedsymbol\qedsymbol
  \def\@begintheorem#1#2[#3]{%
    \@@begintheorem{#1}{#2}[#3]% start the theorem
    \@ifempty{#2}{\let\qedsymbol\@qedsymbol}{\gdef\qedsymbol{\fbox{#2}}}
  }
\makeatother

\swapnumbers\numberwithin{equation}{section}
\newtheorem{Proposition}[equation]{Proposition}
\newtheorem{Lemma}[equation]{Lemma}

\begin{document}
  \section{Important facts}
  \begin{Lemma}
    $1+1=2$
  \end{Lemma}

  \begin{proof}Count.
  \end{proof}

  \begin{Proposition}
    $1+3=4$
  \end{Proposition}

  \begin{proof}Count more carefully.
  \end{proof}

  \begin{Proposition}
    $2+2=4$
  \end{Proposition}

  \begin{proof}Count more carefully.
  \end{proof}
\end{document}

Another way to do this, which would fix the issue with an intermediate result appearing in the middle of a proof, would to be redefine \qedsymbol at the start of the proof environment. This approach would only be possible, however, if all theorem-like environments used the same counter (as in my MWE). The code above has the advantage that it will (should?:) work even if different theorem-like environments use different counters.

  • I'm going to try tinkering with your solution some as soon as I'm done with class, etc., to see if I can nail down a solution which allows both nesting and different counters; in particular, the part about defining \newcommand\QedSymbol[1]{\gdef\qedsymbol{\fbox{\ref{#1}}}} and manually setting \qedsymbol equal to a boxed \ref command is a little daunting without actually jumping in there and doing something. If clarifying wouldn't be a lot of work for you, feel free; otherwise, I'll tinker and then perhaps come back and seek guidance after. Many thanks! – cstover Sep 24 '14 at 13:57
  • 1
    @twigg1313 I have added a longer explanation about what I meant regarding \Qedsymbol -- it would be not daunting to use. It is time for me to crash but I will check back here tomorrow but possibly think about nesting. – Andrew Sep 24 '14 at 14:16
3

This is adapted from what I have used in the past (not sure where I got it from):

enter image description here

Code:

\documentclass{article}
\usepackage{amsmath}

\newcommand*{\QED}[1]{%
        \ifmmode% Check for math mode.
            \tag*{\fbox{#1}}% 
        \else%
            {\rightskip\fill\parfillskip-\rightskip%
            \linepenalty100%
            \exhyphenpenalty0%
            \linebreak[0] % <-- Need space here (allows for a break.
            \hspace*{\fill}\fbox{#1}}%
        \fi%
}%


\begin{document}
In text mode you can use \verb|QED| as shown here.\QED{2.2}

You can also use it in math mode
\begin{align*}
             F &= ma \\
    \implies E &= mc^2\QED{2.3}
\end{align*}
\end{document}
  • This is definitely a good start, and one which I appreciate tremendously. My ignorance regarding modification of theorem environments remains, however: If I have \begin{theorem}...\end{theorem}, how can I capture "information about the theorem" (its number, its name if I've given it a name, etc.) to include within the \QED{}? Essentially, I'd like to modify your method to make it more "automated" (so that it's "fed" by the information associated to the theorem/lemma/proposition), if that makes sense...you know, to avoid having to manually type the theorem number within \QED each time, etc. – cstover Sep 24 '14 at 5:51
0

The ntheorem package allows to redefine \qedsymbol quite simply. Combining with xparse, I define a Proof environmment, that can take two optional arguments: the first is the optional argument of a classical proof environment, but delimited by parentheses ; the second optional argument is the end-of-proof symbol, which defaults to a square, but can be a reference to the theorem that is proved (actually, any reference or any text). In the latter case, I chose to put it in an \fcolorbox.

Example:

\documentclass[A4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[svgnames]{xcolor}
\usepackage{amsfonts,empheq}
\usepackage[amsmath, thref, thmmarks]{ntheorem}
\usepackage{cleveref}
\usepackage{xparse}
\usepackage{chngcntr}

\theoremstyle{plain}
\theoremseparator{.} \theoremheaderfont{\bfseries}
\theorembodyfont{\itshape}
\newtheorem{Thm}{Theorem}[section]
\newtheorem{Lem}{Lemma}%[section]
\theoremstyle{nonumberplain}
\theoremheaderfont{\itshape}
\theorembodyfont{\upshape}

\newtheorem{proof}{Proof}
\NewDocumentEnvironment{Proof}{d() o}
{\IfNoValueTF{#1}{\begin{proof}}{\begin{proof}[#1]}
 \IfNoValueTF{#2}{\qedsymbol{\ensuremath{\Box}}}{\qedsymbol{\fcolorbox{red}{Lavender}{\color{red}\upshape#2}}}}%
{\qed\end{proof}}%
\counterwithin{Lem}{Thm}
\begin{document}

\section{Some Not So Standard Results}

\begin{Thm}\label{special}
   $ \mathrm{SL}_n(\mathbb{K}) ⊂ \mathrm{ GL}_n(\mathbb{K})$.
\end{Thm}

\begin{Proof}[\Cref{special}]
We shall prove first:

\begin{Lem}\label{basic}
   $ \mathrm{SL}_n(\mathbb{K}) ⊂ \mathrm{ GL}_n(\mathbb{K})$.
\end{Lem}

%% First proof of lemma
\begin{Proof}(of the lemma)[\Cref{basic}]
Easy enough.
\end{Proof}
%% Second proof of lemma
\begin{Proof}(another one)
Still easier proof.
\end{Proof}

%% End of theorem proof
Left as an exercise ;\,o)
\end{Proof}

\end{document} 

enter image description here

  • I can't see proofs here. – egreg Sep 24 '14 at 9:24
  • @egreg. I has misunderstood the question. Read it too hastily. – Bernard Sep 24 '14 at 10:03
  • @Bernard: Even so, I appreciate your response. I did some tinkering using ntheorem, namely by defining a proof environment, etc., but when defining the \qedsymbol for it, I was stuck using fbox around \theThm, etc. Long story short: I can only seem to define a single universal Proof environment and hence can only tie it to one counter; that means it wasn't quite dynamic enough to switch between Theorems, Lemmas, etc. – cstover Sep 24 '14 at 13:44
  • 1
    No — you can define as many proof environments as you wish, and let them their own \qedsymbol — but of course these environments must have different names (say thmproof, lemmaproof, &c.). – Bernard Sep 24 '14 at 13:51
  • 1
    @twigg1313: I completely changed my answer to have dynamic end-of-proofs, with the xparse package. – Bernard Sep 24 '14 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.