[Tex/LaTex] Matrix with evenly spaced columns

amsmathcolumnsmathtoolsmatricesspacing

Similar question: How can I make a similar matrix with even column spacing?


The following code is simple to write/read/maintain, but looks terrible.

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}

\begin{equation*}
    \begin{bmatrix*}[r]
        1 &  0 & 0 \\
        0 & -1 & 0 \\
        0 &  0 & 1
    \end{bmatrix*}  
\end{equation*}

\end{document}

enter image description here

This is because the negative sign adds extra space between the first and second columns, but not between the second and third.


One could naively put a \phantom{-} in each column, but this isn't right either.

\begin{equation*}
    \begin{bmatrix*}[r]
        \phantom{-}1 &  0 & 0 \\
        0 & -1 & 0 \\
        0 &  0 & \phantom{-}1
    \end{bmatrix*}
\end{equation*}

enter image description here

This is because we don't need extra space to the left of each column, but an even amount of space between each column.


\begin{gather*}
    \begin{bmatrix*}[r]
        1 &  0 & 0 \\
        0 & -1 & 0 \\
        0 &  0 & 1
    \end{bmatrix*} \\
    \begin{bmatrix*}[r]
        1 &  0 & 0 \\
        0 & -1 & 0 \\
        0 &  0 & \phantom{-}1
    \end{bmatrix*}  
\end{gather*}

enter image description here

Much better. (The second one, compared to the original above it.)


However, in larger matrices, this can easily become hard to read and quite a hassle to maintain. Is there some simple way to tell it to put an equal amount of space (say, between the right hand sides) of columns 1&2, 2&3, 3&4, … ?

Ideally, I'd like the body of my matrices to contain nothing other than the content:

1 &  0 & 0 \\
0 & -1 & 0 \\
0 &  0 & 1

Best Answer

Here's a version that uses a new environment brmatrix. For the first column the standard r column is used, for the other columns we use a new column type M which also aligns the content right, but uses a fixed width \@brcolwidth. As this is a problem if the content may be wider than the cell and overlap with the other columns, an optional parameter for setting the cell width directly has been added.

A full example with the standard bmatrix in the first and the new brmatrix in the other cases:

\documentclass{article}

\usepackage{array}
\usepackage{amsmath}
\usepackage{mathtools}

\newcolumntype{M}[1]{>{\hbox to #1\bgroup\hss$}l<{$\egroup}}

\makeatletter
\newcommand\@brcolwidth{0.67em}
\newenvironment{brmatrix}{%
    \left[%
    \hskip-\arraycolsep
    \new@ifnextchar[\@brarray{\@brarray[\@brcolwidth]}%
}{%
    \endarray
    \hskip -\arraycolsep
    \right]%
}
\def\@brarray[#1]{\array{r*\c@MaxMatrixCols {M{#1}}}}
\makeatother

\begin{document}

\begin{equation*}
    \begin{bmatrix*}[r]
        1 &  0 & 0 \\
        0 & -1 & 0 \\
        0 &  0 & 1
    \end{bmatrix*}
\end{equation*}

\begin{equation*}
    \begin{brmatrix}
        1 &  0 & 0 \\
        0 & -1 & 0 \\
        0 &  0 & 1
    \end{brmatrix}
\end{equation*}

\begin{equation*}
    \begin{brmatrix}
        -1 &  0 &  0 \\
         0 & -1 &  0 \\
         0 &  0 & -1
    \end{brmatrix}
\end{equation*}

\begin{equation*}
    \begin{brmatrix}[2em]
        123 &   0 & 0 \\
          0 & -12 & 0 \\
          0 &   0 & 12345
    \end{brmatrix}
\end{equation*}

\end{document}

Output:

enter image description here