# Including graphics for both DVI and PNG

I'm asking a question is quite similar to this one (where I don't seem to be able to leave comments):

MikTex error for PNG images when trying to produce DVI

I'm producing a PDF document, but I'm using the DVI previewer inside LeD while doing so. Including graphics is a bit problematic. Suppose I want to include a file simple_ambiguity.png.

\includegraphics[width=\textwidth]{simple_ambiguity.png}


doesn't work with the DVI, because of a bounding box error. This is a nuisance because I don't actually care about having the graphics display accurately inside the DVI, or even having it take up the right amount of space -- I just want it to compile.

At the moment I'm using a workaround: I'm creating a dummy empty picture' file simple_ambiguity.eps whose dimensions I don't really care about, and then writing

\includegraphics[width=\textwidth]{simple_ambiguity}


But this is a nuisance because I have to manually create an eps every time I add a new picture. What I'm looking for is some simple fire-and-forget solution that will make the DVI just compile.

(Of the answers in the question I linked to, the first seems to involve manually creating bounding boxes, and the second involves invoking ImageMagick on every compile, which will be unnecessarily slow & introduce a rather awkward dependency.)

-
I'm confused. If you are using the .dvi route, you need .eps graphics for final output. On the other hand, if you are using .png graphics you must be using PDF output mode, so where does the .dvi part come in? – Joseph Wright Aug 10 '12 at 14:00
You need some minimum reputation to leave comments everywhere. But you can always edit and/or comment your own quaestions. – matth Aug 10 '12 at 14:02
@Joseph Wright: I am using a IDE called LeD, and its built-in viewer only supports DVIs. As I said, I don't actually care about having the graphics display accurately inside the DVI, or even having it take up the right amount of space -- I just want it to compile so I have a rough preview available while editing. – Mohan Aug 10 '12 at 14:48
@Mohan I still don't understand: you are using the dvips route, so you have to have .eps graphics for your final PDF production, quite independent of the viewer in LeD. – Joseph Wright Aug 10 '12 at 15:10
@Joseph Wright: LeD allows one to invoke both latex and pdflatex. I use latex (frequently) while editing, together with the built-in previewer, to check everything except the pictures. I then use pdflatex to get the document I actually want to distribute. – Mohan Aug 10 '12 at 16:51

If you just want the image to go away in dvi/dvips mode then:

\makeatletter
\def\temp{dvips.def}
\ifx\Gin@driver\temp
\def\Ginclude@graphics#1{\def\temp{#1}---image \expandafter\strip@prefix\meaning\temp---}
\fi
\makeatother


In your preamble will have that effect.

-
Thank you. I think this is almost exactly what I want. Is there an easy way to have it e.g. leave two inches of vertical whitespace instead of including nothing? – Mohan Aug 10 '12 at 14:49
[I can insert \bigskip before the ---image, but \vspace and \vskip don't work.] – Mohan Aug 10 '12 at 15:01
put the text into a parbox and use the optional parbox arguments to specify width and height of the box – David Carlisle Aug 10 '12 at 15:48
Thanks! I'll try that. – Mohan Aug 10 '12 at 16:52
Nice one, does the job for me! – Felix Goldberg Nov 12 '13 at 10:45

You could also manually add the natwidth and natheight properties to each image like:

\includegraphics[natwidth=800,natheight=600,width=\textwidth]{simple_ambiguity.png}


This should work with both latex and pdflatex.

-

If you don't care about the size of the picture you can simply use the demo-option of the graphicx package:

\documentclass[a4paper,12pt]{article}
\usepackage[demo]{graphicx}

\begin{document}

\includegraphics[width=\textwidth]{dontexist.png}

\end{document}


Or you can redefine \includegraphics:

\documentclass[a4paper,12pt]{article}
\usepackage{graphicx}
\renewcommand\includegraphics[2][]{\fbox{here graphic #2}}
\begin{document}

\includegraphics[width=\textwidth]{dontexist.png}

\end{document}

-
thanks... but doesn't this mean I would have to comment/uncomment a line every time I switched from DVI target to PDF target? – Mohan Aug 10 '12 at 19:33
Use the ifpdf package if you want a different behaviour depending on the output mode. – Ulrike Fischer Aug 10 '12 at 19:52

you can use png or gif images also with the dvi route, but you have to define some graphic rules for the converting. The following needs

latex <file>
dvips -R0 <file>
ps2pdf <file>.ps


The program convert must be installed. It is part of the ImageMagick bundle

\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][]{{%
\let\@found\@empty
\@for\@type:=\bmpsize@types\do{%
\ifx\@found\@empty
\ifbmpsize@ok
\let\@found=\@type
\fi
\fi
}%
\ifx\@found\@empty\includegraphics[{#1}]{#2}%
\else\includegraphics[{natwidth=\bmpsize@width,natheight=\bmpsize@height,#1}]{#2}%
\fi}}
\makeatother

\begin{document}
\IncludeGraphics{lion.png} % a png image
\IncludeGraphics{lion.gif} % a gif image
\end{document}
`
-
Thank you for theq uick reply. Correct me if I'm wrong, but isn't this the same as 'involves invoking ImageMagick on every compile, which will be unnecessarily slow & introduce a rather awkward dependency'? – Mohan Aug 10 '12 at 14:49