The graphicx package
Includes image files with \includegraphics and sets their size, rotation and cropping through options such as width, angle and trim.
Loading the package
\usepackage{graphicx}LaTeX has no command for images without a package. graphicx provides \includegraphics[options]{file}. With pdfLaTeX the file can be a PNG, a JPG or a PDF; an SVG has to be converted to PDF first.
\includegraphics produces a box that behaves like one large letter. It does not centre the image, add a caption or let it float; for that, put it in a figure environment with \centering and \caption.
The package also scales and rotates text and other material: \rotatebox, \scalebox and \resizebox.
Main commands
\includegraphics[width=0.8\textwidth]{image.png} | Image at 80 % of the text width; the height follows from the aspect ratio. |
height=4cm, keepaspectratio | With width and height together, keepaspectratio fits the image into the box instead of distorting it. |
scale=0.5 | Half the natural size of the image. |
angle=90 | Rotates the image by 90° counterclockwise. |
trim=10 20 30 40, clip | Cuts off 10, 20, 30 and 40 bp at the left, bottom, right and top. |
page=2 | Selects a page of a PDF file with several pages. |
\graphicspath{{images/}} | Folder in which LaTeX also looks for image files. |
\resizebox{\textwidth}{!}{...} | Scales any content, for example a table, to the text width; ! keeps the proportions. |
Complete example
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}[ht]
\centering
\includegraphics[width=0.6\textwidth]{image.png}
\caption{An image at 60\,\% of the text width.}
\label{fig:example}
\end{figure}
Inside a line: \includegraphics[height=1.5ex]{logo.png}.
Rotated: \includegraphics[width=2cm, angle=90]{image.png}.
Cropped: \includegraphics[width=2cm, trim=40 20 40 20, clip]{image.png}.
A page of a PDF: \includegraphics[page=1, width=3cm]{figure.pdf}.
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
- A wrong file name or path gives File `imgae' not found. An SVG file gives Unknown graphics extension: .svg.
- The options are applied from left to right.
angle=90, width=4cmmakes the rotated image 4 cm wide;width=4cm, angle=90scales first and then rotates, so the 4 cm become the height. \graphicspathneeds two pairs of braces, one around each folder.\graphicspath{images/}with one pair finds no file.- The option
draft, also when passed to\documentclass, replaces every image with a frame that shows the file name.
Related pages
- How-to Insert an image in LaTeX
- How-to Resize an image in LaTeX
- How-to Crop an image in LaTeX
- How-to Set the image folder in LaTeX
- Error File `imgae' not found
- How-to Set up a thesis in LaTeX
- How-to Figure placement in LaTeX
- How-to Subfigures in LaTeX
- How-to Figure caption and label in LaTeX
- How-to Wrap text around an image in LaTeX