# How can I include a PNG image using XeTeX?

I can include a pdf image using pdflatex as a compiler, and the code:

\usepackage{graphicx}

....

\begin{figure}
\centering
\includegraphics[scale=0.6]{chiaroscuro-victorian.png}
\end{figure}


But when I compile using XeTeX, I get the error:

! LaTeX Error: Cannot determine size of graphic in chiaroscuro-victorian.png (no BoundingBox).


I'm also using the mla package, if that's relevant at all:

\usepackage{mla}


How can I import this image and use XeTeX?

• As a sanity check: substitute example-image-a.png for your image to check it works. If so, the problem is presumably as it appears and you can probably just tell LaTeX the bounding box explicitly. See the graphicx manual for the required syntax.
– cfr
Dec 4 '14 at 3:56
• You need to use \usepackage[dvips,xetex]{graphicx} before loading mla, which (wrongly I think) tests for pdftex, but not the other engines. This leads to loading graphicx with the dvips driver, which clashes with your desire to use XeTeX on the backend.
– jon
Dec 4 '14 at 4:03
• ... However, inside the .sty file, which is where the documentation (such as it is) can be found, it states the package is "For use with LaTeX and pdflatex." So: caveat utens!
– jon
Dec 4 '14 at 4:10
• Actually, I'd advise against using the package altogether: it uses outdated packages (e.g, times), doesn't use tried and true packages where it could (e.g., geometry, csquotes, setspace), and encourages ad hoc formatting (e.g., using its \tab macro to indent following \section-related commands).
– jon
Dec 4 '14 at 4:27

This will work with XeTeX (i.e., using the xelatex command):

\documentclass{article}
\usepackage[dvips,xetex]{graphicx}
\usepackage{ifpdf,mla}% <-- mla.sty requires ifpdf.sty, but (perversely) doesn't load it
%\usepackage{fontspec}

\begin{document}

\begin{figure}
\centering
\includegraphics[scale=0.6]{chiaroscuro-victorian.png}
\end{figure}

\end{document}


The problem is due to mla.sty (which I'd avoid using). It has the following definition:

% New code thanks to Edward Z. Yang
\ifpdf % We're generating a pdf
\usepackage[pdftex]{color,graphicx}
\pdfpagewidth=\paperwidth
\pdfpageheight=\paperheight
\usepackage{thumbpdf}
%\pdfcompresslevel=9
\else
\usepackage[dvips]{graphicx}
\fi


In essence, if you aren't using pdfTeX as the engine, it loads graphicx with a specific driver, causing the conflict. Loading graphicx first with the xetex option solves the clash (you also need to load dvips so your loading of the package doesn't cause a clash with what mla.sty does.

• Great! But now the images are indented a half-inch, like new paragraphs are. How can I fix that? Dec 4 '14 at 17:35
• Actually, can you explain what you mean by editing your question so it shows the problem? I'd have to make a lot of assumptions about what you're doing otherwise.... (Please strive to make it a minimal example.)
– jon
Dec 4 '14 at 17:55

A PNG image is a raster image, which has no physical dimensions. If you image is 100 pixels by 100 pixels, you can print that out in thumbnail size or as a full page. Since XeTeX is putting the picture on a page, it needs to know how large to make it, e.g.,

\includegraphics[width=3in]{chiaroscuro-victorian.png}


You don't need to specify the width and the height because TeX can calculate it, since it knows the aspect ratio of the image.

• This will not solve the problem in this case, which arises due to mla.sty's less-than-ideal implementation. (Also: scale=0.6 is usually a fine way to specify size for .png graphic inclusions.)
– jon
Dec 4 '14 at 5:05
• I'm curious how large a 100 x 100 image (for instance) appears? Dec 4 '14 at 5:11
• The question here is why the \includegraphics command fails, not about how to specify a specific size. scale=X works in general; but neither it nor width=Y will fix the problem of the OP without fixing the driver clash caused by using XeTeX with the mla.sty package.
– jon
Dec 4 '14 at 5:20