Ova

How Do I Insert a Picture in Overleaf?

Published in LaTeX Graphics 5 mins read

Inserting a picture in Overleaf is straightforward, whether you prefer using the intuitive built-in tool or writing the LaTeX code manually. Both methods ensure your images are correctly placed and formatted within your document.

Uploading Your Image Files

Before you can insert any picture, it must be part of your Overleaf project.

  1. Click the Upload icon: Located in the left-hand panel (usually a cloud with an arrow pointing up).
  2. Drag and drop or select: You can either drag your image files (e.g., .png, .jpg, .pdf, .eps) directly into the upload area or click "Select from your computer" to browse your files.
  3. Organize (Optional): For larger projects, consider organizing your images into a subfolder (e.g., images/) to keep your project tidy.

Using Overleaf's Built-in "Insert Figure" Feature

Overleaf offers a convenient tool that automatically generates the necessary LaTeX code for figures.

  1. Place your cursor: Position your cursor in the .tex file where you want the figure to appear.
  2. Access the "Insert Figure" tool:
    • Click the "Insert" menu option in the Overleaf editor.
    • Select "Figure" from the dropdown menu.
  3. Configure your figure: A dialog box will appear, guiding you through the insertion process:
    • Choose a graphic: Select the image file you've already uploaded to your project from the dropdown list.
    • Add a caption: Decide whether you want a descriptive caption for your figure. If so, type it in the provided field.
    • Include a label: Decide whether to add a label, which is essential for cross-referencing your figure later in your text (e.g., \ref{fig:myimage}).
    • Set image width: Define the width of your image, often as a fraction of \linewidth (e.g., 0.8\linewidth to make it 80% of the text width).
    • Positioning options: Select how you want LaTeX to try and place your figure (e.g., [h] for "here," [t] for "top," [b] for "bottom," [p] for "page of floats").
  4. Generate code: Click "Insert figure" to automatically generate the complete LaTeX figure environment based on your selections.

This feature is particularly helpful for beginners, as it creates a well-structured figure environment like this:

\begin{figure}[h!]
    \centering
    \includegraphics[width=0.8\linewidth]{your_image_filename.png}
    \caption{Your Figure Caption Here}
    \label{fig:your_image_label}
\end{figure}

Manually Inserting Images with LaTeX Code

For more control or if you prefer coding directly, you can manually type the LaTeX commands. This method requires the graphicx package.

1. Include the graphicx Package

Add the following line to your document's preamble (before \begin{document}):

\usepackage{graphicx}

2. Basic Image Insertion (\includegraphics)

The \includegraphics command is used to insert the image file itself.

  • Syntax: \includegraphics[options]{filename}
  • Example:
    \includegraphics{my_image.png} % Inserts with original size
    \includegraphics[width=0.7\textwidth]{another_image.jpg} % Scales to 70% of text width
    \includegraphics[height=5cm]{diagram.pdf} % Scales to a specific height

    Common options include width, height, scale, and angle. Using width relative to \textwidth or \linewidth (which are often the same) is recommended for responsive scaling.

3. Using the figure Environment

To treat your image as a "float" (meaning LaTeX can move it to an optimal position) and add a caption or label, enclose \includegraphics within a figure environment.

  • Syntax:

    \begin{figure}[placement_options]
        \centering % Centers the image and caption
        \includegraphics[options]{filename}
        \caption{Your caption text here.}
        \label{fig:your_label_here}
    \end{figure}
  • placement_options: These hints suggest to LaTeX where the figure can be placed:

    • h: Here (try to place it exactly where it appears in the code)
    • t: Top (at the top of a page)
    • b: Bottom (at the bottom of a page)
    • p: Page (on a dedicated float page)
    • !: Override LaTeX's internal parameters for placing floats. Often used with [h!] for a strong "here" preference.
  • Example:

    \begin{figure}[h!]
        \centering
        \includegraphics[width=0.9\linewidth]{graphs/data_analysis.pdf}
        \caption{Results of the data analysis showing key trends.}
        \label{fig:data_trends}
    \end{figure}

    You can then reference this figure in your text using See Figure~\ref{fig:data_trends} for more details..

Key Packages and Best Practices

  • graphicx: This package is indispensable for including graphics in LaTeX documents. Make sure it's declared in your preamble.
  • Image Formats: LaTeX generally supports .png, .jpg, .pdf, and .eps. For vector graphics that scale well without pixelation, .pdf is often preferred (especially for diagrams generated by software).
  • Image Resolution: Use images with appropriate resolution. High-resolution images result in larger file sizes, while low-resolution images can appear pixelated when printed.
  • Figure Placement: While [h!] is a common request, be aware that LaTeX might still place the figure elsewhere if it doesn't fit well at the requested spot. Don't fight LaTeX's float placement too hard; usually, it knows best. For deep dives into figure placement, consult the LaTeX Graphics Companion.
  • Accessibility: Always provide meaningful captions for figures.
  • Subfigures: For arranging multiple images within a single figure environment, consider using the subfigure or subfig packages.

By following these steps and best practices, you can effectively integrate images into your Overleaf documents, enhancing both their visual appeal and informational content.