107

There exist several types of graphics files, such as jpeg eps pdf png tif jif. Some work with LaTeX + Dvips and some work with PDFLaTeX. How to know which type is for which processor?

Just to confuse matters, metapost produces .eps files, but I can use these with PDFLaTeX as long as I put in a four line incantation.

Can anyone explain this?

1

6 Answers 6

103

Formats that work with LaTeX (dvi mode, using dvips):

  • eps

Formats that work with LaTeX (dvi mode, using dvipdfm(x)):

  • pdf
  • png
  • jpeg
  • eps

Formats that work with pdfLaTeX (pdf mode):

  • eps(*)
  • pdf
  • png
  • jpeg
  • jbig2

LuaTeX can also read

The reason for this way of working is that in dvi mode TeX simply leaves a space for the graphics. eps are included in the output by dvips.

pdfLaTeX includes graphics directly in the pdf, using the features available in that format. The pdf format can include other pdfs (no surprise), png, jpeg, jpeg2000 and jbig2 graphics.

(*) Although pdfTeX can't incorporate eps files directly, the set up in a modern TeX system will automatically convert them to pdf, and thus they are supported in practice.

XeTeX always uses the xdvipdfmx driver so has the same outcomes as using dvipdfmx with a classical DVI-based route.


Note that the LaTeX graphics package and associated helper code will search automatically for appropriate file extensions. As a result the extension should be omitted when including a graphic, for example

\includegraphics{foo}

for a file foo.eps. With a recent TeX system this will allow e.g. pdfLaTeX to auto-convert and .eps file to PDF format and find it 'auto-magically'.

7
  • 2
    Nice answer, but there's a slight typo: you've put a spurious 'd' in "epstopdf". I thought I'd mention it since it's the name of an important package in your answer! Aug 5, 2010 at 13:15
  • 1
    It is worth mentioning here that screenshots should be saved in png (not jpeg) format if they will be imported from within the LaTeX input file compiled using pdfLaTeX. May 30, 2011 at 14:37
  • 2
    It is often misunderstood that latex (dvi mode) support some kind of graphics. That's not true. The output driver decides the image formats: dvips supports PostScript, while dvipdfm(x) supports PDF, PNG and JPEG (with help of a .bb or .xbb file), and EPS (with help of GhostScript).
    – Leo Liu
    Aug 3, 2011 at 18:30
  • @leo liu: it depends what you mean by "support". latex processes an eps file (the dimensions of the figure are present in the file as text), and instructs its back-end (dvips) to incorporate the eps at the correct scale, rotation and position. this isn't "support"? hmmm... Jan 8, 2013 at 23:54
  • 1
    @WilliamEntriken ultimately, each driver is documented separately (so dvips, pdftex, ...)
    – Joseph Wright
    Apr 9, 2020 at 10:27
36

Nowadays everything is actually very simple. In essence, you only need to worry about three different file formats:

  • PDF for vector graphics

  • JPEG for photos

  • PNG for other kinds of raster graphics.

pdflatex supports all of these, and virtually any graphics file can be converted to one of these formats.

And pdflatex not only supports these, but it does it extremely well. For example, a JPEG file is included in the resulting PDF file as is. It is not uncompressed and re-compressed; nothing is lost, and you know that including a 100 KB JPEG file in your document will enlarge the resulting PDF file by exactly 100 KB.

(With tools such as latex + dvips + ps2pdf, all bets are off; typically you will get huge PDF files or low-quality re-compressed JPEG files or both. But fortunately nowadays you can use almost always pdflatex instead of latex.)

2
  • 1
    If you want to print, png is a bad choice since it does not support CMYK. Also it is recommended that jpegs are wrapped in pdf to make it easier for latex.
    – Patrick
    Apr 10, 2015 at 22:13
  • This answer didn't age well. May 13, 2022 at 20:16
19

This is not an answer to the question as asked, but I think it may be of use to those trying to work out which format to use and how to select it.

If you are using the graphicx package and your intention is to be flexible (in that you want to be able to produce different output formats from the same source file) then there is a simple way to avoid having to know which to use: if you leave off the file extension in the \includegraphics command then it selects the best one available according to the mode. Thus if you've got the .eps and the .pdf versions of the graphic available, then latex + dvips will choose the .eps whilst pdflatex will choose the .pdf. The command is:

\includegraphics{MonaLisa}

Which one it chooses is even customisable: there's a list of extensions for each output type and it goes through the list until it finds a file that exists. Redefining that list changes the priorities. The list is stored in a comma-separated macro called \Gin@extensions so redefining that will change the order. Here's a simple way to ensure that .pdf is selected first when producing PDF output (since, oddly, the list for PDF output starts with .png):

\documentclass{article}

