Creating standalone eps files and using batch files to automate the process

I have been looking at these posts

How to fix EPS with incorrect bounding box?

Cannot determine size of graphic in * (no BoundingBox)

How to convert PDF to EPS?

How to produce EPS instead of PS using latex.exe followed by dvips?

in order to understand how to create a standalone pdf from an eps figure and how to get an eps file also from an eps in which the psfrag labels have not yet been replaced. The above posts have been most useful.

My eps figures are usually done with Adobe Illustrator (AI) and then I use psfrag to replace whatever original labels in the AI figure to whatever I need with laTeX fonts. It works perfectly.

The issue with having such a workflow is that the original eps file from AI doesn't have the correct labels yet. This means that I cannot send someone this eps file from AI. Based on the above-mentioned posts, I have created a workflow that appears good to me in order to generate an independent eps file with all psfrag labels correctly inserted. My objective is to still have all fonts embedded in the final eps file. Here is my workflow with reference to the file jeffcott.eps that I have stored here:

http://petitlien.fr/epspicture

(A). Check the bounding box of jeffcott.eps

gswin32c -dNOPAUSE -dBATCH -q -sDEVICE=bbox jeffcott.eps
%%BoundingBox: 0 0 373 190
%%HiResBoundingBox: 0.000000 0.000211 372.617989 189.934025


(B). Convert to standalone pdf

\documentclass[convert]{standalone}
\usepackage[hiresbb,dvips]{graphicx}
\usepackage[dvips]{color}
\usepackage{psfrag}
\begin{document}
\psfrag{A}{$m$}
\psfrag{B}{$k,c$}
\psfrag{Y}{inner stator bore}
\includegraphics{E:/jeffcott.eps}
\end{document}


(C). Create an eps file from the above resulting pdf file

pdfcrop --hires "standpdf.pdf" "standpdf-temp.pdf"


Next issue the pdftops command:

pdftops -level3 -eps standpdf-temp.pdf standpdf.eps


Now, standpdf.eps is created but Ghostscript warns that it cannot find its bbox. The problem is that the lines %%BoundingBox and %%HiResBoundingBox appear quite far away from the top header lines. I have two solutions:

I: Move these two lines corresponding to the bbox just after the first header line

II: Use ps2eps

ps2eps -f --fixps standpdf.eps


The file standpdf.eps.eps is created by ps2eps and Ghostscript can load the file without any complaint.

Checking the bbox gives:

 gswin32c -dNOPAUSE -dBATCH -q -sDEVICE=bbox standpdf.eps.eps
%%BoundingBox: 0 0 373 190
%%HiResBoundingBox: 0.000000 0.000352 372.617989 189.935994


However when I open the file standpdf.eps.eps in a text editor, I can see the bbox as

 %%BoundingBox: 0 1 372 190
%%HiResBoundingBox: 0.000000 1.000000 372.000000 190.000000


My questions:

1. As you can see from part (c) above, Ghostscript doesn't always read the correct bbox of the eps file. Why is this so?

2. Is it right to use the command ps2eps on an eps file. I have tried to use epstool but it seems that this tool can only copy a bbox from one file to another, and not fix it.

3. I would like to create a batch file on a Windows machine to automate the task in parts (B) and (C) and deleting the temporary file standpdf-temp.pdf along the way. I have looked at the batch files listed in some of the posts mentioned at the beginning of this post, but they do not work.

4. As a step further to Question 3, I would like to create a batch file that will process all the pdf files in a folder and create the corresponding eps files. How to do this?

5. And a related question: Where to learn about creating batch files for Windows? I haven't found any comprehensive source of information yet.

Update:

The batch files provided by xport work fine. There is one small bug however. Looking at BatchF.bat, after processing with ps2eps, the lines that come afterwards do not get processed at all. Ps2eps throws control back to the command prompt but the batch file stops there. How to fix that?

Since this post is getting long, I have created a new post:

Batch files and making the workflow with pdfLaTeX and TeXLive 2010 easier with psfrag

Thank you.

