A JPG is inserted by graphicx at the size stored in the file, not at a size that fits the page. Most image problems in LaTeX come from that, or from figures floating.
What you’ll learn:
- Insert a JPG with graphicx
- Set the image size
- Rotate, trim and crop
- Position the image with figure
- Align, caption and label
- Wrap text around an image
- Put images side by side
Insert a JPG image with graphicx
Use this for any JPG, PNG or PDF image. graphicx ships with every TeX distribution and Overleaf.
Load the package in the preamble:
\documentclass{article}\usepackage{graphicx}\begin{document}Insert the image in the body with
\includegraphics:\includegraphics[width=0.8\textwidth]{example.jpg}Keep the JPG in the same folder as your
.texfile, or give a relative path such asimages/example.jpg.
Result: the image appears inline at 80% of the text width.

If all your images live in one subfolder, set the search path once and drop the folder from every call:
\graphicspath{{images/}} % in the preamble; note the double bracesYou can also omit the extension (\includegraphics{example}), and graphicx looks for a supported file with that name.
Note:
\includegraphicswith no options does not shrink the image to fit. It uses the size implied by the file’s resolution metadata, so a large photo can run past the margin. Always set a width.
PDF pages use the same command; to insert a PDF page as a figure, add page=2 to the options.
Which compiler can read JPG?
pdfLaTeX, XeLaTeX and LuaLaTeX all read JPG and PNG directly and embed the file unchanged. Plain latex (DVI output) can’t, and stops with “Cannot determine size of graphic … (no BoundingBox)”.
In Overleaf, make sure the project’s compiler setting is pdfLaTeX, XeLaTeX or LuaLaTeX, not LaTeX.
Set the image size
Use this whenever the image is larger or smaller than you want. Set one dimension and graphicx scales the other to keep the aspect ratio.
\includegraphics[width=\textwidth]{image.jpg} % full text width\includegraphics[height=5cm]{image.jpg} % 5 cm tall, width follows
If you set both width and height, the image is stretched to that box. Add keepaspectratio to fit inside the box instead:
\includegraphics[width=0.5\textwidth, height=5cm, keepaspectratio]{image.jpg}| Option | What it does | Example |
|---|---|---|
width=\textwidth | Full width of the text block | width=0.5\textwidth |
width=\linewidth | Width of the current line (use inside columns, minipages and lists) | width=0.9\linewidth |
| Absolute units | Fixed size in cm, mm, in or pt | width=10cm |
scale= | Multiplies the natural size | scale=0.5 |
keepaspectratio | With both width and height, fits inside without distortion | width=5cm, height=5cm, keepaspectratio |
For sharp print output, start from an image of at least 300 DPI at its printed size. Scaling down a large image keeps quality; scaling up a small one doesn’t.

Rotate, trim and crop an image
Use this when the photo is sideways or has unwanted borders. All three are \includegraphics options:
\includegraphics[width=0.4\textwidth, angle=90]{photo.jpg} % rotate 90° anticlockwise\includegraphics[trim=2cm 1cm 2cm 1cm, clip, width=0.5\textwidth]{photo.jpg} % cropangle=rotates anticlockwise by the given degrees.trim=left bottom right topremoves that much from each edge.cliphides the trimmed parts. Without it,trimonly changes the space LaTeX reserves.
Position the image with the figure environment
Use this when the image needs a caption, a number, or a sensible place on the page. A figure is a float: LaTeX moves it to avoid half-empty pages.
\begin{figure}[htbp] \centering \includegraphics[width=\textwidth]{example.jpg} \caption{An example image.} \label{fig:example}\end{figure}
The letters in square brackets say where LaTeX may place the figure:
| Specifier | Meaning |
|---|---|
h | Here, roughly where it appears in the source |
t | Top of a page |
b | Bottom of a page |
p | On a separate page of floats |
! | Relax LaTeX’s internal limits, used with others ([!ht]) |
H | Exactly here, no floating (needs \usepackage{float}) |
[htbp] is a good default because it gives LaTeX every option. For fine-tuning, see Overleaf’s guide to positioning images and tables.
Align an image left, right or centre
Use this to change horizontal alignment inside a figure. Put the command at the start of the figure:
\centeringcentres the image and caption.\raggedrightaligns them left.\raggedleftaligns them right.
\begin{figure}[htbp] \centering \includegraphics[width=0.8\textwidth]{example.jpg} \caption{A centred image.} \label{fig:centered}\end{figure}
Outside a figure, use \begin{center}…\end{center} around the \includegraphics call. Avoid center inside a figure; it adds extra vertical space.
Add captions and labels
Use this when you’ll refer to the image from the text. \caption numbers the figure, and \label gives it a name you can reference.
- Put
\caption{…}below the image for a caption underneath, or above it for a caption on top. - Put
\label{fig:name}directly after\caption. A label placed before the caption picks up the section number instead. - Refer to it with
Figure~\ref{fig:name}, and compile twice.
\begin{figure}[htbp] \centering \caption{This caption appears above the image.} \label{fig:example2} \includegraphics[width=0.8\textwidth]{example.jpg}\end{figure}
As shown in Figure~\ref{fig:example2}, the results indicate...
Tables work the same way; see how to reference figures and tables with \ref, including \autoref and \cref.
Wrap text around an image with wrapfig
Use this when a small image should sit beside a paragraph with text flowing around it. Load \usepackage{wrapfig}, then put the image in a wrapfigure placed just before the paragraph.
\begin{wrapfigure}{r}{0.4\textwidth} \centering \includegraphics[width=0.38\textwidth]{example.jpg} \caption{Example image with wrapped text.} \label{fig:wrapexample}\end{wrapfigure}{r}puts the image on the right;{l}puts it on the left.{0.4\textwidth}is the width reserved for the image. Make the image slightly narrower.

The optional first argument is the number of text lines to wrap, which helps when wrapfig guesses wrong. It must be a number:
\begin{wrapfigure}[12]{r}{0.4\textwidth} \vspace{-10pt} % pull the image up slightly \centering \includegraphics[width=0.38\textwidth]{example.jpg} \caption{Adjusted spacing example.} \label{fig:wrapspacing}\end{wrapfigure}Warning: wrapfig doesn’t work inside lists, and wrapping goes wrong if a heading or very short paragraph follows. Start the
wrapfigurejust before a long plain paragraph.
Wrapping text inside table cells is a different job; see how to wrap text inside table cells. The wrapfig documentation on CTAN lists every option.
Put images side by side with subcaption
Use this for two or more related images, each with its own (a), (b) caption. The subcaption package is the current standard.
\usepackage{subcaption} % in the preamble
\begin{figure}[htbp] \centering \begin{subfigure}{0.48\textwidth} \centering \includegraphics[width=\linewidth]{image1.jpg} \caption{First image.} \label{fig:sub1} \end{subfigure} \hfill \begin{subfigure}{0.48\textwidth} \centering \includegraphics[width=\linewidth]{image2.jpg} \caption{Second image.} \label{fig:sub2} \end{subfigure} \caption{Overall caption for the figure with subfigures.} \label{fig:multi}\end{figure}Result: two images on one row, captioned (a) and (b), with one overall figure caption.

Older tutorials use the subfig package’s \subfloat. It still works, but don’t load subfig and subcaption together; they conflict.
If you don’t need sub-captions, two minipage environments do the same job:
\begin{figure}[htbp] \begin{minipage}{0.45\textwidth} \centering \includegraphics[width=\textwidth]{example1.jpg} \caption{First image.} \label{fig:first} \end{minipage} \hfill \begin{minipage}{0.45\textwidth} \centering \includegraphics[width=\textwidth]{example2.jpg} \caption{Second image.} \label{fig:second} \end{minipage}\end{figure}Which method should you use?
| Method | Best for | Package |
|---|---|---|
\includegraphics alone | Inline images with no caption | graphicx |
figure environment | Numbered images with captions | Built in |
figure with [H] | Images that must not move | float |
wrapfigure | Small images with text flowing around | wrapfig |
subfigure | Several related images with (a), (b) captions | subcaption |
Using images in Overleaf
The commands are identical in Overleaf. The only extra step is getting the file into the project:
- Upload the JPG with the upload button in the file tree, or drag it onto the file tree.
- If you put it in a folder, add
\graphicspath{{folder/}}or include the folder in the path. - Recompile.
Common errors
- “File ‘photo.jpg’ not found”: check the spelling, the letter case (Overleaf and Linux are case-sensitive) and the folder. Spaces in file names can also cause trouble; rename the file.
- “Cannot determine size of graphic … (no BoundingBox)”: you’re compiling with plain
latex. Switch to pdfLaTeX, XeLaTeX or LuaLaTeX. - The image runs past the margin: add
width=\linewidthor a fraction of it. - The figure jumps to another page: use
[htbp], shrink the image, or use[H]with the float package. - “Missing number, treated as zero” at a wrapfigure: the optional argument must be a number of lines, such as
[12], not text. \refprints the section number: move\labelafter\caption.
The graphicx package page on CTAN has the full option list. For more figure, table and code tutorials, browse the LaTeX guides.
Working in Word instead? See how to place an image beside text in Word. And if a collaborator needs an editable copy, you can convert your LaTeX file to Word once the figures are in place.