# matrix alignment with preceeding unary minus

I've never quite been pleased with the options for aligning matrices in TeX when there is a preceding unary minus in some of the rows (but not all). I'd like to implement an environment that automatically detects a preceding unary minus and adds a \phantom{-} to each row not containing one. I'm not sure how to go about this but also willing to accept other solution ideas. My main parameter is that it needs to be just as simple of an environment to use as the ams bmatrix environment.

Here is the output demonstrating why I believe \phantom{-} is the way to go

Here is the code to demonstration:

\documentclass{article}

\usepackage{mathtools} %for minus signes in matrices

\usepackage{amsmath,amsfonts,amssymb,amsthm} % For math equations, theorems, sy

\begin{document}

\begin{tabular}{cc}
Usual & \\ \hline
& \\
$\begin{bmatrix} 1 & 2 & 12345\\ -5 & 2 & -5 \\ 0 & -1 & 1 \end{bmatrix}$
&
$\begin{bmatrix} 1-\lambda & 2 & 12345\\ -5 & 2-\lambda & -5 \\ 0 & -1 & 1-\lambda \end{bmatrix}$\\[1cm]

Right Aligned &\\  \hline
& \\
$\begin{bmatrix*}[r] 1 & 2 & 12345\\ -5 & 2 & -5 \\ 0 & -1 & 1 \end{bmatrix*}$
&
$\begin{bmatrix*}[r] 1-\lambda & 2 & 12345\\ -5 & 2-\lambda & -5 \\ 0 & -1 & 1-\lambda \end{bmatrix*}$\\[1cm]

Phantom Minues & \\  \hline
& \\

$\begin{bmatrix} \phantom{-}1 & \phantom{-}2 & \phantom{-}12345\\ -5 & \phantom{-}2 & -5 \\ \phantom{-}0 & -1 & \phantom{-}1 \end{bmatrix}$
&
$\begin{bmatrix} \phantom{-}1-\lambda & \phantom{-}2 & \phantom{-}12345\\ -5 & \phantom{-}2-\lambda & -5 \\ \phantom{-}0 & -1 & \phantom{-}1-\lambda \end{bmatrix}$
\end{tabular}
\end{document}

• not related to your question but notice that amssymb loads amsfonts automatically.
– user4686
May 17, 2014 at 20:09

You can declare a column type that looks for -

\documentclass{article}

\usepackage{array}
\usepackage{mathtools} %for minus signes in matrices

\usepackage{amsmath,amsfonts,amssymb,amsthm} % For math equations, theorems, sy
\makeatletter
\def\zz\ignorespaces{\@ifnextchar-{}{\phantom{-}}}
\newcolumntype{C}{>{\zz}{c}}
\makeatother

\begin{document}

\begin{tabular}{cc}
Usual & \\ \hline
& \\
$\begin{bmatrix*}[C] 1 & 2 & 12345\\ -5 & 2 & -5 \\ 0 & -1 & 1 \end{bmatrix*}$
&
$\begin{bmatrix*}[C] 1-\lambda & 2 & 12345\\ -5 & 2-\lambda & -5 \\ 0 & -1 & 1-\lambda \end{bmatrix*}$\\[1cm]
$\begin{bmatrix*}[C] 1 & 2 & 12345\\ 5 & 2 & -5 \\ 0 & -1 & 1 \end{bmatrix*}$
&
$\begin{bmatrix*}[C] 1-\lambda & 2 & 12345\\ 5 & 2-\lambda & -5 \\ 0 & -1 & 1-\lambda \end{bmatrix*}$\\[1cm]

$\begin{bmatrix} \phantom{-}1 & \phantom{-}2 & \phantom{-}12345\\ -5 & \phantom{-}2 & -5 \\ \phantom{-}0 & -1 & \phantom{-}1 \end{bmatrix}$
&
$\begin{bmatrix} \phantom{-}1-\lambda & \phantom{-}2 & \phantom{-}12345\\ -5 & \phantom{-}2-\lambda & -5 \\ \phantom{-}0 & -1 & \phantom{-}1-\lambda \end{bmatrix}$
\end{tabular}
\end{document}

• Hmm I wouldn't have guessed that adding a \phantom{-} on every row that didn't have a {-} would have goofed the column placement, but it is imperceivable in these examples at least. Thanks :) May 17, 2014 at 16:21
• @NateIverson "goofed" ? May 17, 2014 at 16:23
• change the first column to be 1 5 0 instead, the column is then placed in a different position. Maybe not such a good working example. This is 99% of what I was hoping for, I'll certainly use it unless there is something slightly better. May 17, 2014 at 16:28
• @NateIverson I updated the example with a 5 and showed the image it is as far as I can see identical spacing to the code you suggested with \phantom added explicitly. The column is of course wider than the column would be in a standard bmatrix as there is space left for a - in each entry, Perhaps what you really wanted was a 2-pass system that only adds the phantom- in columns where one cell starts with - but that's a lot more complicated (and not what you asked for:-) May 17, 2014 at 16:37
• David: You would have to remove the explicitly added \phantom{-} also in the example without minuses on the column. yes agreed what I wanted was far more complicated and I didn't communicate that very well. I didn't think it would necessarily take 2 passes, but maybe that's a byproduct of using environments. May 17, 2014 at 16:47