Jump to content

LaTeX/Importing Graphics

From Wikibooks, open books for an open world

Importing external graphics

[edit | edit source]

LaTeX

Getting Started
  1. Introduction
  2. Installation
  3. Installing Extra Packages
  4. Basics
  5. How to get help

Common Elements

  1. Document Structure
  2. Text Formatting
  3. Paragraph Formatting
  4. Colors
  5. Fonts
  6. List Structures
  7. Special Characters
  8. Internationalization
  9. Rotations
  10. Tables
  11. Title creation
  12. Page Layout
  13. Customizing Page Headers and Footers‎
  14. Importing Graphics
  15. Floats, Figures and Captions
  16. Footnotes and Margin Notes
  17. Hyperlinks
  18. Labels and Cross-referencing
  19. Initials

Mechanics

  1. Errors and Warnings
  2. Lengths
  3. Counters
  4. Boxes
  5. Rules and Struts

Technical Text

  1. Mathematics
  2. Advanced Mathematics
  3. Theorems
  4. Chemical Graphics
  5. Algorithms
  6. Source Code Listings
  7. Linguistics

Special Pages

  1. Indexing
  2. Glossary
  3. Bibliography Management
  4. More Bibliographies

Special Documents

  1. Scientific Reports (Bachelor Report, Master Thesis, Dissertation)
  2. Letters
  3. Presentations
  4. Teacher's Corner
  5. Curriculum Vitae
  6. Academic Journals (MLA, APA, etc.)

Creating Graphics

  1. Introducing Procedural Graphics
  2. MetaPost
  3. Picture
  4. PGF/TikZ
  5. PSTricks
  6. Xy-pic
  7. Creating 3D graphics

Programming

  1. Macros
  2. Plain TeX
  3. Creating Packages
  4. Creating Package Documentation
  5. Themes

Miscellaneous

  1. Modular Documents
  2. Collaborative Writing of LaTeX Documents
  3. Export To Other Formats

Help and Recommendations

  1. FAQ
  2. Tips and Tricks

Appendices

  1. Authors
  2. Links
  3. Package Reference
  4. Sample LaTeX documents
  5. Index
  6. Command Glossary

edit this boxedit the TOC

You can import external graphics using package graphicx. The most important command is \includegraphics. LaTeX itself treats the image like normal text, i.e. as a box of certain height and width.

\documentclass{article}
\usepackage{graphicx}
\begin{document}
The following image does not show any wombats
\includegraphics[height=\baselineskip]{example-image}.

\includegraphics[height=3cm]{example-image-a}\includegraphics[width=5cm]{example-image-b}

\includegraphics[height=3cm]{example-image-a} \includegraphics[width=5cm]{example-image-b}
\end{document}

The package documentation list the options width and height, as well as others.

Using pdflatex several graphics formats are supported: pdf, png and jpg. Modern installations of LaTeX can use eps files as well, but indirectly.

LaTeX in dvi-mode supports only eps-files.


width=200px


You can align the images in a matrix. You just have to think of a proper width for the images.

\documentclass{article}
\usepackage{graphicx}
\usepackage{blindtext}
\usepackage{showframe}
\begin{document}
\begin{center}
\includegraphics[width=.3\linewidth]{example-image}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-b}
\\[\baselineskip]% adds vertical line spacing
\includegraphics[width=.3\linewidth]{example-image}\quad\includegraphics[width=.3\linewidth]{example-image-a}\quad\includegraphics[width=.3\linewidth]{example-image-b}
\end{center}
\end{document}

Three images in one line, each image has a width of just 30 % of the available line width, centered with respect to the text below.

Different images can be aligned in a matrix shape easily.


If you want to add a caption and let LaTeX keep track of the numbering, have a look at the floats section.

Converting graphics

[edit | edit source]
Note

You should also take a look at Export To Other Formats for other possibilities.

epstopdf

You can convert EPS to PDF with the epstopdf utility, included in package of the same name. This tool is actually called by pdflatex to convert EPS files to PDF in the background when the graphicx package is loaded. This process is completely invisible to the user.

You can batch convert files using the command-line. In Bourne Shell (Unix) this can be done by:

$ for i in *.eps; do epstopdf "$i"; done

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 a whole page with your small graphics somewhere on it, use

$ epstopdf --gsopt=-dEPSCrop foo.eps

or try using the ps2pdf utility which should be installed with Ghostscript (required for any TeX distribution).

$ ps2pdf -dEPSCrop foo.eps

to crop the final PDF.

eps2eps

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.

imgtops

imgtops is a lightweight graphics utility for conversions between raster graphics (JPG, PNG, ...) and EPS/PS files.

Inkscape

Inkscape can also convert files from and to several formats, either from the GUI or from the command-line. For instance, to obtain a PDF from a SVG image you can do:

$ inkscape -z -D --file=input.svg --export-pdf=output.pdf

It is possible to run this from within a LaTeX file, the Template:LaTeX/package package (when running (pdf)latex with the --shell-escape option) can do this using Inkscape's pdf+tex export option, or a simple macro can be used. See How to include SVG diagrams in LaTeX? -- Stackexchange See Export To Other Formats for more details.

pstoedit

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 (editable with any vector graphics tool like 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).

PDFCreator

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).

Raster graphics converters

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.

PNG alpha channel

[edit | edit source]

Acrobat Reader sometimes has problems with displaying colors correctly if you include graphics in PNG format with alpha channel. You can solve this problem by dropping the alpha channel. On Linux it can be achieved with convert from the ImageMagick program:

