2

I have a rather obscure problem. Not to get too detailed in short I am using a framework that converts my markdown-like files into html pages. One such file includes an animated gif. I am trying to write a script that also takes each of my blog posts and converts it to latex and then a pdf. Obviously this produces an error because normally \includegraphic wont support a GIF.

That said, even though the gif is animated I do not need nor want it to be animated in the final pdf (since this might not show right in some pdf readers). So I am perfectly ok with the gif being treated as a non-animated gif. However what I need specifically is for \includegraphic to handle the gif (as if it were not-animated) and fix the error. Note that because the framework converts the md for me I can not use a command other than \includegraphic. I do however have the ability to insert any latex that is needed into the beginning of the latex document.

EDIT

This problem has two parts to it

  1. convert a gif to a png on the fly
  2. redefine \includegraphics so it handles #1 for gifs automatically, and other images it handles normally.

It was suggested that the question that explains how to convert a gif on the fly was a duplicate of this one. But it isnt since this question, unlike the other, also asks for #2 above.

  • 2
    gifs are not supported, unfortunately. You'll need to convert it to a supported format, like jpg or png – Phelype Oleinik Jul 09 '21 at 02:12
  • @PhelypeOleinik Is there no clever work around? Consider there are packages that can embed animated gifs why is it impossible to do it with static gifs? Then write some kind of macro or something to replace \includegraphic when it sees a gif vs other types? – Jeffrey Phillips Freeman Jul 09 '21 at 02:17
  • 1
    No, the fig format is not supported at the engine level, so no package can make it work. The packages that include animated gifs actually use a series of pngs, as far as I know – Phelype Oleinik Jul 09 '21 at 02:22
  • @PhelypeOleinik if those packages just convert a gif to a png inline why cant the same be done here? use one of those packages somehow to convert it inline. I cant convert it manually because the source document is playing the dual role of generating html (where it needs the gif) and a pdf.. surely it is possible with some creativity even if it just autoconverts it to png. – Jeffrey Phillips Freeman Jul 09 '21 at 02:24
  • 2
    I don't know if they do that automatically, but if they do it's not from within LaTeX, but using an external command like convert animated.gif[0] static.png (the [0] is the frame number). You can use that with -shell-escape to do it from within LaTeX – Phelype Oleinik Jul 09 '21 at 02:30
  • @PhelypeOleinik ahh I see, thanks so much. At that point i might as well just write up some fancy regex and commands into my script that converts it to png automatically.. shame latex doesnt have a better solution for this. – Jeffrey Phillips Freeman Jul 09 '21 at 02:32

1 Answers1

3

GIF images are not supported by TeX at the engine level (I'm not even sure if the PDF format allows GIFs), so there's nothing packages can do to work around that. The best you can do is to convert the GIF to a supported format and include that instead.

Here's graphics rule for including graphics files with .gif extension that will include the first frame of a .gif file in a LaTeX document. It requires ImageMagick installed for the conversion, and it requires you to run LaTeX with the -shell-escape command line option. The rule uses ImageMagick to convert the .gif to a .png file, then includes that .png instead.

\documentclass{article}

\usepackage{graphicx} \usepackage{shellesc} \makeatletter \edef\Gin@extensions{\Gin@extensions,.gif} @namedef{Gin@rule@.gif}#1{{gif}{.gif}{#1}} \def\Gread@gif#1{% \IfFileExists{#1}{% \ifnum\ShellEscapeStatus=1 \ShellEscape{convert #1[0] #1-converted.png}% \Gread@png{#1-converted.png}% \else \errmessage{Shell escape disabled.}% \fi} {\errmessage{gif}{File '#1' does not exist.}}} \def\Ginclude@gif#1{\Ginclude@png{#1-converted.png}} \makeatother

\begin{document}

\includegraphics{giphy.gif}

\end{document}

  • Thanks, sadly this wont work because the framework uses \includegraphics.. Is there a way to "rename" the built in \includegraphics then write our own replacement for \includegraphics similar to your solution? If so that would probably fix my issue! – Jeffrey Phillips Freeman Jul 09 '21 at 02:50
  • 1
    @JeffreyPhillipsFreeman you could set this up as a "graphics rule" for \includegraphics similar to how pdftex.def sets up epstopdf to include eps files but really I wouldn't. it is much simpler to convert them with a script before hand. using shell escape within the tex run opens up all kinds of security issues, and it makes the tex run slower. – David Carlisle Jul 09 '21 at 07:48
  • @JeffreyPhillipsFreeman What David said. I edited my answer to replace the \includegif command by a graphics rule for files with .gif extension. The requirements are still the same. – Phelype Oleinik Jul 09 '21 at 10:33