You can open the project used in the video in ShareLaTeX by following this link:


In the last video we looked at using BibTeX to add bibliographies into our documents. In this video we’ll look at inserting tables and matrices.

To insert tables in LaTeX we use the ‘tabular’ environment. So let’s add in our begin and end commands. Immediately following the \begin{tabular} command we need to tell LaTeX the column specifications. There are a few key characters we use to do this. Firstly, a lower case ‘l’ specifies a left justified column, a lower case ‘r’ specifies a right justified column and a lower case ‘c’ specifies a centred column. In my example I’m going to specify six columns, the first and last will be left-justified and the rest will be centred. To put vertical lines between the columns we can use the bar symbol and place it inbetween the letters where we want the lines to appear. When entering the tables’ contents we start each row on a new line, separate column entries using an ampersand and finish each row, except the last, with a double backslash.

Now to add horizontal lines we use the \hline command. To make a double horizontal line simply use the command twice. One potential problem with this table is that if I add a long line of text into one of the cells in the final column it will sprawl off the page. To fix this there is another option we can use when declaring column specifications. It is a lower case ‘p’ followed by a width in centimetres in curly brackets. So if I change the final ‘l’ in the declaration to a ‘p’ with 5cm you will see the text has been wrapped so that the final column’s width is 5cm.

So that’s the basics of writing tables. However, just like with images, we often want more control over positioning and we may want to add a caption and label. To do this we use an environment called ‘table’ which is similar to the figure environment we used for images. To put our existing table in the table environment we simply enclose the code in \begin{table} and \end{table} commands. We can then set the position specifier using a combination of h, t, b, p and exclamation mark. I can also add a caption and label.

We will now briefly look at matrices as they use similar syntax as tables. At this point make sure you’ve loaded the ‘amsmath’ package. Before we add a matrix we need to tell LaTeX that we are about to add some maths by opening an environment. We could use the equation environment as we did a few videos ago, however here we’ll use the displaymath environment as it has nice LaTeX shorthand. This means instead of using a \begin command we can use a backslash and open square bracket and instead of an end command we can use a backslash and close square bracket. Matrices can be inserted using the matrix environment. With matrices we don’t need to declare how many columns we use, we can simply start adding the entries. Again we enter each row on a new line, separate entries using an ampersand and separate rows using a double backslash. To change the brackets surrounding the matrix, we change the environment. The ‘pmatrix’ environment uses parenthesis, the ‘bmatrix’ uses square brackets, ‘Bmatrix’ with a capital B uses curly brackets, ‘vmatrix’ uses vertical lines and ‘Vmatrix’ with a capital V uses double vertical lines.

Finally we’ll finish this video with a more interesting matrix. What we are doing in this example is nesting matrices inside another matrix. So what we actually have here is a two by two matrix built with the ‘pmatrix’ environment, where the top left and bottom right entries are also matrices, this time built with the matrix environment to avoid multiple brackets.

This concludes our discussion on tables and matrices. In the next video we’ll look at composing larger documents.

Published on 26 Jul 2013