64

Is there any way to include an animated GIF directly in either PDFLaTeX or XeLaTeX? I realize the animate package can include animations in a PDF, but it doesn't support animated GIFs and you have to split them up manually into EPS or PNG files as far as I can tell.

ptomato
  • 1,453
  • 2
    What is the problem with splitting the gif first? The manual part of the job is to enter something like `convert my.gif my.png' at the command line. – AlexG Nov 15 '10 at 12:34
  • 15
    It's a pain in the butt! – ptomato Nov 15 '10 at 12:36
  • If you have imagemagick installed on your computer you could rename its convert.exe binary into im-convert.exe. This prevents the system convert.exe program from being run. Other solutions on http://savage.net.au/ImageMagick/html/install-convert.html#Solutions – AlexG Nov 15 '10 at 13:00

2 Answers2

36

For animated GIF, package animate should be used. Animated GIF sequences tend to be short and don't justify a video embedding package, such as media9, that embeds a videoplayer component alongside the sequence and which requires the GIF to be transcoded into MP4 first.

animate is a lightweight alternative with the bonus of producing embedded animations that work in AR versions for Win, OSX and Linux, while media9 embedded video only works in AR for Win and OSX. For use with animate, the GIF must be split into a PNG or JPEG sequence, optimized GIF must be un-optimized first:

These are the steps for a 100 frames animated GIF, using package animate and playing at 12 frames per second:

  1. gifsicle --unoptimize animated.gif | convert - frame-%d.png

  2. \animategraphics[loop,autoplay]{12}{frame-}{0}{99}


With the deprecated movie15 package it was, at least theoretically, possible to embed animated GIF without preprocessing. This method depended on the availability of a third-party plugin, some QickTime component to be specific, used by AR for displaying the GIF. This never worked reliably, though.

AlexG
  • 54,894
  • Animated GIFs can be embedded in PDFs using the pre-v9 plugin model. – Charles Stewart Jan 13 '14 at 23:48
  • 1
    @CharlesStewart. This uses a QuickTime sub-plugin. I never got it reliably working. – AlexG Jan 14 '14 at 07:08
  • Right - problems with plugins are the reason why the annotations model was introduced with the PDF v9 standard, and the reason that CTAN deprecates movie15. But they are supported in the PDF spec, so what the qn wants is possible. – Charles Stewart Jan 14 '14 at 08:25
  • 1
    Unfortunately this solution depends on JavaScript support, so it won't work on Evince and Okular. – Mateus Araújo Mar 24 '15 at 18:17
  • 4
    @MateusAraújo Exactly. Somebody should take the challenge and add some opensource JS Engine, such as JavaScriptCore from webkit (used by many popular web browsers, btw), to opensource PDF viewers. – AlexG Mar 25 '15 at 07:47
  • 2
    +1 I was using convert to extract the frames, but they looked horrible when including them in the tex document. Using gifsicle saved my day! – Adri C.S. Feb 25 '16 at 10:07
  • I tried using this technique, but in addition to the lack of viewer support, pdflatex was unacceptably slow to compile the document. –  Jan 22 '18 at 21:11
  • 2
    @ben Cannot comment on your post without seeing your example and the image files. Usually, compiling time is not an issue. As viewers, acrobat reader, foxit and pdfxchange can be used. – AlexG Jan 22 '18 at 21:36
  • Note that the support for this plugin is really not popular. I tried out three different PDF viewers that are installed on my system (Chromium, Wondershare PDF, and Microsoft Edge), and none of them was able to display the animated video. As not even attachfile is supported by all of these readers, for compatibility reasons, it might be a better idea to upload the file to some cloud servicer, back it up using web.archive.org, and add a URL to it in your document. – Christoph Thiede Jan 14 '23 at 15:40
  • There is no plugin at all. Animations with pkg animate rely on JavaScript and here, indeed, support is scarce among PDF viewers. But you can generate SVG output instead. And for SVG, support is fantastic in all major web browsers, even on mobile devices, for example: https://tex.stackexchange.com/a/235180 – AlexG Jan 16 '23 at 12:23
30

Yes, use the movie15 package (in Latex), which supports GIFs directly. You will need to use a PDF viewer that has the right plugin to supported GIF animations.

Note on media9

The movie15 package has been marked deprecated on CTAN for some time in favour of the media9 package, because media9 uses the better supported approach to embedding media of Adobe's Rich Media Annotations, rather than the old, ad-hoc, plug-in based approach of movie15. This has the consequence that building rich media documents in media9 is a more flexible process, supporting several workflows, and the results typically can be displayed with more viewers. However, media9 does not support animated GIFs - the GIFs would have to be converted to a supported format such as FLV or MP4 before embedding.

Charles Stewart
  • 21,014
  • 5
  • 65
  • 121
  • 49
    What – I can't get animations on paper? They do have that in the Harry Potter movies. Why can't we? – Harald Hanche-Olsen Nov 15 '10 at 10:57
  • 14
    @Harald: yes, paper is a sort of PDF viewer, but it's hard to find paper that supports animated content. I've had no personal experience with magic paper. I'd love to hear of other people's... – Charles Stewart Nov 15 '10 at 11:18
  • This seems to work, although it doesn't compile in XeLaTeX, only in PDFLaTeX. Unfortunately I haven't managed to find a PDF viewer that will show the animated GIF! Even Acrobat says there's no plugin for GIFs available. – ptomato Nov 15 '10 at 11:30
  • 9
    @Charles: Oh, it's been around for a while. Remember the old comics when you need to flip quickly through the pages to see an animation effect? Now, I'm sure some journals would object heavily to my 1200+ page manuscript containing 60s of video @ 24 fps.. – Martin Tapankov Nov 15 '10 at 13:34
  • @ptomato: Really? There are several converters for animated GIF to Flash video (very much supported by Adobe) out there, cf. http://superuser.com/questions/102977/gif-to-flv-software-on-gnu-linux-or-cross-platform – Charles Stewart Nov 15 '10 at 14:03
  • @ptomato: For a Windows converter, I can't comment, but you could ask on Super User. – Charles Stewart Nov 15 '10 at 14:05
  • 9
    @Martin: You are quite right! Someone must write a printer driver that creates flip books when given an animated GIF. – Charles Stewart Nov 16 '10 at 10:19
  • 2
    Now should we use the media9package instead ? – Stéphane Laurent Aug 21 '13 at 15:22
  • According to media9s description: "It replaces the now obsolete movie15 package." So i quess that media9 is the correct one now. – CodeTower Jan 13 '14 at 18:37
  • @Rasmus - Ok, ok, I've updated my answer. It's a bit trickier than that. – Charles Stewart Jan 13 '14 at 19:25
  • Can Gif9 embed the movie (MP4 or whatever) into the PDF file? – Demis May 12 '16 at 17:11
  • @Demis- There is no Gif9 package. Both movie15 and media9 embed media in the PDF, but only movie15 allows animated GIFs to be embedded. – Charles Stewart May 30 '16 at 09:30