Prerequisites: Basic LaTeX knowledge and the graphicx package
Time to complete: 25-30 minutes
Difficulty: Intermediate
What you’ll learn: Image formats, positioning, sizing, captions, subfigures, wrapping text, and advanced techniques
Time to complete: 25-30 minutes
Difficulty: Intermediate
What you’ll learn: Image formats, positioning, sizing, captions, subfigures, wrapping text, and advanced techniques
Understanding Image Basics
Supported Image Formats
LaTeX supports different image formats depending on the compiler:- pdfLaTeX
- XeLaTeX/LuaLaTeX
- Traditional LaTeX
Supported formats:
- PDF - Vector graphics, best quality
- PNG - Lossless compression, good for diagrams
- JPG/JPEG - Lossy compression, good for photos
- EPS - With epstopdf package
Copy
\usepackage{graphicx}
\usepackage{epstopdf} % For EPS support
Supported formats:
- All pdfLaTeX formats
- SVG - With svg package
- Additional formats via system tools
Copy
\usepackage{graphicx}
\usepackage{svg} % For SVG support
Supported formats:
- EPS - Encapsulated PostScript
- PS - PostScript
Copy
\usepackage{graphicx}
\DeclareGraphicsExtensions{.eps,.ps}
Basic Image Inclusion
Copy
\documentclass{article}
\usepackage{graphicx}
\begin{document}
% Basic image inclusion
\includegraphics{example-image}
% With explicit width
\includegraphics[width=5cm]{example-image}
% With relative width
\includegraphics[width=0.8\textwidth]{example-image}
% With height specification
\includegraphics[height=3cm]{example-image}
% Maintaining aspect ratio
\includegraphics[width=5cm, keepaspectratio]{example-image}
\end{document}
Figure Environments
Basic Figures with Captions
Copy
\begin{figure}[htbp]
\centering
\includegraphics[width=0.8\textwidth]{example-image}
\caption{A descriptive caption for the image}
\label{fig:example}
\end{figure}
% Reference the figure
As shown in Figure~\ref{fig:example}, the results are clear.
% Short caption for list of figures
\begin{figure}[htbp]
\centering
\includegraphics[width=0.6\textwidth]{data-plot}
\caption[Short caption]{Long descriptive caption with detailed explanation}
\label{fig:data}
\end{figure}
Advanced Caption Formatting
Copy
\usepackage{caption}
\usepackage{subcaption}
% Global caption setup
\captionsetup{
font=small,
labelfont=bf,
format=plain,
justification=centering,
skip=10pt
}
% Per-figure caption style
\begin{figure}[htbp]
\centering
\includegraphics[width=0.6\textwidth]{photo}
\captionsetup{font=footnotesize, labelfont=sc}
\caption{A photo with custom caption style}
\end{figure}
% Caption without label
\begin{figure}[htbp]
\centering
\includegraphics[width=0.5\textwidth]{art}
\caption*{An artistic image without Figure label}
\end{figure}
Multiple Images
Side-by-Side Images
Copy
% Method 1: Using minipage
\begin{figure}[htbp]
\centering
\begin{minipage}{0.45\textwidth}
\centering
\includegraphics[width=\textwidth]{image1}
\caption{First image}
\label{fig:first}
\end{minipage}
\hfill
\begin{minipage}{0.45\textwidth}
\centering
\includegraphics[width=\textwidth]{image2}
\caption{Second image}
\label{fig:second}
\end{minipage}
\end{figure}
% Method 2: Using subfigure (deprecated, use subcaption)
\usepackage{subcaption}
\begin{figure}[htbp]
\centering
\begin{subfigure}{0.45\textwidth}
\includegraphics[width=\textwidth]{before}
\caption{Before treatment}
\label{fig:before}
\end{subfigure}
\hfill
\begin{subfigure}{0.45\textwidth}
\includegraphics[width=\textwidth]{after}
\caption{After treatment}
\label{fig:after}
\end{subfigure}
\caption{Comparison of results}
\label{fig:comparison}
\end{figure}
% Reference subfigures
Figure~\ref{fig:before} shows the initial state, while
Figure~\ref{fig:after} demonstrates the improvement.
The overall comparison is shown in Figure~\ref{fig:comparison}.
Grid Layouts
Copy
\begin{figure}[htbp]
\centering
\begin{subfigure}{0.3\textwidth}
\includegraphics[width=\textwidth]{img1}
\caption{Sample A}
\end{subfigure}
\hfill
\begin{subfigure}{0.3\textwidth}
\includegraphics[width=\textwidth]{img2}
\caption{Sample B}
\end{subfigure}
\hfill
\begin{subfigure}{0.3\textwidth}
\includegraphics[width=\textwidth]{img3}
\caption{Sample C}
\end{subfigure}
\vspace{0.5cm}
\begin{subfigure}{0.3\textwidth}
\includegraphics[width=\textwidth]{img4}
\caption{Sample D}
\end{subfigure}
\hfill
\begin{subfigure}{0.3\textwidth}
\includegraphics[width=\textwidth]{img5}
\caption{Sample E}
\end{subfigure}
\hfill
\begin{subfigure}{0.3\textwidth}
\includegraphics[width=\textwidth]{img6}
\caption{Sample F}
\end{subfigure}
\caption{Grid of experimental samples}
\label{fig:grid}
\end{figure}
Text Wrapping
Wrapping Text Around Images
Copy
\usepackage{wrapfig}
\usepackage{lipsum} % For dummy text
\begin{document}
\section{Text Wrapping Examples}
% Right-aligned wrapped figure
\begin{wrapfigure}{r}{0.4\textwidth}
\centering
\includegraphics[width=0.35\textwidth]{portrait}
\caption{A wrapped figure}
\end{wrapfigure}
\lipsum[1-2] % Dummy text that wraps around the figure
% Left-aligned wrapped figure
\begin{wrapfigure}{l}{0.3\textwidth}
\vspace{-20pt} % Adjust vertical position
\centering
\includegraphics[width=0.25\textwidth]{icon}
\caption{Left-aligned image}
\vspace{-20pt} % Reduce space after
\end{wrapfigure}
\lipsum[3]
% Inner/outer alignment for two-sided documents
\begin{wrapfigure}{o}{0.35\textwidth} % o = outer margin
\centering
\includegraphics[width=0.3\textwidth]{diagram}
\caption{Outer margin placement}
\end{wrapfigure}
\lipsum[4]
\end{document}
Image Transformations
Scaling and Rotating
Copy
\usepackage{graphicx}
% Scaling options
\includegraphics[scale=0.5]{image} % 50% of original
\includegraphics[scale=1.2]{image} % 120% of original
% Rotation
\includegraphics[angle=90]{image} % 90 degrees counterclockwise
\includegraphics[angle=-45]{image} % 45 degrees clockwise
% Combined transformations
\includegraphics[width=5cm, angle=30]{image}
% Reflection
\reflectbox{\includegraphics[width=3cm]{image}}
% Scale to specific dimensions
\resizebox{5cm}{3cm}{%
\includegraphics{image}%
}
% Scale proportionally
\resizebox{5cm}{!}{% ! maintains aspect ratio
\includegraphics{image}%
}
Special Effects
Copy
\usepackage{graphicx}
\usepackage[pdftex]{transparent}
% Transparency (requires pdflatex)
\begin{figure}[htbp]
\centering
\transparent{0.5}\includegraphics[width=0.5\textwidth]{watermark}
\caption{Semi-transparent image}
\end{figure}
% Framed images
\usepackage{fancybox}
\begin{figure}[htbp]
\centering
\shadowbox{\includegraphics[width=0.4\textwidth]{photo}}
\caption{Image with shadow box}
\end{figure}
\begin{figure}[htbp]
\centering
\ovalbox{\includegraphics[width=0.4\textwidth]{portrait}}
\caption{Image with oval frame}
\end{figure}
% Custom frames
\setlength{\fboxsep}{10pt}
\setlength{\fboxrule}{2pt}
\fbox{\includegraphics[width=0.3\textwidth]{art}}
Advanced Techniques
Dynamic Image Paths
Copy
% Conditional image inclusion
\newif\ifprintversion
\printversiontrue % or \printversionfalse
\begin{figure}[htbp]
\centering
\ifprintversion
\includegraphics[width=0.8\textwidth]{high-res-image}
\else
\includegraphics[width=0.8\textwidth]{web-image}
\fi
\caption{Resolution-appropriate image}
\end{figure}
% Multiple format search
\DeclareGraphicsExtensions{.pdf,.png,.jpg}
\includegraphics{myimage} % Searches for myimage.pdf, then .png, then .jpg
% Platform-specific paths
\usepackage{iftex}
\ifXeTeX
\graphicspath{{xelatex-images/}}
\else
\graphicspath{{pdflatex-images/}}
\fi
External Graphics Tools
Copy
% Auto-convert formats
\usepackage{epstopdf}
\epstopdfsetup{update} % Only convert if source is newer
% Include matplotlib plots
\usepackage{pgf}
\input{figure.pgf} % Generated by matplotlib
% Include Inkscape SVG
\usepackage{svg}
\includesvg[width=0.5\textwidth]{diagram}
% TikZ external
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[prefix=figures/]
\begin{figure}[htbp]
\centering
\begin{tikzpicture}
% Complex TikZ drawing
\end{tikzpicture}
\caption{Externalized TikZ figure}
\end{figure}
Optimizing Images
File Size Management
Best practices for image optimization:
-
Choose the right format:
- PDF: Vector graphics, diagrams, plots
- PNG: Screenshots, diagrams with text
- JPG: Photographs, complex images
-
Optimize before including:
Copy
# Compress PDF gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf # Optimize PNG optipng -o7 image.png # Compress JPG jpegoptim --max=85 image.jpg -
Use appropriate resolution:
- Print: 300 DPI
- Screen: 72-96 DPI
- Web: 72 DPI
Performance Tips
Copy
% Preload frequently used images
\usepackage{graphicx}
\newsavebox{\mylogo}
\savebox{\mylogo}{\includegraphics[width=2cm]{logo}}
% Use the preloaded image multiple times
\usebox{\mylogo} % Fast, no reloading
% Bounding box for faster compilation
\includegraphics[bb=0 0 100 100, width=5cm]{complex-image}
% External bounding box files
% Create image.bb file with: %%BoundingBox: 0 0 595 842
\includegraphics[width=\textwidth]{image} % Reads image.bb
Troubleshooting
Common Issues and Solutions
Common image problems:
-
“File not found” errors:
Copy
% Check file extension \includegraphics{image.png} % Wrong \includegraphics{image} % Correct % Check path \graphicspath{{./images/}{../figures/}} -
Wrong image size:
Copy
% Don't use both width and height unless needed \includegraphics[width=5cm, height=3cm]{image} % May distort \includegraphics[width=5cm]{image} % Maintains ratio -
Figure placement issues:
Copy
% Too strict placement \begin{figure}[h] % May cause bad spacing \begin{figure}[htbp] % More flexible -
Overflow into margins:
Copy
% Image too wide \includegraphics[width=\textwidth]{image} % Better: leave some margin \includegraphics[width=0.95\textwidth]{image}
Debug Mode
Copy
% Show frame around images
\usepackage[draft]{graphicx}
% Show figure boundaries
\usepackage{showframe}
% Track float placement
\usepackage{float}
\floatplacement{figure}{H}
% List all figures
\listoffigures
Best Practices Checklist
✅ Image workflow checklist:
- Choose appropriate file format
- Optimize file size before inclusion
- Use relative widths (
\textwidth) - Always include captions
- Add meaningful labels
- Test different positions
- Check output at final resolution
- Verify all images are included in repository
- Use consistent naming convention
- Document image sources
Complete Example
Copy
\documentclass[11pt, a4paper]{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{caption}
% Setup
\graphicspath{{images/}{figures/}}
\captionsetup{font=small, labelfont=bf}
\begin{document}
\title{Comprehensive Image Examples}
\author{Your Name}
\date{\today}
\maketitle
\section{Introduction}
This document demonstrates various image inclusion techniques in LaTeX.
\section{Basic Figure}
\begin{figure}[htbp]
\centering
\includegraphics[width=0.6\textwidth]{example-image-a}
\caption{A simple centered figure}
\label{fig:simple}
\end{figure}
As shown in Figure~\ref{fig:simple}, basic image inclusion is straightforward.
\section{Wrapped Figure}
\begin{wrapfigure}{r}{0.4\textwidth}
\centering
\includegraphics[width=0.35\textwidth]{example-image-b}
\caption{Wrapped figure}
\label{fig:wrapped}
\end{wrapfigure}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris. The wrapped figure appears to the right of this text, demonstrating how text flows around images.
\section{Multiple Images}
\begin{figure}[htbp]
\centering
\begin{subfigure}{0.45\textwidth}
\includegraphics[width=\textwidth]{example-image-a}
\caption{First subfigure}
\label{fig:sub1}
\end{subfigure}
\hfill
\begin{subfigure}{0.45\textwidth}
\includegraphics[width=\textwidth]{example-image-b}
\caption{Second subfigure}
\label{fig:sub2}
\end{subfigure}
\vspace{0.5cm}
\begin{subfigure}{0.45\textwidth}
\includegraphics[width=\textwidth]{example-image-c}
\caption{Third subfigure}
\label{fig:sub3}
\end{subfigure}
\hfill
\begin{subfigure}{0.45\textwidth}
\includegraphics[width=\textwidth]{example-image}
\caption{Fourth subfigure}
\label{fig:sub4}
\end{subfigure}
\caption{Grid layout with four subfigures}
\label{fig:grid}
\end{figure}
Figure~\ref{fig:grid} shows a 2×2 grid layout, with individual subfigures \ref{fig:sub1} through \ref{fig:sub4}.
\section{Rotated and Scaled Image}
\begin{figure}[H]
\centering
\includegraphics[angle=45, scale=0.5]{example-image}
\caption{Rotated and scaled image}
\label{fig:rotated}
\end{figure}
\section{Conclusion}
This document demonstrated various image handling techniques in LaTeX, from basic inclusion to advanced layouts.
\listoffigures
\end{document}
Next Steps
Now that you’ve mastered image handling:Creating Tables
Learn to create professional tables
TikZ Graphics
Create diagrams with TikZ
Managing Large Documents
Handle images in multi-file projects
Troubleshooting
Fix common image-related errors
Pro tip: Always keep high-resolution originals of your images. You can create lower-resolution versions for drafts and switch to high-resolution for final output using conditional inclusion.
