# Aligning the entries of two matrices vertically

When it comes to matrix multiplication AB=C, it becomes easier to visualize everything if I put B on top of C. However, when the actual entries vary in length, matrices B and C fail to align vertically. Below is my code

    \begin{aligned} & \begin{bmatrix} 1&1&0\\ 1&0&1\\ 1&-1&0\\ 1&0&-1 \end{bmatrix} \\ \begin{bmatrix} 1&1&1&1\\ 1&0&-1&0\\ 0&1&0&-1 \end{bmatrix} & \begin{bmatrix} 4&0&0\\ 0&2&0\\ 0&0&2 \end{bmatrix} \end{aligned}


and here is the outcome:

Right now, I'm simply tring to add a bunch of \; after each entry to increase the width of the matrix. This method might be expedient, but it certainly won't work well when the matrices are large and has many entries...

Is there a way to fix this tiny issue? Thank you.

If you prefer the centered alignment, the tabstackengine package allows the fixing of all column widths to the widest in the matrix. This way, you only need to pad a single element of the narrower matrix to bring them to a common width:

\documentclass{article}
\usepackage{amsmath,tabstackengine}
\setstacktabbedgap{10pt}
\begin{document}
\fixTABwidth{T} \begin{aligned} & \bracketMatrixstack{ 1&1&0\\ 1&0&1\\ 1&-1&0\\ 1&0&-1 } \\ \bracketMatrixstack{ 1&1&1&1\\ 1&0&-1&0\\ 0&1&0&-1 } & \bracketMatrixstack{ 4&0&0\\ 0&2&0\\ 0&0&\mkern7mu 2\mkern7mu } \end{aligned}
\end{document}


If right alignment is preferred, a single \phantom{-} is necessary:

\documentclass{article}
\usepackage{amsmath,tabstackengine}
\setstacktabbedgap{10pt}
\begin{document}
\fixTABwidth{T} \begin{aligned} & \bracketMatrixstack[r]{ 1&1&0\\ 1&0&1\\ 1&-1&0\\ 1&0&-1 } \\ \bracketMatrixstack[r]{ 1&1&1&1\\ 1&0&-1&0\\ 0&1&0&-1 } & \bracketMatrixstack[r]{ 4&0&0\\ 0&2&0\\ 0&0&\phantom{-}2 } \end{aligned}
\end{document}


I suggest you (a) load the mathtools package, (b) use {bmatrix*}[r] environments instead of bmatrix environments, and (c) use one \phantom{-} instruction each in columns 2 and 3 of the third matrix, to equalize the widths of these columns with the columns of the matrix that hovers above it.

\documentclass{article}
\usepackage{mathtools} % for 'bmatrix*' env.
\begin{document}
\begin{aligned} & \begin{bmatrix*}[r] 1&1&0\\ 1&0&1\\ 1&-1&0\\ 1&0&-1 \end{bmatrix*} \\ \begin{bmatrix*}[r] 1&1&1&1\\ 1&0&-1&0\\ 0&1&0&-1 \end{bmatrix*} & \begin{bmatrix*}[r] 4&\phantom{-}0&\phantom{-}0\\ 0&2&0\\ 0&0&2 \end{bmatrix*} \end{aligned}
\end{document}

• Awesome!! The {bmatrix*}[r] with \phantom completely solved this issue! It seems like \phantom{-} basically creates a "ghost" - without actually displaying it. I noticed that now every number is vertically aligned, and the negative sign doesn't change the alignment. Is that what the [r] after {bmatrix*} does? – Skorpion May 27 at 9:29
• @Skorpion - Indeed, the [r] portion in {bmatrix*}[r] performs right-hand alignment of the contents of each column. I'll let you work out that {bmatrix*}[l] might bring about. :-) – Mico May 27 at 9:32

A nicematrix attempt:

• Wrap the aligned environment inside NiceMatrixBlock, and use option auto-columns-width.
• Use bNiceMatrix instead of bmatrix environment, and use options R to align every cell to the right.

Note that the option auto-colums-width uses equal width for every column, hence the result may be a bit too wide for some column without

\documentclass{article}
\usepackage{nicematrix}

\begin{document}
\begin{NiceMatrixBlock}[auto-columns-width] \begin{aligned} & \begin{bNiceMatrix}[R] 1 & 1 & 0 \\ 1 & 0 & 1 \\ 1 & -1 & 0 \\ 1 & 0 & -1 \end{bNiceMatrix} \\ \begin{bNiceMatrix}[R] 1 & 1 & 1 & 1 \\ 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1 \end{bNiceMatrix} & \begin{bNiceMatrix}[R] 4 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 2 \end{bNiceMatrix} \end{aligned} \end{NiceMatrixBlock}
\end{document}



This sets every column inside the block with same width, which may not be the best, but is rather automatic.