3

resolution is undefined in XeTeX so the following code will produce different values when compiled with pdfTeX and XeTeX.

\documentclass{article}
\usepackage{graphicx}

\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=1]{koala.jpg}}

\begin{document}
\the\wd\IBox
\end{document}

How to force pdfTeX and XeTeX to produce the same measurement for the imported image?

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
Display Name
  • 46,933
  • 3
    'Convert the image to a PDF' seems to be the most obvious answer. I'm afraid that XeTeX and pdfTeX are going to be different at a low-level due to the difference in drivers (assuming we are talking about pdfTeX in PDF mode, of course). – Joseph Wright Aug 03 '11 at 18:29
  • 3
    I get exactly the same value with my test file –  Aug 03 '11 at 18:41
  • 4
    What do you mean with "resolution is undefined in xetex". What is resolution? Which values are you getting for pdftex? How big are the differences? They simply might be some rounding issues (bp to pt) conversion. Please don't keep asking "doesn't work" questions without details! BTW, scale=1 is useless but simply adds some overhead. Is it part of the problem? If so, state it explicitly, if not it doesn't has anything to do in a MWE. – Martin Scharrer Aug 03 '11 at 18:52
  • @Martin: why should there be some rounding errors? –  Aug 03 '11 at 19:04
  • You are aware that the output of identify gives pixels and not .pt? If the image doesn't specify the dpi value (i.e. the resolution) than TeX cannot know how many pixels to put into a 1pt length. Apparently pdftex and xetex have different default values. – Caramdir Aug 03 '11 at 19:06
  • 1
    There is no proper, universally working default. Images come in everything from 50dpi to several thousand dpi. How should TeX know what to choose if the image doesn't specify? – Caramdir Aug 03 '11 at 19:11
  • 2
    run identify -verbose and see whether Resolution is set. – Caramdir Aug 03 '11 at 19:13
  • @Herbert: I get rounding errors when I produce tightly cropped PDFs from TikZ (e.g. say 100x100pt) and then include them back as images. As you know PDF use bp as unit, so there is a conversion from pt to bp and then back. This gives me rounding errors, i.e. \includegraphics{tikzimage.pdf} has a slightly different size than \input{tikzimage.tex}. – Martin Scharrer Aug 03 '11 at 19:21
  • 2
  • 1
    @Martin: sure, but we were talking about including a jpeg ;-) –  Aug 03 '11 at 19:54
  • I downloaded the Koala file from the Internet (can't find it on my Windows installation) and I get the same output from pdftex and xetex that xport gets, but identify gives me a resolution of 96x96ppi. So I guess identify is not a reliable way of seeing whether the resolution is set in an image? – Caramdir Aug 03 '11 at 20:07
  • @Herbert: Sure, but there will be some pixel to pt conversation as well. I just mentioned the possibility, – Martin Scharrer Aug 03 '11 at 20:13
  • http://www.tex.ac.uk/cgi-bin/texfaq2html?label=whatengine – HansBKK Dec 22 '11 at 08:52

2 Answers2

6

tl;dr version: The metadata in the Koala file is inconclusive, as the Exif header exists, but doesn't specify the resolution. To solve the problem use some tool to either add the resolution to the Exif data or remove the Exif data completely.


First some technicalities on how metadata of (modern) jpg images is stored (I'm not an expert on this, so the following might not be entirely correct): There are two formats for metadata, called JFIF (JPEG file interchange format) and EXIF (Exchangeable image file format). The first was created to solve some deficiencies with the original JPEG file format (JIF), the second one to add metadate from cameras. Both can be present in the same file (and usually are). Both have a field to set the resolution of the image. Note that the original JPEG file format (JIF) had no way of storing the resolution.

The Koala.jpg image that comes with Windows includes both kinds of metadata (though most Exif fields are unused). However it only has the resoltution stored in the JFIF metadata, while the Exif resolution field is empty. It seems that pdflatex and xelatex behave differently in this setting.

  • pdflatex either always ignors the Exif data or falls back to the JFIF data when the Exif data it is incomplete. So it successfully reads the resolution information from the file and applies it. (I guess Taco's solution attempts only set the default resolution and hence where overridden by the successfully read resolution from the file.)
  • xelatex sees the Exif metadata and thus tries to read from it. But since there is no resolution information in it, it falls back to its default resolution of 72ppi. It seems that when some Exif data is present, xelatex ignores the entire JFIF block (I don't know whether this is a feature or a bug). When the Exif data is completely removed (e.g. via exiv2 rm Koala.jpg), then it reads the JFIF metadata and correctly applies the resolution stored there.
Caramdir
  • 89,023
  • 26
  • 255
  • 291
2

Looks like the image does not have any actual resolution information, and that xetex and pdftex use different defaults.

To elaborate: I think this the case, because pdftex appears to be using 96dpi, which matches its idea of what a pixel resolution is. It follows that I expect that the 72dpi that identify reports is not actually inside the jpg file, but just a default that it uses.

Taco Hoekwater
  • 13,724
  • 43
  • 67