%%%%%%%%%%%%%%%%%%%%%%   rlepsf.tex   %%%%%%%%%%%%%%%%
%                     Version 1.0 (beta test)
%
%    Comments, problems etc to :   cpr@maths.warwick.ac.uk
%
%    Macros to relabel an eps file with TeX labels,  
%    written by Michael Greene, with TeX front end 
%    written by Colin Rourke. 
%
%    Designed for use with the macro package epsf.tex 
%    and dvips converter (written by Rokicki and Knuth) 
%    and with eps output from a variety of drawing 
%    packages (tested with output from xfig and Adobe 
%    Illustrator, and with hand-drawn figures). 
%    
%    To use this package type  \input rlepsf  (in place of
%    the usual  \input epsf ).  Then place each eps file
%    in a  \relabelbox ... \endrelabelbox  with relabelling
%    instructions after the  \epsfbox{..} .
%
%    The eps file is called exactly as usual with epsf.tex
%    typical instructions being  \epsfbox{filename.eps}
%    and   \epsfxsize dimension \epsfbox{filename.eps}
%    (where "dimension" is the required finished width of 
%    the diagram).
%
%    There are three (re)labelling macros:
%
%       \relabel {pslabel}{TeX label}
%       \adjustrelabel <dx,dy> {pslabel}{TeX label}  
%       \extralabel <x,y> {extra label}
%
%    \relabel  uses the ps file position for the new TeX
%    label (which can be any TeX output).  The new label
%    is positioned with same reference point (left-hand 
%    end of baseline) as the original ps label.  
%    \adjustrelabel  adjusts the position by  dx,dy  and 
%    is useful for fine tuning the position of the
%    new label.  \extralabel  allows extra TeX labels to be
%    inserted.  The positions x,y are measured from the
%    bottom right-hand corner of the box (so x is typically
%    negative) !  The  re/extra-labelling instructions can
%    be given in any order and any TeX dimensions can
%    be used for x,y,dx,dy.   
%
%    The result is a box of dimensions the original epsbox
%    but with new TeX labels.  This can then be inserted in
%    $$ ... $$  or \centerline{ ... }  for display, or
%    combined with other boxes to form a larger display.
%
%    The  \relabelbox ... \endrelabelbox  forms a TeX group;
%    thus instructions for resizing text for the new labels
%    (eg  \small ) will remain local to this group.
%
%    There are two main uses for the package :   
%
%    (1)  Creating graphics with TeX labels :   
%    Draw the graphic with dummy ps labels (all different and in
%    some common font eg Times-Roman).  Then use \relabel 'ling 
%    instructions to replace the dummy labels by the genuine TeX
%    labels.  Use roughly the same size dummy labels as the final
%    TeX labels and then the position will probably be perfect
%    first time.  Adjust the positions if necessary by editing
%    \relabel  to  \adjustrelabel  with appropriate  <dx,dy>.
%
%    (2)  Editing the ps labels in an existing eps file to TeX
%    labels :   
%    Use  \relabel  and  \adjustrelabel  to replace the 
%    existing labels.  You may have to use  \extralabel  for
%    some of the new labels (or to edit the eps file to change
%    repeated ps labels to be different), see note (2) below.
%
%    Notes:  
%    (1)  All ps labels are removed whether or not
%    instructions for relabelling are given. 
%    (2)  If the eps file has two or more labels the
%    same, then only one will be replaced.  Therefore
%    if you are designing a diagram from scratch then
%    use different temporary ps labels for your labels
%    even if you wish them to finish the same.  If
%    you are relabelling an existing eps diagram with TeX
%    labels then use \extralabel's to replace the other 
%    label(s) or edit the eps file to change the repeated
%    labels to different ones.
%    (3)  The macros are sensitive to extra spaces and 
%    the above syntax should be used.  
%    (4)  The output will not view correctly on a dvi
%    viewer which will typically show the new labels
%    superimposed at the bottom right-hand corner.  
%    However those produced by  \extralabel  will be shown
%    correctly positioned.
%
%    A typical example of use is appended.   More realistic
%    examples are given in the postscript documentation file
%    rlepsdoc.ps.                            CPR   16-12-96
%
%
\input epsf
%
% Initialise for MTG control
%
\special{! (MTG set-up) pop userdict /MTGdict 99 dict put MTGdict begin
  /Mshow {[ MTGdict /rllist get aload length 2 add -1 roll
    currentpoint transform ] MTGdict /rllist 3 2 roll put} def
  /findlabel {/sought exch def
    /MTGx 3 index def /MTGy 2 index def aload length 3 idiv dup 0 eq
      {pop}
      {{2 index sought eq
          {/MTGy exch def /MTGx exch def pop}
          {pop pop pop}
        ifelse}
      repeat}
    ifelse exch MTGx exch sub exch MTGy exch sub idtransform translate
    } def
  end}%
%
\def\relabelbox{%
  \hbox\bgroup%
  \special{ps:(MTG takes control) pop 
    /MTGsavestate save def /p /show load def MTGdict begin gsave
    /rllist [] def /save {false} def /restore {pop} def /show {Mshow} def
    /ashow {Mshow pop pop} def /widthshow {Mshow pop pop pop} def /awidthshow
    {Mshow 5 {pop} repeat} def /xshow {pop Mshow} def /yshow {pop Mshow} def
    /xyshow {pop Mshow} def /cshow {Mshow pop} def /kshow {Mshow pop} def}%
   \special{ps:/a {moveto} bind def}%
}%
\def\endrelabelbox{%
  \special{ps:(MTG signs off) pop grestore end /restore {restore} bind def
    MTGsavestate restore}\egroup%
}%
\def\relabel #1#2 {%
  \special{ps:/a {} def}%
  \special{ps:gsave /a {moveto} bind def transform MTGdict /rllist get (#1) findlabel}%
  \smash{\rlap{#2}}%
  \special{ps:grestore}%
}%
\def\adjustrelabel <#1,#2> #3#4 {%
  \special{ps:/a {} def}%
  \special{ps:gsave /a {moveto} bind def transform MTGdict /rllist get (#3) findlabel}%
  \smash{\rlap{\kern #1 \raise #2\hbox{#4}}}%
  \special{ps:grestore}%
}%
\def\extralabel <#1,#2> #3 {\smash{\rlap{\kern #1 \raise #2\hbox{#3}}}}%
\endinput
%
%%%%   Typical example of use :
%
\input rlepsf.tex
The line before the figure.\hfil\break
\centerline{%
\relabelbox            
\epsfxsize 3truein \epsfbox {figure1.eps}  
%%  Give label size instruction (eg \small) here. 
\adjustrelabel <0pt,1pt> {Ab}{$A^b$}
\adjustrelabel <-4pt,0pt> {P}{$P$}
\relabel {Bc}{$B_c$}
\extralabel <-.3truein,0.3truein> {First extra label}
\adjustrelabel <1pt,0pt> {IP"}{$\int P''$}
\adjustrelabel <1pt,0pt> {P'}{$P'$}
\extralabel <-1.5cm,1.5cm> {Second extra label}
\endrelabelbox
}
The line after the figure.

\bye

