[Tex/LaTex] \underbrace and align environment

alignmath-modeunderbrace

I want to typeset a proof and want to emphasize what I do in each step e.g. by underbracing an equal-sign when I substitute a variable. In addition I also want to align equal-signs in every line. Unfortunately I don't know how to align an underbraced equal-sign and a non underbraced equal-sign at the same time.

Result of the MWE

Question: How can I align the underbraced and the non underbraced equal-sign vertically?

\documentclass{report}

\usepackage{amsmath} % Mathe
\usepackage{mathtools} % Mathe
\usepackage{amsfonts} % Mathesymbole

\begin{document}

\begin{align*}
    D(D^TD)^{-1} &\underbrace{=}_{\mathclap{D=B(B^TB)^{-1}}} B(B^TB)^{-1}((B(B^TB)^{-1})^TB(B^TB)^{-1})^{-1} \\
      &= B(B^TB)^{-1}(((B^TB)^{-1})^TB^TB(B^TB)^{-1})^{-1}
\end{align*}

\end{document}

Best Answer

You need first of all to make the \underbrace a math relation, for spacing; but you also have to extend the space reserved to it, because an underbrace has a minimum width:

\documentclass{report}

\usepackage{amsmath} % Mathe
\usepackage{mathtools} % Mathe
\usepackage{amsfonts} % Mathesymbole
\usepackage{calc}

\newcommand{\ueq}[1][]{%
  \if\relax\detokenize{#1}\relax
    \sbox0{$\underbrace{=}_{}$}%
    \mathrel{\mathmakebox[\wd0]{=}}
  \else
    \mathrel{\underbrace{=}_{\mathclap{#1}}}
  \fi}

\begin{document}

\begin{align*}
    D(D^TD)^{-1} &\ueq[D=B(B^TB)^{-1}] B(B^TB)^{-1}((B(B^TB)^{-1})^TB(B^TB)^{-1})^{-1} \\
      &\ueq B(B^TB)^{-1}(((B^TB)^{-1})^TB^TB(B^TB)^{-1})^{-1}
\end{align*}

\end{document}

So, if \ueq has no optional argument it produces an equal sign as wide as an underbraced one, otherwise it adds the underbrace.

enter image description here

Related Question