# Subtracting matrices of the same dimension, how to make them align?

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} 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?

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} • Who would have thought of using a macro with phantom in its name – Carlos - the Mongoose - Danger Oct 5 '16 at 1:28
• @BeachedWhale - Don Knuth, the creator of TeX, came up with the name. :-) In fact, there's \phantom and \hphantom as well as \vphantom. The "h" and "v" stand for horizontal and vertical, respectively. \phantom{A} is an invisible object -- hence, a "phantom" -- with the exact height, depth, and width of its argument, here, the letter "A". – Mico Oct 5 '16 at 13:20

Instead of inserting custom-sized \vphantoms, you could also insert \mathstrut directives -- one each in both rows of the first bmatrix. (Aside: \mathstrut is defined as \vphantom{(}, i.e., a \mathstrut is a vertical phantom with the depth and height of a ( parenthesis.)

\documentclass{article}
\usepackage{amsmath} % for "bmatrix" environment
\begin{document}
$\begin{bmatrix} x_1\mathstrut & 0 \\ 0\mathstrut & x_2 \end{bmatrix} - \begin{bmatrix} x_1^2 & 0 \\ 0 & x_2^2 \end{bmatrix}$
\end{document}


The \bracketMatrixstack of the tabstackengine package is automatically \strutted, and so both matrices are of the same height. Note that inter-column gap and inter-row baselineskip can also be set independently, by way of

\setstacktabbedgap{1.5ex}
\setstackgap{L}{1.3\normalbaselineskip}


Here is the MWE.

\documentclass{article}
\usepackage{tabstackengine}
\setstacktabbedgap{1.5ex}
\setstackgap{L}{1.3\normalbaselineskip}
\begin{document}
$\bracketMatrixstack{x_1 & 0 \\ 0 & x_2} - \bracketMatrixstack{ x_1^2 & 0 \\ 0 & x_2^2 }$
\end{document} You can modify (locally) \arraystretch

\documentclass{article}
\usepackage{amsmath}

\begin{document}
My solution is to act on \verb|\arraystretch|
$\renewcommand{\arraystretch}{1.1} \begin{bmatrix} x_1 & 0 \\ 0 & x_2 \end{bmatrix} - \begin{bmatrix} x_1^2 & 0 \\ 0 & x_2^2 \end{bmatrix}$
and it can be compared with Heiko's using \verb|\vphantom|
$\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}


Resetting \arraystretch should be done outside alignment environments such as align, because alignment cells form groups. It's not difficult to define a new environment taking as argument the factor \arraystretch should be reset to. 