# QED symbol with theorem counter as subscript

I am using the amsthm package and the proof enviroment it provides. Occasionally I am writing proofs for theorems which include several claims with their respective proofs. I would like to redefine the qed symbol to be the usual tombstone symbol, having a subscript mentioning the number of the preceding claim/theorem.

I am not an advanced latex user so I am not sure how to refer to the theorem/claim counter, so any help is appreciated.

• Welcome to TEX.SE-Please try to make your question clear and simple by giving a minimal working example (MWE). – Sebastiano Mar 12 '17 at 20:25

Is this similar to what you'd like to get?

\documentclass{article}
\usepackage{amsthm,regexpatch}

\makeatletter
\let\cproof\proof
\def\endcproof{%
\renewcommand\qedsymbol{\openbox\rlap{\textsubscript{\std@currentclaim}}}%
\endproof
}
\makeatother

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{claim}{Claim}[theorem]
\renewcommand{\theclaim}{\arabic{claim}}

\makeatletter
\xpretocmd{\claim}{\std@patch@thm}{}{}
\newcommand{\std@patch@thm}{%
\makeatletter
\regexpatchcmd{\@thm}
{(\c{refstepcounter}\cB.\cP.2\cE.)}
{\1\c{std@savelabel}}
{}{}%
\makeatother
}
\newcommand{\std@savelabel}{\xdef\std@currentclaim{\@currentlabel}}
\makeatother

\begin{document}

\begin{theorem}
Every duck can fly.
\end{theorem}

\begin{proof}
We made a series of steps.

\begin{claim}
Ducks are nice.
\end{claim}

\begin{cproof}
Who can say differently?
\end{cproof}

\begin{claim}
Ducks rule.
\end{claim}

\begin{cproof}
See the \TeX.StackExchange chat.
\end{cproof}

The main statement follows.
\end{proof}

\end{document}


LaTeX has no notion of “last theorem”. So we have to hook in \@thm when it does \refstepcounter. This code limits the surgery to the claim environment.

A slightly different approach. I add a hook to \@thm just after \refstepcounter{#2}, so the \@currentlabel will be available.

\documentclass{article}
\usepackage{amsthm,xpatch}

\makeatletter
% add a hook to \@thm
\xpatchcmd{\@thm}
{\refstepcounter{#2}}
{\refstepcounter{#2}\csname #2hook\endcsname}
{}{}
\newcommand{\settheoremhook}[2]{%
\@namedef{#1hook}{#2}%
}
\let\cproof\proof
\def\endcproof{%
\renewcommand\qedsymbol{\openbox\rlap{\textsubscript{\currentclaim}}}%
\endproof
}
\makeatother

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{claim}{Claim}[theorem]
\renewcommand{\theclaim}{\arabic{claim}}

\makeatletter
\settheoremhook{claim}{\global\let\currentclaim\@currentlabel}
\makeatother

\begin{document}

\begin{theorem}
Every duck can fly.
\end{theorem}

\begin{proof}
We made a series of steps.

\begin{claim}
Ducks are nice.
\end{claim}

\begin{cproof}
Who can say differently?
\end{cproof}

\begin{claim}
Ducks rule.
\end{claim}

\begin{cproof}
See the \TeX.StackExchange chat.
\end{cproof}

The main statement follows.
\end{proof}

\end{document}


The hook executes \csname #2hook\endcsname, so it will do nothing unless the hook has been defined with \settheoremhook. Such hook can only be set for “main” theorems, so not for the children of other theorems (in the example, you cannot do \settheoremhook{lemma}, for instance).

The output is the same.

# With hyperref

If hyperref is loaded, the patch has to be delayed (and is different).

Here is code that works independently of hyperref being loaded.

\documentclass{article}
\usepackage{amsthm,xpatch}
\usepackage{hyperref}

\makeatletter
% add a hook to \@thm
\if@tempswa
\catcode#=12
\AtBeginDocument{%
\xpatchcmd{\@thm}
{\@currentHref}
{\@currentHref\csname #2hook\endcsname}
{}{}%
}
\catcode#=6
\else
\xpatchcmd{\@thm}
{\refstepcounter{#2}}
{\refstepcounter{#2}\csname #2hook\endcsname}
{}{}
\fi

\newcommand{\settheoremhook}[2]{%
\@namedef{#1hook}{#2}%
}
\let\cproof\proof
\def\endcproof{%
\renewcommand\qedsymbol{\openbox\rlap{\textsubscript{\currentclaim}}}%
\endproof
}
\makeatother

\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{claim}{Claim}[theorem]
\renewcommand{\theclaim}{\arabic{claim}}

\makeatletter
\settheoremhook{claim}{\global\let\currentclaim\@currentlabel}
\makeatother

\begin{document}

\begin{theorem}
Every duck can fly.
\end{theorem}

\begin{proof}
We made a series of steps.

\begin{claim}
Ducks are nice.
\end{claim}

\begin{cproof}
Who can say differently?
\end{cproof}

\begin{claim}
Ducks rule.
\end{claim}

\begin{cproof}
See the \TeX.StackExchange chat.
\end{cproof}

The main statement follows.
\end{proof}

\end{document}

• Thank you. Your solution does more or less what I needed. – St. D. Mar 13 '17 at 17:56
• @St.D. I have some ideas about how to improve the interface. Feel free to add comments. – egreg Mar 13 '17 at 18:04
• One improvement I can think of is to have "Claim x" as a subscript instead of just the number x. – St. D. Mar 14 '17 at 10:20
• @St.D. Just change \textsubscript{\currentclaim} into \textsubscript{Claim \currentclaim} – egreg Mar 14 '17 at 10:21
• For some reason, when I implement the commands in my main document, I get the error "! Undefined control sequence. <argument> ...\z@ \selectfont Claim \currentclaim". I am still looking at what is the problem, but do you have any idea what might be? – St. D. Mar 14 '17 at 10:28

Instead of redefining the \qed command I would define a version with an optional parameter to be written as subscript.

\documentclass{article}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}
\newcommand\myqed[1][]{\qed\rlap{$_{\mathit{#1}}$}}
\begin{document}
\begin{theorem}
Let $a$ and $b$ be two odd integers. Then
\begin{enumerate}
\item $ab$ is odd
\item $a+ b$ is even
\end{enumerate}
\end{theorem}
\begin{proof}
Let $a=2k+1$ and $b=2l+1$. Then $ab=(2k+1)(2l+1)=4kl+2k+2l+1=2(2kl+k+l)+1$ which proves the first part.
\myqed[1]

Similarly $a+b=2k+2l+2=2(k+l+1)$, which is even, and the second part is proven.
\myqed[2]

This completes the proof.
\end{proof}
\end{document}