Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Inserting Images

Four ways to insert images (create LaTeX figures) in Overleaf

The options are as follows:

  1. Use theInsert Figure button(The Insert Figure button on the editor toolbar), located on the editor toolbar, to insert a figure intoVisual Editor orCode Editor.
  2. Copy and paste an image intoVisual Editor orCode Editor.
  3. You can use drag and drop to create a figure. Select a file from your local file system, or one of the files uploaded to your Overleaf project, and drop it intoVisual Editor orCode Editor. Overleaf displays a window with options to customize your figure settings.
  4. UseCode Editor to write LaTeX code that inserts a graphic and places it inside a figure environment.

Options 1, 2 and 3 automatically generate the LaTeX code required to create your figure, but here we explore option 4, which provides the most flexibility.

Introduction

In this article we explain how to include images in the most common formats, how to shrink, enlarge and rotate them, and how to reference them within your document. We will start with an example to demonstrate how to import a picture.

\documentclass{article}\usepackage{graphicx}\graphicspath{{./images/}}\begin{document}The universe is immense and it seems to be homogeneous, in a large scale, everywhere we look at.\includegraphics{universe}There's a picture of a galaxy above\end{document}

Example of inserting an image

Latex can not manage images by itself, so we need to use thegraphicx package. To use it, we include the following line in the preamble:\usepackage{graphicx}.

The command\graphicspath{ {./images/} } tellsLaTeX that the images are kept in a folder namedimages under the directory of the main document.

The\includegraphics{universe} command is the one that actually included the image in the document. Hereuniverse is the name of the file containing the image without the extension, thenuniverse.PNG becomesuniverse. The file name of the image should not contain white spaces nor multiple dots.

Note: The file extension is allowed to be included, but it's a good idea to omit it. If the file extension is omitted it will prompt LaTeX to search for all the supported formats. For more details see the section aboutgenerating high resolution and low resolution images.

 Open an images example in Overleaf


The folder path to images

When working on a document which includes several images it's possible to keep those images in one or more separated folders so that your project is more organised.

The command\graphicspath{ {images/} } tellsLaTeX to look in theimages folder. The path isrelative to the current working directory—so, the compiler will look for the file in the same folder as the code where the image is included. The path to the folder is relative by default, if there is no initial directory specified, for instance

%Path relative to the .tex file containing the \includegraphics command\graphicspath{{images/}}

This is a typically straightforward way to reach the graphics folder within a file tree, but can leads to complications when.tex files within folders are included in the main.tex file. Then, the compiler may end up looking for the images folder in the wrong place. Thus,it is best practice to specify the graphics path to be relative to the main.tex file, denoting the main.tex file directory as ./, for instance:

%Path relative to the main .tex file\graphicspath{{./images/}}

as in the introduction.

The path can also beabsolute, if the exact location of the file on your system is specified. For example, if you were working on a local LaTeX installation on your own computer:

%Path in Windows format:\graphicspath{{c:/user/images/}}%Path in Unix-like (Linux, Mac OS) format\graphicspath{{/home/user/images/}}

Notice that this command requires a trailing slash / and that the path is in between double braces.

You can also set multiple paths if the images are saved in more than one folder. For instance, if there are two folders namedimages1 andimages2, use the command

\graphicspath{{./images1/}{./images2/}}

 Open an images example in Overleaf


Changing the image size and rotating the picture

If we want to further specify howLaTeX should include our image in the document (length, height, etc), we can pass those settings in the following format:

\begin{document}Overleaf is a great professional tool to edit online documents, share and backup your\LaTeX{} projects. Also offers a rather large help documentation.\includegraphics[scale=1.5]{overleaf-logo}

Example of changing image size

The command\includegraphics[scale=1.5]{overleaf-logo} will include the imageoverleaf-logo in the document, the extra parameterscale=1.5 will do exactly that, scale the image 1.5 of its real size.

You can also scale the image to a some specific width and height.

\begin{document}Overleaf is a great professional tool to edit online documents, share and backup your\LaTeX{} projects. Also offers a rather large help documentation.\includegraphics[width=5cm, height=4cm]{overleaf-logo}

