# How to add a matrix to a LaTeX document

How do we add a matrix to a LaTeX document?

Replay

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 matrix
• pmatrix: matrix surrounded by parentheses
• bmatrix: matrix surrounded by square brackets
• vmatrix: matrix surrounded by single vertical lines
• Vmatrix: matrix surrounded by double vertical lines

This info is found in "The LaTeX Companion", and the amsmath manual section 4.

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, vmatrix and Vmatrix have (respectively) ( ), [ ], { }, | |, and || || delimiters built in. There is also a matrix environment sans delimiters, and an array environment 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 smallmatrix environment [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, \cases should be avoided in LaTeX (and when the amsmath package 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.

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}$



For flexible typesetting of matrices with color, lines and justification done by formatting parameters see An extension to amsmath matrix environments.

$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|$


Category: math mode Time: 2008-08-18 Views: 3

## Related post

• How to add separate TOC when breaking LaTeX document using htlatex in each separate HTML page? 2013-05-28

Related question tex4ht: limit toc I give up trying things. This is too hard. I have this document \section A \section B \subsection B.1 \subsection B.2 \section C I want one main TOC, but also want TOC for each section that includes its subsections.

• How to clean up files related to LaTeX document for ArXiv publication? 2015-03-15

My directory looks like this where I have many bibliographies sourced, many third-party documents related to the publication, scripts and main LaTex file sources the chapters from the Chapters folder. In order to submit my paper to ArXiv, how can I b

• How to add a parameter to a LaTeX class via the LyX layout? 2012-03-07

I want to have this LaTeX document class code produced by LyX: \documentclass[english]{lni} My Layout class looks like this: #% Do not delete the line below; configure depends on this # \DeclareLaTeXClass[lni]{article (LNI)} # Input general definitio

• How to add a publication list using LaTeX? 2013-02-06

I have seen Multiple bibliographies (cited references + list of publications) and How can I include a bibliography at the end of each part of a book? but could find an answer. I am writing a thesis where there is an overall bibliography. Plus, I want

• How to add a version counter to a document in LaTeX? 2015-03-23

Well this can be a silly question, but now I begin to give copies of my thesis for review, I would like to add a counter versions of the document. Then I have curious if there are any package or some example code that allows to add that to my thesis.

• How to add a matrix to itself in an iterative way? 2016-01-22

I have a code which looks like: X = [[12,7,3], [4 ,5,6], [7 ,8,9]] result = [[0,0,0], [0,0,0], [0,0,0]] Final = [[0,0,0], [0,0,0], [0,0,0]] for i in range(len(X)): for j in range(len(X)): for x in range(4): result[i][j] = X[i][j] + X[i][j] Final[i][j

• How to use Windows font in a LaTeX document? 2009-11-04

How do I use a Windows font in a LaTeX document? Is it even possible? If yes, how? I am on Windows XP and using the MikTeX system for LaTeX. My LaTeX editor is LEd. I fell in love with the free Fontin font. I have installed it into my Windows fonts a

• How to add an invisible watermark to pdf documents? 2011-05-24

I'd like to add an invisible watermark to each PDF document that I produce so that in case it leaks, I know where it leaked from. Anyone knows what would a good way for an invisible watermark be in a PDF document and how to add one? --------------Sol

• How to be able to use ∞ in latex document? 2016-01-15

I would like to use the unicode character ∞ in a LaTeX document. I use the default font. If this character is not defined in this font, I can replace it by $\infty$. How can I do that? I tried to play with \DeclareUnicodeCharacter{}{$\infty$} but did

• How can the concatenation of two valid LaTeX documents fail to be valid? 2012-01-30

The question in the title is confusing, because it doesn't make sense (though it's legal) to concatenate LaTeX documents; everything after the first \end{document} will be ignored. What I mean is that I had two wodges of text, AAA and BBB, such that

• How do I embed vector graphics to LaTeX document 2012-04-16

I have a vector grahpics file. Could someone please help me embed a image into my LaTeX document ? I have no clue. --------------Solutions------------- There are several vector graphics format in use these days by vector graphics packages, the most c

• How to balance last page of a LaTeX document? 2014-10-19

I am trying to balance the last page of my LaTeX document, i.e. a conference paper. I am using the flushend package to do it. All of it goes well except for the last line. The columns are balanced but the last line sticks out to the left. I have trie

• How to add a URL to a LaTeX bibtex file? 2009-09-15

I'm using bibtex for my bibliography in LaTeX. I have some URL's I need to cite in the paper. How do I add URLs into the .bib file? A typical section in my .bib file looks like this: @conference{eigenfacepaper, title={{Face recognition using eigenfac

• How to use tikz matrix in a LaTeX command? 2010-08-05

For answering a question yesterday I wanted to define a shortcut for creating a matrix inside a tikzpicture. As a minimal example, consider \newcommand\mymatrix{ \begin{tikzpicture} \matrix[matrix of math nodes] {#1}; \end{tikzpicture}% } which wo

• How to add a custom symbol to LaTeX? 2011-03-16

Suppose I need some crazy custom symbol for personal use. e.g. when I write \foobar, LaTeX should show . How do I achieve this? Update: I was writing Klingon writing system for fun. Thank you very much you all. Looks like this now --------------Solut

• How to add conditional text to a latex table and get rid of warnings after running pdflatex in ubuntu 14? 2011-04-11

I am looking for a method to do minimal floating point / integer calculations in LaTeX, not for the purpose of package writing, but for the production of actual text. Here is an example to illustrate. Given the following input text The experiment inc

• How should I add a file in my LaTeX document? 2013-10-09

I am creating my BE project report using LaTeX and in that I have to add my sponsorship letter. Is there any solution to do this? --------------Solutions------------- If your sponsorship letter is a pdf, then you can use the following package: \usepa

• How do I insert pictures into my LaTeX document? 2010-12-23

Related: Convert gif image to png on the fly I have an image file -- a gif file, which is the graph of a function. How do I add it to a LaTeX file? Will it come out OK in the subsequent pdf file? --------------Solutions------------- Use movie15 packa

• How do I put chapters into a LaTeX document? 2012-01-04

I used the following LaTeX tag to make a chapter \chapter{title here} but it did not compile. The LaTeX compiler was talking about Undefined control sequence How can I get over this error? --------------Solutions------------- Not all document classes