Introduction
Inserting images in LaTeX is one of the most common tasks for anyone who writes scientific papers, theses, or technical reports. While LaTeX’s primary strength lies in typesetting mathematics, its graphic capabilities are equally powerful when you learn the right commands and packages. This article walks you through every step required to embed figures smoothly, from preparing the image file to fine‑tuning placement, captions, and cross‑references. By the end, you will be able to add raster and vector graphics, control scaling, and create professional‑looking layouts that satisfy journal guidelines and impress reviewers Easy to understand, harder to ignore..
Why Use LaTeX for Images?
- Consistent styling – fonts, numbering, and caption format automatically match the rest of the document.
- Precise positioning – LaTeX’s float mechanism lets you let the compiler decide the best spot, or you can force a location with optional arguments.
- Scalable vector graphics – PDF, EPS, and SVG files retain crisp quality at any resolution, which is essential for line drawings and plots.
- Cross‑referencing – Figures receive automatic numbers and can be referenced with
\ref{}just like equations or sections.
Required Packages
The core package for graphics is graphicx, which extends LaTeX’s original graphics package with scaling, rotation, and more. Include it in the preamble:
\usepackage{graphicx} % basic image inclusion
\usepackage{float} % optional: provides [H] placement specifier
\usepackage{subcaption} % optional: for sub‑figures
\usepackage{caption} % optional: customize caption style
If you plan to use PDF, PNG, or JPG images, graphicx alone is sufficient. For EPS files (common in older scientific workflows), you may need the epstopdf package or compile with the latex -> dvips -> ps2pdf chain.
Preparing Your Image Files
-
Choose the right format
- Vector graphics (PDF, EPS, SVG) for diagrams, circuit schematics, and plots – they scale without pixelation.
- Raster graphics (PNG, JPG) for photographs or screenshots. PNG is lossless and preferable for line art; JPG is acceptable for photos where file size matters.
-
Set the correct resolution
- For raster images, 300 dpi is the standard for print quality.
- Avoid overly large files; compress PNGs with tools like
optipngorpngcrush.
-
Name files sensibly
Use lowercase letters, numbers, and hyphens only (e.g.,experiment-1-plot.pdf). LaTeX can stumble on spaces or special characters That's the part that actually makes a difference. Took long enough.. -
Store images in a dedicated folder
Keeping all graphics in a subdirectory such asfigures/simplifies path management and keeps the main folder tidy.
Basic Syntax for Including an Image
The fundamental command is \includegraphics. Its simplest form looks like this:
\begin{figure}[htbp]
\centering
\includegraphics{figures/example-image.pdf}
\caption{A brief description of the image.}
\label{fig:example}
\end{figure}
Explanation of the components
| Element | Purpose |
|---|---|
\begin{figure}[htbp] |
Starts a float environment. Think about it: the optional argument tells LaTeX where it may place the figure: here, top, bottom, page of floats. Even so, |
\centering |
Centers the image horizontally inside the figure box. Plus, |
\includegraphics{... } |
Loads the image file. |
\caption{...} |
Generates the figure number and caption text. Practically speaking, |
\label{fig:... } |
Creates a reference key for \ref{fig:...Because of that, } elsewhere. |
\end{figure} |
Ends the float. |
Controlling Size and Scaling
Often the original image is too large for the page width. graphicx offers several key options:
- Width – scale relative to the line width:
\includegraphics[width=0.8\linewidth]{...} - Height – set a fixed height:
\includegraphics[height=5cm]{...} - Scale – multiply both dimensions:
\includegraphics[scale=0.5]{...} - Angle – rotate the picture:
\includegraphics[angle=90]{...}
Example: Insert a figure that occupies 70 % of the text width and rotates 15 degrees clockwise:
\includegraphics[width=0.7\linewidth, angle=-15]{figures/rotated-diagram.pdf}
Maintaining Aspect Ratio
When you specify only one dimension (width or height), LaTeX automatically preserves the aspect ratio. If you provide both, the image may be stretched; use keepaspectratio=true to avoid distortion:
\includegraphics[width=0.6\linewidth, height=4cm, keepaspectratio]{figures/logo.png}
Placement Strategies
Default Float Behavior
LaTeX tries to place floats where they fit best while respecting the [htbp] specifiers. This often yields aesthetically pleasing results, but the figure may appear far from the point of mention.
Forcing Exact Placement
If you need the figure exactly where it is written, use the float package and the [H] specifier:
\begin{figure}[H]
\centering
\includegraphics[width=\linewidth]{figures/forced-placement.pdf}
\caption{Forced placement example}
\end{figure}
Caution: Overusing [H] can break the page layout and produce large white spaces. Reserve it for small figures or when the surrounding text demands strict proximity.
Side‑by‑Side Figures (Sub‑figures)
When you have multiple related images, the subcaption package lets you create sub‑figures with individual captions:
\begin{figure}[htbp]
\centering
\begin{subfigure}[b]{0.45\linewidth}
\includegraphics[width=\linewidth]{figures/a.pdf}
\caption{First view}
\label{fig:sub-a}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.45\linewidth}
\includegraphics[width=\linewidth]{figures/b.pdf}
\caption{Second view}
\label{fig:sub-b}
\end{subfigure}
\caption{Comparison of two experimental setups}
\label{fig:comparison}
\end{figure}
The \hfill command adds horizontal space, ensuring the two sub‑figures sit side by side with a small gap Small thing, real impact. And it works..
Adding Captions and Labels Effectively
- Keep captions concise but informative; the first sentence often appears in the List of Figures.
- Place the
\labelcommand after\captionso the reference picks up the correct figure number. - Use semantic labels (e.g.,
fig:method-scheme,fig:result-heatmap) to make cross‑referencing intuitive.
Cross‑Referencing Figures
In the main text, refer to a figure with \ref{} or \autoref{} (if you load the hyperref package):
As shown in Figure~\ref{fig:comparison}, the second configuration yields a higher efficiency.
The tilde (~) prevents a line break between “Figure” and the number But it adds up..
Dealing with Common Issues
| Symptom | Likely Cause | Fix |
|---|---|---|
| “File `example.g.Also, png' not found” | Wrong path or filename typo | Verify the relative path (`figures/example. Think about it: |
| Figure overshoots the page margins | Width set larger than \linewidth |
Use a fraction of \linewidth (e. Think about it: |
| Image appears blank or empty box | PDFLaTeX cannot read EPS, or missing epstopdf conversion |
Convert EPS to PDF (epstopdf example. eps) or compile with the latex -> dvips -> ps2pdf workflow. png`) and ensure the file name matches case‑sensitively. |
| Caption does not appear | \caption placed outside the figure environment |
Ensure \caption is inside \begin{figure}…\end{figure}. 9\linewidth). That's why , 0. |
| Float stays at the end of the document | Too many unprocessed floats; LaTeX ran out of float slots | Insert \clearpage before the problematic section, or increase the float limits with \setcounter{totalnumber}{5} etc. |
Advanced Tips
1. Using \includegraphics with \input for Reusable Figures
If you generate plots programmatically (e.g., with Python’s Matplotlib), you can store the TikZ code in a separate .tex file and include it:
\begin{figure}[htbp]
\centering
\input{figures/plot-tikz.tex}
\caption{Programmatically generated plot}
\label{fig:tikz-plot}
\end{figure}
This approach keeps the source file editable and version‑controlled.
2. Adding a Border or Frame
A thin frame can help distinguish figures in drafts:
\includegraphics[width=\linewidth, frame]{figures/diagram.pdf}
The frame key draws a 1 pt black box around the image.
3. Transparent Backgrounds
When embedding PNGs with transparent backgrounds, compile with PDFLaTeX (or XeLaTeX/LuaLaTeX) to preserve transparency. EPS files lose transparency, so convert them to PDF first That's the whole idea..
4. Using \adjustbox for Complex Layouts
The adjustbox package offers extra control, such as clipping, trimming, or adding background colors:
\usepackage{adjustbox}
...
\adjustbox{trim=0 0 0 0,clip,margin=1cm,bgcolor=gray!10}{%
\includegraphics[width=0.8\linewidth]{figures/boxed.pdf}}
5. Automatic Figure Placement with \FloatBarrier
If you want all pending floats to be placed before a new section, insert \FloatBarrier from the placeins package:
\usepackage{placeins}
...
\section{Results}
\FloatBarrier % forces all earlier figures to appear before this section
Frequently Asked Questions
Q1: Can I insert an image without a caption?
Yes. Omit the \caption{} line, but keep the figure environment if you still want automatic numbering and referencing. If you do not need numbering, use a plain \centerline{\includegraphics{...}} instead.
Q2: What is the difference between \includegraphics{file} and \input{file}?
\includegraphics reads an external image file (PDF, PNG, etc.) and treats it as a graphic box. \input reads another LaTeX source file and processes its content as regular LaTeX code—useful for TikZ pictures or code-generated plots Worth keeping that in mind..
Q3: How do I ensure my figures appear in the List of Figures?
Provide a \caption{...} inside the figure environment. The caption text is automatically added to the List of Figures when you use \listoffigures Worth keeping that in mind..
Q4: My PDF output shows a low‑resolution image even though I used a high‑resolution PNG.
Check that you are not scaling the image down excessively, and verify that the compiler is not downsampling. With PDFLaTeX, PNGs are embedded at their native resolution; however, some PDF viewers may render them at screen resolution. Use a vector format for line art whenever possible.
Q5: Can I place a figure inside a table?
Yes, but it is generally discouraged because it mixes two float types. If needed, embed the image inside a tabular environment without a surrounding figure float, and manually add a caption using the caption package’s \captionof{figure}{...} command.
\begin{table}[htbp]
\centering
\begin{tabular}{c}
\includegraphics[width=0.6\linewidth]{figures/inside-table.pdf} \\
\captionof{figure}{Figure inside a table}
\end{tabular}
\end{table}
Conclusion
Inserting images in LaTeX may initially seem intimidating, but with the graphicx package and a handful of best‑practice guidelines, the process becomes straightforward and highly customizable. Remember to choose the appropriate file format, keep your graphics organized, and use the float options ([htbp], [H]) wisely. Proper scaling, captioning, and labeling not only improve the visual appeal of your document but also enhance its searchability and referencing capabilities—key factors for academic publishing and collaborative projects Easy to understand, harder to ignore..
By mastering these techniques, you can produce documents where figures easily integrate with the surrounding text, obey journal style requirements, and convey complex information with clarity. The next time you compile a manuscript, experiment with sub‑figures, custom borders, or TikZ‑generated plots, and watch your LaTeX document transform into a polished, publication‑ready work.