10 Array Environment

Arrays

Arrays line items up in columns. Here are some basic steps for making arrays

(a) Type \begin{array}.
(b) Use an argument to describe how you want your table to be justified. Immediately following the
\begin{array} command, add a set of brackets.
Inside the brackets, use the letters r (right), c (center),
and l (left) for each column to describe how it will be formatted.
For example, if you have a three-column array and you want the text to be right-justified in the first column, centered in the second, and
left-justified in the third, the argument would be {rcl}.
(c) Type your data, using & to separate columns and \\ to move to the next row.
(d) End the array with \end{array}.

Using Arrays to Align Equations

Example: 
[latex]\begin{array} {lcl} f(x) & = & (a+b)^2 \\ & = & a^2+2ab+b^2 \end{array}[/latex]
[latex]\begin{array}{lcl}
f(x) & = & (a+b)^2 \\
& = & a^2+2ab+b^2 \\
\end{array}[/latex]

Exercises

Typeset the following, using an array environment

[latex]\begin{array}{rcl} f: R^3 & \to & R \\ (x,y,z) & \to & x + y + z \\ f(x,y,z) & = & x + y + z \end{array}[/latex]

Using Array to Create Tables

Example: 
[latex]\begin{array}{|l|c|r|} left & center & right\\ \hline d & e & f \end{array}[/latex]
[latex]\begin{array}{|l|c|r|}

left & center & right\\ \hline d & e & f \end{array}[/latex]

  • vertical bars for |l|c|r| draw a vertical line before and
    after each column;
  • \hline draws a horizontal line. It should only be used after \\, or at the very beginning.

 

Exercises

Typeset the following, using an array environment

[latex]\begin{array}{lll} \text{Symbol} & \text{Number} & \text{Expression}\\ \hline \pi & 3.14159 & 4\sum_{k\leq1}(-1)^{k+1}/(2k-1)\\ e & 2.71828 & \sum_{k\geq0}1/\left(k!\right) \end{array}[/latex]

Using Array to Create Matrices

Example:

[latex]\left[\begin{array}{rrrrr} 1 & 1 & 1 & 1 &1 \\ 0 & 1 & 2 & 3 & 5 \\ 0 & -2 & 0 & -2 & 2 \\ 0 & 1 & -2 & 3 & 1 \\ \end{array}\right][/latex]

[latex]\left[\begin{array}{rrrrr}
  1 & 1 & 1 & 1 &1 \\
  0 & 1 & 2 & 3 & 5 \\
   0 & -2 & 0 & -2 & 2 \\
  0 & 1 & -2 & 3 & 1 \\
  \end{array}\right]
[/latex]

Definition by Cases

The array environment can be used to typeset a definition by cases.

You need to write cases in math mode using \[enter your case here\].

Example: Using an array environment

\[f_y(y)=\left\{\begin{array}{cl}
2y,&0\leq y \leq 1\\
0,& \mbox{elsewhere}\end{array}\right.\]

\[f_y(y)=\left\{\begin{array}{cl}
2y,&0\leq y \leq 1\\
0,& \mbox{elsewhere}\end{array}\right.\]
  • Notice the use of the \mbox command. It allows you to write text in text mode while inside the math mode, such that looks like normal text. If we didn’t use the \mbox command, we would get a
    result like this:

\[f_y(y)=\left\{\begin{array}{cl}
2y,&0\leq y \leq 1\\
0,& elsewhere\end{array}\right.\]

\[f_y(y)=\left\{\begin{array}{cl}
2y,&0\leq y \leq 1\\
0,& elsewhere\end{array}\right.\]

Example: Using a case environment

\[|x|=\begin{cases}
x, & \mbox{for}\; x\geq 0\\
-x, & \mbox{for}\; x \leq 0
\end{cases}\]

\[|x|=\begin{cases}
x, & \mbox{for}\; x\geq 0\\
-x, & \mbox{for}\; x \leq 0
\end{cases}\]
  • Note the space after the word “for” in the curly brackets. This provides a space between the word “for” and variable “x”.

Exercises

Typeset the following cases:

a) \[f(n) = \begin{cases} n/2, & \mbox{if } n\mbox{ is even} \\ 3n+1, & \mbox{if } n\mbox{ is odd} \end{cases}\]

b) \[\begin{align} \det(A)&=1+(-1)^{n+1} \\ &= \begin{cases} 2 & \mbox{ if } n \text{ is odd}\\ 0 & \mbox{ if } n \text{ is even}\end{cases}\end{align}\]

HINT: Combine the align and case environment.

 

 


License

Icon for the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License

Using Latex in Pressbooks Copyright © 2021 by Sanja Krajisnik and Jelena Loncar is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License, except where otherwise noted.

Share This Book