Pushing \qed to the right within a displayed formula

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


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


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{[email protected]}{%
    \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 [email protected]\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.