Why are my matrix columns not aligned properly? (Left or right whitespace)

If I put the & on the right I get whitespace on the right. If I put the & on the left I get whitespace on the left. I can't find anything about this problem. I just want my matrices to look normal like everyone else's!

$\begin{bmatrix} 1 & -2 & 0 & \\ 5 & 0 & 1 & \\ 1 & 2 & -1 & \end{bmatrix} \begin{bmatrix} & x \\ & y \\ & z \end{bmatrix} = \begin{bmatrix} & 4 \\ & 7 \\ & 3 \end{bmatrix}$


• Welcome to TeX.SE! Vectors (one column matrix) should not contain ampersands: \begin{bmatrix} x \\ y \\ z \end{bmatrix} – Zarko Oct 14 '20 at 3:36

I just want my matrices to look normal like everyone else's!

You didn't define what "to look normal" means to you, so I'll guess that you don't want whitespace and you want integers right-aligned within a column, i.e., as if their implicit decimal markers were aligned.

The amsmath package provides several environments that streamline the inputting of matrices. Among them are matrix (no enclosure), pmatrix (enclosed by round parentheses), bmatrix (enclosed by square brackets), and vmatrix (enclosed by vertical lines). One thing that's nice about their setup is that you don't have a to pre-specify the number of columns; LaTeX can determine the number of columns automatically from the inputs it's given. The column separator character is &, just as is the case in array and tabular environments. To input a column vector, be sure to use no & characters, just \\ line-break directives.

The columns of these environments are centered by default. If you want to left-align or right-align all columns in the matrix, I'd suggest you load the mathtools package (a superset of the amsmath package) and use its "starred" variants of the environments mentioned above. E.g., \begin{bmatrix*}[r] ... \end{bmatrix*} tells LaTeX to right-align the contents of all columns.

I have a hunch that what you really want is this. Am I right?

\documentclass{article}
\usepackage{mathtools}
\begin{document}
$\begin{bmatrix*}[r] 1 & -2 & 0 \\ 5 & 0 & 1 \\ 1 & 2 & -1 \end{bmatrix*} \begin{bmatrix} x \\ y \\ z \end{bmatrix} = \begin{bmatrix} 4 \\ 7 \\ 3 \end{bmatrix}$
\end{document}


You have defined 4 columns with

\begin{bmatrix}
1 & -2 & 0 & \\
5 & 0 & 1 & \\
1 & 2 & -1 &
\end{bmatrix}


and 2 columns with

\begin{bmatrix}
& x \\ & y \\ & z
\end{bmatrix} =
\begin{bmatrix}
& 4 \\ & 7 \\ & 3
\end{bmatrix},


so Latex leaves spaces for each empty columns.

Using the package spalign you can obtain the same answer of @Mico. In fact if I add this MWE:

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath}
\usepackage{spalign}

\begin{document}
\spaligndelims{[}{]}\spalignmat[r]{1 -2 0; 5 0 1;1 2 -1}\spaligndelims{[}{]}\spalignvector{x; y; z} =\spaligndelims{[}{]}\spalignvector{4; 7; 3}
\end{document}


you will have:

For the matrix you have always the option [r] (see \spalignmat[r]) where I have put the command \spaligndelims{[}{]} to switch to square brackets. This package does not use the & and \\ but it is very sensitive to the blank spaces. In fact, to separate the elements of an array you need to insert a blank space.

• Interesting package, spalign (+1), however, the same you can obtain with bmatrix* defined in the mathtools`. – Zarko Oct 14 '20 at 22:54
• @Zarko Dear Zarko, it is absolutely true what you wrote. I simply wanted to inform users that there are also some little-used packages that provide almost identical results. – Sebastiano Oct 15 '20 at 7:41