-
I'm so happy that Till Tantau created TikZ! I love pdflatex and the fact that I do not have these issues any longer. –  Martin Scharrer Jul 18 '11 at 9:00
"Ghostscript doesn't always read the correct bbox of the eps file" The -sDEVICE=bbox calculates the bounding box not reads them from the file, so it should be more correct. –  Martin Scharrer Jul 18 '11 at 9:03
The problem with gs complaining it can't find the bbox is an error in the eps from pdftops -eps. You can fix it by piping through sed '2s/^/%/' to double the % at the beginning of the second line. –  Lev Bishop Jul 18 '11 at 16:21
Thanks. Could you be a bit clearer when you mention the piping part. How to achieve this on windows? –  yCalleecharan Jul 19 '11 at 6:54
@yCall: see my update! –  xport Jul 19 '11 at 15:55

I will provide you with 6 batch files. Each takes a (La)TeX input file name without extension. It will compile a single (La)TeX input file to produce an output in both PDF and EPS. In development phase, you may compile the same (La)TeX input file with each of the 6 batch files, and investigate which one suits you best. Each of the 6 batch files processes a single (La)TeX input file using different steps internally. The description will be clearer later in the next explanation.

In the production phase, you should have decided which one among them will be applied on a bunch of (La)TeX input files you have. Since applying the batch file on a bunch of (La)TeX input files is very tedious job, I provide you with an additional batch file to automate the process. Thus there are 7 batch files in total.

BatchA.bat

rem batchA.bat
rem echo off
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
rem acrord32 %1.pdf
del %1.log
del %1.aux
del %1.dvi
del %1.ps
del %1-temp.pdf

1. It can be used for an input file with any paper size.
2. Steps: TEX -> DVI -> PDF -> cropping -> EPS.
3. pdfcrop takes a significant amount of time to do cropping. If you don't need cropping, don't use this batch file.

BatchB.bat

rem batchB.bat
echo off
latex -interaction=nonstopmode %1
dvips -R -t unknown %1
ps2pdf -dAutoRotatePages#/None -dCompatibilityLevel#1.5 -dPDFSETTINGS#/prepress %1.ps
pdftops -level3 -eps %1.pdf
rem acrord32 %1.pdf
del %1.log
del %1.aux
del %1.dvi
del %1.ps

1. It can be used only for an input file with tight paper size.
2. Steps: TEX -> DVI -> PDF -> EPS.
3. It runs faster than batchA.bat because no cropping with pdfcrop. The drawback is you must specify the paper size tightly.

BatchC.bat

rem batchC.bat
echo off
latex %1
dvips -t unknown %1
gswin32c -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=%1.pdf %1.ps
pdftops -eps %1.pdf
rem acrord32 %1.pdf
del %1.log
del %1.aux
del %1.dvi
del %1.ps

1. It is almost the same as the batchB.bat but with fewer switches to speed up the compilation.
2. It can be used only for an input file with tight paper size.
3. Steps: TEX -> DVI -> PDF -> EPS.
4. It runs faster than batchA.bat because no cropping with pdfcrop. The drawback is you must specify the paper size tightly.

BatchD.bat

rem batchD.bat
echo off
tex %1
dvips -t unknown %1
gswin32c -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=%1.pdf %1.ps
pdftops -eps %1.pdf
rem acrord32 %1.pdf
del %1.log
del %1.aux
del %1.dvi
del %1.ps

1. It is almost the same as the batchC.bat but using tex.exe instead of latex.exe. The input file must be in plain TeX format. I hope it becomes faster than methodC.bat, but it has not been tested yet as I have many troubles to convert the input file from LaTeX to plain TeX. Benchmarking will be done soon.
2. It can be used only for an input file with tight paper size.
3. Steps: TEX -> DVI -> PDF -> EPS.
4. It runs faster than batchA.bat because no cropping with pdfcrop. The drawback is you must specify the paper size tightly.

BatchE.bat

rem batchE.bat
rem echo off
latex -interaction=nonstopmode %1
dvips -R -t unknown -E %1 -o %1-temp.eps
epstool --copy --bbox %1-temp.eps %1.eps
epstopdf --hires %1.eps
rem acrord32 %1.pdf
del %1.log
del %1.aux
del %1.dvi
del %1-temp.eps

1. It can be used for an input file with any paper size.
2. Steps: TEX -> DVI -> EPS -> bounding box correction -> PDF.
3. The file size of the resulting EPS is larger than that of one produced by each of the first 4 batch files (a,b,c,d).

BatchF.bat

