Images in a LaTeX Document

Published May 26, 2022  -  By Marco Garosi

Adding images to TeX\TeX ( LaTeX\LaTeX) document may look challenging. It could seem so difficult that you should just go back to other tools and software. But is it? No, not really: it’s pretty simple, actually. Here’s how to add images to a LaTeX document.

Setup

You should already have a LaTeX document. You may get a skeleton in the related note. Following is the code to add images in a LaTeX document.

In your preamble, that is before the \\begin{document} instruction, you should add the following code.

\usepackage{graphicx}
\graphicspath{ {./images/} } % path to the image folder

The latter command tells the package graphicx where the images are located. You should write the relative path to the images from the .tex file you’re writing in. Let’s make an example: your file is main.tex and is located in /Users/john/main.tex. If your images are in /Users/john/images/, then the command should be the one above. If your images are located in /Users/john/, your graphicxpath should be just ./.

Usage

Now that you’ve imported and configured the package, you may want to actually include images and display them. You may do with the following code, that should be placed anywhere between \\begin{document} and \\end{document}. Of course, you can include as many images as you want.

\includegraphics[width=\textwidth]{image-name.extension}

Let’s say you want your image of a cat — cat.png — be half the width of the page content; then you can use the command:

\includegraphics[width=0.5\textwidth]{cat.png}

You should not forget that images are not placed where you write the command: TeX decides where they best fit based on many parameters. You should not care about their positioning until your document is ready — that is, the content is complete and won’t be modified anymore. If that is the case, then you could try moving the \\includegraphics command around in your text to try to place the image where you want it to be.

This can be rather complicated. You should ask yourself: “Is it really necessary?“. If not, you shouldn’t worry: TeX knows its business pretty well.

Labeling and referencing images

One of the main reasons people use TeX\TeX and LaTeX\LaTeX is because they handle cross-referencing excellently. If you want to reference to your images — and you want them to appear in the list of figures — you may use the following code:

\begin{figure}[htbp]
    \centering % center the image horizontally
    \includegraphics[width=0.5\textwidth]{your-image.extension}
    \caption{Let's add a caption as well - why not?!}
    \label{fig:choose-a-name}
\end{figure}

Since you may have quite a few to tens or hundreds of images, writing all of the above code for every one may be stressing and error-prone. Hence you can define a new command to make your life a lot easire. You should add the following code in the preamble:

\newcommand{\myimgcmd}[4]{
\begin{figure}[htbp]
	\centering
	\includegraphics[width=#1\textwidth]{#2}
	\caption{#3}
	\label{#4}
\end{figure}
}

The command thus defined can be used anywhere in the document with \\myimgcmd. It has four arguments:

  1. figure width;
  2. figure name;
  3. figure caption;
  4. figure label name.

Let’s say you want to include cat.png with a fancy description somewhere in your document; then you can use:

\myimgcmd{0.5}{cat.png}{This is fancy description for a cool and lovely cat.}{cat-image}

Then let’s say that, to make things easier, you want all your image labels to start with fig:, so that you know what you are referencing easier. It’s very easy: you just have to edit the \\newcommand definition; go to the \\label{#4} command and change it into \\label{fig:#4}. Now, all of your images have a label that starts with fig:.

Share on: