6

I'd like to insert a GIF image in my text and I found this Convert GIF image to PNG on the fly.

So I pasted in my document

\documentclass[a4paper]{article}
\usepackage{graphicx}
\DeclareGraphicsRule{.JPG}{eps}{.JPG}{`convert #1 eps:-}
\begin{document}
\framebox{\includegraphics[0,0][150,200]{DSC00121-SMALL.JPG}}
\end{document} 

Namely I have

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

and then

 \framebox{\includegraphics[0,0][150,200]{nn.JPG}}

Also I have renamed the picture as nn.JPG

But it doesn't work! Any help?

Martin Scharrer
  • 262,582
  • Includegraphics command has a single optional argument (in [ ] ), with possibly several comma separated key=value pairs. Hence your syntax is wrong. For a bitmap you could have to provide the bunding box, but for an eps (even if created on the fly) it is read from the file. Try \includegraphics[width=0.5\textwidth]{small.gif} . – Jhor Feb 21 '19 at 08:51
  • 1
    By the way I assume that you have defined the graphicsrule for gif and not only for jpg. Of course renaming .gif to .jpg can not work, as their format is quite different. – Jhor Feb 21 '19 at 08:54
  • @Jhor Thanks for answering! I tried \includegraphics[width=0.5\textwidth]{small.gif} but it doesn't work either. It says "unknown graphics extention .gif" – 1123581321 Feb 21 '19 at 08:57
  • @Jhor I have renamed the picture small.gif – 1123581321 Feb 21 '19 at 08:59
  • As said in my second comment, you need to replicate the \DeclareGraphicsRule{.JPG}{eps}{.JPG}{convert #1 eps:-}` into a gif version. – Jhor Feb 21 '19 at 09:01
  • @Jhor I'd appreciate it if you could show me how to do this. – 1123581321 Feb 21 '19 at 09:02
  • 1
    Which motor are you using (LaTeX, pdfLaTeX, XeTeX ??) – Jhor Feb 21 '19 at 09:06
  • @Jhor I do it in overleaf.com where can I find the motor that I use? – 1123581321 Feb 21 '19 at 09:07
  • look at the first lines of your .log file – Jhor Feb 21 '19 at 09:14
  • @Jhor I think it is in pdfLaTeX – 1123581321 Feb 21 '19 at 09:21
  • @Jhor Thank you very much! I actually solved my problem – 1123581321 Feb 21 '19 at 10:28

1 Answers1

10

Here is the MWE for pdfLaTeX, assuming that you have the command line convert from Image Magick installed, and using the .png route, as pdfLaTeX already has the method for PNG:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
% loading epstopdf package might be needed, 
% but is often automatically loaded by graphicx when running pdfLaTeX
%\usepackage{epstopdf}
% epstopdf setup for GIF
\DeclareGraphicsRule{.gif}{png}{.png}{%
  \noexpand\epstopdfcall{convert #1 \noexpand\OutputFile}%
}
\AppendGraphicsExtensions{.gif}

\begin{document} \subsection{This is a GIF version} \includegraphics[width=0.5\linewidth]{tmp.gif} % The GIF file is converted to tmp-gif-converted-to.png \subsection{This is a jpg version} \includegraphics[width=0.5\linewidth]{tmp.jpg} \subsection{This is a png version} \includegraphics[width=0.5\linewidth]{tmp.png} \subsection{This is a pdf version} \includegraphics[width=0.5\linewidth]{tmp} \end{document}

enter image description here

EDIT: With old fashioned LaTeX (in dvi mode) one would have to convert GIFs to EPS. For this purpose, one can replace the \DeclareGraphicsRule above by the following one:

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

Then the conversion command is written verbatim in the .dvi file, and executed by dvips provided it is run with the -R0 option. Finally the PDF file can be produced by using ps2pdf.

However, this method has several drawbacks:

  • the converted figures are huge;
  • it does not work with dvipdfm(x).
  • it works only if the .gif.bb file has been created, that can be done with a command like: identify tmp.gif |sed -r -e "s/(.*)\s+([0-9]{2,})x([0-9]{2,})\s+(.*)/%%BoundingBox: 0 0 \2 \3/" > tmp.gif.bb

for each graphic file tmp.gif.

In this context, it becomes much more efficient to write a batch to perform the conversion of all the files outside of LaTeX/dvips, by using convert with suitable options, or Netpbm, or Irfanview (windows only) and so on.

EDIT 2: On Windows, the convert command is a disk handling tool, and recent versions of ImageMagick use imagemagick convert (with the space) instead of the bare convert. Then \epstopdfcall must be modified accordingly.

Jhor
  • 4,179
  • Your minimal example gives: ! Undefined control sequence. l.11 \AppendGraphicsExtensions. I had to add \usepackage{grfext} for it to work. – Thomas Oct 13 '22 at 11:24
  • @Thomas. Actually \AppendGraphicsExtensions is defined in grfext package. The latter is automatically loaded by epstopdf package. Hence if this package is not automatically loaded via the .cfg file or your pdftex (system dependant), you have to load it, or load grfext as you did. – Jhor Oct 17 '22 at 10:01