5

I've run into a problem when converting a book manuscript from LaTeX to XeLaTeX. The problem is that my image sizing and placement has not survived the conversion.

The book has numerous picture inclusions where the source is a .png file. While .png has much to recommend itself, it does not include bounding box information in the file itself. So I use a script to cache the bounding box information in a separate file which I then use to set the bounding box of the image. In my document, I prepare the way with:

\DeclareGraphicsExtensions{.png,.bb}
\DeclareGraphicsRule{.png}{eps}{.bb}{}
\graphicspath{{./images/}}

I created the .bb files with a Q&D (Quick and Dirty) perl script.

With LaTeX, all was now well. Images were of the correct size and correctly placed on each page. Then I switched to XeLaTeX only to have my carefully crafted scheme join the aft gang aglay crowd!

What's going wrong? The bounding boxes are the same and yet the images are no longer placed correctly in my document?

5
  • 3
    Contributions such as this are more than welcome, but I think it would work better if phrased as a question and an answer. Since this is a Q&A site, people expect posts to have a certain format. It would also make it clearer to a casual reader what circumstances your script is useful for. Commented Jan 23, 2013 at 10:11
  • @AndrewStacey Excellent point---one that I was unsure of since this is my first attempt at this form. You are correct but perhaps the posting with your comment will also be useful to those who 'drop by'...(will phrase the next such offering accordingly---thank you!)
    – hsmyers
    Commented Jan 23, 2013 at 11:07
  • My comment isn't really enough -- comments aren't always read and can change order (or even not be immediately displayed). So it really would be better if you rephrased this one as a question and an answer. Commented Jan 23, 2013 at 11:31
  • ALL---I bow to group dynamics (I'd say peer presure, but you are all master craftsmen to my lowly apprentice so hardly peer :) ) and have split the post to question and answer. As usual I have no problem with anyone who wishes to improve or tidy---edit away!
    – hsmyers
    Commented Jan 23, 2013 at 13:29
  • It took me a couple of readings to understand the issue (initially, I thought that you only needed the script for the XeLaTeX version not for LaTeX) so I've had a go at editing to try to make it clearer what the problem is. Commented Jan 23, 2013 at 13:36

1 Answer 1

6

The solution to my problem of bad sizing on the images when I switched to XeLaTeX was solved when I discovered the difference in dpi one to another. The original version of this script did not allow for 72 versus 96---it does now and I offer it to others in the hope that it might be of use. This is a sort of Thank You! to the community. Please use and enjoy! For those who worry about IP and permissions, this is released into the wild without restriction or warrenty---free as in free beer.

#!/usr/bin/perl
# box2bb.pl -- create image.type.bb files for LaTeX
# if xelatex is first option on command line, changes to 72dpi from 96dpi
#
use strict;
use warnings;
use diagnostics;
use Image::Magick;
use POSIX qw(ceil);

our $VERSION = '0.03';

my $option = shift @ARGV;

if ($option ne 'xelatex') {
    push(@ARGV,$option);
}
for (map { glob } @ARGV) {
    my $h;
    my $w;
    my $p = new Image::Magick;
    my $fh;

    $p->Read($_);
    ($w,$h) = $p->Get('width','height');
    s/\.\w+\Z/.bb/;
    open($fh,'>',"$_");
    $w = convertPX2PT($w);
    $h = convertPX2PT($h);
    print $fh "\%\%BoundingBox: 0 0 $w $h\n";
}

sub convertPX2PT {
    if ($option eq 'xelatex') {
        return ceil(((shift) / 72.0) * 72.27);
    }
    else {
        return ceil(((shift) / 96.0) * 72.27);
    }
}
1
  • 3
    Bitmap images can have different resolutions (if the resolution is provided at all). pdfTeX respects them, in case of XeTeX, the script that generates the Bounding Box files should respect them. In case of dvipdfm(x), package bmpsize helps. Commented Jan 23, 2013 at 14:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .