How to typeset and annotate a polynomial long division

Here is one way to do this:

enter image description here

Notes:

  • The booktabs package was used to provide flexible horizontal rules.
  • The \Ph macro uses \hphantom{)} to insert a horizontal space equivalent to the closing bracket to get things all aligned.

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{array}
\newcommand*{\Ph}{\hphantom{)}}%
\begin{document}
$\begin{array}{r@{} r@{} r r}
  x^2 &{}+4x\Ph&{}+4\Ph       &\fbox{$(x+2)$} \\
-(x^2 &{}+ 2x) &\\
\cmidrule{1-2}
      & 2x\Ph &{}+4\Ph\\
      &-(2x\Ph &{}+4) \\
\cmidrule{2-3}
      & &0\Ph
\end{array}$
\end{document}

Try looking at the package polynom.sty (CTAN, TeXdoc). It can even do the division for you.