latex - Adding Figures in Latex - Embed an Image/picture - latex tutorial



How to Add Figures in Latex?

  • It is necessary to add pictures to your documents.
  • Using LaTeX all pictures will be indexed automatically and tagged with successive numbers when using the figure environment and the graphicx package.

Sample Code:

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\begin{figure}

  \includegraphics[width=\linewidth]{boat.jpg}

  \caption{A boat.}

  \label{fig:boat1}

\end{figure}

Figure \ref{fig:boat1} shows a boat.

\end{document}

This code will create the following pdf:

 output of adding figures in latex

learn latex tutorial - output of adding figures in latex - latex example

  • The figure environment takes care of the numbering and positioning of the image within the document.
  • In order to include a figure, you must use the \includegraphics command.
  • It takes the image width as an option in brackets and the path to your image file.
  • If you \linewidth is mentioned into the brackets, which means the picture will be scaled to fit the width of the document.
  • As a result the smaller pictures are upscaled and larger pictures are downscaled respectively.
  • In this case the image is stored in the same directory as my .tex file, so I simply put boat.jpg here to include it.
  • For large documents, you want to store image files in a different folder, say we created a folder images, then we would simply write images/boat.jpg into the braces.
  • In the next command we set a \caption, which is the text shown below the image and a \label which is invisible, but useful if we want to refer to our figure in our document.
  • You can use the \ref command to refer to the figure (marked by label) in your text and it will then be replaced by the correct number.
  • LaTeX is smart enough to retrieve the correct numbers for all your images automatically.
  • Note that you will need to include the graphicx package in order to use this code.

Force the picture to a certain position - Set the float:

  • You will notice that the figure doesn't necessarily show up in the exact place as you put your code in the .tex file.
  • If your document contains a lot of text, it's possible that LaTeX will put the picture on the next page, or any other page where it finds sufficient space.
  • To prevent this behavior, it's necessary to set the float value for the figure environment.
%...

\begin{figure}[h!]

%...
  • Setting the float by adding [h!] behind the figure environment \begin tag will force the figure to be shown at the location in the document. Possible values are:
    • h (here) - same location
    • t (top) - top of page
    • b (bottom) - bottom of page
    • p (page) - on an extra page
    • ! (override) - will force the specified location
  • However, I have only used the [h!] option so far. The float package (\usepackage{float}) allows to set the option to [H], which is even stricter than [h!].


Related Searches to Adding figures in LaTeX - Embed an image/picture