Example of setting image height and width

As you probably have guessed, the parameters inside the brackets[width=3cm, height=4cm] define the width and the height of the picture. You can usedifferent units for these parameters. If only thewidth parameter is passed, the height will be scaled to keep the aspect ratio.

The length units can also be relative to some elements in document. If you want, for instance, make a picture the same width as the text:

\begin{document}The universe is immense and it seems to be homogeneous, in a large scale, everywhere we look at.\includegraphics[width=\textwidth]{universe}

Example of image set to text width

Instead of\textwidth you can use any other defaultLaTeX length:\columnsep,\linewidth,\textheight,\paperheight, etc. See the reference guide for a further description of these units.

There is another common option when including a picture within your document, to rotate it. This can easily accomplished inLaTeX:

\begin{document}Overleaf is a great professional tool to edit online, share and backup your\LaTeX{} projects. Also offers a rather large base of help documentation.\includegraphics[scale=1.2, angle=45]{overleaf-logo}

Example of rotating an image

The parameterangle=45 rotates the picture 45 degrees counter-clockwise. To rotate the picture clockwise use a negative number.

Positioning

In the previous section was explained how to include images in your document, but the combination of text and images may not look as we expected. To change this we need to introduce a newenvironment.

In the next example the figure will be positioned right below this sentence.\begin{figure}[h]\includegraphics[width=8cm]{Plot}\end{figure}

Example of positioning figures

Thefigure environment is used to display pictures as floating elements within the document. This means you include the picture inside thefigure environment and you don't have to worry about it's placement,LaTeX will position it in a such way that it fits the flow of the document.

Anyway, sometimes we need to have more control on the way the figures are displayed. An additional parameter can be passed to determine the figure positioning. In the example,begin{figure}[h], the parameter inside the brackets set the position of the figure tohere. Below a table to list the possible positioning values.

ParameterPosition
hPlace the floathere, i.e.,approximately at the same point it occurs in the source text (however, notexactly at the spot)
tPosition at thetop of the page.
bPosition at thebottom of the page.
pPut on a specialpage for floats only.
!Override internal parameters LaTeX uses for determining "good" float positions.
HPlaces the float at precisely the location in theLaTeX code. Requires thefloat package, though may cause problems occasionally. This is somewhat equivalent toh!.

In the next example you can see a picture at thetop of the document, despite being declared below the text.

In this picture you can see a bar graph that showsthe results of a survey which involved some importantdata studied as time passed.\begin{figure}[t]\includegraphics[width=8cm]{Plot}\centering\end{figure}

Example of bar graph

The additional command\centering will centre the picture. The default alignment isleft.

Wrapping text around figures

It's also possible towrap the text around a figure. When the document contains small pictures this makes it look better.

\begin{wrapfigure}{r}{0.25\textwidth}%this figure will be at the right\centering\includegraphics[width=0.25\textwidth]{mesh}\end{wrapfigure}There are several ways to plot a function of two variables, depending on the information you are interested in. For instance, if you want to see the mesh of a function so it easier to see the derivative you can use a plot like the one on the left.\begin{wrapfigure}{l}{0.25\textwidth}\centering\includegraphics[width=0.25\textwidth]{contour}\end{wrapfigure}On the other side, if you are only interested oncertain values you can use the contour plot, you can use the contour plot, you can use the contour plot, you can use the contour plot, you can use the contour plot, you can use the contour plot, you can use the contour plot, like the one on the left.On the other side, if you are only interested on certain values you can use the contour plot, you can use the contour plot, you can use the contour plot, you can use the contour plot, you can use the contour plot, you can use the contour plot, you can use the contour plot, like the one on the left.

Example of plot

For the commands in the example to work, you have to import thewrapfig package. To usewrapfig, include the following line in the document preamble:

\usepackage{wrapfig}

This makes thewrapfigure environment available and we can place an\includegraphics command inside it to create a figure around which text will be wrapped. Here is how we can specify awrapfigure environment:

\begin{wrapfigure}[lineheight]{position}{width}  ...\end{wrapfigure}

Theposition parameter has eight possible values:

rRright side of the text
lLleft side of the text
iIinside edge–near the binding (in atwoside document)
oOoutside edge–far from the binding

The uppercase version allows the figure to float. The lowercase version meansexactly here.

Now you can define thewrapfigure environment by means of the commands\begin{wrapfigure}{l}{0.25\textwidth} \end{wrapfigure}. Notice that the environment has two additional parameters enclosed in braces. Below the code is explained with more detail:

{l}
This defines the alignment of the figure. Setl for left andr for right. Furthermore, if you are using a book or any similar format, use insteado for the outer edge andi for the inner edge of the page.
{0.25\textwidth}
This is the width of figure box. It's not the width of the image itself, that must be set in the\includegraphics command. Notice that the length is relative to the text width, but normal units can also be used (cm, in, mm, etc). See thereference guide for a list of units.
\centering
This was already explained, but in this example the image will be centred by using its container as reference, instead of the whole text.

For a more complete article about image positioning seePositioning images and tables

 Open an images example in Overleaf


Captioning, labelling and referencing

Captioning images to add a brief description and labelling them for further reference are two important tools when working on a lengthy text.

Captions

Let's start with a caption example:

\begin{figure}[h]\caption{Example of a parametric plot ($\sin(x),\cos(x), x$)}\centering\includegraphics[width=0.5\textwidth]{spiral}\end{figure}

Example of parametric plot

It's really easy, just add the\caption{Some caption} and inside the braces write the text to be shown. The placement of the caption depends on where you place the command; if it's above the\includegraphics then the caption will be on top of it, if it's below then the caption will also be set below the figure.

Captions can also be placed right after the figures. Thesidecap package uses similar code to the one in the previous example to accomplish this.

\documentclass{article}\usepackage[rightcaption]{sidecap}\usepackage{graphicx}%package to manage images\graphicspath{{images/}}\begin{SCfigure}[0.5][h]\caption{Using again the picture of the universe.This caption will be on the right}\includegraphics[width=0.6\textwidth]{universe}\end{SCfigure}

Example of parametric plot with caption

There are two new commands

\usepackage[rightcaption]{sidecap}
As you may expect this line will import a package namedsidecap, but there is an additional parameter:rightcaption. This parameter establishes the placement of the caption at the right of the picture, you can also useleftcaption. In book-like documentsoutercaption andinnercaption are also available. The names of these are self-descriptive.
\begin{SCfigure}[0.5][h] \end{SCfigure}
Defines an environment similar tofigure. The first parameter is the width of the caption relative to the size of the image, as declared in\includegraphics. The second parameterh works exactly as in thefigure environment. See theplacement section for more information.

You can do a more advanced management of the caption formatting. Check thefurther reading section for references.

Labels and cross-references

Figures, just as many other elements in aLaTeX document (equations, tables, plots, etc) can be referenced within the text. This is very easy, just add a\label to thefigure orSCfigure environment, then later use that label to refer the picture.

\begin{figure}[h]\centering\includegraphics[width=0.25\textwidth]{mesh}\caption{a nice plot}\label{fig:mesh1}\end{figure}As you can see in the figure\ref{fig:mesh1}, the function grows near 0. Also, in the page\pageref{fig:mesh1} is the same example.

Example of figure with label

There are three commands that generate cross-references in this example.

\label{fig:mesh1}
This will set a label for this figure. Since labels can be used in several types of elements within the document, it's a good practice to use a prefix, such asfig: in the example.
\ref{fig:mesh1}
This command will insert the number assigned to the figure. It's automatically generated and will be updated if insert some other figure before the referenced one.
\pageref{fig:mesh1}
This prints out the page number where the referenced image appears.

The\caption is mandatory to reference a figure.

Another great characteristic in aLaTeX document is the ability to automatically generate alist of figures. This is straightforward.

\listoffigures

Example of list of figures

This command only works on captioned figures, since it uses the caption in the table. The example above lists the images in this article.

Important Note: When using cross-references yourLaTeX project must be compiled twice, otherwise the references, the page references and the table of figures won't work—Overleaf takes care of that for you.

