summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/pstool/pstool.sty
blob: 89fd0b24ff9b53d2cdd0902a51fbe2a6cd3793a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
% TODO: convert this package into expl3 syntax (will save many lines of code).

\ProvidesPackage{pstool}[2013/03/11 v1.5b
  Wrapper for processing PostScript/psfrag figures]

% External packages:
\RequirePackage{
  catchfile,color,ifpdf,ifplatform,filemod,
  graphicx,psfrag,suffix,trimspaces,xkeyval,expl3
}

% Add an additional command before trimspaces.sty is updated formally:
\providecommand*{\trim@multiple@spaces@in}[1]{%
  \let\trim@temp#1%
  \trim@spaces@in#1%
  \ifx\trim@temp#1%
  \else
    \expandafter\trim@multiple@spaces@in\expandafter#1%
  \fi
}

% \subsection{Allocations}
\expandafter\newif\csname if@pstool@pdfcrop@\endcsname
\expandafter\newif\csname if@pstool@verbose@\endcsname
\expandafter\newif\csname if@pstool@write@aux\endcsname

\newwrite\pstool@out
\newread\pstool@mainfile@ior
\newread\pstool@auxfile@ior

% Macro used to store the name of the graphic's macro file:
\let\pstool@tex\@empty

% \subsection{Package options}

\define@choicekey*{pstool.sty}{crop}
    [\@tempa\@tempb]{preview,pdfcrop}{%
  \ifcase\@tempb\relax
    \@pstool@pdfcrop@false
  \or
    \@pstool@pdfcrop@true
  \or
  \fi
}

\define@choicekey*{pstool.sty}{process}
    [\@tempa\pstool@process@choice]{all,none,auto}{}
\ExecuteOptionsX{process=auto}

\define@choicekey*{pstool.sty}{mode}
    [\@tempa\@tempb]{errorstop,nonstop,batch}{%
  \ifnum\@tempb=2\relax
    \@pstool@verbose@false
  \else
    \@pstool@verbose@true
  \fi
  \edef\pstool@mode{\@tempa mode}%
}
\ExecuteOptionsX{mode=batch}

