How do we add a matrix to a LaTeX document?
-
3Also see: Where is the \matrix command? – Werner Oct 11 '11 at 07:01
5 Answers
Ash's answer typesets the matrix inline with the text. A (perhaps) nicer way to do this is to use the smallmatrix environment in the amsmath package. Add to the document preamble:
\usepackage{amsmath}
And then you can do:
$M = \begin{smallmatrix} a&b\\ c&d \end{smallmatrix}$
If you want to bracket the matrix you can also do:
$M = \left( \begin{smallmatrix} a&b\\ c&d \end{smallmatrix} \right)$
The amsmath package also offers the shortcut matrix environments which default to centered alignment for their columns:
matrix: unbracketed matrixpmatrix: matrix surrounded by parenthesesbmatrix: matrix surrounded by square bracketsvmatrix: matrix surrounded by single vertical linesVmatrix: matrix surrounded by double vertical lines
This info is found in "The LaTeX Companion", and the amsmath manual section 4.
- 28,884
- 1,361
-
7Just to add some info, with
mathtoolspackage you have the environmentsbsmallmatrix,psmallmatrix, … – Manuel Feb 07 '13 at 13:18
First: if you intend to do math in LaTeX, you SHOULD learn and use AMS LaTeX. The best reference is the Short Math Guide for LaTeX. In this guide, you will learn that there are many different matrix macros available when you use the amsmath package (e.g., \usepackage{amsmath} ).
To quote the document,
4.4. Matrices The environments
pmatrix,bmatrix,Bmatrix,vmatrixandVmatrixhave (respectively) ( ), [ ], { }, | |, and || || delimiters built in. There is also amatrixenvironment sans delimiters, and anarrayenvironment that can be used to obtain left alignment or other variations in the column specs. [ed. To produce a matrix with parenthesis around it, use:]
\begin{pmatrix}
\alpha & \beta^{*}\\
\gamma^{*} & \delta
\end{pmatrix}
To produce a small matrix suitable for use in text, there is a
smallmatrixenvironment [ed. here was a matrix appropriate for text mode] that comes closer to fitting within a single text line than a normal matrix. This example was produced by
\bigl( \begin{smallmatrix}
a & b\\
c & d
\end{smallmatrix} \bigr)
To produce a row of dots in a matrix spanning a given number of columns, use \hdotsfor. For example,
\hdotsfor{3}in the second column of a four-column matrix will print a row of dots across the final three columns.Note. The plain TeX form
\matrix{...\cr...\cr}and the related commands\pmatrix,\casesshould be avoided in LaTeX (and when theamsmathpackage is loaded they are disabled).
Finally, I'd like to mention that, while it is possible to set matrices without AMS LaTeX, just use it. It offers so many benefits that until you get the hang of LaTeX, it's the best single macro package for math.
- 158,329
- 661
-
Is there any way of adding in AMS to get boxed matrix? Ie. in array it would be with \begin{array}{rrr|rr}? – Maja Piechotka Nov 07 '09 at 19:13
-
@PaulrBear the "Short Math Guide for LaTeX" link is to an FTP site, which your browser may not support accessing. But I can still access it as of 2019-03-13. – Joseph Holsten Mar 13 '19 at 23:19
-
Hmm, I see. That's not a very accessible format (if you cannot open it in Chrome, at least two-thirds of internet users cannot open it!) – Paul Wintz Mar 14 '19 at 06:47
For a matrix of the form:
M = x y
z w
use the LaTeX code:
$M =
\begin{array}{cc}
x & y \\
z & w \\
\end{array}$
- 158,329
- 2,530
-
A complete tutorial can be found here http://en.wikibooks.org/wiki/LaTeX/Mathematics – Apr 07 '10 at 17:11
-
1I agree with Ashwin's simple approach. Sometimes you can't rely on amsmath being available, so if you're after straightforward matrices, then use straightforward Latex commands. See more at http://andy-roberts.net/misc/latex/latextutorial9.html for how to wrap your 'array' in brackets. – andyroberts May 05 '09 at 19:28
For flexible typesetting of matrices with color, lines and justification done by formatting parameters see An extension to amsmath matrix environments.
- 36,086
- 231,401
$M =
\left[ {\begin{array}{*{20}c}
x & y \\
z & w \\
\end{array} } \right]
$
$M =
\left( {\begin{array}{*{20}c}
x & y \\
z & w \\
\end{array} } \right)
$
$M =
\left| {\begin{array}{*{20}c}
x & y \\
z & w \\
\end{array} } \right|
$
- 158,329
- 161
-
-
@IgorKotelnikov I don't know either, but it seems to me, that it could be "20 columns centered if the content needs 20 columns, otherwise less". – Zelphir Kaltstahl Sep 16 '16 at 13:59