57

In my own custom theorem environment, I'm using the \qed command to create a white box. This works wonderfully when the theorem ends with a regular paragraph -- the box is pushed to the right where it should be.

Note that I just type out the \qed manually whenever I need it. It works fine. I don't need it automated.

However, if I use this command when the theorem ends with displayed mathmode, the box is just put there right beside the formula -- it is not pushed to the right. How can I accomplish that?

2
42

If the QED symbol (or tombstone) is needed only rarely, I suggest to use the \qedhere feature provided by amsthm.

\documentclass{article}
\usepackage{amsthm}

\newtheorem{thm}{Theorem}
\begin{document}

\begin{thm}
Some text some text some text some text some text some text some text some text some text
some text some text some text some text some text some text\qed
\end{thm}

\begin{thm}
Some text some text some text some text some text some text some text some text some text
some text some text some text some text some text some text
\[
\pushQED{\qed} 
a=b\qedhere
\popQED
\]     
\end{thm}     

\end{document}

enter image description here

5
  • 9
    Can you explain what the pushQED/popQED commands do?
    – blinry
    May 30 '16 at 11:06
  • 2
    @blinry amsthm implements a stack for nesting proofs, possibly with different commands (usually \qed), so it's best to comply.
    – egreg
    May 30 '16 at 11:51
  • 3
    Why does this not work if pushQED and popQED are removed? is there a way to do it without push and pop?
    – 6005
    Mar 24 '19 at 0:54
  • Additionally, does this require being inside the theorem environment? And thanks a lot for the helpful answer.
    – 6005
    Mar 24 '19 at 0:55
  • 1
    @6005 No, it doesn't require being in theorem
    – egreg
    Mar 24 '19 at 9:52
18

For the qed symbol to appear flushed right in the last line of aligned equations, I use:

\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}

\renewcommand{\j}{\ensuremath{\text{j}}}
\newcommand{\qed}{\tag*{$\blacksquare$}}

\begin{document}
    \begin{align*}
        \left(z_1\cdot z_2\right)^*
            &=\left(\left(a_1+\j b_1\right)\cdot \left(a_2+\j b_2\right)\right)^* \\
            &=\left(\left(a_1a_2+b_1b_2\right)+\j\left(a_1b_2+a_2b_1\right)\right)^* \\
            &=\left(a_1a_2-b_1b_2\right)-\j\left(a_1b_2+a_2b_1\right) \\
            &=a_1a_2-b_1b_2-\j a_1b_2-\j a_2b_1 \\
            &=a_1a_2-a_1\j b_2-a_2\j b_1+\left(-\j\right)^2b_1b_2 \\
            &=\left(a_1-\j b_1\right)\cdot\left(a_2-\j b_2\right) \\
            &=\left(a_1+\j b_1\right)^*\cdot\left(a_2+\j b_2\right)^* \\
            &=z_1^*\cdot z_2^*
            \qed
    \end{align*}            
\end{document}

enter image description here

5
  • 1
    Thanks! I've been reading quite often, but this is the first time I felt I could actually contribute.
    – Douba
    Nov 14 '13 at 14:46
  • Amazing. That did for me.
    – Ory Band
    Mar 14 '14 at 13:21
  • 2
    @Douba Maybe it is better to use \mathrm{j} instead of \text{j}.
    – L. F.
    Mar 22 '19 at 12:07
  • @L.F. You may be right, thank you for the suggestion. I do not usually use \mathrm, because it explicitly requests a roman font. I prefer being able to switch the looks of a document in a centralized way and only mark up semantics elsewhere. However, I see that \text falls back to the surrounding font, which may not be what I expect either.
    – Douba
    Jan 31 '20 at 9:49
  • 1
    Used with the proof environment, this method places \qed symbol twice. To avoid this, I add the command \renewcommand{\qedsymbol}{} and \vspace{-\baselineskip} right before \end{proof}.
    – mathreader
    Oct 23 '20 at 3:42
9

You should consider using the ntheorem package which does this kind of thing from the start. But if you really need to use your custom theorem environments, you can take inspiration from what amsthm does. By stripping the code of everything which is not necessary, you would get:

result of the code

\documentclass{article}

\usepackage{lipsum}

\newtheorem{theorem}{Theorem}

\providecommand{\openbox}{\leavevmode
  \hbox to.77778em{%
  \hfil\vrule
  \vbox to.675em{\hrule width.6em\vfil\hrule}%
  \vrule\hfil}}
\makeatletter
\DeclareRobustCommand{\qed}{%
  \ifmmode
    \eqno \def\@badmath{$$}%$$
    \let\eqno\relax \let\leqno\relax \let\veqno\relax
    \hbox{\openbox}%
  \else
    \leavevmode\unskip\penalty9999 \hbox{}\nobreak\hfill
    \quad\hbox{\openbox}%
  \fi
}
\makeatother

\begin{document}

\begin{theorem}
\lipsum*[1] \qed
\end{theorem}

\begin{theorem}
\lipsum*[1]
\[
  f(x)=ax^2+bx+c \qed
\]
\end{theorem}

\end{document}

Of course, in some situations, you might have problems, so for a more robust way of doing it, you should copy/paste the amsthm way of doing it:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{lipsum}