\usepackage{ifpdf}

\ifpdf
\PassOptionsToPackage{pdftex}{graphicx}
\fi

\usepackage{graphicx}
\ifpdf
\makeatletter
\let\orig@Gin@extensions\Gin@extensions 
\def\Gin@extensions{.pdf,\orig@Gin@extensions} %prepend .pdf before .png
\makeatother
\fi
\begin{document}

\includegraphics{pullback}

\end{document}

For actually converting between formats, there are of course many ways to do that. TeXLive comes with a program epstopdf which converts an .eps file to a .pdf and there is even a LaTeX package epstopdf which will attempt to do the conversion for you (assuming that PDFTeX is allowed to do shell escapes) if it detects an .eps image!

For commandline conversion (ie on a U*nx system, including MacOSX), ghostscript can easily convert back and forth and comes with two scripts pdf2ps and ps2pdf that will do the conversion and the netpbm tools can convert between PDF, PS and lots of other graphics formats (of course there are many other such tools).

Thus by ensuring that you have each type of the image in the directory, you can be ensure that latex will pick up the right one for the right output.

11
  • That doesn't really answer the question: this feature of graphicx only helps if you already have the picture in a format that your TeX engine will understand; if you're running pdfTeX and you only have an EPS file, it won't magically convert it to something pdfTeX can use. Aug 5, 2010 at 12:08
  • 2
    @Arthur Reutenauer: nonetheless, I think that it is a useful feature of graphicx for people to know about and that someone interested in this question might well find it useful. However, I have added a little on converting back and forth and, in particular, found a package that will do the conversion that you want! Aug 5, 2010 at 12:37
  • Sure, it's useful. But it's definitely not a “way to avoid having to know which [format] to use”, that's just wrong. On the contrary, you need to know which format to use in which case, and you need to do the conversion beforehand (except in the notable case of epstopdf, as mentioned by Joseph). Aug 5, 2010 at 13:14
  • 2
    @Arthur Reutenauer: I interpret it a little differently, I think. The question "I always use pdflatex so which do I use?" has a simple - and boring - answer. The question "I find myself using both pdflatex and latex and get confused as to what to use. Can anyone help me?" is a little more complicated. What I do, is ensure that I have both formats available so that whichever I use, it Just Works^{TM}. That is what I was trying to explain, and that is what I think is useful to know. Aug 5, 2010 at 13:31
  • 1
    I know what you do. But you don't explain it in your main answer. As is, your first sentence is really misleading and unhelpful. Aug 5, 2010 at 13:51
11

The graphics formats that you can use depend on the graphics driver not on (pdf)LaTeX:

As others have said pdfLaTeX in pdf-mode can use pdf, png, jpg, and mps.

LaTeX in dvi-mode and dvips as driver can use eps (and mps).

LaTeX in dvi-mode and dvipdfmx as driver can use eps (and mps), pdf, png, and jpg if you provide information about the bounding box and inform graphicx that you use dvipdfmx:

% test.tex
\documentclass{article}
\usepackage[dvipdfm]{graphicx}

\begin{document}

\includegraphics{demo.png}

\end{document}

First generate demo.bb with the bounding box information:

ebb demo.png

then compile to dvi with latex:

latex test.tex 

and finally generate the pdf:

dvipdfmx test.dvi
9

This question has received excellent answers to the general question relating to what graphics formats the two Tex engines, but the Metapost part has been neglected (Just to confuse matters, metapost produces .eps but I can use these with pdflatex as long as I put in a four line incantation), so I shall document what you can get from Metapost.

Hobby's original implementation, mpost, only supported Postscript as its scaleable graphics format Metapost, and came with some other utilities to allow typesetting of labels (makempx, mpto). there are several other options.

  1. Most usefully for the original problem, there is mptopdf, which is a modification of Hobby's mpost to create PDFs natively that was implemented, I think, by Hans Hagen together with the Metafun extensions, and is the parent of the mplib codebase used to set Metapost in Luatex. See Pragma's Metapost and Metafun pages.
  2. John Hobby extended the Metapost programs to support their use with the Troff typesetting system in a manner very similar to its use with Tex. This opens up possibilities for setting Metapost, but ones that are inconvenient to use with Tex and which I think are not actively maintained.
  3. Taco Hoekwater has extended mplib/mptopdf to natively support SVG, which is an image format that can be embedded in PDFs and is a more convenient graphical representation than either PS or PDF in some circumstances.
4

unfortunately, the psfrag command which can be of little help when dealing with graphics is not handled by pdflatex. For font consistency along your document, it may be a non-negligible shortcoming (you can still use tikz instead). Also, the next-generation vector format svg should become the most used one soon. I cannot tell if either latex or pdflatex accept it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .