67

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]
\]
2

5 Answers 5

43

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.

1
  • 5
    @strpeter that's no reason to downvote, it's a valid answer
    – osvein
    Oct 29, 2018 at 0:15
93

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

\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\]$
4
  • 1
    In mathtools we also have bsmallmatrix environment.
    – percusse
    Sep 16, 2014 at 14:01
  • I don't like it, it only has one line! !matrix Feb 22, 2015 at 13:22
  • He wrote '\' instead of '\\'. I'll edit that.
    – Astrinus
    Apr 17, 2015 at 10:15
  • 1
    @Astrinus: Thank you. Actually this was not my mistake: meta.tex.stackexchange.com/questions/5978/…. I hope this is fixed.
    – bodo
    Apr 17, 2015 at 11:26
14

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.

1
  • Thanks you so much
    – Denis
    Jan 17, 2020 at 10:10
11

Using the pmatrix (p for parentheses) or bmatrix (b for brackets) environments as suggested above, also yields much better spacing than does array with \left( and \right) (for parentheses) or \left[ and \right] (for brackets). With array, the space between the parenthesis or bracket and the numbers is too large. You can see it for yourself comparing the results of this code:

\begin{equation}
R^2 = 
\left({\begin{array}{cc} c & s \end{array}}\right)
\left(\begin{array}{cc} 1 & 0\\ 0 & 1 \end{array}\right)
\left(\begin{array}{c} c \\ s \end{array}\right) 
= c^2 + s^2
\end{equation}

\begin{equation}
R^2 = 
\begin{pmatrix} c & s \end{pmatrix} 
\begin{pmatrix} 1 & 0\\ 0 & 1 \end{pmatrix}
\begin{pmatrix} c \\ s \end{pmatrix} 
= c^2 + s^2
\end{equation}

That outputs the following:

enter image description here

0

It will not work because you have declared 2 columns (cc). Number of columns in your case should be 4, i.e., cccc. Then it will work.

1
  • 4
    Please. add MWE, that OP will be able simple test your (correct) solution.
    – Zarko
    Apr 3, 2020 at 11:17

You must log in to answer this question.

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