Figures, Tables & Graphics

  • JNext lesson
  • KPrevious lesson
  • FSearch lessons
  • EscClear search

Documents without visuals are walls of text. This chapter covers everything from inserting images to creating professional tables, drawing with TikZ, and using tcolorbox for styled content blocks.

Including Images

\usepackage{graphicx}

\includegraphics[width=0.8\textwidth]{filename.pdf}

Common options:

  • Option: width=0.8\textwidth | Effect: Scale to 80% of text width

  • Option: height=5cm | Effect: Fixed height

  • Option: scale=0.5 | Effect: 50% of original size

  • Option: angle=90 | Effect: Rotate 90 degrees

  • Option: trim=1cm 2cm 1cm 0cm | Effect: Crop (left, bottom, right, top)

  • Option: clip | Effect: Enable cropping (use with trim)

  • Option: page=3 | Effect: Include page 3 of a multi-page PDF

    Supported Formats

  • Format: PDF | Engine: All | Best For: Vector graphics, diagrams, other LaTeX output

  • Format: PNG | Engine: All | Best For: Screenshots, raster images with transparency

  • Format: JPG | Engine: All | Best For: Photographs

  • Format: SVG | Engine: xelatex/lualatex | Best For: Via conversion to PDF (see below)

  • Format: EPS | Engine: pdflatex (with epstopdf) | Best For: Legacy vector format

    Tip: Use PDF for all diagrams and vector graphics. It scales perfectly at any size and keeps file sizes small. Save photographs as JPG. Use PNG only when you need transparency.

SVG Workflow

SVG files can’t be included directly. Convert them to PDF first:

# Using rsvg-convert (recommended)
rsvg-convert -f pdf -o diagram.pdf diagram.svg

# Using Inkscape
inkscape diagram.svg --export-filename=diagram.pdf

# Batch convert all SVGs in a directory
for f in *.svg; do
    rsvg-convert -f pdf -o "${f%.svg}.pdf" "$f"
done

Floats and Placement

Figures and tables are “floats” — LaTeX moves them to optimal positions on the page.

\begin{figure}[htbp]
    \centering
    \includegraphics[width=0.8\textwidth]{diagram.pdf}
    \caption{A descriptive caption.}
    \label{fig:my-diagram}
\end{figure}

See Figure~\ref{fig:my-diagram} for details.

Placement Specifiers

  • Specifier: h | Meaning: Here (approximately where it appears in source)
  • Specifier: t | Meaning: Top of page
  • Specifier: b | Meaning: Bottom of page
  • Specifier: p | Meaning: Separate float page
  • Specifier: ! | Meaning: Override internal restrictions
  • Specifier: H | Meaning: Exactly here (requires float package)

    Warning: Don’t use [H] everywhere. It forces the figure to appear exactly where you put it, which often creates large blank spaces. Use [htbp] and let LaTeX optimize placement. Only use [H] when position is semantically important (e.g., a figure that must appear between two specific paragraphs).

Side-by-Side Figures

\begin{figure}[htbp]
    \centering
    \begin{minipage}{0.48\textwidth}
        \centering
        \includegraphics[width=\textwidth]{fig1.pdf}
        \caption{First figure.}
        \label{fig:first}
    \end{minipage}
    \hfill
    \begin{minipage}{0.48\textwidth}
        \centering
        \includegraphics[width=\textwidth]{fig2.pdf}
        \caption{Second figure.}
        \label{fig:second}
    \end{minipage}
\end{figure}

Tables

Basic Tables

Source:

\begin{tabular}{lrr}
  \toprule
  Name & Score & Grade \\
  \midrule
  Alice & 95 & A \\
  Bob   & 82 & B \\
  Carol & 78 & C+ \\
  \bottomrule
\end{tabular}

Result:

  • Name: Alice | Score: 95 | Grade: A

  • Name: Bob | Score: 82 | Grade: B

  • Name: Carol | Score: 78 | Grade: C+
    Column specifiers:

  • l — left aligned

  • c — centered

  • r — right aligned

  • p{3cm} — paragraph column with fixed width

  • | — vertical line (avoid in professional tables)

Important: Professional tables use booktabs:

  • \toprule instead of \hline at the top
  • \midrule for separating header from body
  • \bottomrule at the bottom
  • No vertical lines. Ever.

The difference is dramatic. Compare tables with and without booktabs and you’ll never go back.

Table Float

\begin{table}[htbp]
    \centering
    \caption{Student grades.}
    \label{tab:grades}
    \begin{tabular}{lrr}
        \toprule
        Name & Score & Grade \\
        \midrule
        Alice & 95 & A \\
        Bob   & 82 & B \\
        \bottomrule
    \end{tabular}
\end{table}

Tip: For tables, put the \caption above the table (convention). For figures, put the \caption below the image.

Full-Width Tables

Use tabularx for tables that fill the text width:

\usepackage{tabularx}

\begin{tabularx}{\textwidth}{lXr}
    \toprule
    Package & Description & Version \\
    \midrule
    amsmath & Advanced math typesetting with environments
              for aligned equations, matrices, and more & 2.17 \\
    graphicx & Image inclusion with scaling, rotation,
               and cropping & 1.2 \\
    \bottomrule
\end{tabularx}

The X column type automatically expands to fill available space with wrapped text.

The tcolorbox Package

tcolorbox creates styled boxes for tips, warnings, code blocks, theorems, and other highlighted content. This book uses seven custom tcolorbox environments (defined in preamble.tex).

\newtcolorbox{mybox}[1][]{
    enhanced,
    colback=blue!5,
    colframe=blue!75,
    boxrule=0.8pt,
    arc=3pt,
    fonttitle=\bfseries,
    title={#1},
    breakable
}

\begin{mybox}[Important Note]
Content goes here.
\end{mybox}

Key options:

  • colback — background color
  • colframe — border color
  • boxrule — border thickness
  • arc — corner radius
  • breakable — allow box to span pages
  • enhanced — enable advanced features (shadows, skins)
  • title — box title

TikZ Basics

TikZ draws vector graphics directly in your LaTeX document. Full coverage of TikZ would fill a book on its own (the TikZ manual is 1,300 pages), but here are the essentials.

The following TikZ code draws a simple coordinate axes diagram with an arrow along the x-axis labeled \( x \), an arrow along the y-axis labeled \( y \), a thick blue line from the origin to the point (3, 2) labeled \( r \), and a dashed vertical line showing the y-component:

\usepackage{tikz}

% A simple diagram
\begin{tikzpicture}
    \draw[->] (0,0) -- (4,0) node[right] {$x$};
    \draw[->] (0,0) -- (0,3) node[above] {$y$};
    \draw[thick,blue] (0,0) -- (3,2)
          node[midway,above] {$r$};
    \draw[dashed] (3,0) -- (3,2);
    \node at (1,0) [below] {$x$};
    \node at (3,1) [right] {$y$};
\end{tikzpicture}

Tip: For complex diagrams, create them as standalone SVG or PDF files and include them with \includegraphics. Reserve inline TikZ for small, simple diagrams that benefit from using the same fonts as your text.

Code Listings

The listings package formats source code with syntax highlighting:

\usepackage{listings}

\lstset{
    basicstyle=\small\ttfamily,
    keywordstyle=\color{blue}\bfseries,
    commentstyle=\color{green!50!black}\itshape,
    numbers=left,
    numberstyle=\tiny\color{gray},
    frame=single,
    breaklines=true
}

\begin{lstlisting}[language=Python]
def fibonacci(n):
    """Return the nth Fibonacci number."""
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)
\end{lstlisting}

For more advanced code formatting, consider the minted package, which uses Pygments for syntax highlighting and supports many more languages.

Exercises:

  1. Create a document with three figures: one at the top of a page ([t]), one at the bottom ([b]), and one on its own page ([p]). Add captions and cross-reference all three.
  2. Build a comparison table of three LaTeX editors (features, price, platforms) using booktabs. No vertical lines.
  3. Create a custom tcolorbox environment with a colored sidebar (instead of a full frame) and use it for margin notes or definitions.
  4. Draw a simple coordinate axes diagram in TikZ with a labeled function (e.g., \( y = x^2 \)).