\makeatletter
\let\@xp\expandafter
\DeclareRobustCommand{\qed}{%
  \ifmmode \mathqed
  \else
    \leavevmode\unskip\penalty9999 \hbox{}\nobreak\hfill
    \quad\hbox{\qedsymbol}%
  \fi
}
\let\QED@stack\@empty
\let\qed@elt\relax
\newcommand{\pushQED}[1]{%
  \toks@{\qed@elt{#1}}\@temptokena\expandafter{\QED@stack}%
  \xdef\QED@stack{\the\toks@\the\@temptokena}%
}
\newcommand{\popQED}{%
  \begingroup\let\qed@elt\popQED@elt \QED@stack\relax\relax\endgroup
}
\def\popQED@elt#1#2\relax{#1\gdef\QED@stack{#2}}
\newcommand{\qedhere}{%
  \begingroup \let\mathqed\math@qedhere
    \let\qed@elt\setQED@elt \QED@stack\relax\relax \endgroup
}
\newif\ifmeasuring@
\newif\iffirstchoice@ \firstchoice@true
\def\setQED@elt#1#2\relax{%
  \ifmeasuring@
  \else \iffirstchoice@ \gdef\QED@stack{\qed@elt{}#2}\fi
  \fi
  #1%
}
\def\qed@warning{%
  \PackageWarning{amsthm}{The \@nx\qedhere command may not work
    correctly here}%
}
\newcommand{\mathqed}{\quad\hbox{\qedsymbol}}
\def\linebox@qed{\hfil\hbox{\qedsymbol}\hfilneg}
\@ifpackageloaded{amsmath}{%
  \def\math@qedhere{%
    \@ifundefined{\@currenvir @qed}{%
      \qed@warning\quad\hbox{\qedsymbol}%
    }{%
      \@xp\aftergroup\csname\@currenvir @qed\endcsname
    }%
  }
  \def\displaymath@qed{%
    \relax
    \ifmmode
      \ifinner \aftergroup\linebox@qed
      \else
        \eqno
        \let\eqno\relax \let\leqno\relax \let\veqno\relax
        \hbox{\qedsymbol}%
      \fi
    \else
       \aftergroup\linebox@qed
    \fi
  }
  \@xp\let\csname equation*@qed\endcsname\displaymath@qed
  \def\equation@qed{%
    \iftagsleft@
      \hbox{\phantom{\quad\qedsymbol}}%
      \gdef\alt@tag{%
        \rlap{\hbox to\displaywidth{\hfil\qedsymbol}}%
        \global\let\alt@tag\@empty
      }%
    \else
      \gdef\alt@tag{%
        \global\let\alt@tag\@empty
        \vtop{\ialign{\hfil####\cr
                \tagform@\theequation\cr
                \qedsymbol\cr}}%
        \setbox\z@
      }%
    \fi
  }
  \def\qed@tag{%
    \global\tag@true \nonumber
    &\omit\setboxz@h {\strut@ \qedsymbol}\tagsleft@false
    \place@tag@gather
    \kern-\tabskip
    \ifst@rred \else \global\@eqnswtrue \fi \global\advance\row@\@ne \cr
  }
  \def\split@qed{%
    \def\endsplit{\crcr\egroup \egroup \ctagsplit@false \rendsplit@
      \aftergroup\align@qed
    }%
  }
  \def\align@qed{%
    \ifmeasuring@ \tag*{\qedsymbol}%
    \else \let\math@cr@@@\qed@tag
    \fi
  }
  \@xp\let\csname align*@qed\endcsname\align@qed
  \@xp\let\csname gather*@qed\endcsname\align@qed
%% Needs some patching up for amsmath 1.2
}{% end of amsmath branch, start plain LaTeX branch
  \def\math@qedhere{%
    \@ifundefined{\@currenvir @qed}{%
      \qed@warning \aftergroup\displaymath@qed
    }{%
      \@xp\aftergroup\csname\@currenvir @qed\endcsname
    }%
  }
  \def\displaymath@qed{%
    \relax
    \ifmmode
      \ifinner \aftergroup\aftergroup\aftergroup\linebox@qed
      \else
        \eqno \def\@badmath{$$}%
        \let\eqno\relax \let\leqno\relax \let\veqno\relax
        \hbox{\qedsymbol}%
      \fi
    \else
       \aftergroup\linebox@qed
    \fi
  }
  \@ifundefined{ver@leqno.clo}{%
    \def\equation@qed{\displaymath@qed \quad}%
  }{%
    \def\equation@qed{\displaymath@qed}%
  }
  \def\@tempa#1$#2#3\@nil{%
    \def\[{#1$#2\def\@currenvir{displaymath}#3}%
  }%
  \expandafter\@tempa\[\@nil
}
\@ifpackageloaded{amstex}{%
  \def\@tempa{TT}%
}{%
  \@ifpackageloaded{amsmath}{%
    \def\@tempb#1 v#2.#3\@nil{#2}%
    \ifnum\@xp\@xp\@xp\@tempb\csname ver@amsmath.sty\endcsname v0.0\@nil
       <\tw@
      \def\@tempa{TT}%
    \else
      \def\@tempa{TF}%
    \fi
  }{%
    \def\@tempa{TF}
  }%
}
\if\@tempa
  \renewcommand{\math@qedhere}{\quad\hbox{\qedsymbol}}%
\fi
\newcommand{\openbox}{\leavevmode
  \hbox to.77778em{%
  \hfil\vrule
  \vbox to.675em{\hrule width.6em\vfil\hrule}%
  \vrule\hfil}}
\DeclareRobustCommand{\textsquare}{%
  \begingroup \usefont{U}{msa}{m}{n}\thr@@\endgroup
}
\providecommand{\qedsymbol}{\openbox}
\makeatother

\newtheorem{theorem}{Theorem}
\preto{\theorem}{\pushQED{\qed}}
\preto{\endtheorem}{\popQED}

\begin{document}

\begin{theorem}
\lipsum*[1] \qedhere
\end{theorem}

\begin{theorem}
\lipsum*[1]
\[
  f(x)=ax^2+bx+c \qedhere
\]
\end{theorem}

\end{document}

A few comments on the code: the big chunk is just a copy/paste of the file amsthm.sty. If you decide to use this package, you won't need it anymore. The important point is to modify the {theorem} environment by adding \pushQED{\qed} before it starts and \popQED before it ends. For this, I've used the \preto command from the etoolbox package.

9

Not sure with LaTeX, haven't worked with it for a long time, but in Plain, I would use \eqno.

$$\sum_{n=1}^\infty {1 \over n^2} = {\pi^2 \over 6} \eqno\qed$$
2
  • 1
    This worked for me after other options not working! Sep 12 '13 at 2:37
  • @Palec; For LaTeX, I am using \tag{\qed} to get the same result.....
    – MadyYuvi
    May 23 '19 at 12:58
6

You can insert the display equation in text mode with a forced \displaystyle:

enter image description here

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\newcommand{\qed}{\hfill\rule{1ex}{1ex}}
\newtheorem{theorem}{Theorem}
\begin{document}
\begin{theorem}
\lipsum*[1] \qed
\end{theorem}
\begin{theorem}
\lipsum*[1]
\[
  f(x)=ax^2+bx+c \qed
\]
\end{theorem}
\begin{theorem}
\lipsum*[1]\par
\vspace{\abovedisplayshortskip}
\hfill $\displaystyle f(x)=ax^2+bx+c$ \qed
\end{theorem}
\end{document}

Since the last line of the preceding paragraph and "short", I've inserted a skip of \abovedisplayshortskip. A "long" paragraph ending should use \abovedisplayskip.

3
  • This works. However, there is a problem with "abovedisplayshortskip" because it only works with your particular example, in which the last line of the previous paragraph is short enough. However, the skip should be larger if this line is a bit longer. Can you adjust this so that it works for all situations? Aug 7 '12 at 4:29
  • @MayGodBlessKnuth: As I mentioned at the end of my post, you should use \abovedisplayskip for a longer paragraph. I'll see what can be done to automate this requirement.
    – Werner
    Aug 7 '12 at 4:30
  • Oh, sorry. I must have missed that. But is there no way to combine both into one solution to make it automatic? Aug 7 '12 at 4:30
6

You can use \displaymath@qed from the amsthm package. Due to the @ in the name you'll need a wrapper macro. In the code below I "overload" \qed in such a way that it acts as \displaymath@qed in math mode and as the standard \qed in text mode.

output

\documentclass{article}
\usepackage{amsthm}
\makeatletter
\let\saveqed\qed
\renewcommand\qed{%
   \ifmmode\displaymath@qed
   \else\saveqed
   \fi}
\begin{document}
text
\[
  a=b\makeatletter\displaymath@qed
\]
more text \qed
\end{document}
1
  • 2
    Shouldn't the \makeatletter\displaymath@qed inside the document be just \qed? Feb 19 '17 at 10:49
1

I think I found a much easier work around. Hopefully this helps anyone else who stumbles across this question:

\begin{align}
&E^2 + \frac{1}{4}(-2E^2 +2B^2)\\
 &= 1/2 (E^2 +B^2)\\
 &= \mathlarger{\boldsymbol{\varepsilon}}
\end{align}

\begin{flushright}
\qedsymbol
\end{flushright}
2
  • The question is how to insert the QED symbol within a formula (and not afterwards). In case of an unfavorable page break formula and QED could be separated here.
    – dexteritas
    Sep 28 '20 at 12:08
  • 2
    Hey, thank you for your response. What I read was "..if I use this command when the theorem ends with displayed mathmode, the box is just put there right beside the formula -- it is not pushed to the right. How can I accomplish that?" I feel as though I did help answer that, though I suppose you feel there are better and more nuanced replies. That's fair.
    – Max D
    Sep 29 '20 at 5:48
0

A hack I can use even in mathjax when I take notes in typora is to set the equation number to the qed symbol. Example:

\begin{align}
k_m(x,y) =& \sum_{\substack{h, h'}} k((x,h), (y, h'))p(h|x)p(h'|y)\\
=&  \sum_{\substack{h, h'}} k((y,h), (x,h')) p(h'|x)p(h|y) \\
=& k_m(y, x) \tag*{$\blacksquare$} \\
\end{align}

enter image description here

Caveat:
If you ever reference this equation, you will have that symbol instead of an equation number. But you can of course just reference the line before that instead.

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.