31

From what I can see, \matrix was a TeX command, but I cannot seem to find documentation on it.

It works in MathJax, so I wonder if it can be used in LaTeX.

ie this is valid MathJax:

$$
\left[
\matrix
{
newx.x&newy.x&newz.x \\
newx.y&newy.y&newz.y \\
newx.z&newy.z&newz.z    
}
\right]
$$

As can be seen on math.stackexchange.

In my LaTeX editor (which uses MiKTeX underneath), I have to use \begin{matrix} .. \end{matrix}, so I'm wondering what happened to the \matrix command.

5
  • 2
    \usepackage{amsmath}
    – Seamus
    Aug 24, 2011 at 20:35
  • I would upvote purely for the doom avatar. But I've reached the limit cap. Wait, is that doom or wolfenstein?
    – Seamus
    Aug 24, 2011 at 20:44
  • @Seamus: doom and I upped for you :)
    – percusse
    Aug 24, 2011 at 20:51
  • Doom is 18 years old: 1993! OH wow. Makes me feel old...
    – Seamus
    Aug 24, 2011 at 21:32
  • 1
    MathJaX is not a reliable guide as to what is available in "standard" LaTeX. It "loads" several packages (or, rather, simulates loading several packages) that are considered "standard" (in that many mathematicians use them). Aug 25, 2011 at 7:23

4 Answers 4

70

In addition to some already provided, here are a number of ways of creating matrices in LaTeX. Using

  • an array structure to place items in a rigid row/column environment;
  • \begin{matrix}...\end{matrix} from the amsmath package, which allows you to specify the matrix delimiters yourself (using \left and \right);
  • pmatrix, bmatrix, Bmatrix, vmatrix and Vmatrix variations to the above (also from amsmath) to fix the delimiters to ( ), [ ], { }, | |, and || ||, respectively;
  • \bordermatrix{...} which is a TeX command and will specify row and column indicies;
  • \kbordermatrix{...} which is similar to the above, but provides more flexibility;
  • the blkarray package and the associated blockarray and block environments to construct your matrix.

Here is an example file showing some of the different styles:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{kbordermatrix}% http://www.hss.caltech.edu/~kcb/TeX/kbordermatrix.sty
\usepackage{blkarray}% http://ctan.org/pkg/blkarray
\begin{document}

\[
\begin{array}{lc}
  \verb|array| & \left(\begin{array}{@{}ccc@{}}
                    a & b & c \\
                    d & e & f \\
                    g & h & i
                  \end{array}\right) \\[15pt]
  \verb|matrix| & \left(\begin{matrix}
                    a & b & c \\
                    d & e & f \\
                    g & h & i
                  \end{matrix}\right) \\[15pt]
  \verb|pmatrix| & \begin{pmatrix}
                    a & b & c \\
                    d & e & f \\
                    g & h & i
                  \end{pmatrix} \\[15pt]
  \verb|bmatrix| & \begin{bmatrix}
                    a & b & c \\
                    d & e & f \\
                    g & h & i
                  \end{bmatrix} \\[15pt]
  \verb|Bmatrix| & \begin{Bmatrix}
                    a & b & c \\
                    d & e & f \\
                    g & h & i
                  \end{Bmatrix} \\[15pt]
  \verb|vmatrix| & \begin{vmatrix}
                    a & b & c \\
                    d & e & f \\
                    g & h & i
                  \end{vmatrix} \\[15pt]
  \verb|Vmatrix| & \begin{Vmatrix}
                    a & b & c \\
                    d & e & f \\
                    g & h & i
                  \end{Vmatrix} \\[15pt]
  \verb|bordermatrix| & \bordermatrix{\text{corner}&c_1&c_2&\ldots &c_n\cr
                r_1&a_{11} &  0  & \ldots & a_{1n}\cr
                r_2& 0  &  a_{22} & \ldots & a_{2n}\cr
                r_3& \vdots & \vdots & \ddots & \vdots\cr
                r_4& 0  &   0       &\ldots & a_{nn}} \\[15pt]
  \verb|kbordermatrix| & \kbordermatrix{\text{corner}&c_1&c_2&\ldots &c_n\cr
                r_1&a_{11} &  0  & \ldots & a_{1n}\cr
                r_2& 0  &  a_{22} & \ldots & a_{2n}\cr
                r_3& \vdots & \vdots & \ddots & \vdots\cr
                r_4& 0  &   0       &\ldots & a_{nn}} \\[25pt]
  \verb|blkarray| & \begin{blockarray}{[cc]c\}}
                11 & 22 & 33 \\
                1 & 2 & 3 \\
                \begin{block}{(ll)l\}}
                  11 & 22 & 33 \\
                  1 & 2 & 3 \\
                \end{block}
                1 & 2 & 3
                \end{blockarray}
