1

I am generating a latex file with many \includegraphics commands from a program. The image files change from run to run. The latex file might, for example, look like the one in this question. Occasionally an image file that I receive is corrupted. I can eliminate nearly all such situations by checking the size of the image file and emitting some vertical space instead of the \includegraphics command should the the image file be too small to reasonably be the sort of image I am expecting; however, a very small number of corrupted image files are larger. In that case pdflatex stops with an error message. All my image files are .jpg files. I believe that jpegtran on UNIX will fix most of these and I could pass every jpeg through that but I am concerned about the impact on runtime if I do that and also there could still be a very few that that does not address. Is there a way to handle this? Basically I need something like: "include a jpg file using includegraphics without generating an error and if that fails then emit some vertical space otherwise the includegraphics can stand".

user1189687
  • 1,953
  • Are your images all in the same folder? I would run some external script that can pick up the corrupted images and replace them with some standard faulty image (like mwe's example-image-a). – Werner Sep 21 '12 at 21:56
  • I could run jpegtran on them all in advance but its not really that convenient and also I am not sure that that would necessarily catch them all although it probably would catch most. I would prefer if all this were self contained in the report so I don't have to start messing with external processes. – user1189687 Sep 21 '12 at 22:00
  • TeX can't even adequately handle bounding box issues, so I don't think it would be able to detect corrupted JPGs internally/at compile time (let alone efficiently). – Werner Sep 21 '12 at 22:07
  • 1
    So there is no try...catch processing possible in tex? Seems like a serious omission. – user1189687 Sep 21 '12 at 22:55
  • You could write a wrapper macro around \includegraphics which uses a local \nonstopmode to make LaTeX keep going after an error. I'm not 100% sure if this will stop all driver errors as well. – Martin Scharrer Sep 22 '12 at 08:10
  • @Martin, Under Linux I tried running \documentclass{article} \nonstopmode \usepackage{graphicx} \begin{document} \includegraphics{corrupt.jpg} \end{document} with pdflatex -interaction=nonstopmode mytest.texbut it still stopped. The first line of output of pdflatex --version gives pdfeTeX 3.141592-1.21a-2.2 (Web2C 7.5.4); however, the same procedure did work under Windows Vista. In that case pdflatex --version gives MiKTeX-pdfTeX 2.8.3563 (1.40.10) (MiKTeX 2.8) . – user1189687 Sep 22 '12 at 13:46
  • I guess that's a pdftex issue, i.e. in the PDF part, not in the TeX part. Then it wouldn't technically be an TeX error, but a PDF format one. It would help if you post the full error message. – Martin Scharrer Sep 22 '12 at 14:36
  • @Martin, The error message was: Error: pdflatex (file corrupt.jpg): reading JPEG image failed ==> Fatal error occurred, the output PDF file is not finished! . Its as if nonstopmode had not been turned on. – user1189687 Sep 22 '12 at 14:50
  • Like I feared. That's a PDF specific error which isn't handled by the normal TeX layers at all. I don't think you can do anything about this from inside TeX. – Martin Scharrer Sep 22 '12 at 14:53
  • @Martin, Thanks. I have not tried this but this user found that changing versions fixed it: https://bbs.archlinux.org/viewtopic.php?id=77211 – user1189687 Sep 22 '12 at 15:08
  • @MartinScharrer Can you give an answer (in the negative, I fear)? – egreg Nov 03 '12 at 23:13
  • @egreg: Now done. – Martin Scharrer Nov 04 '12 at 10:39

1 Answers1

7

The error you get:

Error: pdflatex (file corrupt.jpg): reading JPEG image failed 
==> Fatal error occurred, the output PDF file is not finished!

Even with nonstopmode active indicates to me that this is a pdfTeX specific issue which occurs in the special PDF-related code of the binary which is responsible for reading JPEG files and not a real TeX error.

Therefore there seems to be nothing you can do in (La)TeX itself. If you can't manually sort out the corrupted files then I would try to write a script which compiles a small test document for each image and checks if the compilation was successful.

Martin Scharrer
  • 262,582