Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I tried using the below code but it works only for 2*2 matrix.

 \[
   M=
  \left[ {\begin{array}{cc}
   1 & 2 \\
   3 & 4 \\
  \end{array} } \right]
\]

Now the following doesn't work for me:

\[
  M=
  \left[ {\begin{array}{cc}
   1 & 2 & 3 & 4 & 5\\
   3 & 4 & 5 & 6 & 7\\
  \end{array} } \right]
\]
share|improve this question
    
It should work with any size. Can you be more specific about what happens when it "doesn't work"? And give an example of input that doesn't give the output you want? –  hobbs Sep 1 '12 at 0:00
add comment

migrated from stackoverflow.com Sep 3 '12 at 21:36

This question came from our site for professional and enthusiast programmers.

3 Answers

up vote 10 down vote accepted

In the example you have, you need the opening line to be

\left[ {\begin{array}{ccccc}

rather than

\left[ {\begin{array}{cc}

When you start with just two cs, you're telling it the matrix only has two columns (and that you want them centered). Then it breaks when you give it data for 5 columns.

share|improve this answer
add comment

A better way to do it, is the TheHe meantioned, with amsmath:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\[
M=
  \begin{bmatrix}
    1 & 2 & 3 & 4 & 5\\
    3 & 4 & 5 & 6 & 7\\
  \end{bmatrix}
\]

\end{document}

The bmatrix environment will give you [] braces. () braces are also very common. They are created with the pmatrix environment. To include a matrix inline, you can write:

$M = \left\[ \begin{smallmatrix} 1 & 2 \\ 3 & 4 \end{smallmatrix} \right\]$
share|improve this answer
add comment

if you use the amsmath package, you can chose out of a lot of matrices like pmatrix or bmatrix.

Check out this list at Wikibooks.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.