All guides

Figure in LaTeX

Updated:

An image is inserted with \includegraphics from the graphicx package. The figure environment adds a caption and a number, and lets LaTeX place the figure wherever it fits on the page.

Preamble

\usepackage{graphicx}

With pdflatex, graphicx accepts PNG, JPG and PDF files. Charts are best saved as PDF: they stay sharp at any zoom level. The file extension in \includegraphics can be left out.

Putting a lab report together? In CamteX you draw formulas and tables, then send the finished document to Overleaf or download it as a PDF. Try it free

Figure with a caption

\begin{figure}[h]
  \centering
  \includegraphics[width=0.7\textwidth]{chart.png}
  \caption{Period as a function of pendulum length}
  \label{fig:chart}
\end{figure}

[h] asks LaTeX to place the figure here, but it is a request, not an order: when there is not enough room, the figure moves to the next page. \centering inside the environment avoids the extra vertical space the center environment would add.

Figure exactly here

\usepackage{float}
% ...
\begin{figure}[H]
  \centering
  \includegraphics[width=0.7\textwidth]{chart.png}
  \caption{Period T against length L}
\end{figure}

The capital H from the float package puts the figure exactly where it is in the source, so in a lab report the chart stays right under the description of the measurement. The price can be empty space at the bottom of the previous page.

Two figures side by side

\usepackage{subcaption}
% ...
\begin{figure}[h]
  \centering
  \begin{subfigure}{0.48\textwidth}
    \includegraphics[width=\linewidth]{series1.png}
    \caption{First series}
  \end{subfigure}\hfill
  \begin{subfigure}{0.48\textwidth}
    \includegraphics[width=\linewidth]{series2.png}
    \caption{Second series}
  \end{subfigure}
  \caption{Comparison of the measurement series}
\end{figure}

Each subfigure gets a letter, (a) and (b), and the whole group shares one number. Inside a subfigure \linewidth is the width of that column, not of the page, and \hfill pushes the two images to the edges.

Images in a separate folder

\graphicspath{{figures/}}

When images live in a subfolder, declare it once in the preamble and use bare file names afterwards. The trailing slash is required, and the double braces are not a typo: you can list several folders, each in its own pair.

Rotating and cropping

\includegraphics[width=0.5\textwidth, angle=90]{diagram.png}
\includegraphics[trim=1cm 0 1cm 0, clip, width=0.6\textwidth]{photo.jpg}

angle rotates the image by the given number of degrees. trim cuts the margins in the order left, bottom, right, top, and clip hides the trimmed parts, so a photo of the setup can be fixed without an image editor.

Common mistakes

See also

Don't want to memorize commands?

Draw a formula or paste a screenshot, and CamteX turns it into ready LaTeX.

Try it free