[Tex/LaTex] Using pdfcrop within epstopdf

epstopdfpdfcrop

Currently, my epstopdf is not able to convert eps to pdf correctly due to the boundingbox (atend) bug that the epstopdf does not support. However, I intend to use pdfcrop on the generated pdf file and that usually fix the white space problem. However, I have to manually fix every generated file. Is there a way to automate this in latex? For example, automatically calling pdfcrop immediately after epstopdf. I tried using DeclareGraphicsRule in epstopdf but it wasn't working.

Please advise. Thanks a lot ! 🙂

Best Answer

The following adds an option crop to graphicx/\includegraphics. If enabled, it adds pdfcrop to the conversion of .eps to .pdf.

Because pdfcrop is not added to the restricted shell escape command list, full shell escape is needed: --shell-escape (TeX Live) or --enable-write18 (MiKTeX).

If the file is converted and uptodate, changing the option crop does not have an effect. Also the option does not affect other image files.

\usepackage{graphicx}
\usepackage{epstopdf}

\makeatletter
\newif\ifGin@pdfcrop
\Gin@pdfcroptrue
\define@key{Gin}{crop}[true]{%
  \csname Gin@pdfcrop#1\endcsname
}
\epstopdfDeclareGraphicsRule{.eps}{pdf}{.pdf}{%
  epstopdf --outfile=%
  \ifGin@pdfcrop
    tmp-image.pdf #1 &&
    pdfcrop tmp-image.pdf \OutputFile
  \else
    \OutputFile\space#1%
  \fi
}
\makeatother

Example usages:

\includegraphics{foo1}
\includegraphics[crop]{foo2}
\includegraphics[crop=false]{foo3}
\setkeys{Gin}{crop}
\setkeys{Gin}{crop=false}