rem batchF.bat
echo off
latex -interaction=nonstopmode %1
dvips -R -t unknown %1 -o %1-temp.ps
ps2eps %1-temp.ps
epstool --copy --bbox %1-temp.eps %1.eps
epstopdf --hires %1.eps
rem acrord32 %1.pdf
del %1.log
del %1.aux
del %1.dvi
del %1-temp.ps
del %1-temp.eps

1. It can be used for an input file with any paper size.
2. Steps: TEX -> DVI -> PS -> EPS -> bounding box correction -> PDF.
3. The file size of the resulting EPS is larger than that of one produced by each of the first four batch files (a,b,c,d).
4. I forgot the benchmark result when comparing batchE.bat and batchF.bat.

Automate.bat

rem automate.bat
rem it takes a single character from {a,b,c,d,e,f}.
rem the options are case-insensitive.
rem for example: automate a
rem another example: automate F
echo off
for %%x in (*.tex) do batch%1.bat %%~nx
pause


If you have any problem, drop a comment.

Update in response to your misuse.

Each of {batchA.bat,batchB.bat,batchC.bat,batchD.bat,batchE.bat,batchF.bat} takes one input file name without extension. For example, if you want to use batchE.bat for helloworld.tex, then you must type batchE helloworld and hit enter.

If you want to use batchE.bat for a bunch of input files that exists in the same directory in which all batch files exist, then you type automate E and hit enter.

-
For the sake of simplicity, place all batch files in the same directory in which your (La)TeX input files exist. –  xport Jul 18 '11 at 16:49
Be careful: My batch files above only accept file names without spaces. If your input file names contain spaces then encloses any file place holder in the batch with double quotes, for example: "%1-temp.pdf". –  xport Jul 18 '11 at 16:57
Thank you for your extensive writing and help. I have run BatchA.bat but it doesn't work properly. I get some eps and pdf outputs but they are blank files. I have put the output up in my post so that you can see where things are going bad and possibly tell me what Im doing wrong. Thanks –  yCalleecharan Jul 19 '11 at 6:25
@yCal: see my update! –  xport Jul 19 '11 at 15:55
Thanks. Yes I figured this out some hours afterwards when I read your answer again. So I have cleaned my post, taking away irrelevant info. I have another question about the ps2eps tool that makes any lines that come afterwards in the batch file useless. how to fix this? More in my post. –  yCalleecharan Jul 20 '11 at 4:53

One nice tool for such things is inkscape which is a vector graphic editor but can also be run on the command line. It comes for Linux, Max and Windows. However, it uses additional software like Ghostscript by itself.

If you using it -D option then the file is groped down to the drawing. It can export EPS and PDF as well as SVG which is nice for websites.

The steps would be:

latex file
dvips file
inkscape -D --export-eps file.eps file.ps


This gives me a nice groped EPS file from an A4 document with a single drawing in it. If you replace --export-eps with --export-pdf you get PDF output.

-
the eps export of inkscape is a lousy one ... Using dvips with the -E option and epstool makes more sense. –  Herbert Jul 18 '11 at 13:38

How to create a batch file

Please skim the following example first and try to understand without reading my explanation that will follow.

rem this is a batch file named xport.bat
rem it need a single argument which is a name of TeX input file.
rem don't include the trailing .tex please!
echo off
latex %1
dvips -t unknown %1
gswin32c -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=%1.pdf %1.ps
pdftops -eps %1.pdf
acrord32 %1.pdf
del %1.log
del %1.aux
del %1.dvi
del %1.ps
rename %1.pdf xport.pdf


Explanation:

1. A batch file may take optional arguments. If you execute xport.bat with an argument as follows,

xport mytexinputfilewithoutextension


the %1 will be set to mytexinputfilewithoutextension.

2. rem is a batch keyword to make a comment. It is analogous to % in TeX.

3. echo off is used not to display the syntax of command being executed.

4. latex %1 stands for executing latex on %1 file which is myinputfilewithoutextension. latex and dvips do not need file extension, but gswin32c, pdftops and acrord32, del, rename do need the file extension.

5. del %1.log is used to delete a file %1.log.

6. And rename %1.pdf xport.pdf is used to rename %1.pdf to xport.pdf.

-
Again: the correct language marker is <!-- language: lang-none --> with *no space between language and the :. With the space it doesn't work. –  Martin Scharrer Jul 18 '11 at 9:53