Debian Codegraph @

texmf-dist/source/latex/psfrag/psfrag.dtx

raw
1 %
2 % \iffalse
3 % psfrag.dtx Copyright (C) 1996 Craig Barratt, Michael C. Grant,
4 % and David Carlisle.
5 % All rights are reserved.
6 %
7 % This system is distributed in the hope that it will be
8 % useful, but WITHOUT ANY WARRANTY; without even the
9 % implied warranty of MERCHANTABILITY or FITNESS FOR A
10 % PARTICULAR PURPOSE. Don't come complaining to us if you
11 % modify this file and it doesn't work! If this file is
12 % modified by anyone but the authors, those changes and
13 % their authors must be explicitly stated HERE.
14 %
15 %<package>\NeedsTeXFormat{LaTeX2e}[1995/12/01]
16 %<package>\ProvidesPackage{psfrag}[1998/04/11 v3.04 PSfrag (MCG)]
17 % \fi
18 %
19 % \iffalse
20 %<*driver>
21 \documentclass{ltxdoc}
22 \usepackage{graphicx,psfrag}
23 \begin{document}
24 \DocInput{psfrag.dtx}
25 \end{document}
26 %</driver>
27 % \fi
28 %
29 % \GetFileInfo{psfrag.sty}
30 % \RecordChanges
31 %
32 % \let\pkg\textsf
33 % \let\fname\texttt
34 % \let\pscom\texttt
35 % \newcommand{\pfg}{\textsf{PSfrag}}
36 % \newcommand{\bsl}{\protect\bslash}
37 % \newcommand{\ie}{\emph{i.e.}}
38 % \newcommand{\eg}{\emph{e.g.}}
39 % \newcommand{\etc}{\emph{etc.}}
40 % \newcommand{\netaddress}[1]{\texttt{#1}}
41 % \marginparsep 0pt
42 %
43 % \title{\fname{psfrag.sty} and \fname{psfrag.pro}%^^A
44 % \thanks{This file has version number \fileversion,
45 % last revised \filedate.}}
46 %
47 % \author{Michael Grant, David Carlisle, and Craig Barratt \\
48 % \netaddress{[email protected]}}
49 % \date{\filedate}
50 % \maketitle
51 % \PrintChanges
52 %
53 % \changes{3.04}{1998/04/11}{Made some (theoretically) back-compatible
54 % changes to \fname{psfrag.pro} which should make it more friendly
55 % to EPS and page-manipulation packages like \pkg{psnup}. Fixed
56 % orientation problem for DVIPSone. Added support for EPS files
57 % as \pfg\ replacements.}
58 % \changes{3.03}{1997/01/21}{Fixed some typographical errors that
59 % broke the \pscom{awidthshow} command. Improved the scanner to
60 % handle multi-line \cs{tex} commands. Fixed a bug in the
61 % \pscom{replace} command which would break some legal EPS files.}
62 % \changes{3.02}{1996/12/06}{Continued to improve the transformation
63 % code to make it more robust to rotations and odd scaling.
64 % Improved the range of strings accepted as PSfrag tags.}
65 % \changes{3.01}{1996/11/26}{Expanded the PSfrag dictionary
66 % for Level 1 printers. Fixed bugs that produce incorrect output
67 % for rotated figures. Output should now print properly when
68 % combined with page-manipulation programs like \pkg{psnup}.}
69 % \changes{3.0}{1996/10/31}{Major rewrite. Elimination of the
70 % preprocessing step, deprecation of the \pscom{tex} command. I've
71 % wiped most of the source-level change entries as a result.}
72 % \changes{2.0}{1995/04/04}{Bug fixes}
73 % \changes{2.0}{1995/03/14}{Bug fixes. Better support for \LaTeX\ 2.09
74 % \pfg\ files, including both naming schemes. Improved
75 % documentation and separated it from this file.}
76 % \changes{1.99}{1995/02/14}{Taken over from Craig, completely
77 % overhauled for \LaTeXe. Completely changed the placement code;
78 % baseline alignment added. Integrated \pscom{psfrag} commands into
79 % the PostScript file. \textsf{graphics} package used for
80 % portability across multiple DVI-to-PostScript packages.
81 % Improved ps2frag.ps file provides improved (correct) support
82 % for ashow, widthshow, awidthshow, and kshow.}
83 % \changes{1.1}{1992/10/20}{Released version 1.1, adding support for
84 % ashow, widthshow, awidthshow, and kshow.}
85 % \changes{1.0}{1992/06/01}{Released version 1.0.}
86 %
87 % \section{\fname{psfrag.sty}}
88 %
89 % \begin{macrocode}
90 %<*package>
91 % \end{macrocode}
92 %
93 % \subsection{Intialization}
94 %
95 % \begin{macrocode}
96 \newif\ifpfg@compat
97 \newif\ifpfg@prepass
98 \newif\ifpfg@debug \pfg@debugfalse
99 \if@compatibility
100 \pfg@compattrue\pfg@prepasstrue
101 \else
102 \pfg@compatfalse\pfg@prepassfalse
103 \fi
104 \def\psfragscanon{\pfg@prepasstrue}
105 \def\psfragscanoff{\pfg@prepassfalse}
106 \def\psfragdebugon{\pfg@debugtrue}
107 \def\psfragdebugoff{\pfg@debugfalse}
108 % \end{macrocode}
109 %
110 % \begin{macrocode}
111 \DeclareOption{scanall}{\pfg@prepasstrue}
112 \DeclareOption{2emode}{\pfg@compatfalse\pfg@prepassfalse}
113 \DeclareOption{209mode}{\pfg@compattrue\pfg@prepasstrue}
114 \DeclareOption{debugshow}{\pfg@debugtrue
115 \PassOptionsToPackage\CurrentOption{graphics}}
116 \DeclareOption*{\PassOptionsToPackage\CurrentOption{graphics}}
117 \ProcessOptions
118 % \end{macrocode}
119 %
120 % \fname{psfrag.sty} requires the \pkg{graphics} package and the
121 % support file \fname{psfrag.pro}. (\fname{epsf.sty} can still be
122 % used,but \pkg{graphics} package is still used for internal
123 % purposes.) To load \fname{psfrag.pro} in a portable way, we provide
124 % and use the \cs{Gin@PS@file@header} command, which the
125 % \pkg{graphics} will eventually provide for us.
126 % \changes{3.01}{1996/11/25}{Added the \cs{AtBeginDocument} command
127 % to save some important global transformation information.}
128 % \changes{3.02}{1996/12/03}{Deleted this \cs{AtBeginDocument} info;
129 % see \cs{pfg@orient}.}
130 % \begin{macrocode}
131 \RequirePackage{graphics}
132 \providecommand\Gin@PS@file@header[1]{\AtBeginDvi{\special{header=#1}}}
133 \Gin@PS@file@header{psfrag.pro}
134 % \end{macrocode}
135 %
136 % \begin{macro}{\pfg@orient}
137 % In order to properly handle all cases of rotation, scaling,
138 % \etc\ that \pfg\ may encounter, it is necessary that we know a bit
139 % about the transformations that the DVI to PS driver uses itself.
140 % Specifically, we need to know what transformations, if any, the
141 % DVI to PS driver appends to PostScript's \pscom{defaultmatrix}
142 % before it starts to typeset text. At the moment, all of the drivers
143 % which work with \pfg\ reverse the orientation of the $y$ axis;
144 % new drivers may not. But until we can find a portable way to make
145 % this determination in PostScript itself, we have to supply that
146 % information here.
147 % \changes{3.02}{1996/12/03}{Created. Added \pkg{dvips} and
148 % \pkg{textures} as ``$y$-reversers.''}
149 % \changes{3.04}{1998/04/11}{\pkg{dvipsone} now uses the same
150 % orientation as \pkg{dvips}, so we added a line for it here.}
151 % \begin{macrocode}
152 \def\pfg@orient{1}
153 \def\@tempa{dvipsone.def}\ifx\Gin@driver\@tempa\def\pfg@orient{-1}\fi%
154 \def\@tempa{dvips.def}\ifx\Gin@driver\@tempa\def\pfg@orient{-1}\fi%
155 \def\@tempa{textures.def}\ifx\Gin@driver\@tempa\def\pfg@orient{-1}\fi%
156 % \end{macrocode}
157 % \end{macro}
158 %
159 % \subsection{Interface to the \pkg{graphics}
160 % and \pkg{epsf} packages}
161 %
162 % \begin{macro}{\pfg@epsfbox}
163 % The original definition of \cs{epsfbox} from \pkg{epsf.sty} or \pkg{graphics}.
164 % \begin{macro}{\pfg@Ginclude@eps}
165 % The original definition of \cs{Ginclude@eps} from \pkg{graphics},
166 % the key routine inside the \cs{includegraphics} routine for EPS files.
167 %
168 % Below we redefine these two commands to add \pfg\ functionality.
169 % \begin{macrocode}
170 \let\pfg@epsfbox\epsfbox
171 \let\pfg@Ginclude@eps\Ginclude@eps
172 % \end{macrocode}
173 % \end{macro}\end{macro}
174 %
175 % \begin{macro}{\pfg@pcount}
176 % Counts how many fragments are active in the current figure.
177 % \begin{macro}{\pfg@scount}
178 % Counts how many replacements strings are active in the current
179 % figure.
180 %
181 % Since it is possible for a single string to accept multiple
182 % replacements, \cs{pfg@scount} $\le$ \cs{pfg@pcount}.
183 % \begin{macrocode}
184 \newcount\pfg@pcount \pfg@pcount=\z@
185 \newcount\pfg@scount \pfg@scount=\z@
186 % \end{macrocode}
187 % \end{macro}\end{macro}
188 %
189 % \begin{macro}{\epsfbox}
190 % Here we intercept calls to \cs{epsfbox} and wrap them with the
191 % \pfg\ processing. The first thing we do is revert \cs{epsfbox}
192 % and \cs{Ginclude@eps} back to their original definitions, to
193 % prevent recursion---which could occur if some of the replacements
194 % are themselves EPS files.
195 % \changes{3.04}{1998/04/11}
196 % {Improved the recursion protection by reverting
197 % \cs{epsfbox} and \cs{Ginclude@eps} back to their original
198 % definitions within the block. Deleted \cs{ifpfg@epsf}, which
199 % is no longer needed in this new system.}
200 % \begin{macrocode}
201 \def\epsfbox#1{%
202 \begingroup
203 \let\Ginclude@eps\pfg@Ginclude@eps
204 \let\epsfbox\pfg@epsfbox
205 \ifpfg@prepass\pfg@scan{#1}\fi
206 \ifnum\pfg@scount>\z@\pfg@hidestart\fi
207 \pfg@epsfbox{#1}%
208 \ifnum\pfg@scount>\z@\pfg@hideend\fi
209 \endgroup}
210 % \end{macrocode}
211 % \end{macro}
212 %
213 % \begin{macro}{\Ginclude@eps}
214 % We need to override this function from the \pkg{graphics}
215 % package in a similar fashion to \cs{epsfbox}. As we did for
216 % \cs{epsfbox}, we revert \cs{epsfbox} and \cs{Ginclude@eps}
217 % back to their original definitions in order to prevent recursion.
218 % \changes{3.01}{1996/11/26}
219 % {Added the \cs{pfg@hideinit} functionality.}
220 % \changes{3.02}{1996/12/01}
221 % {Removed \cs{pfg@hideinit}.}
222 % \changes{3.04}{1998/04/11}
223 % {Improved the recursion protection by reverting
224 % \cs{epsfbox} and \cs{Ginclude@eps} back to their original
225 % definitions within the block.}
226 % \begin{macrocode}
227 \def\Ginclude@eps#1{%
228 \begingroup
229 \let\Ginclude@eps\pfg@Ginclude@eps
230 \let\epsfbox\pfg@epsfbox
231 \ifpfg@prepass\pfg@scan{#1}\fi
232 \ifnum\pfg@scount>\z@\pfg@hidestart\fi
233 \pfg@Ginclude@eps{#1}%
234 \ifnum\pfg@scount>\z@\pfg@hideend\fi
235 \endgroup}
236 % \end{macrocode}
237 % \end{macro}
238 %
239 % \begin{macro}{psfrags}
240 % Defines a dummy environment to delimit the scope of \pfg\
241 % replacements.
242 % \begin{macrocode}
243 \newenvironment{psfrags}{\ignorespaces}{\global\@ignoretrue}
244 % \end{macrocode}
245 % \end{macro}
246 %
247 % \begin{macro}{\psfragspecial}
248 % This macro is no longer supported.
249 % \begin{macrocode}
250 \ifpfg@compat
251 \def\psfragspecial#1#2#3#4#5#6{%
252 \PackageError{PSfrag}%
253 {\string\psfragspecial no longer implemented.}{}}
254 \fi
255 % \end{macrocode}
256 % \end{macro}
257 %
258 % \begin{macro}{\psfrag}
259 % \marg{tag}\oarg{posn}\oarg{psposn}\oarg{scl}\oarg{rot}\\
260 % The main macro \cs{psfrag} must be defined specially in order
261 % to handle its odd combination of optional and required arguments.
262 % In addition, catcode tricks (thanks to David Carlisle and others)
263 % allow special characters to be used inside the tag text. It also
264 % replaces spaces with their octal equivalents to prevent drivers
265 % like \pkg{dvips} from breaking one in the middle.
266 % \changes{3.01}{1996/11/26}{Now changes every space in the tag
267 % text by its octal code. Some DVI to PS drivers were breaking the
268 % strings onto multiple lines; this should prevent that from happening.}
269 % \changes{3.01}{1996/11/26}{Removed octal code in favour of convert
270 % routine in the prologue}
271 % \changes{3.03}{1997/1/6}{Added another ``special'' character.}
272 % \begin{macrocode}
273 \newif\ifpfg@star
274 \def\psfrag{\@ifstar{\pfg@startrue\@psfraga}{\pfg@starfalse\@psfraga}}
275 % \end{macrocode}
276 %
277 % \begin{macrocode}
278 \def\@psfraga{\begingroup
279 \@makeother\"\@makeother\*\@makeother\!\@makeother\~%
280 \@makeother\:\@makeother\\\@makeother\%\@makeother\#%
281 \@makeother\ \@psfragb}
282 % \end{macrocode}
283 %
284 % \begin{macrocode}
285 \ifpfg@compat
286 \def\@psfragb#1{\xdef\@gtempa{#1}\endgroup
287 \@ifnextchar[{\@psfragc{\@gtempa}}%
288 {\@psfrag{\@gtempa}[bl][bl][1][0]}}
289 \def\@psfragc#1[#2]{\@ifnextchar [{\@psfragd{#1}[#2]}%
290 {\@psfrag{#1}[#2][bl][1][0]}}
291 % \end{macrocode}
292 %
293 % \begin{macrocode}
294 \else
295 \def\@psfragb#1{\xdef\@gtempa{#1}\endgroup
296 \@ifnextchar [{\@psfragc{\@gtempa}}%
297 {\@psfrag{\@gtempa}[Bl][Bl][1][0]}}
298 \def\@psfragc#1[#2]{\@ifnextchar [{\@psfragd{#1}[#2]}%
299 {\@psfrag{#1}[#2][Bl][1][0]}}
300 \fi
301 % \end{macrocode}
302 %
303 % \begin{macrocode}
304 \def\@psfragd#1[#2][#3]{\@ifnextchar [{\@psfrage{#1}[#2][#3]}%
305 {\@psfrag{#1}[#2][#3][1][0]}}
306 \def\@psfrage#1[#2][#3][#4]{\@ifnextchar [{\@psfrag{#1}[#2][#3][#4]}%
307 {\@psfrag{#1}[#2][#3][#4][0]}}
308 % \end{macrocode}
309 % \end{macro}
310 %
311 % \begin{macro}{\@psfrag}\marg{tag}\\
312 % Now that we have all of the arguments, we need to check to check to
313 % see if the string has been used yet; if not, increment the string
314 % count and create a place for the new string. Then pass the string
315 % number on down the line to \cs{@@psfrag}.
316 % \begin{macrocode}
317 \def\pfg@newstring#1.#2{%
318 \@nameedef{pfg@using@#2}{#1}%
319 \pfg@starfalse\advance\pfg@scount\@ne}
320 % \end{macrocode}
321 %
322 % \begin{macrocode}
323 \def\@psfrag#1{%
324 \@ifundefined{pfg@using@#1}%
325 {\expandafter\pfg@newstring\the\pfg@scount.{#1}}%
326 {}%
327 \edef\@tempa{\@nameuse{pfg@using@#1}}%
328 \expandafter\@@psfrag\@tempa.{#1}}
329 % \end{macrocode}
330 % \end{macro}
331 %
332 % \begin{macro}{\@@psfrag}\marg{tag-key}.\marg{tag}\\
333 % \cs{@@psfrag} passes along the current \emph{replacement} number to
334 % to \cs{@@@psfrag}, which is doing all of the dirty work.
335 % \begin{macrocode}
336 \def\@@psfrag{\expandafter\@@@psfrag\the\pfg@pcount.}
337 % \end{macrocode}
338 % \end{macro}
339 %
340 % \begin{macro}{\@@@psfrag}
341 % \marg{repl-key}.\marg{tag-key}.\marg{tag}\oarg{posn}%
342 % \oarg{psposn}\oarg{scl}\oarg{rot}\oarg{repl}
343 % \begin{macro}{\pfg@align}\marg{tag-key}.
344 % \begin{macro}{\pfg@place}\marg{repl-key}.\\
345 % \cs{@@@psfrag}, armed with numeric keys for the tag string and the
346 % replacement, creates a \cs{pfg@place} procedure call for this
347 % fragment and appends the control point, scale, and rotation
348 % information to the proper tag's list, found in
349 % \cs{pfg@align\emph{<tag-key>}.}
350 % \changes{3.01}{1996/11/26}{Added some extra spaces in
351 % \cs{pfg@align@} to encourage more benevlolent line-breaking
352 % by DVI to PS drivers.}
353 % \begin{macrocode}
354 \def\@nameedef#1#2{\expandafter\edef\csname #1\endcsname{#2}}
355 % \end{macrocode}
356 %
357 % \begin{macrocode}
358 \def\@@@psfrag#1.#2.#3[#4][#5][#6][#7]#8{%
359 \advance\pfg@pcount\@ne
360 \ifpfg@star\else\@nameedef{pfg@align@#2}{(#3)[}\fi
361 \@nameedef{pfg@align@#2}{\@nameuse{pfg@align@#2}[#1(#5)#6 #7]}%
362 \@namedef{pfg@place@#1}{\pfg@@place{#1}{#4}{#8}}\ignorespaces}
363 % \end{macrocode}
364 %
365 % \begin{macrocode}
366 \def\pfg@align#1.{\@nameuse{pfg@align@#1}}
367 \def\pfg@place#1.{\@nameuse{pfg@place@#1}}
368 % \end{macrocode}
369 % \end{macro}\end{macro}\end{macro}
370 %
371 % \subsection{Fragment processing}
372 %
373 % \begin{macro}{\pfg@hidestart}
374 % This procedure provides PostScript with a list of the strings that
375 % \pfg\ would like to replace, and the control points, scales, and
376 % rotations of each one. The PostScript code in \fname{psfrag.pro}
377 % will use this information to construct transformation matrices for
378 % each replacement to move them into proper position.
379 % \changes{3.01}{1996/11/26}{Moved the \pkg{xdvi} stub to
380 % \cs{pfg@hideinit}}.
381 % \changes{3.02}{1996/12/01}{Moved it back.}
382 % \begin{macrocode}
383 \def\pfg@hidestart{%
384 \def\pfg@{}\count@=\z@\loop\ifnum\count@<\pfg@scount
385 \toks@=\expandafter{\expandafter\pfg@align\the\count@.}%
386 \edef\pfg@{\pfg@\the\toks@]}%
387 \advance\count@ by\@ne\repeat
388 \Gin@PS@raw{/PSfrag where{pop\pfg@\the\pfg@scount\space
389 \ifpfg@debug1\else0\fi\space\pfg@orient/Begin
390 PSfrag}{userdict /PSfrag{pop}put}ifelse}}
391 % \end{macrocode}
392 % \end{macro}
393 %
394 % \begin{macro}{\pfg@hideend}
395 % Now that the EPS file has been loaded, and (therefore) all of the
396 % replacement positions have been determined, move each replacement
397 % to its proper position by calling the \cs{pfg@place} procedure for
398 % each one. Except for the calls to \cs{Gin@PS@raw}, it looks like
399 % we're building a vertical list of the replacements: that's the idea,
400 % because we want it to look decent when viewed by a DVI viewer.
401 % \begin{macrocode}
402 \def\pfg@hideend{%
403 \Gin@PS@raw{/End PSfrag}%
404 \vbox to\z@{\vss
405 \Gin@PS@raw{/Hide PSfrag}%
406 \hbox to\z@{\hss \raisebox{\depth}{%
407 \underline{PSfrag replacements}}}%
408 \Gin@PS@raw{/Unhide PSfrag}%
409 \count@=\z@\loop\ifnum\count@<\pfg@pcount
410 \expandafter\pfg@place\the\count@.%
411 \advance\count@ by\@ne\repeat}}
412 % \end{macrocode}
413 % \end{macro}
414 %
415 % \begin{macro}{\pfg@@place}
416 % \marg{repl-key}\marg{posn}\marg{replacement}\\
417 % The following macro does the dirty work. Its job is to insert the
418 % appropriate code so that the \TeX\ replacement is transformed to
419 % its requested location; and, if desired, repeated for as many times
420 % as the PostScript tag text appeared in the figure.
421 %
422 % It performs this magic by first surrounding the \TeX-produced code
423 % in curly braces, so we can defer its execution while we figure out
424 % where to place it. Then, we call the \pscom{Place} routine in
425 % \fname{psfrag.pro}, which takes \marg{repl-key}
426 % and provides the proper coordinate transformations.
427 %
428 % We're allowed to reassign these dimension registers because we are
429 % always inside a group when we use them. This technique is taken from
430 % \fname{graphics.sty}...
431 % \begin{macrocode}
432 \let\pfg@dp\leftmargini\let\pfg@wd\leftmarginii
433 \let\pfg@dx\leftmarginiii\let\pfg@dy\leftmarginiv
434 % \end{macrocode}
435 % We need to stuff the curly braces into macros.
436 % \begin{macrocode}
437 \edef\pfg@bchar{ \string{ }
438 \edef\pfg@echar{ \string} }
439 \def\pfg@@place#1#2#3{\begingroup
440 % \end{macrocode}
441 % Typeset the replacement into a box and determine its dimensions.
442 % \begin{macrocode}
443 \sbox\z@{#3}%
444 \dimen@=\ht\z@ \advance\dimen@ by\dp\z@%
445 % \end{macrocode}
446 % Determine the desired alignment of the \TeX\ box, and the glue
447 % that will be needed in order to achieve this alignment.
448 % \begin{macrocode}
449 \pfg@wd=0.5\wd\z@\pfg@dp=0.5\dimen@
450 \pfg@dx=\pfg@wd\pfg@dy=\pfg@dp
451 \@tfor\@tempa:=#2\do{%
452 \if l\@tempa \pfg@dx=\z@ \pfg@wd=\wd\z@ \else
453 \if r\@tempa \pfg@dx=\wd\z@ \pfg@wd=\z@ \else
454 \if b\@tempa \pfg@dy=\z@ \pfg@dp=\z@ \else
455 \if t\@tempa \pfg@dy=\dimen@\pfg@dp=\dimen@\else
456 \if B\@tempa \pfg@dy=\dp\z@ \pfg@dp=\dp\z@ \fi\fi\fi\fi\fi}%
457 % \end{macrocode}
458 % Create a zero-area box with the desired \pfg\ replacement
459 % text typeset inside of it (and aligned with the glue). This
460 % is actually the box that will be moved, rotated, and/or scaled.
461 % \begin{macrocode}
462 \setbox\z@\hbox to\z@{\hskip-\pfg@dx\box\z@\hss}%
463 \setbox\z@\vbox to\z@{\vss\box\z@\vskip-\pfg@dy}%
464 % \end{macrocode}
465 % Typeset the box. The inner, zero-size box is the one we place
466 % with the PostScript code, and the outer, full-height, right-\
467 % justified box is the one that we use to stack up the preview
468 % list.
469 % \begin{macrocode}
470 \vbox to\dimen@{\vss\hbox to\z@{\hss
471 \Gin@PS@raw{\pfg@bchar}\box\z@
472 \Gin@PS@raw{\pfg@echar#1/Place PSfrag}%
473 \hskip\pfg@wd}\vskip\pfg@dp}%
474 \endgroup}
475 % \end{macrocode}
476 % \end{macro}
477 %
478 % \begin{macrocode}
479 %</package>
480 %<*filepro>
481 % \end{macrocode}
482 %
483 % \section{The PostScript library, \fname{psfrag.pro}}
484 % This code must perform two functions: 1) determine and save the
485 % alignment point, rotation, and scaling for each piece of PostScript
486 % text that is going to serve as a \pfg\ tag; and 2) use that
487 % information to properly transform the coordinate system for each
488 % \TeX\ replacement.
489 %
490 % The only two symbols that we define globally are \pscom{PSfragLib},
491 % the library of support routines, \pscom{PSfragDict}, the dictionary
492 % containing the ``magic'' \pscom{show} commands,
493 % and \pscom{PSfrag}, the gateway from
494 % the global namespace to \pscom{PSfragLib}. This technique minimizes
495 % the possibility that our procedure names will conflict with those
496 % defined by the PostScript driver or the figure itself.
497 % \changes{3.02}{1996/12/03}{Added code to insure that \pscom{PSfragLib}
498 % and \pscom{PSfrag} are stored in \pscom{userdict}. Some cruel DVI to
499 % PostScript drivers try and force these into their own dictionary. Also
500 % moved \pscom{PSfragDict} out of \pscom{PSfragLib}.}
501 % \begin{macrocode}
502 userdict begin
503 /PSfragLib 90 dict def
504 /PSfragDict 6 dict def
505 /PSfrag { PSfragLib begin load exec end } bind def
506 end
507 % \end{macrocode}
508 %
509 % \begin{macro}{PSfragLib}
510 % \pscom{PSfragLib} is the dictionary with the meaty stuff. We're also
511 % defining some useful abbreviations. Of note is the \pscom{OE} command
512 % (``outside exec''), which executes some code after removing
513 % \pscom{PSfragLib} from the stack (to insure that there are no
514 % collisions with \pscom{PSfragLib}'s internal names.
515 % \changes{3.01}{1996/11/26}{Fixed \pscom{islev2}.}
516 % \changes{3.02}{1996/12/03}{Added \pscom{readonly} to \pscom{BD} to
517 % help insure that this dictionary isn't being trampled on.}
518 % \changes{3.02}{1996/12/03}{Added some abbreviations.}
519 % \changes{3.04}{1998/04/11}{Added the default definition of \pscom{S} to
520 % avert potential problems if it is defined elsewhere. Thanks again to
521 % J. Scott Berg for this fix.}
522 % \begin{macrocode}
523 PSfragLib begin
524 /RO /readonly load def
525 /CP /currentpoint load def
526 /CM /currentmatrix load def
527 /B { bind RO def } bind def
528 /X { exch def } B
529 /MD { { X } forall } B
530 /OE { end exec PSfragLib begin } B
531 /S false def
532 /tstr 8 string def
533 /islev2 { languagelevel } stopped { false } { 2 ge } ifelse def
534 % \end{macrocode}
535 % Allocate some space for matrices that we will use frequently.
536 % \pscom{sM} is the matrix we encounter at the time this library
537 % is defined; this should theoretically be \pscom{initmatrix}, unless
538 % we are in an EPS/psnup situation.
539 % \pscom{srcFM} contains a matrix which describes the difference
540 % between PostScript's \pscom{defaultmatrix} and the transformation
541 % defined by the DVI-to-PS driver's ``default matrix.'' This information
542 % is crucial to the proper placing of the \pfg\ replacements.
543 % \changes{3.02}{1996/12/01}{Changed the names to be a tiny bit
544 % more descriptive, and added a couple.}
545 % \changes{3.02}{1996/12/03}{Added the \pscom{readonly} commands.}
546 % \changes{3.04}{1998/04/11}{Added \pscom{sM}, which stores the
547 % \pscom{currentmatrix} found at the time \pscom{PSfragLib} is defined.
548 % See the \pscom{replace} command for its use. Thanks to J. Scott Berg.}
549 % \begin{macrocode}
550 [ /sM /tM /srcM /dstM /dM /idM /srcFM /dstFM ] { matrix def } forall
551 sM currentmatrix RO pop
552 dM defaultmatrix RO idM invertmatrix RO pop
553 srcFM identmatrix pop
554 % \end{macrocode}
555 % \end{macro}
556 %
557 % \begin{macro}{Hide}
558 % \begin{macro}{Unhide}
559 % Surrounding a (fairly) arbitrary piece of PostScript code with calls
560 % to \pscom{Hide} and \pscom{Unhide} should render it invisible.
561 % \pfg\ uses these calls to hide unused replacements. Note that we are
562 % assuming that the DVI-to-PS driver isn't relying on any of the
563 % information that will be wiped out by the \pscom{grestore}, except
564 % for the \pscom{currentpoint}.
565 % \begin{macrocode}
566 /Hide { gsave { CP } stopped not newpath clip { moveto } if } B
567 /Unhide { { CP } stopped not grestore { moveto } if } B
568 % \end{macrocode}
569 % \end{macro}\end{macro}
570 %
571 % \begin{macro}{setrepl}
572 % \begin{macro}{getrepl}
573 % These macros are defined differently for Level 1 and Level 2
574 % PostScript. \pscom{setrepl} accepts an array of N elements,
575 % followed by the length N, and store them in a global variable (or
576 % the equivalent) for later use. \pscom{getrepl} restores that exact
577 % information to the stack. This is used to store the replacement
578 % information in a persistent fashion.
579 % \begin{macrocode}
580 /setrepl islev2 {{ /glob currentglobal def true setglobal array astore
581 globaldict exch /PSfrags exch put glob setglobal }}
582 {{ array astore /PSfrags X }} ifelse B
583 /getrepl islev2 {{ globaldict /PSfrags get aload length }}
584 {{ PSfrags aload length }} ifelse B
585 % \end{macrocode}
586 % \end{macro}\end{macro}
587 %
588 % \begin{macro}{convert}
589 % This routine takes a string and replaces every character whose
590 % ASCII code is less than 32 with a space. This makes the replacement
591 % dictionary robust to multiple-line tags, etc.
592 % \begin{macrocode}
593 /convert {
594 /src X src length string
595 /c 0 def src length {
596 dup c src c get dup 32 lt { pop 32 } if put /c c 1 add def
597 } repeat
598 } B
599 % \end{macrocode}
600 % \end{macro}
601 %
602 % \begin{macro}{Begin}
603 % \pfg\ calls this routine once per figure, once it has placed
604 % on the stack all of each tag's alignment, rotation, and scaling
605 % information. This information is collected into a dictionary:
606 % each tag has an entry which is an array of [ repl-key, (psposn),
607 % scl, rot ] quads. Then it saves the current transformation
608 % information in a form ready for \pscom{/replace} to use.
609 % \changes{3.02}{1996/12/03}{\pscom{PSfragDict} now goes at the
610 % \emph{bottom} of the read-write portion of the dictionary stack,
611 % to insure that it does not conflict with dictionary manipulation
612 % by the EPS file or the DVI to PS driver.}
613 % \changes{3.02}{1996/12/03}{Changes to account for transformation
614 % rearrangement; see \pscom{/replace}.}
615 % \begin{macrocode}
616 /Begin {
617 /saver save def
618 srcFM exch 3 exch put
619 0 ne /debugMode X 0 setrepl
620 dup /S exch dict def { S 3 1 roll exch convert exch put } repeat
621 srcM CM dup invertmatrix pop
622 mark { currentdict { end } stopped { pop exit } if } loop
623 PSfragDict counttomark { begin } repeat pop
624 } B
625 % \end{macrocode}
626 % \end{macro}
627 %
628 % \begin{macro}{End}
629 % End converts the alignment information that has been collected
630 % into a dictionary: each repl-key that was encountered in
631 % \pscom{Begin} has an entry which is an array of
632 % transformation matrices.
633 % \changes{3.02}{1996/12/03}{Changes to the dictionary placement
634 % as discussed in \pscom{/Begin}.}
635 % \begin{macrocode}
636 /End {
637 mark { currentdict end dup PSfragDict eq { pop exit } if } loop
638 counttomark { begin } repeat pop
639 getrepl saver restore
640 7 idiv dup /S exch dict def {
641 6 array astore /mtrx X tstr cvs /K X
642 S K [ S K known { S K get aload pop } if mtrx ] put
643 } repeat
644 } B
645 % \end{macrocode}
646 % \end{macro}
647 %
648 % \begin{macro}{Place}
649 % This macro is called once for each \pfg\ replacement. It checks to
650 % see if the replacement's tag was actually encountered in the PS
651 % figure; if not, it surrounds the code in a ``clip all'' context and
652 % executes it. This makes it invisible, as requested, but still allows
653 % it to run so that any side effects that the code may have will take
654 % place.
655 %
656 % If the replacement's tag \emph{was} encountered, however, it
657 % retrieves the transformation matrix that was calculated for it; it
658 % prepends this calculation to the current (\TeX) transformation matrix
659 % (some tricky math to wade through there!), and then draws the
660 % replacement.
661 %
662 % If there are multiple copies of the replacement to be laid down,
663 % this will do it, surrounding all but the last replacement in a
664 % \pscom{save}-\pscom{restore} pair. This \pscom{save}-\pscom{restore}
665 % pair may slow things down, but theoretically it is necessary,
666 % because the procedure might modify other variables in the PostScript
667 % namespace. We need to be sure that those modifications do not
668 % ``accumulate''.
669 %
670 % For multiple replacements to work, the code that \pfg\ surrounds with
671 % curly braces must leave the stack the same way it found it. So far,
672 % we have confirmed this fact for \pkg{dvips} and \pkg{DVIPSONE}.
673 % \changes{3.02}{1996/12/03}{Changes to account for transformation
674 % rearrangement; see \pscom{/replace}.}
675 % \begin{macrocode}
676 /Place {
677 tstr cvs /K X
678 S K known {
679 bind /proc X tM CM pop
680 CP /cY X /cX X
681 0 0 transform idtransform neg /aY X neg /aX X
682 S K get dup length /maxiter X
683 /iter 1 def {
684 iter maxiter ne { /saver save def } if
685 tM setmatrix aX aY translate
686 [ exch aload pop idtransform ] concat
687 cX neg cY neg translate cX cY moveto
688 /proc load OE
689 iter maxiter ne { saver restore /iter iter 1 add def } if
690 } forall
691 /noXY { CP /cY X /cX X } stopped def
692 tM setmatrix noXY { newpath } { cX cY moveto } ifelse
693 } {
694 Hide OE Unhide
695 } ifelse
696 } B
697 % \end{macrocode}
698 % \end{macro}
699 %
700 % \begin{macro}{normalize}
701 % This function accepts three numbers, $x$, $y$, and $s$, and returns
702 % $sx/\sqrt{x^2+y^2}$ and $sy/\sqrt{x^2+y^2}$.
703 % \begin{macrocode}
704 /normalize {
705 2 index dup mul 2 index dup mul add sqrt div
706 dup 4 -1 roll exch mul 3 1 roll mul
707 } B
708 % \end{macrocode}
709 % \end{macro}
710 %
711 % \begin{macro}{replace}
712 % This routine is called for every string in the string list. It
713 % must determine the translation, scaling, and rotation necessary to
714 % move a piece of \TeX code to the proper orientation. It stores in
715 % \pscom{replArray}, for each alignment/scale/rotation combination, a
716 % transformation matrix that achieves this.
717 % \begin{macrocode}
718 /replace {
719 aload pop MD
720 % \end{macrocode}
721 % Trace out the text and determine its bounding box. We need to
722 % temporarily revert to an identity transformation matrix, so that
723 % we can be sure to get the tightest bounding box possible. If the
724 % text is rotated, the bounding box that PostScript produces with the
725 % \pscom{pathbbox} command is not optimal.
726 %
727 % We also need to save the position of the point following the text
728 % placement, and move to that position when we've finished, to fool
729 % the PS interpreter into thinking we actually drew that text.
730 % \changes{3.01}{1996/11/22}{Modified this transformation to put the
731 % beginning of the text at the origin, and forced its width to be
732 % $\sim5$ in. Some figures were using coordinate systems that were
733 % so ``large'' that they would break PostScript with
734 % ``limitcheck'' errors here.}
735 % \changes{3.01}{1996/12/03}{The added transformation is no longer
736 % ``slipped under'' the CTM, but now it is more properly appended.
737 % this should allow the replacements to move properly if the entire
738 % figure is rotated or transformed. This required changes throughout
739 % the code, but primarily here.}
740 % \changes{3.03}{1997/01/06}{Added a \pscom{gsave}/\pscom{grestore}
741 % pair around the bounding-box calculations. I thought I was being
742 % clever by avoiding it; but it wasn't infallible, so I was wrong.}
743 % \changes{3.04}{1998/04/11}{Changed \pscom{initmatrix} to \pscom{sM}
744 % \pscom{setmatrix}, to be more EPS-friendly. Thanks to J. Scott Berg.}
745 % \begin{macrocode}
746 CP /bY X /lX X gsave sM setmatrix
747 str stringwidth abs exch abs add dup 0 eq
748 { pop } { 360 exch div dup scale } ifelse
749 lX neg bY neg translate newpath lX bY moveto
750 str { /ch X ( ) dup 0 ch put false charpath ch Kproc } forall
751 flattenpath pathbbox [ /uY /uX /lY /lX ] MD
752 CP grestore moveto
753 % \end{macrocode}
754 % If the FontMatrix shows that the font is draw in the opposite X or
755 % opposite Y direction, then the X or Y coordinates of the bounding
756 % box, respectively, need to be swapped. This actually occurs quite
757 % often. Here we also determine the center of the box.
758 % \begin{macrocode}
759 currentfont /FontMatrix get dstFM copy dup
760 0 get 0 lt { uX lX /uX X /lX X } if
761 3 get 0 lt { uY lY /uY X /lY X } if
762 /cX uX lX add 0.5 mul def
763 /cY uY lY add 0.5 mul def
764 % \end{macrocode}
765 % If debug mode has been enabled, we draw the bounding box, the
766 % baseline, and the center lines of the text.
767 % \begin{macrocode}
768 debugMode { gsave 0 setgray 1 setlinewidth
769 lX lY moveto lX uY lineto uX uY lineto uX lY lineto closepath
770 lX bY moveto uX bY lineto lX cY moveto uX cY lineto
771 cX lY moveto cX uY lineto stroke
772 grestore } if
773 % \end{macrocode}
774 % Add each replacement's alignment to \pscom{replArray}.
775 % \changes{3.02}{1996/12/01}{Now saves the current CTM here.}
776 % \begin{macrocode}
777 dstFM dup invertmatrix dstM CM srcM
778 2 { dstM concatmatrix } repeat pop
779 getrepl /temp X
780 S str convert get {
781 % \end{macrocode}
782 % Retrieve the replacement information
783 % \begin{macrocode}
784 aload pop [ /rot /scl /loc /K ] MD
785 % \end{macrocode}
786 % Determine the PostScript alignment point.
787 % \begin{macrocode}
788 /aX cX def /aY cY def
789 loc {
790 dup 66 eq { /aY bY def } { % B
791 dup 98 eq { /aY lY def } { % b
792 dup 108 eq { /aX lX def } { % l
793 dup 114 eq { /aX uX def } { % r
794 dup 116 eq { /aY uY def } % t
795 if } ifelse } ifelse } ifelse } ifelse pop
796 } forall
797 % \end{macrocode}
798 % Store the replacement key, and the transformation information, in
799 % \pscom{replArray}. These transformations are appended to the CTM
800 % encountered when \pscom{Place} is run. Indeed, this and
801 % \pscom{Place} are the two most difficult pieces of code to
802 % understand.
803 % \changes{3.03}{1996/12/03}{The added transformation is no longer
804 % ``slipped under'' the CTM, but now it is more properly appended.
805 % this should allow the replacements to move properly if the entire
806 % figure is rotated or transformed. This required changes throughout
807 % the code, but primarily here.}
808 % \begin{macrocode}
809 K srcFM rot tM rotate dstM
810 2 { tM concatmatrix } repeat aload pop pop pop
811 2 { scl normalize 4 2 roll } repeat
812 aX aY transform
813 /temp temp 7 add def
814 } forall
815 temp setrepl
816 } B
817 % \end{macrocode}
818 % \end{macro}
819 %
820 % \begin{macro}{rs}
821 % \begin{macro}{rks}
822 % \begin{macro}{ras}
823 % \begin{macro}{rws}
824 % \begin{macro}{raws}
825 % These macros intercept calls to their PostScript
826 % counterparts and execute \pscom{replace} in their stead if the
827 % argument string matches one in the replacement list.
828 % \changes{3.03}{1996/12/17}{Corrected typo for \pscom{raws}.}
829 % \begin{macrocode}
830 /Rif {
831 S 3 index convert known { pop replace } { exch pop OE } ifelse
832 } B
833 /XA { bind [ /Kproc /str } B /XC { ] 2 array astore def } B
834 /xs { pop } XA XC
835 /xks { /kern load OE } XA /kern XC
836 /xas { pop ax ay rmoveto } XA /ay /ax XC
837 /xws { c eq { cx cy rmoveto } if } XA /c /cy /cx XC
838 /xaws { ax ay rmoveto c eq { cx cy rmoveto } if }
839 XA /ay /ax /c /cy /cx XC
840 /raws { xaws { awidthshow } Rif } B
841 /rws { xws { widthshow } Rif } B
842 /rks { xks { kshow } Rif } B
843 /ras { xas { ashow } Rif } B
844 /rs { xs { show } Rif } B
845 % \end{macrocode}
846 % \end{macro}\end{macro}\end{macro}\end{macro}\end{macro}
847 %
848 % \begin{macro}{restore}
849 % In Level 1 PostScript, this is necessary because global variables
850 % do not exist (and we need one!). Every call to \pscom{restore}
851 % wipes out any variables that were modified since the previous call
852 % to \pscom{save}. Unfortunately, this is not guaranteed to work,
853 % although it does work for \pkg{dvips}.
854 %
855 % For Level 2, we can use global variables so this is not necessary.
856 % We make this distinction \emph{at print time}, so a file that does
857 % not seem to work on a Level 1 printer for which it was originally
858 % intended should still work on a Level 2 printer without
859 % modification.
860 % \changes{3.04}{1998/04/11}{Added a fix recommended by J. Scott Berg.}
861 % \begin{macrocode}
862 /rrs { getrepl dup 2 add -1 roll //restore exec setrepl } B
863 % \end{macrocode}
864 % \end{macro}
865 %
866 % \begin{macro}{Dict}
867 % This dictionary is added to the stack just before the figure, so
868 % that its definitions of the \pscom{show} operators can substitute
869 % for the originals.
870 % \changes{3.02}{1996/12/03}{Made \pscom{Dict} read-only to help
871 % track down problems.}
872 % \changes{3.03}{1996/12/17}{Corrected typo in \pscom{awidthshow}.}
873 % \begin{macrocode}
874 PSfragDict begin
875 islev2 not { /restore { /rrs PSfrag } B } if
876 /show { /rs PSfrag } B
877 /kshow { /rks PSfrag } B
878 /ashow { /ras PSfrag } B
879 /widthshow { /rws PSfrag } B
880 /awidthshow { /raws PSfrag } B
881 end PSfragDict RO pop
882 end
883 % \end{macrocode}
884 % \end{macro}
885 %
886 % \begin{macrocode}
887 %</filepro>
888 % \end{macrocode}
889 %
890 % \begin{macrocode}
891 %<*package>
892 % \end{macrocode}
893 %
894 % \section{The scanner}
895 % This code implements the \TeX-based scanner that performs the work
896 % that the \pfg\ Perl script used to perform. This is
897 % very similar to the code used
898 % to scan for bounding boxes in \fname{graphics.sty}.
899 %
900 % We need to read the string with two sets of catcodes: as `verbatim
901 % text' for using in the first argument of |\psfrag|, and as normal
902 % \TeX\ commands for using in the replacemnt text argument.
903 % The only way to do this sensibly in standard \TeX\ is to write
904 % the string to a file and then read it back. So while reading the
905 % EPS file, the character |\| will be `active'.
906 % Then the argument can be duplicated
907 % before writing this temp file, once with |\| expanding to |\\| (two
908 % catcode 12 tokens) and once to |\| (one catcode 12 token). When this
909 % is read back, these tokens will be re-catcoded by the |psfrag|
910 % command.
911 % \begin{macrocode}
912 \newwrite\pfg@temp
913 % \end{macrocode}
914 %
915 % \begin{macro}{\pfg@scan}
916 % The main command. |#1| is the name of the file. It might be better
917 % to use the bounding box (`|.bb|') file name so
918 % that compressed files could work by you copying any lines with
919 % |(\\tex|\ldots strings in them to the |bb| file before compressing.
920 % However, if you are
921 % doing that, you may as well just move the |\psfrag| commands to the
922 % main \TeX\ file. So currently compression is not supported by this
923 % system.
924 % \begin{macrocode}
925 \def\pfg@scan#1{\begingroup
926 % \end{macrocode}
927 % First the standard making `safe' of illegal characters.
928 % \begin{macrocode}
929 \@tempcnta\z@
930 \loop
931 \ifnum\@tempcnta<\@xxxii
932 \catcode\@tempcnta12 \advance\@tempcnta\@ne
933 \repeat
934 % \end{macrocode}
935 %
936 % Now normalise some catcodes we need. Most things are treated verbatim,
937 % but |{}| have their normal catcodes so |\read| will read multi-line
938 % |\\tex| expresions; and, as discussed above, |\| is made active.
939 % The end of a line is treated as space. Originally this meant that
940 % multi-line |\tex| commands did not work, but with v3.02, the PS side
941 % of things normalises white space to `space', as well so everyone is in
942 % agreement.
943 % \begin{macrocode}
944 \let\do\@makeother\dospecials\catcode`\ 10 %
945 \catcode`\{=1\catcode`\}=2\catcode127=12 %
946 \catcode`\\=\active\catcode\endlinechar5 %
947 % \end{macrocode}
948 %
949 % Open the scratch file. Don't complain if it already exists as that
950 % is probably just a previous graphic, or a previous run.
951 % \fname{pfgguide.tex} warns that any existing file of this name will
952 % be zapped, so: you've been warned.
953 % \begin{macrocode}
954 \immediate\openout\pfg@temp=\jobname.pfg %
955 % \end{macrocode}
956 %
957 % If the graphic file is not there, complain, else start reading it
958 % line by line. Look at each line with |\pfg@find|. Unlike the looking
959 % for |%%BoundingBox| where you can stop once you've found it, here
960 % you need to go to the end of the file looking for all the occurrences
961 % of |\tex| so if you give it megabytes of data, this may take a while.
962 % \begin{macrocode}
963 \immediate\openin\@inputcheck=#1 %
964 \ifeof\@inputcheck
965 \PackageWarning{psfrag}{Could not scan #1...}\endgroup
966 \else
967 \message{<psfrag:#1>}%
968 \@tempswatrue
969 \loop
970 \ifeof\@inputcheck
971 \@tempswafalse
972 \else
973 \read\@inputcheck to\@tempa
974 \expandafter\pfg@find\@tempa{\@nil}%
975 \fi
976 \if@tempswa
977 \repeat
978 \closein\@inputcheck
979 \fi
980 \immediate\closeout\pfg@temp
981 \endgroup
982 \@input{\jobname.pfg}}
983 % \end{macrocode}
984 % \end{macro}
985 %
986 % \begin{macro}{\pfg@find}
987 % \changes{3.03}{1997/01/07}
988 % {Find strings even if nested inside brace groups}
989 % This command first looks for an explicit brace group |{ }|.
990 % (one definitely exists as |{\@nil}| is added at the end as
991 % a `marker').
992 % \begin{macrocode}
993 \long\def\pfg@find#1#{\pfg@finda{#1}}
994 % \end{macrocode}
995 % \end{macro}
996 %
997 % To enable an `active' copy or |\| to be accessed easily, use a special
998 % lowercase table while defining these macros.
999 % \begin{macrocode}
1000 \begingroup
1001 \lccode`\~=`\\
1002 \lowercase{\endgroup
1003 % \end{macrocode}
1004 %
1005 % \begin{macro}{\pfg@finda}
1006 % If the group found contained the |\@nil| marker stop, else
1007 % start looking for |(\\tex| in the tokens before the group.
1008 % \begin{macrocode}
1009 \long\def\pfg@finda#1#2{%
1010 \def\@tempa{#2}%
1011 \ifx\@tempa\@nnil
1012 \else
1013 \pfg@findb#1(~~tex(~~tex%
1014 \fi}
1015 % \end{macrocode}
1016 % \end{macro}
1017 %
1018 % \begin{macro}{\pfg@findb}
1019 % Having found a brace group, look for the string |\\tex| in the
1020 % preceding tokens. If there is such, discard any tokens before that
1021 % then take all tokens after |\\tex| (up to the brace already found)
1022 % to be the optional arguments. In that case write out a suitable
1023 % call to |\psfrag|, and then continue after the brace group.
1024 % Otherwise if no |\\tex| is found, Add the contents of the brace group
1025 % (without the braces) back in front of the list before restarting the
1026 % search. In that way any strings inside brace groups will be found.
1027 %
1028 % As |{}| have their normal catcodes, multiline |\tex| commands work
1029 % as long as any line breaks occur inside the main |{ }| argument, not
1030 % between the optional arguments (normally there is no space at all
1031 % there, so no possibility of a break). This is because \TeX\ will
1032 % read more than a line if necessary to ensure that braces balance.
1033 %
1034 % The arguments are delimited by |(\\tex| sequences:\\
1035 % |#1| Is always discarded; the tokens before any |(\\tex| string.\\
1036 % |#2| contains any optional arguments (complete with |[]| brackets).\\
1037 % |#3| Will be the `dummy tokens' placed at the end, unless
1038 % no |(\\tex| is in the string, in which case |#3| will be empty.
1039 % This argument is |\fi| delimited which allows for tail recursion
1040 % in a slightly sneaky way.
1041 % \begin{macrocode}
1042 \long\def\pfg@findb#1(~~tex#2(~~tex#3\fi{%
1043 \fi
1044 \ifx\box#3\box
1045 \else
1046 % \end{macrocode}
1047 % Since |\| is the PS escape character in strings, as well as the \TeX\
1048 % escape character, the following looks a bit weird, but it's probably
1049 % right. A |\| will appear in the PS string as |\\|, so\ldots
1050 %
1051 % First set |\| to be |\string| (recall |\| is active, and can be written
1052 % as |~| due to the |\lowercase| above).
1053 % This means that
1054 % |\\foo| expands to |\foo| (one catcode 12 |\| coming from |\string\|
1055 % and then a catcode 11 |foo|). This expansion is frozen by an |\edef|
1056 % for use in the second argument. Then redefine |\| to be |\relax|
1057 % so it is a non-expandable active token, which writes as itself.
1058 %
1059 % Now write a call to |\psfrag| to the temp file.
1060 % The `user' string |\foo| which was the literal PS |\\foo| thus ends
1061 % up as:\\
1062 % |\psfrag{|\ldots |\\foo| \ldots |}{| \ldots |\foo| |}%|
1063 %
1064 % When this file is read back |\psfrag| will read the first argument
1065 % verbatim, and the second argument with normal catcodes so finally
1066 % |\foo| gets to be a single token instead of 4, and is executed
1067 % as part of the replacement text.
1068 %
1069 % Finally reset |\@tempa| (which contatined the contents of the brace group
1070 % to empty, as we've done with that.
1071 % \begin{macrocode}
1072 {\let~\string
1073 \edef\@tempb{\@tempa}%
1074 \let~\relax
1075 \immediate\write\pfg@temp{%
1076 \string\psfrag\space
1077 {\string\\tex#2{\@tempa}}#2\@percentchar
1078 ^^J\@spaces\@spaces{\@tempb}\@percentchar}}%
1079 \let\@tempa\@empty
1080 \fi
1081 % \end{macrocode}
1082 %
1083 % Now start looking for the next brace group to test. First tip
1084 % the contents of |\@tempa| in front of the tokens not yet seen, so that
1085 % the contents of a brace group will be searched if they were not
1086 % used as the argument to |\tex|.
1087 % \begin{macrocode}
1088 \expandafter\pfg@find\@tempa}
1089 % \end{macrocode}
1090 % \end{macro}
1091 %
1092 %
1093 % Finally close the call to |\lowercase|.
1094 % \begin{macrocode}
1095 }
1096 % \end{macrocode}
1097 %
1098 % \begin{macrocode}
1099 %</package>
1100 % \end{macrocode}
1101 %
1102 %