Images

LaTeX will make a judgement on where best to place an image although you can force its hand with several commands as we shall see.

To help organise your document, I recommend creating a dedicated folder for your images. Do this using the file panel on the left hand side. I have uploaded an image of my cat called bose.jpeg

Inserting An Image

To insert an image we need to load the graphicx package. Include the following code near the top of your document with the other packages

\usepackage{graphicx}

To insert an image use the following code:

\begin{figure}[h]
  \includegraphics[width=0.8\linewidth]{img/bose.jpeg}
  \centering
  \caption{This is a cat.}
\end{figure}
  • The caption is optional but LaTeX will automatically number your images if you include one.

  • The 0.5\linewidth tells LaTeX to make the image half the width of the page. Adjust accordingly.

  • The [h] after \begin{figure} tells LaTeX to attempt to put the images where you have placed it in your code. The following other command are available.

Captions, labels and references

Images can be captioned, labelled and referenced by means of the figure environment as shown below:

\begin{figure}[h]
    \centering
    \includegraphics[width=0.25\textwidth]{mesh}
    \caption{a nice plot}
    \label{fig:mesh1}
\end{figure}

As you can see in the figure \ref{fig:mesh1}, the 
function grows near 0. Also, in the page \pageref{fig:mesh1} 
is the same example.
InsertingImages.PNG

Open an example in Overleaf

There are three important commands in the example:

  • \caption{a nice plot}: As you may expect this command sets the caption for the figure. If you create a list of figures this caption will be used there. You can place it above or below the figure.

  • \label{fig:mesh1}: If you need to refer the image within your document, set a label with this command. The label will number the image, and combined with the next command will allow you to reference it.

  • \ref{fig:mesh1}: This code will be substituted by the number corresponding to the referenced figure.

When placing images in a LATEX document, we should always put them inside a figure environment or similar so that LATEX will position the image in a way that fits in with the rest of your text.

Note: If you are using captions and references on your own computer, you will have to compile the document twice for the references to work. Overleaf will do this for you automatically.'

Last updated

Was this helpful?