convert -alpha off input.png output.png

Converting a color EPS to grayscale

[edit | edit source]

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 [1] programs:

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

Third-party graphics tools

[edit | edit source]

We will not tackle the topic of procedural graphics created from within LaTeX code here (TikZ, PSTricks, MetaPost and friends). See Introducing Procedural Graphics for that.

You should prefer vector graphics over raster graphics for their quality. Raster graphics should only be used in case of photos. Diagrams of any sort should be vectors.

As we have seen before, LaTeX handles

  • EPS and PDF for vector graphics;
  • PNG and JPG for raster graphics.

If some tools cannot save in those formats, you may want to convert them before importing them.

Vector graphics

[edit | edit source]
Dia

Dia is a cross platform diagramming utility which can export eps images, or generate tex drawn using the tikz package.

Inkscape

Another program for creating vector graphics is Inkscape. It can run natively under Windows, Linux or Mac OS X (with X11). 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, similar to that offered by Xfig. 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.[1]. Today there is the svg package[2] which provides an \includesvg command to convert and include svg-graphics directly in your LaTeX document using Inkscape. You may have a look at this extended example too.

An extremely useful plug-in is textext, which can import LaTeX objects. This can be used for inserting mathematical notation or LaTeX fonts into graphics (which may then be imported into LaTeX documents).

Ipe

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 for geometric constructions.

lpic

Yet another solution is provided by the lpic packages [2], which allows TeX annotations to imported graphics. See Labels in the figures.

LibreOffice

It is also possible to export vector graphics to EPS format using LibreOffice Draw, which is an open source office suite available for Windows, Linux and Mac.

TpX

Vector editor TpX separates geometric objects from text objects. Geometric objects are saved into .PDF file, the rest is saved in .TpX file to be processed by LaTeX. User just create the graphics in TpX editor and calls the .TpX file from latex file by command \input{...TpX}.

Xfig

Xfig is a basic program that can produce vector graphics, which can be exported to LaTeX. It can be installed on Unix platforms.

On Microsoft Windows systems, Xfig can only be installed using Cygwin-X; however, this will require a fast internet connection and about 2 gigabytes of space on your computer. With Cygwin, to run Xfig, you need to first start the "Start X - Server", then launch "xterm" to bring up a terminal. In this terminal type "xfig" (without the quotation marks) and press return.

Alternatively, WinFIG. WinFIG is an attempt to achieve the functionality of xfig on Windows computers.

There are many ways to use xfig to create graphics for LaTeX documents. One method is to export the drawing as a LaTeX document. This method, however, suffers from various drawbacks: lines can be drawn only at angles that are multiples of 30 and 45 degrees, lines with arrows can only be drawn at angles that are multiples of 45 degrees, several curves are not supported, etc.

Exporting a file as PDF/LaTeX or PS/LaTeX, on the other hand, offers a good deal more flexibility in drawing. Here's how it's done:

  1. Create the drawing in xfig. Wherever you need LaTeX text, such as a mathematical formula, enter a LaTeX string in a textbox.
  2. Use the Edit tool to open the properties of each of those textboxes, and change the option on the "Special Flag" field to Special. This tells LaTeX to interpret these textboxes when it opens the figure.
  3. Go to File -> Export and export the file as PDF/LaTeX (both parts) or PS/LaTeX (both parts), depending on whether you are using pdflatex or pslatex to compile your file.
  4. In your LaTeX document, where the picture should be, use the following, where "test" is replaced by the name of the image:
    \begin{figure}
     \centering
     \input{test.pdf_t}
     \caption{Your figure}
     \label{figure:example}
    \end{figure}
    

    Observe that this is just like including a picture, except that rather than using \includegraphics, we use \input. If the export was into PS/LaTeX, the file extension to include would be .pstex_t instead of .pdf_t.

  5. Make sure to include packages graphicx and color in the file, with the \usepackage command right below the \documentclass command, like this:
    \usepackage{graphicx}
    \usepackage{color}
    

And you're done!

For more details on using xfig with LaTeX, this chapter of the xfig User Manual may prove helpful.

Other tools

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 X platforms.

Raster graphics

[edit | edit source]
Adobe Photoshop

Photoshop can save to EPS.

GIMP

GIMP, has a graphical user interface, and it is multi-platform. It can save to EPS and PDF.

Plots and Charts

[edit | edit source]
Generic Mapping Tools (GMT)

Generic Mapping Tools (GMT), maps and a wide range of highly customisable plots.

Gnumeric

Gnumeric, spreadsheets has SVG, EPS, PDF export

Gnuplot

Gnuplot, producing scientific graphics since 1986. If you want to make mathematical plots, then Gnuplot can save in any format. You can get best results when used along PGF/TikZ.

matplotlib

matplotlib, plotting library written in python, with PDF and EPS export. On the other hand there is a PGF export also. 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.

R

R, statistical and scientific figures.

Editing EPS graphics

[edit | edit source]

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, changing the bounding box, or moving items slightly, but anything further becomes cumbersome. Vector graphics editors, like Inkscape, may also be able to import EPS files for subsequent editing. This approach also for easier editing. However, the importing process may occasionally modify the original EPS image.

Notes and References

[edit | edit source]
  1. Johan B. C. Engelen. "How to include an SVG image in LATEX" (PDF). mirrorcatalogs.com.
  2. Philip Ilten. "The svg package on CTAN". ctan.org.


Previous: Page Layout Index Next: Floats, Figures and Captions