183

I'm trying to include graphics in my Latex-file, which I compiled with latex+dvipdf on OS X. Latex however returns this error: "Cannot determine size of graphic"

My graphic is exported from PowerPoint, so I have tried both .pdf and .png. I get the same errors for both.

Sample code that doesn't work:

\begin{figure}[htb]
\begin{center}
\leavevmode
\includegraphics[width=0.8\textwidth]{graph.png}
\end{center}
\end{figure}

If I have to manually set some measurements of the image, please tell me how I can find out those measurements. I need the image to take up about 80% of text width and be centered. Thanks.

2

6 Answers 6

181

The DVI producing latex doesn't support reading the size of PNG, JPG or PDF images. You need to use pdflatex for this. Actually latex is pdflatex in DVI mode in modern distributions, but it can read the sizes only in PDF mode for some reason. You are however able to state the natural size of the images using natwidth and natheight which will make latex compile without error. The produced DVI file will only link to the PDF and the DVI-to-PDF converter will need to include it in the final PDF. AFAIK dvpdf doesn't support this but dvipdfm does.

\documentclass{article}

\usepackage{graphicx}

\begin{document}
\begin{figure}
    \centering
    \includegraphics[width=0.8\textwidth,natwidth=610,natheight=642]{tiger.pdf}
\end{figure}
\end{document}

Compile with either

pdflatex <file base>

or

latex <file base>
dvipdfm <file base>
9
  • 4
    For dvipdfm(x), one should use dvipdfm or dvipdfmx option of graphicx pacakge, and use extractbb (ebb, xbb) program to produce .bb or .xbb file. bmpsize package can also used to replace extractbb.
    – Leo Liu
    May 8, 2011 at 15:15
  • 7
    Thank you for the solution provided. I simply added \usepackage[dvipdfm]{graphicx} \usepackage{bmpsize} to my file, and it worked perfectly ! No need to spend time to manually set the bounding box with the time-consuming trial-and-errors method (what I did until this day).
    – user41288
    Nov 19, 2013 at 15:17
  • I used this solution for a png-figure with latex, dvipdfmx and graphicx-package, which worked for me without doing anything additional (2013 TeX Live)
    – Bernhard
    Jul 3, 2014 at 12:09
  • 5
    user41288 solution is great, but there is a typo in the provided code (dvipdfmx). The following one worked for me: \usepackage[dvipdfmx]{graphicx} \usepackage{bmpsize}
    – zeroos
    May 12, 2015 at 20:56
  • Just to add: specificying natwidth and natheight also solved a problem I had with jpgs having incorrect bounding boxes (so the tex file compiles, but the image ends up misplaced). Nov 5, 2015 at 1:10
38

A non-Latex solution

A very fast solution is to convert the png file into eps format. It can be done easily using graphic software, for instance Inkscape.

So, you only need to open the file in Inkscape and then SaveAs and select eps format.

Now change the extension in the tex file from

\includegraphics[width=0.8\textwidth]{tiger.png}

to

\includegraphics[width=0.8\textwidth]{tiger.eps}
5
  • 7
    excellent and simple solution. I'd add using command-line convert tiger.jpg eps2:tiger.eps for another easy & easily scriptable way of converting images, as proposed in this answer: stackoverflow.com/a/14220700/3551299
    – tlegutko
    May 15, 2017 at 21:52
  • 1
    Nice! I used gimp to convert from .png to .eps gimp.org Jul 24, 2017 at 21:48
  • Perfect! I used gimp!
    – Sergio
    Sep 30, 2017 at 5:01
  • 1
    png also doesnt work
    – Trect
    Aug 12, 2018 at 16:37
  • 2
    Unfortunately EPS files are about ten times the size of PNG files.
    – U. Windl
    Mar 3, 2019 at 22:27
37

Fix the file name

I got this error with a pdf file which contained dots in the filename. Removing the dots (except the .pdf, of course) fixed the issue.

Fix file type detection

The LaTeX graphics/graphicx package uses the first dot to find the extension. Simply adding \usepackage{graphicx} \usepackage{grffile} fixed the issue without changing file names. Package grffile changes the algorithm to check for known extensions (option multidot, enabled by default).

6
  • 2
    yep, here, too. Is there an open bug report about it anywhere? Apr 13, 2018 at 14:02
  • 1
    how true it is! very unlikely widely used programs like gimp or inkscape randomly produce wrong images what (Xe)LaTeX can not process. indeed the dot was the reason.
    – deeenes
    Oct 15, 2018 at 3:43
  • 1
    This is still an issue today (2019-02-20) with the latest updated MikTeX on Windows. My document had been building fine with an outdated MikTeX install, but I started getting this error after updating and came across this answer while trying to resolve the problem. Turns out renaming the image files to not have dots in the names fixed it.
    – Herohtar
    Feb 20, 2019 at 19:18
  • 3
    Confirming this is still an issue (MacTex 2018); switching from dots to underscores in the filename fixed the issue mentioned in the OP. Mar 3, 2019 at 19:46
  • 1
    Simply adding \usepackage{graphicx} \usepackage{grffile} fixed the issue without changing file names. The LaTeX graphics/graphicx package uses the first dot to find the extension. Package grffile changes the algorithm to check for known extensions (option multidot, enabled by default).
    – hyiltiz
    Jun 8, 2020 at 22:52
3

I saw this issue with a jpeg file. By changing its extension from .JPG to .jpg (yes, just capital to lower) the error went away.

$ pdflatex --version
This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/Debian) restricted \write18 enabled.
1

Sometimes I get this errors when I compile using latexmk myfile.tex and include pdf pictures... It's because I forget to compile to pdf, the correct command must be:

latexmk -pdf myfile.tex
-1

remove the extension in the latex code, all will be fine. The file keep its extension (correct: jpg if jpg, png if png...).

\begin{figure}[htb]
    \begin{center}
       \leavevmode
       \includegraphics[width=0.8\textwidth]{graph}
    \end{center}
\end{figure}

With the latex compiler, the allowed file type are ps or eps, and with pdflatex, png, jpg (jpeg also is found as extension) or pdf. The file must have its extension, and its real extension. A png is the result of a particular encoding of an image, a jpg another. Latex does not recompile images, it looks for files with the right name, with the extensions it handles. You just have to give it the base of the name, then it manages.

3
  • Not working. If I remove the extension I get file not found, as expected. Jul 6, 2020 at 19:36
  • @LuisA.Florit presumably you need to rename the image file to remove the extension from the filename too. Did you do both this and change the reference to the file in the latex? Sep 9, 2020 at 9:48
  • No, it's perfect if the file keep its extension. With the latex compiler, the allowed file type are ps or eps, and with pdflatex, png, jpg (jpeg also is found as extension) or pdf. The file must have its extension.
    – Mih Zam
    Sep 23, 2020 at 19:40