Related to this question Dimension too large. error from pdflatex when using \includegraphics

I run large computation in Mathematica, which generates thousands of image file in pdf. Few of them are too large to include in Latex and my build can fail in the middle.

Is there a way to check, in Latex, using the image file name, that its size and dimensions \maxdimen will not cause \includegraphics to fail? This way I can add a conditional in Latex and avoid loading these image files. Here is a MWE


The above will fail on this file since the file size is too large.

What Latex code do I need to add to check the image size is valid before calling \includegraphics?

TL 2015, Linux


Thanks to cfr comment, using pdfinfo -box image.pdf gives

>pdfinfo -box image.pdf
Creator:        Wolfram Mathematica for Microsoft Windows (64-bit) (March 23, 2015) Student Edition - Personal Use Only
CreationDate:   Tue Jul 21 13:15:48 2015
ModDate:        Tue Jul 21 13:15:48 2015
Tagged:         no
Form:           none
Pages:          1
Encrypted:      no
Page size:      504 x 18988 pts
Page rot:       0
MediaBox:           0.00     0.00   504.00 18988.00
CropBox:            0.00     0.00   504.00 18988.00
BleedBox:           0.00     0.00   504.00 18988.00
TrimBox:            0.00     0.00   504.00 18988.00
ArtBox:             0.00     0.00   504.00 18988.00
File size:      337034 bytes
Optimized:      no
PDF version:    1.5

I need to figure how to do this inside the Latex document and get the dimension to check against \maxdimen and add Latex conditional to avoid loading this particular image. This has to be done in Latex, since the Latex files are also generated automatically and the image files as well, all by running scripts. So this is all have to be automated.

It will be nice if Latex provides a macro/package that will obtain the image dimensions before loading.

  • 1
    You could use e.g. pdfinfo -box to get the size. But this will depend on your OS.
    – cfr
    Commented Jul 26, 2015 at 23:05

2 Answers 2


It seems rather pointless to include an image like this but anyway tex can allow boxes bigger than \maxdimen so long as you never do any arithmetic on the lengths:

enter image description here



\mbox{\pdfimage height \textheight{image.pdf}}

  • Thanks. There are few images out of over 55,000 images that have this problem. Most are small and cause no problem. Also pdf allows zooming so one can zoom to see the image if needed. The point is not to break the compilation each time new images are generated. So your solutions allows me to compile the pdf and not have to worry about breaking the build. But I would need it to fit to one page as before, even if it become too smal to see. But will try and see.
    – Nasser
    Commented Jul 27, 2015 at 0:53
  • @Nasser it would seem better to fix this at the generation side really. Commented Jul 27, 2015 at 0:57
  • I just tried your solution and the image does show on only ONE page, which is great! that is what I want.
    – Nasser
    Commented Jul 27, 2015 at 0:59
  • @Nasser what else could an image do but appear on one page? Commented Jul 27, 2015 at 1:00
  • 1
    @Sigur you should be able to surround the whole with a \clipbox from adjustbox, I would guess. Commented Nov 29, 2018 at 7:57

This is surely not the most efficient or elegant way but I was curious whether this might work.

bashful allows you to run scripts from within a LaTeX document provided, of course, that you compile with shell escape enabled.

Put the following in myimagewidth.sh:

#!/bin/sh -
pdfinfo $1.pdf  | grep "Page size" | sed -e 's/^Page size:[[:space:]]*//' -e 's/ x.*$//' -e 's/\..*//' -e 's/[^0-9]*//g'

and the following into myimageheight.sh:

#!/bin/sh -
pdfinfo $1.pdf  | grep "Page size" | sed -e 's/^Page size:[[:space:]]*.* x //' -e 's/\..*$//' -e 's/[^0-9]*//g'

and make them both executable.

chmod +x myimagewidth.sh myimageheight.sh

The following code assumes the scripts are located in the same directory as your .tex file. The scripts and code also assume that the PDF files you want to test are located in the same directory and are called enfys.pdf and tiger-eps-converted-to.pdf. Obviously, you'd want to adapt this for your own case!

./myimagewidth.sh enfys
./myimageheight.sh enfys
./myimagewidth.sh tiger-eps-converted-to
./myimageheight.sh tiger-eps-converted-to

You must log in to answer this question.

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