2

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

\documentclass[10pt,notitlepage]{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=\textwidth,height=\textheight,keepaspectratio]{image}
\end{document}

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

Update:

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

>pdfinfo -box image.pdf
Creator:        Wolfram Mathematica 10.1.0.0 for Microsoft Windows (64-bit) (March 23, 2015) Student Edition - Personal Use Only
Producer:       
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.

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

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

\documentclass[10pt,notitlepage]{article}
\usepackage{graphicx}

\begin{document}


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

\end{document}
  • 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 Jul 27 '15 at 0:53
  • @Nasser it would seem better to fix this at the generation side really. – David Carlisle Jul 27 '15 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 Jul 27 '15 at 0:59
  • @Nasser what else could an image do but appear on one page? – David Carlisle Jul 27 '15 at 1:00
  • 1
    @Sigur you should be able to surround the whole with a \clipbox from adjustbox, I would guess. – David Carlisle Nov 29 '18 at 7:57
1

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!

\documentclass{article}
\usepackage{graphicx,bashful}
\newcounter{myimageheight}
\newcounter{myimagewidth}
\newcommand*\myincludegraphics[2][]{%
  \stepcounter{myimagewidth}%
  \stepcounter{myimageheight}%
  \ifnum16384>\value{myimagewidth}%
  \ifnum16384>\value{myimageheight}%
  \includegraphics[#1]{#2}%
  \fi
  \fi}
\begin{document}
\bash
./myimagewidth.sh enfys
\END
\setcounter{myimagewidth}{\bashStdout}
\bash
./myimageheight.sh enfys
\END
\setcounter{myimageheight}{\bashStdout}%
\myincludegraphics[width=\textwidth,height=\textheight,keepaspectratio]{enfys}
\bash
./myimagewidth.sh tiger-eps-converted-to
\END
\setcounter{myimagewidth}{\bashStdout}
\bash
./myimageheight.sh tiger-eps-converted-to
\END
\setcounter{myimageheight}{\bashStdout}%
\myincludegraphics{tiger-eps-converted-to}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.