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.


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.

\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 ./.


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.


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:


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:

    \centering % center the image horizontally
    \caption{Let's add a caption as well - why not?!}

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:


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: