How to Insert an Image in LaTeX

Load \usepackage{graphicx} and write \includegraphics[width=0.8\linewidth]{photo.png}. To number the image and give it a caption, put it in a figure environment with \caption{…} and \label{fig:photo}, then refer to it with \ref{fig:photo}.

Image commands

  • \usepackage{graphicx}
    Text mode, no math preview

    Preamble: enables \includegraphics.

  • \includegraphics{file.png}
    Text mode, no math preview

    Insert the image at its natural size.

  • \includegraphics[width=0.5\linewidth]{file}
    Text mode, no math preview

    Half the text width; height scales with it.

  • \includegraphics[scale=0.4, angle=90]{file}
    Text mode, no math preview

    Scale and rotate.

  • \begin{figure}[htbp] ... \end{figure}
    Text mode, no math preview

    Floating figure with caption and number.

  • \graphicspath{{images/}}
    Text mode, no math preview

    Folder where LaTeX looks for images.

Try it live

Change the formula and the preview redraws as you type. Copy the LaTeX, download a PNG or SVG, copy it for Word, or use Edit here on any example below to load it.

Templates

Insert an image examples

Each result below is drawn from the LaTeX under it. Copy the code, edit it on this page, or open it in the full editor.

  • A formula you might caption

    y = e − x 2 / 2
    LaTeX
    y = e^{-x^2/2}
  • Reference to a figure in a formula context

    see Figure 2:  lim n → ∞ a n = 0
    LaTeX
    \text{see Figure 2: } \lim_{n \to \infty} a_n = 0

In a LaTeX document

These commands work in the text of a .tex document (Overleaf, TeX Live, MiKTeX), not inside math. The box on the right shows what the compiled PDF looks like.

  • Figure with caption and label

    LaTeX
    \usepackage{graphicx}
    ...
    \begin{figure}[htbp]
      \centering
      \includegraphics[width=0.7\linewidth]{results.png}
      \caption{Measured values and the fitted curve.}
      \label{fig:results}
    \end{figure}
    
    Figure~\ref{fig:results} shows the fit.
  • Two images side by side

    LaTeX
    \usepackage{subcaption}
    ...
    \begin{figure}[htbp]
      \centering
      \begin{subfigure}{0.48\linewidth}
        \includegraphics[width=\linewidth]{before.png}
        \caption{Before}
      \end{subfigure}\hfill
      \begin{subfigure}{0.48\linewidth}
        \includegraphics[width=\linewidth]{after.png}
        \caption{After}
      \end{subfigure}
      \caption{The sample before and after.}
    \end{figure}
  • Image exactly here, not floating

    LaTeX
    \usepackage{float}
    ...
    \begin{figure}[H]
      \centering
      \includegraphics[width=0.5\linewidth]{diagram.pdf}
      \caption{Setup.}
    \end{figure}

File formats

pdfLaTeX, XeLaTeX and LuaLaTeX (Overleaf's engines) accept PNG, JPG and PDF. Use PDF or SVG converted to PDF for diagrams and plots so they stay sharp, and PNG for screenshots. EPS works with LaTeX + dvips, or with pdfLaTeX through automatic conversion.

Leave out the extension (\includegraphics{results}) and LaTeX picks the best format it finds. Avoid spaces and extra dots in file names.

Placement

A figure floats: LaTeX moves it to where it fits best, which is why it may not appear exactly where it is in the source. The option [htbp] allows here, top, bottom or a float page. [H] from the float package forces "exactly here". Refer to figures by number (Figure~\ref{…}) instead of "below".

For a picture of an equation, consider not using an image at all: the image to LaTeX converter on this site turns a screenshot of it into real, editable LaTeX.

Common mistakes

  • "Undefined control sequence" until the package is loaded.

    Instead of
    \includegraphics{photo.png}  (no graphicx)
    Write
    \usepackage{graphicx}
  • The label must come after the caption, or \ref prints the section number.

    Instead of
    \label{fig:a}\caption{…}
    Write
    \caption{…}\label{fig:a}
  • Spaces and extra dots in file names can stop the image from loading.

    Instead of
    \includegraphics{My Photo.v2.png}
    Write
    \includegraphics{my-photo-v2.png}

In Overleaf, MathJax and Word

  • Overleaf and pdfLaTeX

    Upload the image with the Upload button, then reference it by its path in the project. Overleaf supports PNG, JPG, PDF and EPS.

  • MathJax and KaTeX

    Web math renderers cannot include images; on a web page use an HTML img element next to the formula.

  • Microsoft Word

    In Word use Insert › Pictures, then References › Insert Caption for numbered figures.

Already have the whole document?

Move complete papers between PDF, Word and LaTeX without retyping; every equation stays editable.

Single printed formulas are free: 3 a day without an account, 6 with a free account. New accounts get 30 credits to try handwriting, pages and documents; failed conversions return their credits. See plans and credit prices.

Insert an image in LaTeX: questions

How do I insert an image in LaTeX?
Load graphicx and write \includegraphics[width=\linewidth]{file}.
Why is my image not where I put it?
Figures float. Use [htbp], or [H] with the float package to fix the position.
How do I put two images side by side?
Use two subfigure environments of width 0.48\linewidth inside one figure (subcaption package).
How do I resize an image?
Give a width (width=0.5\linewidth), a height or a scale in the options.

Have the formula as a picture?

Paste a screenshot or photo of the equation and get its LaTeX, ready to copy or open in the editor.

Convert an image to LaTeX