9

I have a tex file with \includegraphic elements for some PNG images. I use MikTeX on Windows and the PDF is fine.

Now, I need a DVI so I tried using: \pdfoutput=0 This creates a DVI, but only after I remove all the PNG images… Otherwise I get errors like:

! LaTeX Error: Cannot determine size of graphic in math-box-plot.png (no BoundingBox).
7

TeX is text based, it cannot determine the size of PNG image itself by default.

A quick solution:

\usepackage{bmpsize}

And use

\usepackage[dvipdfmx]{graphicx}

It is better to use extractbb (also named ebb, xbb) program to produce .bb or .xbb file for LaTeX. Use this command in 'Command Prompt':

for %i in (*.png) do xbb %i

(or for %i in (*.png) do extractbb -x %i, I'm not sure about MiKTeX.)

And use

\usepackage[dvipdfmx]{graphicx}

(I use TeX Live, only dvipdfmx supports png images. But MiKTeX seems different, I can't remember well.)

  • 1
    Leo, you better use <code> ... </code> blocks for the command line and similar text to avoid the TeX syntax highlighter. – Martin Scharrer Feb 16 '11 at 8:41
  • @Martin: I did not realise that the two were handled differently – Joseph Wright Feb 16 '11 at 9:28
  • @Joseph: The syntax highlighting is done in <pre class="prettyprint lang-tex"> tags around <code>. Therefore single <code> tags on their own are not highlighted. – Martin Scharrer Feb 16 '11 at 9:36
3

Install the program ImageMagick, then you can do it on the fly:

\documentclass{article}

\usepackage[dvips]{graphicx}
\DeclareGraphicsRule{.png}{eps}{.bb}{`convert #1 eps:-}
\DeclareGraphicsRule{.gif}{eps}{.bb}{`convert #1 eps:-}
\usepackage{grfext}
\AppendGraphicsExtensions*{.png,.gif}
\usepackage{bmpsize}

\makeatletter
\newcommand*{\IncludeGraphics}[2][]{%
  \begingroup
    \let\@found\@empty
    \@for\@type:=\bmpsize@types\do{%
      \ifx\@found\@empty
        \@nameuse{bmpsize@read@\@type}{#2.\@type}%
        \ifbmpsize@ok
          \let\@found=\@type
        \fi
      \fi
    }%
    \ifx\@found\@empty
      \includegraphics[{#1}]{#2}%
    \else
      \includegraphics[{natwidth=\bmpsize@width,natheight=\bmpsize@height,#1}]{#2}%
    \fi
  \endgroup
}
\makeatother

\begin{document}
  \IncludeGraphics{lion}\qquad % a png image
  \IncludeGraphics{knuth-tex}  % a gif image
\end{document}

btw: you do not need the \pdfoutput=0 simply run latex instead of pdflatex

  • It may be too tricky for new TeXers, with no actual advantage. BTW, ImageMagick may be overkill, bmeps is enough. – Leo Liu Feb 16 '11 at 11:34
  • 1
    no, it isn't, and ImagaMagick can be used for any other conversion, hence it is no overkill. – user2478 Feb 16 '11 at 12:07
2

I always make portable latex source equally suitable for compiling both with latex and pdflatex. My remedy is rather simple.

Put \usepackage{graphicx} into preample (without any driver specified).

Import eps or pdf graphics with \includegraphics{filename}; latex compiler then loads filename.eps whereas pdflatex takes filename.pdf. MikTeX automatically converts eps into pdf on fly if the epstopdf package is loaded.

Import png graphics with \includegraphics[nathwith=<XX>bp, natheight=<YY>bp, width=<width>]{filename.png} with .png extension explicitly shown; in that case you dont need to invent \DeclareGraphicsRule. Natural height and width of bitmap graphics can be determined from file properties in Windows Explorer or any graphics editor, e.g. Windows Paint.

  • Does this work with plain DVI-producing latex, not just pdflatex? – einpoklum Jul 6 '14 at 15:52
  • @Igor Kotelnikov: I can't do "two-character swaped edits"; maybe fix nathwith – U. Windl Mar 4 at 2:37
  • @U.Windl if possible neither natwidth nor natheight are recommended You should try to use the natural PNG dpi default of 96dpi in both directions then simply control image import placement with width OR if necessary height – user170109 Mar 4 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.