This is the second stage of my journey to find the best way to get a cropped PDF and EPS graphics. You can find the first stage in Searching for a better workflow to get a cropped PDF and EPS graphics - Stage 1 [1].
I need your suggestion which method do you propose and explain the underlying consideration.
psgrid
for the final result.This method will use the following batch file named MethodA.bat
.
latex -interaction=nonstopmode %1
dvips -R -t unknown %1
ps2pdf -dAutoRotatePages#/None -dCompatibilityLevel#1.5 -dPDFSETTINGS#/prepress %1.ps %1-temp.pdf
pdfcrop --restricted --hires %1-temp %1.pdf
pdftops -level3 -eps %1.pdf
The input file does not specify the paper size. It is possible because the batch file above use pdfcrop
to trim the surrounding whitespace.
% methodA.tex -- paper size is not specified !
\documentclass[cmyk]{article}
\usepackage{pstricks,pst-func}
%\usepackage[paperwidth=4cm,paperheight=8cm,margin=0cm]{geometry}
\pagestyle{empty}
\parindent=0cm
\begin{document}
\psset{yunit=0.4cm,xunit=0.5cm}
\begin{pspicture*}(-4,-5)(4,15)
\psaxes[Dy=2,Dx=2,labelFontSize=\bfseries\tiny,mathLabel=false]{<->}(0,0)(-4,-5)(4,15)
\psset{linewidth=1pt,linecolor=blue}
\psPolynomial[coeff=-1 0 1,linecolor=red]{-4}{4}
\psPolynomial[coeff=-1 0 1,xShift=1,linestyle=dashed]{-4}{4}
\rput[rt](3,12){\textcolor{red}{$f(x)$}}
\rput[lt](2.5,1){\textcolor{blue}{$g(x)$}}
\psframe(-4,-5)(4,15)
\end{pspicture*}
\end{document}
Batch file named MethodB.bat
. It does not use pdfcrop
to save CPU time.
latex -interaction=nonstopmode %1
dvips -R -t unknown %1
ps2pdf -dAutoRotatePages#/None -dCompatibilityLevel#1.5 -dPDFSETTINGS#/prepress %1.ps
pdftops -eps %1.pdf
And the input file needs to specify the paper size exactly.
% methodB.tex -- paper size must be specified exactly !
\documentclass[cmyk]{article}
\usepackage{pstricks,pst-func}
\usepackage[paperwidth=4cm,paperheight=8cm,margin=0cm]{geometry}
\pagestyle{empty}
\parindent=0cm
\begin{document}
\psset{yunit=0.4cm,xunit=0.5cm}
\begin{pspicture*}(-4,-5)(4,15)
\psaxes[Dy=2,Dx=2,labelFontSize=\bfseries\tiny,mathLabel=false]{<->}(0,0)(-4,-5)(4,15)
\psset{linewidth=1pt,linecolor=blue}
\psPolynomial[coeff=-1 0 1,linecolor=red]{-4}{4}
\psPolynomial[coeff=-1 0 1,xShift=1,linestyle=dashed]{-4}{4}
\rput[rt](3,12){\textcolor{red}{$f(x)$}}
\rput[lt](2.5,1){\textcolor{blue}{$g(x)$}}
\psframe(-4,-5)(4,15)
\end{pspicture*}
\end{document}
pdfcrop
takes a significant amount of time. For a web-based system, it really hurts a system performance.pdfcrop
cannot crop a valuable object when the object is surrounded by a non-white background. See the pdfcrop author's confirmation in question:
pdfcrop fails to crop a PDF document with non-white background
[1] for the details.pdfcrop
might produce a bad cropping as follows.
input file to be cropped by pdfcrop
:
% cropped-n.tex
\documentclass{minimal}
\parindent=0pt
\begin{document}
$\displaystyle n$%
\end{document}
main input file:
% comparator.tex
\documentclass{minimal}
\usepackage{graphicx}
\begin{document}
$\displaystyle n$ \includegraphics{cropped-n}
\end{document}
output: It is difficult for normal eyes to detect the defect unless we magnify it 6400 times that is the maximum magnification scale provided by Adobe Reader.
As long as the valuable object is surrounded by a white background, pdfcrop
can trim the excessive white spaces. Thus we don't need to specify manually and exactly a tight paper size in advance. Using pdfcrop
is practically very useful when we don't know the tight paper size in advance because of glyph issues that exist such as in the following examples.
% example-1.tex
\documentclass{minimal}
\usepackage{xcolor}
\usepackage[a5paper]{geometry}
\parindent=0bp
\begin{document}
$\displaystyle E=mc^2$
\end{document}
or
% example-2.tex
\documentclass{minimal}
\usepackage{pstricks}
\usepackage[a5paper]{geometry}
\parindent=0bp
\begin{document}
\begin{pspicture}[showgrid](2,3)
\end{pspicture}
\end{document}
We need to specify manually a paper size in advance. The paper size can be specified exactly if there is no glyph issue, otherwise we must find a padding constant manually by trial and error. Here are the workarounds for the examples given in Pros of Method A:
% workaround-for-example-1.tex
\documentclass{minimal}
\newlength\Left
\newlength\Right
\newlength\Bottom
\newlength\Top
% there are 4 magic numbers obtained by trial & errors.
\setlength{\Left}{-0.420bp}
\setlength{\Right}{-0.945bp}
\setlength{\Bottom}{0.093bp}
\setlength{\Top}{0.155bp}
\newsavebox\IBox
\savebox\IBox
{%
\raisebox{\dimexpr\depth+\Bottom\relax}
[\dimexpr\totalheight+\Bottom+\Top\relax]
{\hspace\Left$\displaystyle E=mc^2$\hspace\Right}
}
\paperwidth=\wd\IBox
\paperheight=\ht\IBox
\voffset=-1in
\hoffset=-1in
\topskip=0bp
\special{papersize=\the\paperwidth,\the\paperheight}
\parindent=0bp
\begin{document}
\usebox{\IBox}
\end{document}
and
% workaround-for-example-2.tex
\documentclass{minimal}
\usepackage{pstricks}
\newlength\padding
\newlength\Width
\newlength\Height
% it is a magic number obtained by a trial and error.
% it is not necessary to make it so tight because it will be set to 0 at the final.
\setlength{\padding}{8.4bp}% set it to 0 if you turn off the grid.
\setlength{\Width}{2cm}
\setlength{\Height}{3cm}
\paperwidth=\dimexpr2\padding+\Width\relax
\paperheight=\dimexpr2\padding+\Height\relax
\topskip=0bp
\voffset=\dimexpr\padding-1in\relax
\hoffset=\dimexpr\padding-1in\relax
\special{papersize=\the\paperwidth,\the\paperheight}
\parindent=0bp
\begin{document}
\begin{pspicture}[showgrid](\Width,\Height)
\end{pspicture}
\end{document
Using the following batch file, we can compile workaround-for-example-1.tex
and workaround-for-example-2.tex
faster. We also don't need to specify too many switches such as embedding fonts, etc.
rem simplified batch for method B.
latex %1
dvips -t unknown %1
gswin32c -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=%1.pdf %1.ps
pdftops -eps %1.pdf
\pagecolor{<non-white-color>}
.Good bye pdfcrop
...
pdfcrop
"cannot do cropping when the page background color is set to any color other than white". \documentclass{minimal} \usepackage{xcolor} \begin{document} \pagecolor{red} Hello \end{document}
- Lev Bishop
\pagecolor
- Lev Bishop
pdfTeX 3.1415926-1.40.11-2.2 (TeX Live 2010)
, XeTeX 3.1415926-2.2-0.9997.4 (TeX Live 2010
, LuaTeX, Version beta-0.60.2-2010071218
, PDFCROP 1.31, 2010/09/17
. I get a A4 PDF after cropping. - Martin Scharrer
pdfcrop
your temp PDF it is still A4. I think it is the underlying Ghostscript. Which version are you using? I have GPL Ghostscript 9.01 (2011-02-07)
. - Martin Scharrer
GPL Ghostscript 8.63 (2008-08-01)
. Here is pdfcrop --verbose --hires --restricted colorback-temp.pdf colorback.pdf
output: dl.dropbox.com/u/5560769/pdfcrop.log - Lev Bishop
%%BoundingBox: 0 0 612 792
%%HiResBoundingBox: 0.000000 0.000000 611.999981 791.999976
- Martin Scharrer
comp.text.tex
to make the author of pdfcrop
aware of it. However, there is not much you or he can do about it. Maybe I write a bug report for GPL ghostscript as well. - Martin Scharrer
comp.text.tex
link (took a few minutes to appear): groups.google.com/d/topic/comp.text.tex/sVM3FtWe95w/discussion - Martin Scharrer
\includegraphics
without white margins in another pdflatex file? - Martin