8

I'm writing a document which has many plots with quite a lot of data points. Those plots are stored as .pdf vector images and their rendering takes a lot of time. So every time I stumble upon a page with one of those, my editor freezes for a solid minute.

I know that graphicsx has a draft option but that hides the images which is not a very good solution since then I can't tell the images apart just by the filename. Those are results of experimental runs with quite generic names.

Is there perhaps a way to rasterize the images (e.g. into .png) in draft mode and use the original .pdf in final mode? I know that I could use ImageMagick or whatever external tools to do the conversion but that seems like a lot of extra work since I would need to add every image to a makefile and duplicate the \includegraphics for draft and final.

There are some parameters of graphicsx such as type or command which are not explained in the documentation. Could this perhaps be achieved using one of those? Could I perhaps hook the \includegraphics and call an external command? I would manage writing the command but I have no Idea how hooks work in LaTeX.

Otherwise, I'm well aware of the fact that raster graphics are inferior to vector graphics as far as the quality is concerned. I'm going to use vector graphics in the final version. This is intended solely for quick draft preview.

  • It may be possible to reduce the size of the vector images without affecting their quality. What software produced them? – Ian Thompson Mar 22 '16 at 18:58
  • @IanThompson Those were made with MS Excel. I was already thinking about down-sampling the data series but that would also be a bit laborous. – the swine Mar 22 '16 at 19:00
  • texdoc graphicx page 10 lists both type and command keys. Which documentation did you use? – David Carlisle Mar 22 '16 at 19:06
  • Not a TeX solution, but may serve. Can you prepare one document with all the images and the filenames, then scroll in that one to see relevant figures when editing and compiling the text in graphicx draft mode? – Ethan Bolker Mar 22 '16 at 19:16
  • 1
    There is a utility called polygone which can sometimes reduce the size of eps files by a substantial percentage. I've never tried it with files from Excel, though. – Ian Thompson Mar 22 '16 at 19:22
12

The search path for the images can be changed:

  • \graphicspath with a directory for PDF images or it counterparts, the PNG images.
  • \DeclareGraphicsExtensions can be used to change the order, which extension is used first, when the image file was given without extension. Package grfext provides an interface for changing the setting.

Package epstopdf can be used to call the conversion from PDF to PNG automatically, if pdfTeX or luaTeX is used and the shell escape feature is enabled.

Example:

If \mydraft is defined to be Y (\let\mydraft=Y), then the rule for the extension .pdf is overwritten to make a conversion to .png via ImageMagick's convert. Otherwise the .pdf extension is preferred.

\documentclass{article}
\usepackage{graphicx}
\usepackage{grfext}

\ifx\mydraft Y
  \usepackage{epstopdf-base}
  \epstopdfDeclareGraphicsRule{.pdf}{png}{.png}{%
    convert #1 \OutputFile
  }  
  \epstopdfDeclareGraphicsRule{.PDF}{png}{.png}{%
    convert #1 \OutputFile
  }  
\else
  \PrependGraphicsExtension*{.pdf}
\fi

\begin{document}
\includegraphics{image}
\end{document}

The special draft mode is enabled by \let\mydraft=Y, which can also be given on the command line, e.g.:

pdflatex --shell-escape "\let\mydraft=Y\input{mydocument}"

Then image.pdf is converted and included as image-pdf-converted-to.png.

Otherwise, image.pdf is included, even if image.png lies around.

Key for \includegraphics

The conversion feature can also be limited to \includegraphics by use of a key pdfpng. The key code changes the graphics rule for .pdf files to convert them to PNG. If \mydraft is not Y, then the key is a no-op.

\documentclass{article}
\usepackage{graphicx}
\usepackage{grfext}

\ifx\mydraft Y
  \usepackage{epstopdf-base}
  \csname define@key\endcsname{Gin}{pdfpng}[]{%
    \epstopdfDeclareGraphicsRule{.pdf}{png}{.png}{%
      convert ##1 \OutputFile
    }%
  }
\else
  \PrependGraphicsExtensions*{.pdf}
  \csname define@key\endcsname{Gin}{pdfpng}[]{}
\fi

\begin{document}
\includegraphics{image1}
\includegraphics[pdfpng]{image2-converted-to-png-when-mydraft}
\includegraphics{image3}
\end{document}

(\define@key can be used instead of \csname define@key\endcsname, when \makeatletter is active.)

  • Wow, tremendous solution I must say! I have one issue though, the tex is using the new file names but I'm getting Package pdftex.def Error: File 'figs/example_matrix-pdf-converted-to.png' not found. And the file is really not there. I have made sure that convert is in the path. Any ideas what might be the problem? Maybe some unwanted interaction with other packages? I'm using classicthesis. Or did I misunderstand and do I have to do the conversions manually? – the swine Mar 22 '16 at 21:32
  • Ah ha, I'm using miktex so rather than the mandatory --shell-escape, I need to use --enable-write18. – the swine Mar 22 '16 at 21:51
  • Just wondering, is there a way to enable this only for selected images? I was thinking along the lines of making a dummy extension (e.g. .df2ng) and then using one of ext, type or read to trigger the conversion (the file would still be .pdf same as the others), only I can't seem to get that to work. – the swine Mar 22 '16 at 22:20
  • @theswine See updated answer. – Heiko Oberdiek Mar 22 '16 at 23:42
  • To whom it may concern, some of the pdfs I had were not of standard page size and imagemagick was failing on them. So I made a script called convert.bat and put it next to the .tex file to be picked up first and in it I call gs as gswin32c -o "%2" -dFirstPage=1 -dLastPage=1 -sDEVICE=pngalpha -r72x72 -dUseCropBox -dFitPage "%1" -dBATCH -dNOPAUSE. The key flags here are dUseCropBox -dFitPage which need to be on the command line before the input file. – the swine Mar 23 '16 at 16:41

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.