# Pushing \qed to the right within a displayed formula

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?

-
I've updated my answer to make it simpler (as you seem to want, from a comment of yours in tex.stackexchange.com/q/66193). If none of the answers below are what you're looking for, you should really give us the code you are using for your theorems, as it's difficult to answer in the dark like that. – Philippe Goutet Aug 7 '12 at 17:08
Related Question for similar issue in text mode: Bump right-aligned text to next line iff no room. – Peter Grill Mar 2 '13 at 6:46

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}


-

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:

\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
\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
\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}%
}
\def\linebox@qed{\hfil\hbox{\qedsymbol}\hfilneg}
\def\math@qedhere{%
\@ifundefined{\@currenvir @qed}{%
}{%
\@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@
\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

-
This worked for me after other options not working! – Paul Elfish Sep 12 '13 at 2:37