\end{array}
\]
\end{document}

Matrices

8
  • 8
    This is the sort of thorough answer we need more of on this site! well done!
    – Seamus
    Aug 24, 2011 at 21:24
  • I second @Seamus. Great answer, Werner! There should be a Welcome to the Matrix badge. =) Aug 24, 2011 at 23:21
  • 9
    "Unfortunately, no one can tell you what the matrix is. Except for Werner, he seems to do it pretty well." Aug 25, 2011 at 0:04
  • @Werner: if one wants to use {array} to do a matrix, it is important to suppress the spurious spaces between the two delimiters and the array by using \begin{array}{@{}ccc@{}}. Also, an interesting package to do complex border matrix is the blkarray; there are a few examples of use on this site. Aug 25, 2011 at 6:53
  • @Philippe: Thanks - I've updated it with your suggestions, including an example in the blkarray package documentation.
    – Werner
    Aug 25, 2011 at 7:20
6

You shouldn't use \matrix{ but \begin{matrix} and \end{matrix} are provided by the amsmath package.

5

It's strongly recommended to use amsmath's matrix features. However, answering your question: you can find the definition of \matrix in plain.tex:

\def\matrix#1{\null\,\vcenter{\normalbaselines\m@th
    \ialign{\hfil$##$\hfil&&\quad\hfil$##$\hfil\crcr
      \mathstrut\crcr\noalign{\kern-\baselineskip}
      #1\crcr\mathstrut\crcr\noalign{\kern-\baselineskip}}}\,}

Related:

\def\pmatrix#1{\left(\matrix{#1}\right)}

You can find plain.tex by typing on the command prompt

kpsewhich plain.tex

which gives on a current standard Windows TeX Live installation, for example

c:/texlive/2011/texmf-dist/tex/plain/base/plain.tex

\matrix is documented in the TeX book and various other TeX documentation. LaTeX documentation is mostly about the more modern matrix environment of amsmath.

3
  • So, why doesn't \matrix work in my LaTeX implementation, if its part of TeX?
    – bobobobo
    Aug 24, 2011 at 23:53
  • @bobobobo: MathJax also reports the use of environments which includes amsmath's \begin{matrix}...\end{matrix}. Not sure what happened to \matrix{...}.
    – Werner
    Aug 24, 2011 at 23:59
  • 1
    @bobobobo: The quick answer is that "plain TeX" is not plain TeX. "plain TeX" is an extension of TeX much as LaTeX is and LaTeX is not built on top of "plain TeX". Aug 25, 2011 at 7:22
4

The command \matrix is defined in the LaTeX kernel

% latex.ltx, line 4509:
\def\matrix#1{\null\,\vcenter{\normalbaselines\m@th
    \ialign{\hfil$##$\hfil&&\quad\hfil$##$\hfil\crcr
      \mathstrut\crcr\noalign{\kern-\baselineskip}
      #1\crcr\mathstrut\crcr\noalign{\kern-\baselineskip}}}\,}
\def\pmatrix#1{\left(\matrix{#1}\right)}

(the version of LaTeX used is LaTeX2e <2018-12-01>).

Comparing it with plain.tex

% plain.tex, line 1093:
\def\matrix#1{\null\,\vcenter{\normalbaselines\m@th
    \ialign{\hfil$##$\hfil&&\quad\hfil$##$\hfil\crcr
      \mathstrut\crcr\noalign{\kern-\baselineskip}
      #1\crcr\mathstrut\crcr\noalign{\kern-\baselineskip}}}\,}
\def\pmatrix#1{\left(\matrix{#1}\right)}

we see no difference at all.

The reason is historical: when LaTeX was first issued, it incorporated much of plain.tex, in order to ease switch from one to the other. Moreover, the original LaTeX kernel had very basic support for math: in addition to plain TeX structures it only provided displaymath, equation, eqnarray and eqnarray*. The eqnarray environment and its starred companion were designed as replacement for eqalignno and eqalign (but not the best replacement).

When amsmath came to light (at first named amslatex), a problem arose: the original amstex.tex plug in for plain.tex used \matrix ... \endmatrix which ought to get the LaTeX syntax \begin{matrix}...\end{matrix}. There's of course a conflict: a document using \matrix{...} as supported by the LaTeX kernel would be at stake if amsmath is loaded. Similarly for \pmatrix{...} and \begin{pmatrix}...\end{pmatrix}.

Indeed amsmath has

\renewenvironment{matrix}{%
  \matrix@check\matrix\env@matrix
}{%
  \endarray \hskip -\arraycolsep
}
\def\env@matrix{\hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols c}}
\newcount\c@MaxMatrixCols \c@MaxMatrixCols=10
\def\matrix@check#1{%
  \@xp\ifx\csname\@currenvir\endcsname#1%
  \else\matrix@error#1%
    \@xp\@gobble
  \fi
}
\def\matrix@error#1{%
  \@amsmath@err{%
Old form `\string#1' should be \string\begin{\@xp\@gobble\string#1}%
  }{%
`\string#1{...}' is old Plain-TeX syntax whose use is
ill-advised in LaTeX.%
  }%
}
\renewenvironment{pmatrix}{%
  \left(%
  \matrix@check\pmatrix\env@matrix
}{
  \endmatrix\right)%
}

