[Tex/LaTex] matrix alignment with preceeding unary minus

matrices

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
\phantom{-} is better

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}

Best Answer

enter image description here

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}
Related Question