Introduction
Graphics and figures transform documents from text-heavy monologues into engaging visual communications. LaTeX provides sophisticated tools for including external images, creating diagrams programmatically with TikZ, and controlling figure placement precisely.
This guide covers everything from basic image inclusion to creating publication-quality diagrams with TikZ, helping you add professional visuals to your LaTeX documents.
Basic Figure Inclusion
The figure Environment
\usepackage{graphicx}
\begin{figure}[htbp]
\centering
\includegraphics[width=0.8\linewidth]{example-image}
\caption{Descriptive Caption}
\label{fig:example}
\end{figure}
Key components:
\centering: Centers the figure horizontally\includegraphics: Inserts the image file\caption: Figure caption (automatically numbered)\label: Reference label for cross-referencing
Controlling Image Size
% Width relative to text width
\linewidth % Current line width
\textwidth % Full text width
Column width % In multicolumn documents
% Height control
.height=3in
% Scale factor
[scale=0.5] % 50% of original size
% Combined constraints
[width=0.8\linewidth,height=0.3\textheight,keepaspectratio]
Image File Formats
| Format | Compiler | Use Case |
|---|---|---|
| pdflatex, XeLaTeX | Diagrams, line art | |
| PNG | pdflatex | Screenshots, photos |
| JPG | pdflatex | Photographs |
| EPS | latex (dvips) | Legacy, PostScript |
For best quality, use PDF for diagrams and PNG/JPG for photographs.
Graphics Path Configuration
Setting Graphics Path
\usepackage{graphicx}
% Set additional search paths
\graphicspath{
{./images/}
{./diagrams/}
{/absolute/path/to/graphics/}
}
Subdirectories for Organization
\graphicspath{
{images/}
{images/chapter1/}
{images/chapter2/}
}
Figure Positioning
Placement Specifiers
\begin{figure}[htbp]
% h: Here (approximately where in source)
% t: Top of page
% b: Bottom of page
% p: Separate page for floats
% !: Override internal parameters
\end{figure}
Preventing Figure Drift
% Force specific placement
\begin{figure}[H]
% Requires: \usepackage{float}
\end{figure}
% Set float placement parameters
\floatplacement{figure}{tbp}
Multiple Figures
\begin{figure}[htbp]
\centering
\begin{minipage}{0.45\linewidth}
\centering
\includegraphics[width=\linewidth]{figure1}
\caption{First subfigure}
\label{fig:sub1}
\end{minipage}
\hfill
\begin{minipage}{0.45\linewidth}
\centering
\includegraphics[width=\linewidth]{figure2}
\caption{Second subfigure}
\label{fig:sub2}
\end{minipage}
\end{figure}
Or use the subcaption package:
\usepackage{subcaption}
\begin{figure}[htbp]
\centering
\begin{subfigure}[b]{0.45\linewidth}
\centering
\includegraphics[width=\linewidth]{figure1}
\caption{First}
\label{fig:sub1}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.45\linewidth}
\centering
\includegraphics[width=\linewidth]{figure2}
\caption{Second}
\label{fig:sub2}
\end{subfigure}
\caption{Combined caption}
\label{fig:combined}
\end{figure}
TikZ Graphics
Getting Started with TikZ
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.8]
\draw[->] (0,0) -- (5,0) node[right] {$x$};
\draw[->] (0,0) -- (0,3) node[above] {$y$};
\draw[domain=0:4,smooth,variable=\x,blue] plot ({\x},{0.5*\x*\x});
\end{tikzpicture}
\caption{Simple TikZ Plot}
\label{fig:tikz-plot}
\end{figure}
Basic Shapes
\begin{tikzpicture}
% Rectangle
\draw (0,0) rectangle (3,2);
% Circle
\draw (4,1) circle [radius=1];
% Ellipse
\draw (7,1) ellipse [x radius=1, y radius=0.5];
% Line
\draw (0,-1) -- (3,-1);
% Grid
\draw[step=0.5] (0,-3) grid (3,-1);
\end{tikzpicture}
Node Placement
\begin{tikzpicture}[node distance=2cm]
\node (start) [rectangle,draw] {Start};
\node (process) [rectangle,draw,below of=start] {Process};
\node (end) [rectangle,draw,below of=process] {End};
\draw[->] (start) -- (process);
\draw[->] (process) -- (end);
\end{tikzpicture}
Styling Options
\begin{tikzpicture}[
every node/.style={draw, rounded corners},
every edge/.style={draw, ->, thick, blue}
]
\node (a) at (0,0) {Node A};
\node (b) at (3,0) {Node B};
\node (c) at (1.5,-2) {Node C};
\path (a) edge (b) edge (c);
\end{tikzpicture}
Flowcharts with TikZ
Flowchart Library
\usepackage{tikz}
\usetikzlibrary{flowchart}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[
node distance=1.5cm,
startstop/.style={rectangle, rounded corners, draw=red, fill=red!20},
process/.style={rectangle, draw=blue, fill=blue!20},
decision/.style={diamond, draw=orange, fill=orange!20}
]
\node (start) [startstop] {Start};
\node (input) [process, below of=start] {Input Data};
\node (check) [decision, below of=input] {Valid?};
\node (process1) [process, below of=check, yshift=-0.5cm] {Process};
\node (output) [process, below of=process1] {Output};
\node (stop) [startstop, below of=output] {End};
\draw[->] (start) -- (input);
\draw[->] (input) -- (check);
\draw[->] (check) -- node[anchor=east] {yes} (process1);
\draw[->] (process1) -- (output);
\draw[->] (output) -- (stop);
\draw[->] (check) -| node[anchor=south] {no} (input);
\end{tikzpicture}
\caption{Flowchart Example}
\label{fig:flowchart}
\end{figure}
Diagrams and Schematics
Network Diagram
\begin{tikzpicture}[
server/.style={rectangle, draw, fill=blue!20, minimum width=2cm},
database/.style={cylinder, draw, fill=green!20, minimum height=1.5cm},
cloud/.style={ellipse, draw, fill=gray!20},
->, thick, >=stealth
]
\node (client) [cloud] {Client};
\node (lb) [cloud, right=2cm of client] {Load Balancer};
\node (app1) [server, above right=1cm of lb] {App Server 1};
\node (app2) [server, below right=1cm of lb] {App Server 2};
\node (db) [database, below=2cm of lb] {Database};
\draw (client) -- (lb);
\draw (lb) -- (app1);
\draw (lb) -- (app2);
\draw (app1) -- (db);
\draw (app2) -- (db);
\end{tikzpicture}
Tree Diagram
\begin{tikzpicture}[
level 1/.style={sibling distance=3cm},
level 2/.style={sibling distance=1.5cm},
every node/.style={draw, circle, minimum size=1cm}
]
\node {Root}
child {node {A}
child {node {A1}}
child {node {A2}}
}
child {node {B}
child {node {B1}}
child {node {B2}}
};
\end{tikzpicture}
Importing External Graphics
SVG to PDF Conversion
Convert SVG files for LaTeX inclusion:
# Using Inkscape (command line)
inkscape --export-filename=diagram.pdf diagram.svg
# Using rsvg-convert
rsvg-convert -f pdf -o diagram.pdf diagram.svg
Including EPS Graphics
\usepackage{graphicx}
\usepackage{epstopdf}
% Automatic EPS to PDF conversion
\DeclareGraphicsExtensions{.pdf,.png,.jpg,.eps}
\GraphicsPath{{./figures/}}
Wrapping Text Around Figures
wrapfig Package
\usepackage{wrapfig}
\usepackage{lipsum}
\begin{wrapfigure}{r}{0.5\linewidth}
\centering
\includegraphics[width=\linewidth]{example-image}
\caption{Wrapped figure}
\label{fig:wrapped}
\end{wrapfigure}
\lipsum[1]
Position specifiers: r (right), l (left), i (inside), o (outside)
Side-by-Side Figure and Text
\begin{wraptable}{r}{0.5\linewidth}
\centering
\begin{tabular}{cc}
\toprule
A & B \\
\midrule
1 & 2 \\
3 & 4 \\
\bottomrule
\end{tabular}
\caption{Wrapped table}
\label{tab:wrapped}
\end{wraptable}
Cross-Referencing Graphics
Referencing Figures
\begin{figure}[htbp]
\centering
\includegraphics[width=0.8\linewidth]{diagram}
\caption{System Architecture}
\label{fig:architecture}
\end{figure}
As shown in Figure~\ref{fig:architecture}, the system consists of...
Subfigure References
\begin{subfigure}[b]{0.45\linewidth}
\centering
\includegraphics[width=\linewidth]{fig1a}
\caption{First}
\label{fig:1a}
\end{subfigure}
\begin{subfigure}[b]{0.45\linewidth}
\centering
\includegraphics[width=\linewidth]{fig1b}
\caption{Second}
\label{fig:1b}
\end{subfigure}
Figures~\ref{fig:1a} and~\ref{fig:1b} show...
Best Practices
Image Preparation
- Use vector formats (PDF/SVG) for diagrams and charts
- Compress photographs appropriately (PNG for transparency, JPG for photos)
- Set appropriate resolution (300 DPI for print, 72-150 for screen)
- Name files descriptively:
system-architecture.pdf,experiment-results.png
Figure Design
- Keep figures simple and readable
- Include clear labels and legends
- Use consistent styling across figures
- Add captions explaining what the figure shows
Placement
- Place figures near their first reference
- Use
[htbp]for flexible placement - Consider
[H]for important figures that must stay in place
Conclusion
LaTeX provides powerful graphics capabilities, from simple image inclusion to sophisticated TikZ diagrams. Master these tools and your documents will feature professional, precisely-controlled graphics that enhance communication.
The investment in learning TikZ pays dividendsโinstead of wrestling with external diagram tools, you create diagrams directly in LaTeX, ensuring perfect consistency with your document’s typography.
Comments