# Misplaced alignment tab character when building a matrix

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?

-
You need to load the amsmath package. Check out the mathmode documentation. Also, posting code fragments like this isn't the best way to get answers. See I've just been told I have to write a minimal example, what is that?. –  Alan Munn May 28 '12 at 19:22
For more on matrices, and the variety of mixes out there, see Where is the \matrix command? –  Werner May 28 '12 at 19:31

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

\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.

-
It started giving me this error "command \implies already defined" and "command \overset already defined" –  fmsf May 28 '12 at 19:56
This appened after importing the amsmath –  fmsf May 28 '12 at 20:01
It worked. had to add the package as the first package :) tks –  fmsf May 28 '12 at 20:12

Standard LaTeX does not have a matrix environment. For historical reasons the original version of LaTeX (LaTeX 2.09) contained a more or less full copy of plain TeX commands including a \matrix command. Some of these plain TeX commands made little to no sense in LaTeX as they used quite different conventions (and some of them didn't even work at all with LaTeX such as \topinsert or \beginsection). Some made sense and they got documented in the LaTeX manual as "LaTeX commands".

So when LaTeX2e (the current version of LaTeX) was designed, we took those that did not work out. However, those that did work (even if not documented and with unusual syntax) have been kept for compatibility reasons, just in case somebody did use them in old documents. E.g.,

\maxtrix{5 & 6 & 7 \cr
4 & P & 0 \cr
3 & 2 & 1 \cr}


should work in plain TeX (and in fact in LaTeX). But neither \cr nor \matrix is a documented and official LaTeX command. And as you can see it is a command with one argument not an environment, which is why you get your error message.

In LaTeX it is best to load the amsmath package that provides all kind of good math support including a matrix environment that supports the syntax you used.

-