What does this mean? We know that \begin{foo} first does some bookkeeping, including defining \@currenvir to mean foo and then executes \foo, so replacing it with the tokens given in the “begin part” at definition time. In the case of \matrix and \pmatrix, \matrix@check is performed; this command checks whether the current environment name matches either matrix or pmatrix; this will be true if the document has used \begin{matrix} or \begin{pmatrix}, but false if the plain TeX syntax \matrix{...} or \pmatrix{...} is used. In the latter case, an error is raised and the argument to \matrix or \pmatrix is gobbled for allowing to complete the LaTeX run.

This conflict is not really so serious: every document with substantial use of math should load amsmath and therefore employ the \begin{matrix} or \begin{pmatrix} syntax.

By the way, this has a consequence; if one wants to build over matrix using “macho programming style”, an environment should not be defined like

\newenvironment{foomatrix}
 {<something at the beginning>\matrix}
 {\endmatrix<something at the end>}

because this would trigger \matrix@error. Instead

\makeatletter
\newenvironment{foomatrix}
 {<something at the beginning>\env@matrix}
 {\endmatrix<something at the end>}
\makeatother

should be used; for instance, amsmath has

\newenvironment{bmatrix}{\left[\env@matrix}{\endmatrix\right]}

With MathJax, both

\matrix{ 1 & 0 \cr 0 & 1 }

and

\begin{matrix} 1 & 0 \\ 0 & 1 \end{matrix}

are allowed. The implementation doesn't really “expand” \begin like it's done in LaTeX, so it is free to do other tricks.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .