I am using pdflatex on Linux and attempting to convert GIF to PNG. I can run scripts to find out the image attributes and even and convert the files supplied from a folder but I can not write back to that folder (or I'd convert them by hand). I have hundreds of gif files.

I have found great instructions with regard to converting GIF to EPS. Those weren't working for me, so I thought I'd try PNG files because I normally use PNG files. Thanks in advance for your time for reading and any comments.

Before \begin{document}, I have defined:

\DeclareGraphicsExtensions{.gif, .ps, .eps, .png}
\DeclareGraphicsRule{.gif}{png}{}{`convert #1 'png:-'}

Inside my document, I have


When I run pdflatex on my document, I get an error:

! LaTeX Error: Cannot determine size
of graphic in `convert
figures/filename.gif 'png:-' (no size

So, I added a bounding box manually to the includegraphics command:


I got the dimensions using imagemagick's identify command.

I am able to convert the file, write it to a new file, and view it using imagemagick:

$ convert figures/filename.gif gif:figures/filename.png

I believe that the "convert" command I specified in the graphics rule is not actually running. What am I doing wrong?

Here are some details about my build:

$ pdflatex test.tex
This is pdfTeX, Version 3.14159-14h-released-20010417 (Web2C
LaTeX2e <2001/06/01>
Babel <v3.7h> and hyphenation patterns for american, french, german, ngerman, i
talian, nohyphenation, loaded.
Document Class: article 2001/04/21 v1.4e Standard LaTeX document class
Writing index file test.idx

LaTeX Warning: Unused global option(s):

No file test.aux.
loading : Context Support Macros / Missing
loading : Context Support Macros / PDF
) (/usr/share/texmf/tex/latex/psnfss/ot1ptmcm.fd)
<use `convert figures/remconf_edit.gif 'png:-'> [1{/usr/share/texmf/dvips/confi
g/pdftex.map}] (/usr/share/texmf/tex/latex/psnfss/omsphv.fd) [2] [3] [4]
(./test.aux) )</usr/share/texmf/fonts/type1/bluesky/cm/cmsy10.pfb>{/usr/share/t
  • Can't you dump converted images into another folder? Do you absolutely have to grab them from the read-only folder where they are now?
    – ajeh
    Feb 5, 2014 at 18:51

5 Answers 5


You need to enable shell escapes. With MiKTeX you do -enable-write18 and with TeXlive -shell-escape.

You can set up different conversion rules with the epstopdf package

  {.gif}{png}{.png}{convert gif:\SourceFile.\SourceExt png:\OutputFile}




Using epstopdf you can control if the conversion should be done every time you compile or only if the target file is missing.

  • I think the correct name of the option under TeX Live is -shell-escape.
    – mpg
    Nov 6, 2010 at 0:16
  • Yes. I have corrected the answer. Nov 6, 2010 at 0:21
  • Thank you for this great example of how to use epstopdf to accomplish this image conversion.
    – Amy
    Nov 8, 2010 at 14:54
  • --shell-escape actually seems to work in both TeX Live and MiKTeX now -- most likely also with just one dash. See this blog post of Joseph Wright's that Matthew Leingang linked to in his answer.
    – SamB
    Dec 31, 2010 at 18:28

\DeclareGraphicsRule requires the write18 stream to be enabled in order to talk to the operating system. Ordinarily this is disabled because it can be dangerous. Try the same source file with pdflatex --shell-escape test.tex instead. See Joseph Wright's blog post about enabling write18.

On my machine if \includegraphics{dir/foo.gif} is encountered and DeclareGraphicsRule{.gif}{png}... is declared, LaTeX (1) runs the command; then (2) looks for a file dir/foo_gif-converted-to.png So the graphics rule has to do that. You could use

\DeclareGraphicsRule{.gif}{png}{.png}{`convert #1 `dirname #1`/`basename #1 .gif`-gif-converted-to.png}

But that will write to the same directory the file is originally in. You said you are importing files from a different directory that you do not have write access to. So instead do

\DeclareGraphicsRule{.gif}{png}{.png}{`convert #1 `basename #1 .gif`-gif-converted-to.png}

So the file will be written into the document's directory. But now it won't be in dir/ anymore! So make sure that dir/ is in the TEXINPUTS path (or \graphicspath) and just input it as foo.gif. LaTeX will (0) find foo.gif in dir/ (1) convert to ./foo_gif-converted-to.png, (2) find foo_gif-converted-to.png in the current directory.

Your graphics rule and question title indicate you want to do with using the output stream; that is, not create the converted files. I couldn't get that to work. The side effect is that you have a lot of ugly-titled converted files in your document's directory, and maybe you should have just done that in the first place with a script rather than through LaTeX.

  • Thank you for your reply. The shell escape produced some additional log output, but I do not see any graphics in the output file.
    – Amy
    Nov 5, 2010 at 20:54
  • Do you have a working example of \DeclareGraphicsRule. I tried converting to pdf, eps, and png. When I use eps, I see some output to the sreen. I see an outline the size of the image and also the text (which appears in the graphics rule) `convert figures/figure.gif 'eps:-' .
    – Amy
    Nov 5, 2010 at 21:00
  • @Amy: see edits Nov 6, 2010 at 21:09
  • Thank you for your insight into this problem. I especially appreciate you trying that the graphics rule. I can definitely solve this issue outside latex, but I really wanted to exhaust all the terrific features within latex first. Thanks a lot for your time.
    – Amy
    Nov 8, 2010 at 14:52

This is not a "pure" LaTeX answer but....

Do the convert magic in a script you run before you run pdflatex, that can be called from your makefile (if you use it)


cd figures

for f in *.gif ;
    convert "$f" "${f/%gif/png}"

Now you have converted all the gif:s in dir figures into png:s.

Then you just include them as normal png:s.

  • Any answer that uses purely latex is going to be wrong. Use the right tool for the job--in this case a shell script.
    – nimish
    Nov 7, 2010 at 7:19
  • Thank you very much for taking the time to reply to my post.
    – Amy
    Nov 8, 2010 at 14:53

This works with latex:

\DeclareGraphicsRule{.JPG}{eps}{.JPG}{`convert #1 eps:-}

in this way:

  • latex --shell-escape ... founds JPG file and puts an instruction into dvi file how to process JPG and what to include in page
  • dvips -R0 ... actually launches external command and includes output into a page,
  • ps file can be converted into pdf using ps2pdf

So, it does work on the fly and does not create "intermediate" files. This is how it should be --- all is well. This is how i managed to ask latex to downsample eps graphics before including them --- for downloadable pdf document:

\DeclareGraphicsRule{.eps}{eps}{.eps}{`convert #1 -resample 72x72 eps:-}

Surely, i would like it to work with pdflatex, but for some reason i could not get it working. Sorry, i don't know what's wrong, i am using "plain" pstricks so far, that's why i have a little pdflatex experience.

By the way, this command may be used to insert compressed (eps) images instead of raw ones:

`convert -compress zip #1 -resample 72x72 eps2:-

You could also convert the gif to a flash file (swf). This is better than keeping it as a gif, since Adobe Reader has compatibility by default, which improves accessibility. There are many free converters, such as Format Factory.

Jens Nöckel gives a sample of how to insert a flash movie.

  • 2
    To me this doesn't really answer the question, since it doesn't provide a means to "convert GIF to XXX on the fly". Moreover, SWF may be supported by Adobe Reader, but is that the case across all PDF readers? If not, I don't think it makes it "more accessible" in that format.
    – Werner
    Feb 5, 2014 at 18:21
  • You are correct about the compatability of SWF. Although it is compatible with Adobe Reader by default, most PDF readers cannot see the animations of SWF images. However, all PDF readers will pick up the thumbnail. Also, there is a program that runs on Unix command line called gif2swf that can convert the files on the fly. Feb 12, 2014 at 16:14

You must log in to answer this question.

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