Generating high-res and low-res images

So far while specifying the image file name in the\includegraphics command we have omitted file extensions. However, that is not necessary, though it is often useful. If the file extension is omitted, LaTeX will search for any supported image format in that directory, and will search for various extensions in the default order (which can be modified).

This is useful in switching between development and production environments. In a development environment (when the article/report/book is still in progress), it is desirable to use low-resolution versions of images (typically in .png format) for fast compilation of the preview. In the production environment (when the final version of the article/report/book is produced), it is desirable to include the high-resolution version of the images.

This is accomplished by

  • Not specifying the file extension in the\includegraphics command, and
  • Specifying the desired extension in the preamble.

Thus, if we have two versions of an image, venndiagram.pdf (high-resolution) and venndiagram.png (low-resolution), then we can include the following line in the preamble to use the .png version while developing the report -

\DeclareGraphicsExtensions{.png,.pdf}

The command above will ensure that if two files are encountered with the same base name but different extensions (for example venndiagram.pdf and venndiagram.png), then the .png version will be used first, and in its absence the .pdf version will be used, this is also a good ideas if some low-resolution versions are not available.

Once the report has been developed, to use the high-resolution .pdf version, we can change the line in the preamble specifying the extension search order to

\DeclareGraphicsExtensions{.pdf,.png}

Improving on the technique described in the previous paragraphs, we can also instructLaTeX to generate low-resolution .png versions of images on the fly while compiling the document if there is a PDF that has not been converted to PNG yet. To achieve that, we can include the following in the preamble after\usepackage{graphicx}

\usepackage{epstopdf}\epstopdfDeclareGraphicsRule{.pdf}{png}{.png}{convert #1\OutputFile}\DeclareGraphicsExtensions{.png,.pdf}

If venndiagram2.pdf exists but not venndiagram2.png, the file venndiagram2-pdf-converted-to.png will be created and loaded in its place. The commandconvert #1 is responsible for the conversion and additional parameters may be passed betweenconvert and#1. For example -convert -density 100 #1.

There are some important things to have in mind though:

  • For the automatic conversion to work, we need to callpdflatex with the--shell-escape option.
  • For the finalproduction version, we must comment out the\epstopdfDeclareGraphicsRule, so that only high-resolution PDF files are loaded. We'll also need to change the order of precedence.

 Open an images example in Overleaf


Reference guide

LaTeX units and legths

AbbreviationDefinition
ptA point, is the default length unit. About 0.3515mm
mma millimetre
cma centimetre
inan inch
exthe height of anx in the current font
emthe width of anm in the current font
\columnsepdistance between columns
\columnwidthwidth of the column
\linewidthwidth of the line in the current environment
\paperwidthwidth of the page
\paperheightheight of the page
\textwidthwidth of the text
\textheightheight of the text
\unitlengthunits of length in thepicture environment.

About image types inLaTeX

latex
When compiling withlatex, we can only use EPS images, which is a vector format.
pdflatex
If we are compiling using "pdflatex" to produce a PDF, then we can use a number of image formats -
   JPG: Best choice if we want to insert photos   PNG: Best choice if we want to insert diagrams (if a vector version could not be generated) and screenshots   PDF: Even though we are used to seeing PDF documents, a PDF can also store images    EPS: EPS images can be included using theepstopdf package (we just need to install the package, we         don't need to use\usepackage{} to include it in our document.)
Vector format or bitmap format?
Images can be of either vector format of bitmap format. Generally we don't need to worry about it, but if we do happen to know the format the image is in, we can use that information to choose an appropriate image format to include in our LaTeX document. If we have an image in vector format, we should go for PDF or EPS. If we have it in bitmap format, we should go for JPG or PNG, as storing bitmap pictures in PDF or EPS takes a lot of disk space.

 Open an images example in Overleaf


Further reading

For more information see

We only use cookies for essential purposes and to improve your experience on our site. You can find out more in ourcookie policy.

お問い合わせ

  (オプショナル)


[8]ページ先頭

©2009-2025 Movatter.jp