84

Related:
Convert gif image to png on the fly

I have an image file -- a gif file, which is the graph of a function. How do I add it to a LaTeX file? Will it come out OK in the subsequent pdf file?

0

4 Answers 4

86

Use movie15 package. For example:

% in preamble
\usepackage{movie15}
% in documenet
\includemovie{1cm}{1cm}{ani.gif}

PDFLaTeX is needed, and you must use Adobe Reader with certain media player plug-in. Thus it often fails.

Another method is to use animate package. You have to convert the animated gif to separate images first, using ImageMagick:

convert foo.gif foo.png

This would sometimes create a suboptimal result; if the GIF is optimized try

convert foo.gif -coalesce foo.png

You may probably get foo-0.png, foo-1.png, ..., foo-18.png.

Then include the graphics:

\animategraphics{12}{foo-}{0}{18}

See the manual of animate for more options.

7
  • 10
    Interesting, but the OP didn't request animation ... :-) Commented Feb 6, 2011 at 8:50
  • 2
    Indeed. I noticed @TeX asked for animated GIF, so I appended this answer.
    – Leo Liu
    Commented Feb 6, 2011 at 9:40
  • I tried with different (animated) gif and none shows up in the PDF (Acrobat viewer), can any one point to a sample gif that works with movie15?
    – alfC
    Commented Jan 10, 2012 at 19:18
  • 3
    The original question doesn't mention animated gifs. The workflow for including animated gifs as opposed to plain gifs is so significantly different that I think this answer isn't helpful to the question. Commented Feb 5, 2013 at 20:14
  • 2
    I got the error ! Package movie15 Error: File 'test.gif' cannot be opened for embeddin Commented Oct 26, 2015 at 10:45
18

needs pdflatex -shell-escape ... and an installed program convert which is available for all plarforms

\documentclass{article}
\usepackage{graphicx}
\usepackage{epstopdf}

\epstopdfDeclareGraphicsRule{.gif}{png}{.png}{convert gif:#1 png:\OutputFile}
\AppendGraphicsExtensions{.gif}

\begin{document}

  \includegraphics{demo.eps}\qquad
  \includegraphics{knuth-tex.gif}

\end{document}
2
  • 5
    Unfortunately, animation in the original gif will no longer work after conversion. Commented Dec 23, 2010 at 7:51
  • 1
    And some people use LaTeX (Beamer) for presentation slides, so animation is useful.
    – MakisH
    Commented May 23, 2018 at 11:13
14

With the original latex engine, this would work:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
    \includegraphics{your.gif}
\end{document}

But if you are using pdflatex or xelatex, this will result in an error about .gif being an unknown graphics extension. (If you don't know which engine you are using, it's probably pdflatex.) I would recommend converting the .gif file to a .png file and repeating the above snippet, with your.gif replaced by your.png. In fact, you can just use your; latex has a default set of extensions it will add to find the file.

There are dozens of ways to convert GIFs to PNGs, including many free online services. Google is your friend. The only reason I can see to convert the file on the fly with each compiling of the latex document would be if your gif file changes often as the result of some other process.

6
  • Even with the package graphicx I still get errors about the unknown graphics extension .gif. For me this doesn't work
    – Joris Meys
    Commented Oct 26, 2016 at 8:25
  • 1
    @JorisMeys. Are you using pdflatex or xelatex to compile? If so, read the second sentence. You'll need to convert the graphic to .png some other way before including in a TeX document. Commented Oct 26, 2016 at 8:45
  • Stupid oversight, thank you for reminding me.
    – Joris Meys
    Commented Oct 26, 2016 at 10:19
  • 1
    @JorisMeys: I rewrote the answer to highlight the difference the engine makes in how to approach this problem. Commented Oct 26, 2016 at 12:57
  • But if you convert to png then the animation effect is lost?
    – Nulle
    Commented Jan 22, 2019 at 12:30
1

If you wese using windows, you could just right click the .gif file and "open with" MS-Paint

Then just save the file as .jpg one and use it like you please.

2
  • 4
    GIF is a lossless compressed bitmap format and JPG is lossy. Better convert it to PNG.
    – AlexG
    Commented Oct 25, 2017 at 6:48
  • 1
    This will not work when you have to show an animation using animated gif. Commented Nov 19, 2018 at 2:49

You must log in to answer this question.

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