BudiBadu Logo
Samplebadu

LaTeX by Example: Images

LaTeX2e

Including graphics requires the `graphicx` package. This example demonstrates how to include, scale, and caption images.

Code

\documentclass{article}
\usepackage{graphicx} % Required for images

\begin{document}
    Here is a picture of a cat:
    
    % The figure environment makes the image "float"
    % h = try to place here, t = top, b = bottom
    \begin{figure}[h]
        \centering
        % Include the image file (e.g., cat.jpg or cat.png)
        % width=0.5\textwidth scales it to half the text width
        \includegraphics[width=0.5\textwidth]{cat}
        
        \caption{A cute cat.}
        \label{fig:cat}
    \end{figure}
    
    Figure \ref{fig:cat} shows a cat.
\end{document}

Explanation

To include images in LaTeX, you need the graphicx package. The core command is \includegraphics{filename}. You can specify optional arguments to control the size, such as width=0.5\textwidth (half the width of the text block) or scale=0.8. LaTeX supports common formats like JPG, PNG, and PDF.

Images are usually wrapped in a figure environment. This is a "float", meaning LaTeX decides the best place to put it (top of page, bottom of page, or on a separate page) to avoid awkward page breaks. You can suggest a position using options like [h] (here), [t] (top), or [b] (bottom).

The figure environment also allows you to add a \caption and a \label. The caption provides a description and a figure number. The label allows you to reference the figure number elsewhere in the text using \ref{}, just like with equations.

Code Breakdown

2
\usepackage{graphicx} is mandatory for handling images.
12
\includegraphics[...] inserts the image. The file extension is often optional.