[Tex/LaTex] Subtracting matrices of the same dimension, how to make them align

alignmath-modematricespdftexsize;

This question has been burning inside of my soul for a while now

Sometimes I would like to subtract two matrices of the exact same dimension, but since they differ in content ever so slightly, therefore the size that comes out are not the same.

For example: This line of code generates

\begin{bmatrix} x_1 & 0 \\ 0 & x_2 \end{bmatrix} - \begin{bmatrix} x_1^2 & 0 \\ 0 & x_2^2 \end{bmatrix}

enter image description here

As you can see the second matrix is slightly larger. Is there some way to either make the first matrix larger, or align the two matrices at the top?

Best Answer

Macro \vphantom can be used to insert invisible vertical space with the height and depth of the argument:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
  \begin{bmatrix}
    \vphantom{x_1^2}x_1 & 0 \\
    0 & x_2\vphantom{x_2^2}
  \end{bmatrix}
   - \begin{bmatrix} x_1^2 & 0 \\ 0 & x_2^2 \end{bmatrix}
\]
\end{document}

Result