\DeclareOptionX{cleanup}{\def\pstool@rm@files{#1}}
\ExecuteOptionsX{cleanup={.tex,.dvi,.ps,.pdf,.log}}

\DeclareOptionX{suffix}{\def\pstool@suffix{#1}}
\ExecuteOptionsX{suffix={-pstool}}

% There is an implicit \cs{space} after the bitmap options.
\define@choicekey*{pstool.sty}{bitmap}
    [\@tempa\@tempb]{auto,lossless,lossy}{%
  \ifcase\@tempb
    \let\pstool@bitmap@opts\@empty
  \or
    \def\pstool@bitmap@opts{%
      -dAutoFilterColorImages=false
      -dAutoFilterGrayImages=false
      -dColorImageFilter=/FlateEncode
      -dGrayImageFilter=/FlateEncode % space
    }
  \or
    \def\pstool@bitmap@opts{%
      -dAutoFilterColorImages=false
      -dAutoFilterGrayImages=false
      -dColorImageFilter=/DCTEncode
      -dGrayImageFilter=/DCTEncode % space
    }
  \fi
}
\ExecuteOptionsX{bitmap=lossless}

\DeclareOptionX{latex-options}{\def\pstool@latex@opts{#1}}
\DeclareOptionX{dvips-options}{\def\pstool@dvips@opts{#1}}
\DeclareOptionX{ps2pdf-options}{\def\pstool@pspdf@opts{#1}}
\DeclareOptionX{pdfcrop-options}{\def\pstool@pdfcrop@opts{#1}}

\ExecuteOptionsX{
  latex-options={},
  dvips-options={},
  ps2pdf-options={-dPDFSETTINGS=/prepress},
  pdfcrop-options={}
}

\DeclareOptionX{macro-file}{%
  \IfFileExists{#1}
    {\def\pstool@macrofile{#1}}
    {%
      \let\pstool@macrofile\@empty
      \PackageError{pstool}{^^J\space\space%
      No file `#1' found for "macro-file" package option.^^J
      This warning occurred}
    }
}

% Default:
\IfFileExists{\jobname-pstool.tex}
  {\edef\pstool@macrofile{\jobname-pstool.tex}}
  {\let\pstool@macrofile\@empty}


\ifpdf
  \ifshellescape\else
    \ExecuteOptionsX{process=none}
    \PackageWarning{pstool}{^^J\space\space%
      Package option [process=none] activated^^J\space\space
      because -shell-escape is not enabled.^^J%
      This warning occurred}
  \fi
\fi

\ProcessOptionsX

% A command to set \pkg{pstool} options after the package is loaded.
\newcommand\pstoolsetup{%
  \setkeys{pstool.sty}%
}

% \section{Macros}
% Used to echo information to the console output.
% Can't use \cs{typeout} because it's asynchronous with
% any \cs{immediate}\cs{write18} processes (for some reason).
\def\pstool@echo#1{%
  \if@pstool@verbose@
    \pstool@echo@verbose{#1}%
  \fi
}

\def\pstool@echo@verbose#1{%
  \immediate\write18{echo "#1"}%
}

\let\pstool@includegraphics\includegraphics

% Command line abstractions between platforms:
\edef\pstool@cmdsep{\ifwindows\string&\else\string;\fi\space}
\edef\pstool@rm@cmd{\ifwindows del \else rm -- \fi}
\edef\pstool@cp@cmd{\ifwindows copy \else cp -- \fi}

% Delete a file if it exists:
% #1: path
% #2: filename
\newcommand\pstool@rm[2]{%
  \IfFileExists{#1#2}{%
    \immediate\write18{%
      cd "#1"\pstool@cmdsep\pstool@rm@cmd "#2"
    }%
  }{}%
}

% Generic function to execute a command on the shell and pass its exit status back into \LaTeX. Any number of \cmd\pstool@exe\ statements can be made consecutively followed by \cmd\pstool@endprocess, which also takes an argument. If \emph{any} of the shell calls failed, then the execution immediately skips to the end and expands \cmd\pstool@error\ instead of the argument to \cmd\pstool@endprocess.
% #1: `name' of process
% #2: relative path where to execute the command
% #3: the command itself
\newcommand\pstool@exe[3]{%
  \pstool@echo{^^J=== pstool: #1 ===}%
  \pstool@shellexecute{#2}{#3}%
  \pstool@retrievestatus{#2}%
  \ifnum\pstool@status > \z@
    \PackageWarning{pstool}{%
      Execution failed during process:^^J\space\space
      #3^^JThis warning occurred%
    }%
    \expandafter\pstool@abort
  \fi
}

% Edit this definition to print something else when graphic processing fails.
\def\pstool@error{%
  \fbox{%
    \parbox{0.8\linewidth}{%
      \color{red}\centering\ttfamily\scshape\small
      An error occured processing graphic:\\
      \upshape`%
        \detokenize\expandafter{\pstool@path}%
        \detokenize\expandafter{\pstool@filestub}.eps%
      '\\\bigskip
      \tiny
      Check the log file for compilation errors:\\
      `%
        \detokenize\expandafter{\pstool@path}%
        \detokenize\expandafter{\pstool@filestub}-pstool.log%
      '\\
    }%
  }%
}

\def\pstool@abort#1\pstool@endprocess{\pstool@error\@gobble}
\let\pstool@endprocess\@firstofone

% It is necessary while executing commands on the shell to write the exit status to a temporary file to test for failures in processing. (If all versions of \texttt{pdflatex} supported input pipes, things might be different.)
\def\pstool@shellexecute#1#2{%
  \immediate\write18{%
    cd "#1" \pstool@cmdsep
    #2 \pstool@cmdsep
    \ifwindows
       call echo
         \string^\@percentchar ERRORLEVEL\string^\@percentchar
    \else
       echo \detokenize{$?}
    \fi
    > \pstool@statusfile}%
% That's the execution; now we need to flush the write buffer to the status file. This ensures the file is written to disk properly (allowing it to be read by \cmd\CatchFileEdef). Not necessary on Windows, whose file writing is evidently more crude/immediate.
  \ifwindows\else
    \immediate\write18{%
      touch #1\pstool@statusfile}%
  \fi
}
\def\pstool@statusfile{pstool-statusfile.txt}

% Read the exit status from the temporary file and delete it.
% #1 is the path
% Status is recorded in \cmd\pstool@status.
\def\pstool@retrievestatus#1{%
  \CatchFileEdef{\pstool@status}{#1\pstool@statusfile}{}%
  \pstool@rm{#1}{\pstool@statusfile}%
  \ifx\pstool@status\pstool@statusfail
    \PackageWarning{pstool}{%
      Status of process unable to be determined:^^J  #1^^J%
      Trying to proceed... }%
    \def\pstool@status{0}%
  \fi
}
\def\pstool@statusfail{\par }% what results when \TeX\ reads an empty file

% Grab filename and filepath. Always need a relative path to a filename even if it's in the same directory.
\def\pstool@getpaths#1{%
  \filename@parse{#1}%
  \ifx\filename@area\@empty
    \def\pstool@path{./}%
  \else
    \let\pstool@path\filename@area
  \fi
  \let\pstool@filestub\filename@base
}

% The filename of the figure stripped of its path, if any:
% (analogous to standard \cs{jobname})
\def\pstool@jobname{\pstool@filestub\pstool@suffix}


% \section{Command parsing}
% User input is \cmd\pstool\ (with optional \texttt{*} or \texttt{!} suffix) which turns into one of the following three macros depending on the mode.

\newcommand\pstool@alwaysprocess[3][]{%
  \pstool@getpaths{#2}%
  \pstool@process{#1}{#3}%
}

\ifpdf
  \newcommand\pstool@neverprocess[3][]{%
    \pstool@includegraphics{#2}%
  }
\else
  \newcommand\pstool@neverprocess[3][]{%
    \begingroup
      \setkeys*{pstool.sty}{#1}%
      #3%
      \expandafter\pstool@includegraphics\expandafter[\XKV@rm]{#2}%
    \endgroup
  }
\fi

% Process the figure when:
% -- the PDF file doesn't exist, or
% -- the EPS is newer than the PDF, or
% -- the TeX file is new than the PDF.
\ExplSyntaxOn
\newcommand\pstool@maybeprocess[3][]
 {
  \pstool_if_should_process:nTF {#2}
   { \pstool@process{#1}{#3} }
   { \pstool@includegraphics{#2} }
 }

\prg_set_conditional:Nnn \pstool_if_should_process:n {TF}
 {
  \pstool@getpaths{#1}

  \file_if_exist:nF { #1.pdf }
    { \use_i_delimit_by_q_stop:nw \prg_return_true: }

  \filemodCmp {\pstool@path\pstool@filestub.eps}
              {\pstool@path\pstool@filestub.pdf}
              { \use_i_delimit_by_q_stop:nw \prg_return_true: } {}

  \exp_args:Nx \clist_map_inline:nn { \pstool@macrofile , \pstool@tex }
  % empty entries are ignored in clist mappings, so no need to filter here
   {
    \filemodCmp {##1} {\pstool@path\pstool@filestub.pdf}
     {
      \clist_map_break:n { \use_i_delimit_by_q_stop:nw \prg_return_true: }
     }
     {}
   }

  \filemodCmp {\pstool@path\pstool@filestub.tex}
              {\pstool@path\pstool@filestub.pdf}
              { \use_i_delimit_by_q_stop:nw \prg_return_true: } {}

  \use_i_delimit_by_q_stop:nw \prg_return_false:
  \q_stop
 }
\ExplSyntaxOff

% \section{User commands}
%  Finally, define \cmd\pstool\ as appropriate for the mode: (\texttt{all}, \texttt{none}, \texttt{auto}, respectively)
\ifpdf
  \newcommand\pstool{%
    \ifcase\pstool@process@choice\relax
      \expandafter \pstool@alwaysprocess \or
      \expandafter \pstool@neverprocess  \or
      \expandafter \pstool@maybeprocess
    \fi
  }
  \WithSuffix\def\pstool!{%
    \ifcase\pstool@process@choice\relax
      \expandafter \pstool@alwaysprocess \or
      \expandafter \pstool@neverprocess  \or
      \expandafter \pstool@neverprocess
    \fi
  }
  \WithSuffix\def\pstool*{%
    \ifcase\pstool@process@choice\relax
      \expandafter \pstool@alwaysprocess \or
      \expandafter \pstool@neverprocess  \or
      \expandafter \pstool@alwaysprocess
    \fi
  }
\else
  \let\pstool\pstool@neverprocess
  \WithSuffix\def\pstool!{\pstool@neverprocess}
  \WithSuffix\def\pstool*{\pstool@neverprocess}
\fi

% \section{The figure processing}

% And this is the main macro.
\newcommand\pstool@process[2]{%
  \begingroup
  \setkeys*{pstool.sty}{#1}%
  \pstool@echo@verbose{%
      ^^J^^J=== pstool: begin processing ===}%
  \pstool@write@processfile{#1}
      {\pstool@path\pstool@filestub}{#2}%
  \pstool@exe{auxiliary process: \pstool@filestub\space}
    {./}{latex
      -shell-escape
      -output-format=dvi
      -output-directory="\pstool@path"
      -interaction=\pstool@mode\space
      \pstool@latex@opts\space
          "\pstool@jobname.tex"}%
% Execute \texttt{dvips} in quiet mode if \texttt{latex} is not run in (non/error)stop mode:
  \pstool@exe{dvips}{\pstool@path}{%
    dvips \if@pstool@verbose@\else -q \fi -Ppdf
        \pstool@dvips@opts\space "\pstool@jobname.dvi"}%
% Pre-process \texttt{ps2pdf} options for Windows (sigh):
  \pstool@pspdf@opts@preprocess \pstool@bitmap@opts
  \pstool@pspdf@opts@preprocess \pstool@pspdf@opts
% Generate the PDF:
  \if@pstool@pdfcrop@
    \pstool@exe{ps2pdf}{\pstool@path}{%
      ps2pdf \pstool@bitmap@opts \pstool@pspdf@opts \space
          "\pstool@jobname.ps" "\pstool@jobname.pdf"}%
    \pstool@exe{pdfcrop}{\pstool@path}{%
      pdfcrop \pstool@pdfcrop@opts\space
          "\pstool@jobname.pdf" "\pstool@filestub.pdf"}%
  \else
    \pstool@exe{ps2pdf}{\pstool@path}{%
      ps2pdf \pstool@bitmap@opts \pstool@pspdf@opts \space
          "\pstool@jobname.ps" "\pstool@filestub.pdf"}%
  \fi
% Finish up: (implies success!)
  \pstool@endprocess{%
    \pstool@includegraphics{\pstool@path\pstool@filestub}%
% Emulate \cs{include} (sort of) and have the main document load the auxiliary aux file, in a manner of speaking:
    \pstool@write@aux
    \pstool@cleanup
  }%
  \pstool@echo@verbose{^^J=== pstool: end processing ===^^J}%
  \endgroup
}

\newcommand\pstool@write@aux{%
  \endlinechar=-1
  \@tempswatrue
  \@pstool@write@auxfalse
  \in@false
  \openin \pstool@auxfile@ior "\pstool@path\pstool@jobname.aux"\relax
  \@whilesw \if@tempswa \fi {%
    \readline \pstool@auxfile@ior to \@tempa
    \ifeof \pstool@auxfile@ior
      \@tempswafalse
    \else
      \edef\@tempb{\detokenize\expandafter{\pstool@auxmarker@text/}}%
      \ifx\@tempa\@tempb
        \@tempswafalse
      \else
        \if@pstool@write@aux
          \immediate\write\@mainaux{\unexpanded\expandafter{\@tempa}}%
        \fi
        \edef\@tempb{\detokenize\expandafter{\pstool@auxmarker@text*}}%
        \ifx\@tempa\@tempb
          \@pstool@write@auxtrue
        \fi
      \fi
    \fi
  }
  \closein \pstool@auxfile@ior
}

\ExplSyntaxOn
\cs_new:Npn \pstool@pspdf@opts@preprocess #1
 {
  \ifwindows
    \exp_args:NNnx \tl_replace_all:Nnn #1 {=} { \cs_to_str:N \# }
  \fi
 }
\ExplSyntaxOff

% For what's coming next.
\edef\@begindocument@str{\detokenize\expandafter{\string\begin{document}}}
\edef\@endpreamble@str{\string\EndPreamble}
\def\in@first#1#2{\in@{NEVEROCCUR!#1}{NEVEROCCUR!#2}}

% We need to cache the aux file, so here goes.
% This is because the aux file is cleared for writing after \cs{begin{document}}.
\ifpdf
  \IfFileExists{\jobname.oldaux}{\immediate\write18{\pstool@rm@cmd \jobname.oldaux}}{}
  \immediate\write18{\pstool@cp@cmd \jobname.aux \jobname.oldaux}
  \AtEndDocument{\immediate\write18{\pstool@rm@cmd \jobname.oldaux}}
\fi

\edef\pstool@auxmarker#1{\string\@percentchar\space <#1PSTOOLLABELS>}
\edef\pstool@auxmarker@text#1{\@percentchar <#1PSTOOLLABELS>}

% The file that is written for processing is set up to read the preamble of the original document and set the graphic on an empty page (cropping to size is done either here with \pkg{preview} or later with \pkg{pdfcrop}).
\def\pstool@write@processfile#1#2#3{%
  \immediate\openout\pstool@out #2\pstool@suffix.tex\relax
% Put down a label so we can pass through the current page number:
  \edef\pstool@label{pstool-\pstool@path\pstool@filestub}%
  \protected@write\@auxout{}%
    {\string\newlabel{\pstool@label}{{\@currentlabel}{\the\c@page}}}%
% And copy the main file's bbl file too: (necessary only for biblatex)
  \IfFileExists{\jobname.bbl}{%
    \IfFileExists{\pstool@path\pstool@jobname.bbl}{%
      \immediate\write18{\pstool@rm@cmd \pstool@path\pstool@jobname.bbl}%
    }{}%
    \immediate\write18{\pstool@cp@cmd \jobname.bbl \pstool@path\pstool@jobname.bbl}%
  }{}%
% Scan the main document line by line; print preamble into auxiliary file until the document begins or \cs{EndPreamble} is found:
  \endlinechar=-1
  \def\@tempa{\pdfoutput=0\relax}%
  \in@false
  \openin\pstool@mainfile@ior "\jobname"\relax
  \@whilesw \unless\ifin@ \fi {%
    \immediate\write\pstool@out{\unexpanded\expandafter{\@tempa}}%
    \readline\pstool@mainfile@ior to\@tempa
    \let\@tempc\@tempa
    \trim@multiple@spaces@in\@tempa
    \expandafter\expandafter\expandafter\in@first
    \expandafter\expandafter\expandafter{%
    \expandafter\@begindocument@str
    \expandafter}%
    \expandafter{\@tempa}%
    \unless\ifin@
      \expandafter\expandafter\expandafter\in@first
      \expandafter\expandafter\expandafter{%
      \expandafter\@endpreamble@str
      \expandafter}%
      \expandafter{\@tempa}%
    \fi
  }
  \closein\pstool@mainfile@ior
% Now the preamble of the process file:
  \immediate\write\pstool@out{%
    \if@pstool@pdfcrop@\else
      \noexpand\usepackage[active,tightpage]{preview}^^J%
    \fi
    \unexpanded{%
      \pagestyle{empty}^^J^^J% remove the page number
    }%
    \noexpand\makeatletter^^J%
% Sort out the page numbering here.
% Force the pagestyle locally to output an integer so it can be written to the external file inside a \cs{setcounter} command.
    \expandafter\ifx\csname r@\pstool@label\endcsname\relax\else
      \def\noexpand\thepage{\unexpanded\expandafter{\thepage}}^^J%
      \noexpand\setcounter{page}{%
        \expandafter\expandafter\expandafter
          \@secondoftwo\csname r@\pstool@label\endcsname
      }^^J%
    \fi
% And the document body to place the graphic on a page of its own:
    \noexpand\@input{\jobname.oldaux}^^J%
    \noexpand\makeatother^^J^^J%
    \noexpand\begin{document}^^J%
    \noexpand\makeatletter^^J%
    \unexpanded{\immediate\write\@mainaux}{\pstool@auxmarker*}^^J%
    \noexpand\makeatother^^J^^J%
    \unexpanded{%
      \centering\null\vfill^^J%
    }%
    ^^J%
    \if@pstool@pdfcrop@\else
      \noexpand\begin{preview}^^J%
    \fi
    \unexpanded{#3^^J}
    \noexpand\includegraphics
        [\unexpanded\expandafter{\XKV@rm}]
        {\pstool@filestub}^^J%
    \if@pstool@pdfcrop@\else
      \noexpand\end{preview}^^J%
    \fi
    ^^J%
    \unexpanded{\vfill^^J^^J\makeatletter^^J\immediate\write\@mainaux}{\pstool@auxmarker/}^^J%
    \unexpanded{\makeatother^^J\end{document}}^^J%
  }%
  \immediate\closeout\pstool@out
}

\def\pstool@cleanup{%
  \@for\@ii:=\pstool@rm@files\do{%
    \pstool@rm{\pstool@path}{\pstool@jobname\@ii}%
  }%
}

\providecommand\EndPreamble{}

% \section{User commands}
% These all support the suffixes \texttt{*} and \texttt{!}, so each user command is defined as a wrapper to \cmd\pstool.

% For EPS figures with psfrag:
\newcommand\psfragfig[2][]{\pstool@psfragfig{#1}{#2}{}}
\WithSuffix\newcommand\psfragfig*[2][]{%
  \pstool@psfragfig{#1}{#2}{*}%
}
\WithSuffix\newcommand\psfragfig![2][]{%
  \pstool@psfragfig{#1}{#2}{!}%
}

% Parse optional input definitions:
\newcommand\pstool@psfragfig[3]{%
  \@ifnextchar\bgroup{%
    \pstool@@psfragfig{#1}{#2}{#3}%
  }{%
    \pstool@@psfragfig{#1}{#2}{#3}{}%
  }%
}

% Search for both `filename' and `filename'-psfrag inputs.
%
% #1: possible graphicx options
% #2: graphic name (possibly with path)
% #3: \cs{pstool} suffix (i.e., \texttt{!} or \texttt{*} or `empty')
% #4: possible \pkg{psfrag} (or other) macros
\newcommand\pstool@@psfragfig[4]{%
  % Find the .eps file to use.
  \IfFileExists{#2-psfrag.eps}{%
    \edef\pstool@eps{#2-psfrag}%
    \IfFileExists{#2.eps}{%
      \PackageWarning{pstool}{%
        Graphic "#2.eps" exists but "#2-psfrag.eps" is being used%
      }%
    }{}%
  }{%
    \IfFileExists{#2.eps}{%
      \edef\pstool@eps{#2}%
    }{%
      \PackageError{pstool}{%
        No graphic "#2.eps" or "#2-psfrag.eps" found%
      }{%
        Check the path and whether the file exists.%
      }%
    }%
  }%
  % Find the .tex file to use.
  \IfFileExists{#2-psfrag.tex}{%
    \edef\pstool@tex{#2-psfrag.tex}%
    \IfFileExists{#2.tex}{%
      \PackageWarning{pstool}{%
        File "#2.tex" exists that may contain macros
        for "\pstool@eps.eps"^^J%
        But file "#2-psfrag.tex" is being used instead.%
      }%
    }{}%
  }{%
    \IfFileExists{#2.tex}{%
      \edef\pstool@tex{#2.tex}%
    }{%
      \PackageWarning{pstool}{%
        No file "#2.tex" or "#2-psfrag.tex" can be found
        that may contain macros for "\pstool@eps.eps"%
      }%
    }%
  }%
  % Perform the actual processing step, skipping it entirely if an EPS file hasn't been found.
  % (In which case an error would have been called above; this is to clean up nicely in scrollmode, for example.)
  \ifx\pstool@eps\@undefined\else
    \edef\@tempa{%
      \unexpanded{\pstool#3[#1]}{\pstool@eps}{%
        \ifx\pstool@macrofile\@empty\else
          \unexpanded{\csname @input\endcsname}{\pstool@macrofile}%
        \fi
        \ifx\pstool@tex\@empty\else
          \unexpanded{\csname @input\endcsname}{\pstool@tex}%
        \fi
        \unexpanded{#4}%
      }%
    }\@tempa
  \fi
}
% \centerline{\itshape ---The End---}