×
Klik hier om ShareLaTeX te gebruiken in het Engels

To see the corresponding video for this blog post click here.

In the last post we looked at configuring the page layout of our thesis using the geometry and fancyhdr packages. In this post we’re going to look at using images and tables. If you’ve never added images or tables into a LaTeX document, I recommend you first check out the relevant posts in our beginners tutorial series. You can see our post on using images here, and our post on tables here.

##Images For this project, every image we use we will store in the images folder to keep everything tidy. In the first post we prepared the document for images by loading up the graphicx package and by informing LaTeX where the images are stored using the \graphicspath command.

Whenever we add an image into our thesis, we will use the figure environment. Here’s an example:

\begin{figure}[h]
\centering
\includegraphics[scale=0.5]{graph_a}
\caption{An example graph}
\label{fig:x cubed graph}
\end{figure}

Notice that I’ve halved the size of the image and used the position specifier ‘h’ to put it in the document where the code is in the text. It’s really important to add captions to figures when writing a thesis. This is what it looks like compiled:

basicfig

Notice that LaTeX has automatically numbered it according to what chapter it’s part of. It is also really important to label each figure so you can accurately refer back to it in the text using the \ref command. If you added this in the text:

\ref{fig:x cubed graph}

LaTeX would give you the figure number ‘2.1’ in place of this command in the pdf.

##Subfigures When writing a thesis you may want to include some slightly more complicated figures with multiple images. You can do this using subfigure environments inside a figure environment. Before we can do this though, we need to load up the caption and sub caption packages:

\usepackage{caption}
\usepackage{subcaption}

We’ll do an example with three images along side each other with separate captions and labels. Here’s some example code:

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.3\textwidth}
        \centering
        \includegraphics[width=\textwidth]{graph1}
        \caption{$y=x$}
        \label{fig:y equals x}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.3\textwidth}
        \centering
        \includegraphics[width=\textwidth]{graph2}
        \caption{$y=3sinx$}
        \label{fig:three sin x}
    \end{subfigure}
    \hfill
    \begin{subfigure}[b]{0.3\textwidth}
        \centering
        \includegraphics[width=\textwidth]{graph3}
        \caption{$y=5/x$}
        \label{fig:five over x}
    \end{subfigure}
    \caption{Three simple graphs}
    \label{fig:three graphs}
\end{figure}

To start with, we create a new figure, centre it and then create a new subfigure. In the subfigure command we need to add a placement specifier and then give it a width. As we want three images next to each other we set a width of 0.3 times the text width. You need to make sure that the sum of the widths you specify for the subfigures is less than the text width if you want them all on the same line. When we add the image in we need to specify the width using ‘width=’ followed by the \textwidth command. The reason this works is because the text width within in the subfigure is the width we specified in the \begin{subfigure} command, i.e. 0.3 times the normal text width. Next we give the subfigure a separate caption and label. We can then end the subfigure and add the next two in. To add some spacing between the figures we’ll use the \hfill command. If you didn’t want them all on the same line you could just leave blank lines instead of the \hfill commands. Please note that the indents I have used do not affect the how the code is processed, they just make it more readable. The beauty of these subfigures is that we can refer to each of them individualy in the text due to their individual labels. But we can also give the whole figure a caption and label.

This is what our figure will look like in the document:

three

Now if we add a \listoffigures command just after the table of contents, LaTeX will generate a list of all the figure used in the thesis and inform us where each can be found:

listoffigures

##Tables Now lets talk about tables. When writing a thesis you should enclose all your tables in the table environment. Here’s a basic example:

\begin{table}[h]
\centering
\begin{tabular}{l | l | l}
A & B & C \\
\hline
1 & 2 & 3 \\
4 & 5 & 6
\end{tabular}
\caption{very basic table}
\label{tab:abc}
\end{table}

Again make sure you add both a caption and a label.

basictable

##Subtables Just like with images, you may want to group tables together into a single table environment. This can be done by using subtable environments inside a table environment. Here’s an example:

\begin{table}[h]
    \begin{subtable}[h]{0.45\textwidth}
        \centering
        \begin{tabular}{l | l | l}
        Day & Max Temp & Min Temp \\
        \hline \hline
        Mon & 20 & 13\\
        Tue & 22 & 14\\
        Wed & 23 & 12\\
        Thurs & 25 & 13\\
        Fri & 18 & 7\\
        Sat & 15 & 13\\
        Sun & 20 & 13
        \end{tabular}
        \caption{First Week}
        \label{tab:week1}
    \end{subtable}
    \hfill
    \begin{subtable}[h]{0.45\textwidth}
        \centering
        \begin{tabular}{l | l | l}
        Day & Max Temp & Min Temp \\
        \hline \hline
        Mon & 17 & 11\\
        Tue & 16 & 10\\
        Wed & 14 & 8\\
        Thurs & 12 & 5\\
        Fri & 15 & 7\\
        Sat & 16 & 12\\
        Sun & 15 & 9
        \end{tabular}
        \caption{Second Week}
        \label{tab:week2}
    \end{subtable}
    \caption{Max and min temps recorded in the first two weeks of July}
    \label{tab:temps}
\end{table}

Notice that in each \begin{subtable} command we’ve included a position specifier and a width. Again we can give each subtable a label and caption as well as giving the whole table figure a label and caption.

two

Now in the same way we added a list of figures after the table of contents we can add a list of tables using the \listoftables command.

listoftables

This concludes our discussion on images and tables. In the next post we’ll look at adding a bibliography to our thesis.

###Other posts in this series:

pt 1 - Basic Structure

pt 2 - Page Layout

pt 4 - Bibliographies with Biblatex

pt 5 - Customising Your Title Page and Abstract

Posted by Josh Cassidy on 07 Aug 2013