LaTeX/Importing Graphics : Différence entre versions

De LaTeX
Aller à : navigation, rechercher
(Creating Vector Graphics)
m (Two graphics side by side)
 
(11 révisions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
 
<noinclude>{{LaTeX/Top}}</noinclude>
 
<noinclude>{{LaTeX/Top}}</noinclude>
 
Strictly speaking, LaTeX cannot manage pictures directly: in order to introduce graphics within documents, LaTeX just creates a box with the same size as the image you want to include and embeds the picture, without any other processing. This means you will have to take care that the images you want to include are in the right format to be included. This is not such a hard task because LaTeX supports the most common picture formats around.
 
Strictly speaking, LaTeX cannot manage pictures directly: in order to introduce graphics within documents, LaTeX just creates a box with the same size as the image you want to include and embeds the picture, without any other processing. This means you will have to take care that the images you want to include are in the right format to be included. This is not such a hard task because LaTeX supports the most common picture formats around.
 +
 +
 +
Most mathematical/scientific graphics software allows you to save graphics (figures, diagrams, graphs) in PostScript form; this includes Mathematica, Maple, Matlab, IDL, and xfig (a marvelous X figure-drawing program). Even bitmap images like JPEG and PNG files can be converted to PostScript form with programs like ``xv'' or ``convert''.
 +
  
 
See http://www.ukonline.be/programmation/latex/tutoriel/chapitre11/page3.php as well as http://merkel.zoneo.net/Latex/index.php
 
See http://www.ukonline.be/programmation/latex/tutoriel/chapitre11/page3.php as well as http://merkel.zoneo.net/Latex/index.php
Ligne 194 : Ligne 198 :
  
 
As you may have noticed, the file name of the picture is always without the extensions: LaTeX will take care of getting the right version for us. Consider the following situation: you have added some pictures to your document in JPG and you have successfully compiled it in PDF. Now you want to compile it in DVI, you run ''latex'' and you get a lot of errors... because you forgot to provide the EPS versions of the pictures you want to insert. At the beginning of this book, we had stated that the same LaTeX source can be compiled in both DVI and PDF without any change. This is true, as long as you don't use particular packages, and <code>graphicx</code> is one of those. In any case, you can still use both compilers with documents with pictures as well, as long as you always remember to provide the pictures in two formats (EPS and one of JPG, PNG and PDF).
 
As you may have noticed, the file name of the picture is always without the extensions: LaTeX will take care of getting the right version for us. Consider the following situation: you have added some pictures to your document in JPG and you have successfully compiled it in PDF. Now you want to compile it in DVI, you run ''latex'' and you get a lot of errors... because you forgot to provide the EPS versions of the pictures you want to insert. At the beginning of this book, we had stated that the same LaTeX source can be compiled in both DVI and PDF without any change. This is true, as long as you don't use particular packages, and <code>graphicx</code> is one of those. In any case, you can still use both compilers with documents with pictures as well, as long as you always remember to provide the pictures in two formats (EPS and one of JPG, PNG and PDF).
 +
 +
=== Two graphics side by side ===
 +
<source lang="latex">
 +
Figure~\ref{f:gradient-3} shows how ... and Figure~\ref{f:gradient-4} ...
 +
  \begin{figure}[htbp]
 +
    \setlength{\captionindent}{0pt}
 +
    \begin{minipage}[t]{0.45\textwidth}
 +
    \centering\includegraphics[width=\textwidth]{gradient-3}
 +
  \caption{Gradient in $X$ deduced from gradient in $P$ and $A(p)x$.}\label{f:gradient-3}
 +
    \end{minipage}\hfil%
 +
    \begin{minipage}[t]{0.45\textwidth}
 +
    \centering\includegraphics[width=\textwidth]{gradient-4}
 +
    \caption{$P$ moved to the minimum where $\nabla f(p)=0$ and $\nabla f(x)=A(p)x$. }\label{f:gradient-4}
 +
    \end{minipage}
 +
  \end{figure}
 +
</source>
  
 
=== Borders ===
 
=== Borders ===
 
It is possible to have LaTeX create a border around your image by using <code>fbox</code>:
 
It is possible to have LaTeX create a border around your image by using <code>fbox</code>:
 
 
<source lang="latex">
 
<source lang="latex">
 
\setlength\fboxsep{0pt}
 
\setlength\fboxsep{0pt}
Ligne 289 : Ligne 308 :
 
=== Including full PDF pages ===
 
=== Including full PDF pages ===
  
There is a great package for including full pages of PDF files: [http://www.ctan.org/tex-archive/macros/latex/contrib/pdfpages pdfpages]. It is capable of inserting full pages as is and more pages per one page in any layout (e.g. 2x3). See more information in its [http://www.ctan.org/tex-archive/macros/latex/contrib/pdfpages/pdfpages.pdf documentation].
+
There is a great package for including full pages of PDF files: [http://www.ctan.org/tex-archive/macros/latex/contrib/pdfpages pdfpages] and its [http://www.ctan.org/tex-archive/macros/latex/contrib/pdfpages/pdfpages.pdf documentation]. It is capable of inserting full pages as is and more pages per one page in any layout (e.g. 2x3). Further pagination is ok. You can even keep your current headings (see example below). Be careful that it may not work with scanned PDF documents.
 +
 +
<source lang=latex>
 +
 
 +
% To insert a single page, even a big one like a poster.
 +
\includepdf[pages=1]{myposter.pdf}
 +
\includepdf[pages=1, offset=+1in -1in]{myposter.pdf} % If not centered
 +
 
 +
% You want to insert a scientific article in your thesis keeping
 +
%  the current headings defined earlier in your text. Here the headings are
 +
%  on top of pages and thus the scientific article is shifted downwards in
 +
%  order to not overwrite the headings of the review:
 +
\pagestyle{headings}
 +
...
 +
\includepdf[pages=1-, offset=+0in -1in, pagecommand={%
 +
  \thispagestyle{headings}}{articles/Brouard-Pontone-Scherpereel-mathpop2007.pdf}
 +
 
 +
% To insert pages 3 to 5 of report in 3 different pages
 +
\includepdf[pages=3-5]{myreport.pdf}
 +
 
 +
% Specific pages and ranges of pages can be selected on a single page
 +
% and presented 1, 4, 5 on top half and  6, 8 and 10 on bottom half
 +
\includepdf[nup=3x2, pages={1,4-6,8,10}]{myreport.pdf}
 +
 
 +
% If you want to sort by column, you will get 1, 6 on top,
 +
%  4, 8 in the middle and 5, 10 in the bottom
 +
\includepdf[nup=3x2, pages={1,4-6,8,10}, column]{myreport.pdf}
 +
 
 +
% This will output two pages, forcing landscape for pages 1 (on top)
 +
% and 2 on bottom for the first page
 +
% and  3 (top) 4 (bottom) for the second page.
 +
\includepdf[nup=1x2, pages=1-4]{myreport.pdf}
 +
 
 +
% This will output a single page with an empty subpage and page 5 on top
 +
%  and pages 6 and 7 on bottom
 +
\includepdf[nup=2x2, pages={{},5-7}]{myreport.pdf}
 +
 
 +
% You can frame your subpages as well reduce and give space between them
 +
\includepdf[nup=2x2, pages=3-6, scale=.8, delta=8mm 11mm, frame]{dummy.pdf}
 +
 
 +
% With pdfmerge you can merge some specific pages from different documents
 +
% Here are three document where the title page (of most interest) of the
 +
% first document is on page 2, on first page for doc2 and third for doc3.
 +
%
 +
\includepdfmerge[nup=1x3, landscape, linktodoc] {doc1.pdf, 2, doc2.pdf, 1, doc3.pdf, 3}
 +
\includepdfmerge[nup=1x3, landscape,linktodoc]{articles/Enquête_Cfar-Sfar-Ined_AFAR2002.pdf, 1,
 +
  articles/Brouard-Pontone-Scherpereel-mathpop2007.pdf, 2,
 +
  articles/Démographie_Pontone-Brouard2010_ANNFAR4425.pdf, 3}
 +
 
 +
</source>
 +
See more examples at [http://www-hep2.fzu.cz/tex/texmf-dist/doc/latex/pdfpages/pdf-ex.pdf ].
  
 
== Graphics made with TeX macros or packages ==
 
== Graphics made with TeX macros or packages ==
Ligne 322 : Ligne 391 :
 
==Creating Vector Graphics==
 
==Creating Vector Graphics==
  
=== Gnuplot ===
+
=== "Remember and type" softwares ===
 +
==== Gnuplot ====
 
Gnuplot is a very powerful drawing language in 2D as in 3D. It is used internally by many other software like [[Wikipedia:en:Octave_software]]. At INED, [http://euroreves.ined.fr/imach imach]] as well as the book entitled African Population and AIDS has some dynamic [http://sauvy.ined.fr/popafsi/english/fig25-26e.html pages] using gnuplot at run time.
 
Gnuplot is a very powerful drawing language in 2D as in 3D. It is used internally by many other software like [[Wikipedia:en:Octave_software]]. At INED, [http://euroreves.ined.fr/imach imach]] as well as the book entitled African Population and AIDS has some dynamic [http://sauvy.ined.fr/popafsi/english/fig25-26e.html pages] using gnuplot at run time.
  
 
Gnuplot works on Windows, MaC and Linux. Gnuplot can output various graphic formats, vector formats including svg, eps, pdf or raster formats like png or gif or even animated gifs (see [[Wikipedia:en:Foucault_Pendulum]] for example).  
 
Gnuplot works on Windows, MaC and Linux. Gnuplot can output various graphic formats, vector formats including svg, eps, pdf or raster formats like png or gif or even animated gifs (see [[Wikipedia:en:Foucault_Pendulum]] for example).  
  
See [[LaTeX/Gnuplot]] too.  
+
Latest version of Gnuplot is able to output Tikz TeX sources.
  
===Xfig===
+
See [[LaTeX/Gnuplot]] too.
 +
 
 +
=== WYSIWYG softwares===
 +
====Xfig====
  
 
Vector graphics can be created using the vector painting program Xfig (see [[LaTeX/Installation#Xfig|Installation]]), and exported for LaTeX. In Xfig, once your graphic is saved as a file <tt>test.fig</tt>, you need to export it using the '''File > Export''' drop down menu from the main Xfig window and then select the "Combined PS/Latex (both parts)" in the language drop down list. If you don't change any other settings, two files will be created in the same directory as the <tt>test.fig</tt> file, such as: <tt>test.pstex_t</tt> and <tt>test.pstex</tt>. The figure can then be placed in a LaTeX document:
 
Vector graphics can be created using the vector painting program Xfig (see [[LaTeX/Installation#Xfig|Installation]]), and exported for LaTeX. In Xfig, once your graphic is saved as a file <tt>test.fig</tt>, you need to export it using the '''File > Export''' drop down menu from the main Xfig window and then select the "Combined PS/Latex (both parts)" in the language drop down list. If you don't change any other settings, two files will be created in the same directory as the <tt>test.fig</tt> file, such as: <tt>test.pstex_t</tt> and <tt>test.pstex</tt>. The figure can then be placed in a LaTeX document:
Ligne 342 : Ligne 415 :
 
see [[LaTeX/Xfig]] too.
 
see [[LaTeX/Xfig]] too.
  
===Ipe 7===
+
====Ipe 7====
  
 
The Ipe extensible drawing editor is a free vector graphics editor for creating figures in PDF or EPS format.
 
The Ipe extensible drawing editor is a free vector graphics editor for creating figures in PDF or EPS format.
Ligne 350 : Ligne 423 :
 
See [[LaTeX/Ipe]] too.
 
See [[LaTeX/Ipe]] too.
  
===Inkscape===
+
====Inkscape====
  
 
Another program for creating vector graphics is [http://www.inkscape.org/ Inkscape]. It works with [http://www.w3.org/Graphics/SVG/ Scalable Vector Graphics (SVG)] files, although it can export to many formats that can be included in [[LaTeX]] files, such as EPS and PDF.
 
Another program for creating vector graphics is [http://www.inkscape.org/ Inkscape]. It works with [http://www.w3.org/Graphics/SVG/ Scalable Vector Graphics (SVG)] files, although it can export to many formats that can be included in [[LaTeX]] files, such as EPS and PDF.
 
From version 0.48, there is a combined PDF/EPS/PS+LaTeX output option, like XFig has.
 
From version 0.48, there is a combined PDF/EPS/PS+LaTeX output option, like XFig has.
  
See [[LaTeX/Ipe]] too.
+
See [[LaTeX/Inkscape]] too.
  
 
===Editing EPS graphics===
 
===Editing EPS graphics===

Version actuelle en date du 28 janvier 2015 à 12:40

Strictly speaking, LaTeX cannot manage pictures directly: in order to introduce graphics within documents, LaTeX just creates a box with the same size as the image you want to include and embeds the picture, without any other processing. This means you will have to take care that the images you want to include are in the right format to be included. This is not such a hard task because LaTeX supports the most common picture formats around.


Most mathematical/scientific graphics software allows you to save graphics (figures, diagrams, graphs) in PostScript form; this includes Mathematica, Maple, Matlab, IDL, and xfig (a marvelous X figure-drawing program). Even bitmap images like JPEG and PNG files can be converted to PostScript form with programs like ``xv or ``convert.


See http://www.ukonline.be/programmation/latex/tutoriel/chapitre11/page3.php as well as http://merkel.zoneo.net/Latex/index.php

The graphicx package

As stated before, LaTeX can't manage pictures directly, so we will need some extra help: we have to load the graphicx package in the preamble of our document:

\usepackage{graphicx}

This package accepts as an argument the external driver to be used to manage pictures; however, the latest version of this package takes care of everything by itself, changing the driver according to the compiler you are using, so you don't have to worry about this. Still, just in case you want to understand better how it works, here are the possible options you can pass to the package:

  • dvips (default if compiling with latex), if you are compiling with latex to get a DVI and you want to see your document with a DVI or PS viewer.
  • dvipdfm, if you are compiling with latex to get a DVI that you want to convert to PDF using dvipdfm, to see your document with any PDF viewer.
  • pdftex (default if compiling with pdflatex), if you are compiling with pdftex to get a PDF that you will see with any PDF viewer.

but, again, you don't need to pass any option to the package because the default settings are fine in most of the cases.

In many respects, importing your images into your document using LaTeX is fairly simple... once you have your images in the right format that is! Therefore, I fear for many people the biggest effort will be the process of converting their graphics files. Now we will see which formats we can include and then we will see how to do it.

Document Options

The graphics and graphicx packages recognize the "draft" and "final" options given in the \documentclass[...]{...} command at the start of the file. Using "draft" as the option will suppress the inclusion of the image in the output file and will replace the contents with the name of the image file that would have been seen. Using "final" will result in the image being placed in the output file. The default is "draft".

Supported image formats

As explained before, the image formats you can use depend on the driver that graphicx is using but, since the driver is automatically chosen according to the compiler, then the allowed image formats will depend on the compiler you are using.

Compiling with latex, the only way for including Encapsulated PostScript images

The only format you can include while compiling with latex is Encapsulated PostScript (EPS).

The EPS format was defined by Adobe Systems for making it easy for applications to import postscript-based graphics into documents. Because an EPS file declares the size of the image, it makes it easy for systems like LaTeX to arrange the text and the graphics in the best way. EPS is a vector format—this means that it can have very high quality if it is created properly, with programs that are able to manage vector graphics. It is also possible to store bit-map pictures within EPS, but they will need a lot of disk space.

Many graphics software packages have the ability to save images in the EPS format (extension is normally .eps). Here are some examples of software that can output EPS formats:

  • Printing in an EPS file:
    • Under Windows, PDFCreator is an open source software that can create PDF as well as EPS files. It installs a virtual printer that can be accessed from other software having a "print..." entry in their menu (virtually any program).
  • Creating and converting vector graphics:
    • Commercial vector graphics software, such as Adobe Illustrator, CorelDRAW, and FreeHand are commonly used and can read and write EPS figures. However, these products are limited to Windows and Mac OS platforms.
    • Inkscape can save in vector EPS format, and it can run on multiple platforms. Inkscape cannot open EPS figures directly; however, with the epstopdf utility one can convert EPS into PDF and Inkscape can import PDF. From version 0.48, Inkscape has a special PDF+LaTeX output option (and for EPS/PS too). See Inkscape website.
    • Dia is a cross platform diagramming utility which can export eps.
  • Creating and converting raster-only graphics to EPS:
    • GIMP, has a graphical user interface, and it is multi-platform.
    • For command-line:
    • These three programs operate much the same way, and can convert between most graphics formats. Sam2p however is the most recent of the three and seems to offer both the best quality and to result in the smallest files.
      • imgtops. A lightweight graphics utility.
  • Creating publication-quality vector-based plots and charts:
    • Gnuplot, producing scientific graphics since 1986.
    • R, statistical and scientific figures.
    • Generic Mapping Tools (GMT), maps and a wide range of highly customisable plots.
    • Gnumeric, spreadsheets has SVG, EPS, PDF export
    • matplotlib, plotting library written in python, with PDF and EPS export.

There are some tricks to be able to import formats other than EPS into your DVI document, but they're very complicated. On the other hand, converting any image to EPS is very simple, so it's not worth considering them.

Compiling with pdflatex for inclusion of pdf, as well as jpg and png images

If you are compiling with pdflatex to produce a PDF, you have a wider choice. You can insert:

  • JPG, widely used on Internet, digital cameras, etc. They are the best choice if you want to insert photos
  • PNG, a very common format (even if not as much as JPG); it's a lossless format and it's the best choice for diagrams (if you were not able to generate a vector version) and screenshots
  • PDF, it is widely used for documents but can be used to store images as well. It supports both vector and bit-map images, but it's not recommended for the latter, as JPG or PNG will provide the same result using less disk space.

Vector formats

Vector formats can be used with the help of many softwares like, Inkscape, GeoGebra, IPE etc. You could also create your graphics directly with Inkscape. If you want to make mathematical plots, then Gnuplot can save in any format.

There are instructions on how to save your vector images in a PDF format understood by LaTeX and have LaTeX manage the text styles and sizes in the image automatically.

Raster images

JPG and PNG are supported by any image processing program, so you just have to use the one you prefer. If you want to create high quality vector PDF to embed within your PDF document, you can use Inkscape: it supports many vector formats and so you can use it to convert from one to other.

Note, that EPS files cannot be used with pdflatex, however they can be converted to PDF using the epstopdf utility, included in most LaTeX distributions. This can be called automatically by LaTeX using the epstopdf package. In Windows, multiple files can be converted by placing the following line in a batch file (a text file with a .BAT extension) in the same directory as the images:

for %%f in (*.eps) do epstopdf %%f

which can then be run from the command line. If epstopdf produces whole page with your small graphics somewhere on it, use

$ epstopdf --gsopt=-dEPSCrop foo.eps

or try using ps2pdf utility

$ ps2pdf -dEPSCrop foo.eps

to crop final PDF.

Images can be saved in multiple formats for different purposes. For example, a directory can have "diagram.pdf" for high-resolution printing, while "diagram.png" can be used for previewing on the monitor. You can specify which image file is to be used by pdflatex through the preamble command:

\DeclareGraphicsExtensions{.pdf,.png,.jpg}

which specifies the files to include in the document, if files with the same basename exist, but with different extensions.

Including graphics

Now that we have seen which formats we can include and how we could manage those formats, it's time to learn how to include them in our document. After you have loaded the graphicx package in your preamble, you can include images with \includegraphics, whose syntax is the following:

\includegraphics[attr1=val1, attr2=val2, ..., attrn=valn]{imagename}

As you should hopefully be aware by now, arguments in square brackets are optional, whereas arguments in curly braces are compulsory. The argument in the curly braces is the name of the image. Write it without the extension. This way the LaTeX compiler will look for any supported image format in that directory and will take the best one (EPS if the output is DVI; JPEG, PNG or PDF if the output is PDF). The variety of possible attributes that can be set is fairly large, so only the most common are covered below:

width=xx Specify the preferred width of the imported image to xx. NB. Only specifying either width or height will scale the image whilst maintaining the aspect ratio.
height=xx Specify the preferred height of the imported image to xx.
keepaspectratio This can be set to either true or false. When true, it will scale the image according to both height and width, but will not distort the image, so that neither width nor height are exceeded.
scale=xx Scales the image by the desired scale factor. e.g, 0.5 to reduce by half, or 2 to double.
angle=xx This option can rotate the image by xx degrees (anti-clockwise)
trim=l b r t This option will crop the imported image by l from the left, b from the bottom, r from the right, and t from the top. Where l, b, r and t are lengths.
clip For the trim option to work, you must set clip=true.
page=x If the image file is a pdf file with multiple pages, this parameter allows you to use a different page than the first.

In order to use more than one option at a time, simply separate each with a comma. The order you give the options matters. E.g you should first rotate your graphic (with angle) and then specify its width.

Included graphics will be inserted just there, where you placed the code, and the compiler will handle them as "big boxes". As we will see in the next section, this may lead to a bad output so you'd better place graphics inside floating objects.

Examples

OK, it's time to see graphicx in action. Here are some examples:

\includegraphics{chick}

This simply imports the image, without any other processing. However, it is very large (so I won't display it here!). So, let's scale it down:

\includegraphics[scale=0.5]{chick}
Fichier:Chick1.png

This has now reduced by half. If you wish to be more specific and give actual lengths of the image dimensions, this is how to go about it:

\includegraphics[width=2.5cm]{chick}
Fichier:Chick2.png

One can also specify the scale with respect to the width of a line in the local environment (\linewidth), the width of the text on a page (\textwidth) or the height of the text on a page (\textheight) (pictures not shown):

\includegraphics[width=0.5\linewidth]{chick}
 
\includegraphics[width=0.75\textwidth]{chick}
 
\includegraphics[height=0.75\textheight]{chick}

To rotate (I also scaled the image down):

\includegraphics[scale=0.5, angle=180]{chick}
Fichier:Chick3.png

And finally, an example of how to crop an image should you wish to focus in one particular area of interest:

%trim option's parameter order: left bottom right top
\includegraphics[trim = 10mm 80mm 20mm 5mm, clip, width=3cm]{chick}
Fichier:Chick4.png

Note the presence of clip, as the trim operation will not work without it.

As you may have noticed, the file name of the picture is always without the extensions: LaTeX will take care of getting the right version for us. Consider the following situation: you have added some pictures to your document in JPG and you have successfully compiled it in PDF. Now you want to compile it in DVI, you run latex and you get a lot of errors... because you forgot to provide the EPS versions of the pictures you want to insert. At the beginning of this book, we had stated that the same LaTeX source can be compiled in both DVI and PDF without any change. This is true, as long as you don't use particular packages, and graphicx is one of those. In any case, you can still use both compilers with documents with pictures as well, as long as you always remember to provide the pictures in two formats (EPS and one of JPG, PNG and PDF).

Two graphics side by side

Figure~\ref{f:gradient-3} shows how ... and Figure~\ref{f:gradient-4} ...
  \begin{figure}[htbp]
    \setlength{\captionindent}{0pt}
    \begin{minipage}[t]{0.45\textwidth}
    \centering\includegraphics[width=\textwidth]{gradient-3}
  \caption{Gradient in $X$ deduced from gradient in $P$ and $A(p)x$.}\label{f:gradient-3}
    \end{minipage}\hfil%
    \begin{minipage}[t]{0.45\textwidth}
    \centering\includegraphics[width=\textwidth]{gradient-4}
     \caption{$P$ moved to the minimum where $\nabla f(p)=0$ and $\nabla f(x)=A(p)x$. }\label{f:gradient-4}
    \end{minipage}
  \end{figure}

Borders

It is possible to have LaTeX create a border around your image by using fbox:

\setlength\fboxsep{0pt}
\setlength\fboxrule{0.5pt}
\fbox{\includegraphics{chick}}

You can control the border padding with the \setlength\fboxsep{0pt} command, in this case I set it to 0pt to avoid any padding, so the border will be placed tightly around the image. You can control the thickness of the border by adjusting the \setlength\fboxrule{0.5pt} command.

Graphics storage

There is a way to tell LaTeX where to look for images: for example, it can be useful if you store images centrally for use in many different documents. The answer is in the command \graphicspath which you supply with an argument giving the name of an additional directory path you want searched when a file uses the \includegraphics command, here are some examples (trailing / is required):

\graphicspath{{c:\mypict~1\camera}}
\graphicspath{{c:/mypict~1/camera/}} *
\graphicspath{{/var/lib/images/}}
\graphicspath{{./images/}}
\graphicspath{{images_folder/}{other_folder/}{third_folder/}}
\graphicspath{{images//}}

* goes well in win XP

please see http://www.ctan.org/tex-archive/macros/latex/required/graphics/grfguide.pdf. The last command searches the files recursively because of the double slash "//". In the example shown you would have a directory named "images" in the same directory as your man tex file, i.e. this is RELATIVE addressing. In this manner you can create a "human sensible" directory tree below this that could e.g. make it easier for you to sort/find/edit large numbers of image files. There is a caveat that using this recursive search may hog memory, please keep this in mind.

As you may have noticed, in the first example I've used the "safe" (MS-DOS) form of the Windows MyPictures folder because it's a bad idea to use directory names containing spaces. Using absolute paths, \graphicspath does make your file less portable, while using relative paths (like the last example), you shouldn't have any problem with portability, but remember not to use spaces in file-names. Alternatively, if you are using PDFLaTeX, you can use the package grffile which will then allow you to use spaces in file names.

Images as Figures

There are many scenarios where you might want to accompany an image with a caption and possibly a cross-reference. This is done using the figure environment. The following code sample shows the bare minimum required to use an image as a figure.

png, eps

\begin{figure}[htb]
\includegraphics{image.png}
\end{figure}

gif to png

Because of the history of the license concerning the GIF format, a png format will be preferred. And the transformation to png can be done at least with giftopnm |pnmtopng or convert. If you are modifying the gif files frequently, you can have some benefit to let TeX doing the transformation at compilation time. Your TeX engine must be used with -shell-escape option in order to allow a sub shell process to be created temporarly.

\usepackage{graphicx}  % If you want to insert a graphic (pdf, eps)
\usepackage{epstopdf}
 
\epstopdfDeclareGraphicsRule{.gif}{png}{.png}{ convert gif:#1 png:\OutputFile} % For gif -shell-escape must be added
\AppendGraphicsExtensions{.gif}
 
\begin{figure}[htb]
\includegraphics{ined.gif}
\end{figure}

If you can't get the image, it might be because the -shell-escape option is not set in your editor/compiler or because the convert programme doesn't exist or is not in your path (WINDOWS).


The above code extract is relatively trivial, and doesn't offer much functionality. The following code sample shows an extended use of the figure environment which is almost universally useful, offering a caption and label, centering the image and scaling it to 80% of the width of the text.

\begin{figure}[htb]
\begin{center}
\leavevmode
\includegraphics[width=0.8\textwidth]{image.png}
\end{center}
\caption{Awesome Image}
\label{fig:awesome_image}
\end{figure}

The figure environment is not exclusively used for images. More information on the figure environment and how to use it can be found in Floats, Figures and Captions.


Text Wrapping around Images

Text can also be wrapped around images. (This is especially useful if you include tall pictures.)

%import section
\usepackage{wrapfig}
 
% content section
\begin{wrapfigure}{r}{8cm} % "l" or "r" for the side on the page. And the width parameter for the width of the image space.
\centering
\includegraphics[height=80mm]{Abb/bluesniper.jpg}
\caption{Selbstgebaute „Bluesniper“ um Bluetooth-Geräte aus über 1 km Entfernung anzugreifen. (Stand: 2004)}
\label{bluesniper}
\end{wrapfigure}

Including full PDF pages

There is a great package for including full pages of PDF files: pdfpages and its documentation. It is capable of inserting full pages as is and more pages per one page in any layout (e.g. 2x3). Further pagination is ok. You can even keep your current headings (see example below). Be careful that it may not work with scanned PDF documents.

% To insert a single page, even a big one like a poster.
\includepdf[pages=1]{myposter.pdf}
\includepdf[pages=1, offset=+1in -1in]{myposter.pdf} % If not centered
 
% You want to insert a scientific article in your thesis keeping 
%  the current headings defined earlier in your text. Here the headings are
%  on top of pages and thus the scientific article is shifted downwards in
%  order to not overwrite the headings of the review:
\pagestyle{headings}
...
\includepdf[pages=1-, offset=+0in -1in, pagecommand={%
   \thispagestyle{headings}}{articles/Brouard-Pontone-Scherpereel-mathpop2007.pdf}
 
% To insert pages 3 to 5 of report in 3 different pages
\includepdf[pages=3-5]{myreport.pdf}
 
% Specific pages and ranges of pages can be selected on a single page
% and presented 1, 4, 5 on top half and  6, 8 and 10 on bottom half
\includepdf[nup=3x2, pages={1,4-6,8,10}]{myreport.pdf}
 
% If you want to sort by column, you will get 1, 6 on top, 
%   4, 8 in the middle and 5, 10 in the bottom 
\includepdf[nup=3x2, pages={1,4-6,8,10}, column]{myreport.pdf}
 
% This will output two pages, forcing landscape for pages 1 (on top) 
% and 2 on bottom for the first page
% and  3 (top) 4 (bottom) for the second page. 
\includepdf[nup=1x2, pages=1-4]{myreport.pdf}
 
% This will output a single page with an empty subpage and page 5 on top
%   and pages 6 and 7 on bottom
\includepdf[nup=2x2, pages={{},5-7}]{myreport.pdf}
 
% You can frame your subpages as well reduce and give space between them
\includepdf[nup=2x2, pages=3-6, scale=.8, delta=8mm 11mm, frame]{dummy.pdf}
 
% With pdfmerge you can merge some specific pages from different documents
% Here are three document where the title page (of most interest) of the
% first document is on page 2, on first page for doc2 and third for doc3.
% 
\includepdfmerge[nup=1x3, landscape, linktodoc] {doc1.pdf, 2, doc2.pdf, 1, doc3.pdf, 3}
\includepdfmerge[nup=1x3, landscape,linktodoc]{articles/Enquête_Cfar-Sfar-Ined_AFAR2002.pdf, 1,
  articles/Brouard-Pontone-Scherpereel-mathpop2007.pdf, 2,
  articles/Démographie_Pontone-Brouard2010_ANNFAR4425.pdf, 3}

See more examples at [1].

Graphics made with TeX macros or packages

LaTeX pictures

The LaTeX picture macros are very poor and is no more used.

TikZ/PGF

More thorough introduction to TikZ is available at the Creating Graphics chapter

You can draw graphics directly with TeX commands using the tikz package: http://ftp.dante.de/tex-archive/help/Catalogue/entries/pgf.html It comes with very good documentation with many examples.

% This needs \usepackage{tikz} in the preamble
\begin{figure}
  \centering
  \begin{tikzpicture}
    \draw[thick,rounded corners=8pt] 
(0,0) -- (0,2) -- (1,3.25) -- (2,2) -- (2,0) -- (0,2) -- (2,2) -- (0,0) -- (2,0);
  \end{tikzpicture}
  \caption{This is the caption of my figure}
  \label{fig:test}
\end{figure}

An extensive collection of examples can be found here: http://www.texample.net/tikz/

Other packages building on top of TikZ (e.g. for drawing electrical circuits) can be found here: http://ftp.dante.de/tex-archive/help/Catalogue/bytopic.html#pgftikzsection

xy package

Creating Vector Graphics

"Remember and type" softwares

Gnuplot

Gnuplot is a very powerful drawing language in 2D as in 3D. It is used internally by many other software like Wikipedia:en:Octave_software. At INED, imach] as well as the book entitled African Population and AIDS has some dynamic pages using gnuplot at run time.

Gnuplot works on Windows, MaC and Linux. Gnuplot can output various graphic formats, vector formats including svg, eps, pdf or raster formats like png or gif or even animated gifs (see Wikipedia:en:Foucault_Pendulum for example).

Latest version of Gnuplot is able to output Tikz TeX sources.

See LaTeX/Gnuplot too.

WYSIWYG softwares

Xfig

Vector graphics can be created using the vector painting program Xfig (see Installation), and exported for LaTeX. In Xfig, once your graphic is saved as a file test.fig, you need to export it using the File > Export drop down menu from the main Xfig window and then select the "Combined PS/Latex (both parts)" in the language drop down list. If you don't change any other settings, two files will be created in the same directory as the test.fig file, such as: test.pstex_t and test.pstex. The figure can then be placed in a LaTeX document:

\begin{figure}
  \centering
  \input{./xfig/test.pstex_t}
  \caption{This is the caption of my figure}
  \label{fig:test}
\end{figure}

see LaTeX/Xfig too.

Ipe 7

The Ipe extensible drawing editor is a free vector graphics editor for creating figures in PDF or EPS format. Unlike Xfig, ipe represents LaTeX fonts in their correct size on the screen which makes it easier to place text labels at the right spot. ipe also has various snapping modes (for example, snapping to points, lines, or intersections) that can be used to geometrically construct.

See LaTeX/Ipe too.

Inkscape

Another program for creating vector graphics is Inkscape. It works with Scalable Vector Graphics (SVG) files, although it can export to many formats that can be included in LaTeX files, such as EPS and PDF. From version 0.48, there is a combined PDF/EPS/PS+LaTeX output option, like XFig has.

See LaTeX/Inkscape too.

Editing EPS graphics

As described above, graphics content can be imported into LaTeX from outside programs as EPS files. But sometimes you want to edit or retouch these graphics files. An EPS file can be edited with any text editor since it is formatted as ASCII. In a text editor, you can achieve simple operations like replacing strings or moving items slightly, but anything further becomes cumbersome.

To properly edit an EPS file, you can convert it to an editable format using pstoedit. For instance, to get an Xfig-editable file, do:

$ pstoedit -f fig input.eps output.fig
And to get an SVG file for Inkscape you can do:
$ pstoedit -f plot-svg input.eps output.svg

Sometimes pstoedit fails to create the target format (for example when the EPS file contains clipping information). A more robust way to edit EPS files is achieved by converting it first to PDF and then importing the resulting PDF in Inkscape. Inkscape uses the Cairo library that achieves a high-quality transformation of the original EPS figure:

$ epstopdf input.eps
$ inkscape input.pdf

When all of the above fails, one can simplify the EPS file before attempting other conversions, by using the eps2eps tool (also see next section):

$ eps2eps input.eps input-e2.eps

This will convert all the fonts to pre-drawn images, which is sometimes desirable when submitting manuscripts for publication. However, on the downside, the fonts are NOT converted to lines, but instead to bitmaps, which reduces the quality of the fonts.

Converting a color EPS to grayscale

Sometimes color EPS figures need to be converted to black-and-white or grayscale to meet publication requirements. This can be achieved with the eps2eps of the Ghostscript package and pscol programs:

$ eps2eps input.eps input-e2.eps
$ pscol -0gray input-e2.eps input-gray.eps


Previous: Tables Index Next: Floats, Figures and Captions