How to Fix LaTeX Figure Position — Controlling Image Placement
In this tutorial, you'll learn about How to Fix LaTeX Figure Position. We cover key concepts, practical examples, and best practices.
The Problem
Your figure appears in a different location than expected:
\begin{figure}[h]
\includegraphics{diagram.png}
\caption{My Diagram}
\end{figure}
The image shows up on the next page or at the end of the document. LaTeX's float algorithm places figures based on positioning specifiers, not their source location.
Quick Fix
Step 1: Use the right placement specifier
WRONG — using only [h]:
\begin{figure}[h]
\includegraphics{diagram.png}
\caption{My Diagram}
\end{figure}
RIGHT — use [htbp]:
\begin{figure}[htbp]
\includegraphics{diagram.png}
\caption{My Diagram}
\end{figure}
Step 2: Force placement with the float package
\usepackage{float}
...
\begin{figure}[H]
\includegraphics{diagram.png}
\caption{My Diagram}
\end{figure}
Step 3: Use centering and spacing
\usepackage{caption}
\captionsetup[figure]{skip=6pt}
\begin{figure}[htbp]
\centering
\includegraphics[width=0.8\textwidth]{diagram.png}
\caption{My Diagram}
\end{figure}
Step 4: Use the placeins package
\usepackage{placeins}
...
\FloatBarrier
\begin{figure}[htbp]
\includegraphics{diagram.png}
\caption{My Diagram}
\end{figure}
Step 5: Adjust float parameters globally
\renewcommand{\topfraction}{0.9}
\renewcommand{\bottomfraction}{0.8}
\renewcommand{\textfraction}{0.1}
\setcounter{totalnumber}{3}
Prevention
- Use
[htbp]as your default placement specifier. - Avoid placing figures right after section headings.
- Use
\centeringinside each figure environment. - Let LaTeX handle placement unless the position is critical.
Common Mistakes with figure position
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world LATEX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro