[Tex/LaTex] Misplaced alignment tab character when building a matrix

errorsmath-modematrices

I'm still new to latex. I've been having a problem with building a matrix and I have no idea what I'm doing (or what I'm doing wrong), so be gentle please 🙂

\begin{matrix} 
5 & 6 & 7 \\
4 & P & 0 \\
3 & 2 & 1
\end{matrix}

This is the code for building the matrix and I keep getting error:

Misplaced alignment tab character & 5 &
...

Can anyone shine some light please?

Best Answer

The matrix macro is defined as part of the basic LaTeX distribution, but you're probably after the amsmath implementation of it:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\[
\begin{matrix}
5 & 6 & 7 \\
4 & P & 0 \\
3 & 2 & 1
\end{matrix}
\]
\end{document}

The older (TeX) \matrix equivalent actually looks like this:

\documentclass{article}
\begin{document}
\[
\matrix{%
5 & 6 & 7 \cr
4 & P & 0 \cr
3 & 2 & 1
}
\]
\end{document}

Note that \matrix is a macro taking a single argument, and not an environment.

Related Question