I would do it another way around: (This might actually solve some other issues of you)
Place the whole thing in a savebox in the preamble. (You have to manually enable the normal font use \normalfont
for this AFAIK.) Then you can measure the dimensions of the box and set the page dimensions accordantly. This will give you a PDF or PS which already has to correct size. For larger content I recommend to use a minipage
wrapper as usual.
The \pagecolor
command still works here. I would have thought you manually need to place a colored rectangle (\rule
) with the same size behind the content.
The following works for me using pdflatex
(PDF), latex
->dvips
1 (PS), latex
->dvips
1->ps2pdf
(PDF), xelatex
and lualatex
and produces a correctly sized file.
1 without any options, no -E
required
\documentclass{article}
\usepackage[cmyk]{xcolor}
\pagecolor{cyan}
\pagestyle{empty}
\parindent=0bp
\begin{lrbox}{0}%
\normalfont
\fbox{$\displaystyle E=mc^2 $}%
\end{lrbox}
\sbox0{\raise\dp0\box0}% raise box so it is all height, no depth
%\sbox0{\rlap{\textcolor{cyan}{\rule{\wd0}{\ht0}}}\box0}% \pagecolor surprisingly works so this is not needed
\usepackage[noheadfoot,nomarginpar,margin=0pt,paperwidth=\wd0,paperheight=\ht0]{geometry}
\begin{document}
\box0
\end{document}
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}
Best Answer
When the shell escape feature (
--shell-escape
or--enable-write18
(MiKTeX)) is enabled, the conversion can be called via\immediate\write18{...}
:The conversion can be limited to the cases, where the cropped image file not yet exists: