function preprint(position,bgcolor)

% PREPRINT   "Prepares" figure for printing and shows it on the screen
%       as will be printed on paper.
%     PREPRINT(POSITION,BGCOLOR)  sets the "paperposition" property of
%       a current figure according to the numbers, specified by the
%       POSITION (vector of 4 numbers, in inches) and "color" position
%       property specified by the COLOR (vector of 3 numbers, each from
%       0 to 1).
%       POSITION and COLOR can also be specified by strings, which are
%       then translated into numbers from the "dictionary" file PPMACR.
%       If the program LINK (for creating complex text objects) is on the
%       MATLAB path, PREPRINT also "updates" the links between parts
%       of the complex texts for new size of the figure.
%     PREPRINT MACRO  displays the content of the PPMACR file.
%
%     Example:
%       PREPRINT([.5  5.75  7.5  5]) is equivalet to
%       PREPRINT('UPPER') will set the figure to be printed in
%       the upper half of the sheet of paper;
%       PREPRINT([0  0 11 8.5],[.7 .7 .7])  is equivalent to
%       PREPRINT('LANDSCAPE','YELLOW')  will set the figure to be spread
%       to the whole paper size in the "landscape' orientation and will
%       produce a yellow background.
%       PREPRINT TALLBLUE will specify both parameters in one word -
%       "tall" mode for position and blue background.
%
%       Users can add their own "macros" to the PPMACR file for
%       convenience.

%  Connections: PPMACR, FILE2STRMAT.M, and LINK.M (optional).

%  Kirill K. Pankratov    kirill@plume.mit.edu
%  April 3, 1994

 % Handle input .............................................
ismacr = 0; ispos = 0; isbgc = 0;
if nargin==1
  if isstr(position)
    pos = position; ismacr = 1; ispos = 1;
    if strcmp(pos,'macro'), ismacr = 2; end
  elseif length(position)==3, bgc = position; isbgc = 1;
  else, pos = position; ispos = 1;  end
end
if nargin==2
  pos = position; bgc = bgcolor; ispos=1; isbgc=1;
  if isstr(position)|isstr(bgcolor), ismacr=1; end
end

 % Read macro file if necessary .................................
if ismacr==1
  ppmacr = file2strmat('ppmacr');
elseif ismacr==2
  ppmacr = file2strmat('ppmacr','all');
  disp(ppmacr); return
end

 % Begin position processing ....................................
if ispos
  if isstr(pos)
    [pos,head] = findecho(ppmacr,pos);
    cm = strcmp(head,' macro,portrait')|strcmp(head,' macro,landscape');
    if strcmp(head,' portrait') | strcmp(head,' landscape')
      set(gcf,'paperorient',head)
      if pos(1)~=' '&pos(1)~='?'
        pos = eval(pos);
        set(gcf,'paperunits','inch','paperpos',pos)
      end
    elseif strcmp(head,' color')
      bgc = eval(pos); isbgc = 1;
    elseif cm
      ns = find(head==',');
      set(gcf,'paperorient',head(ns+1:length(head)))
      ns = find(pos==',');  % Separator comma
      bgc = eval(pos(ns+1:length(pos)));
      isbgc = 1;
      pos = eval(pos(1:ns-1));
      set(gcf,'paperunits','inch','paperpos',pos)
    end
  else
    if length(pos)==3
      bgc = pos; isbgc = 1;
    elseif length(pos)==4
      set(gcf,'paperposition',pos)
    elseif length(pos)<3|length(pos)>4
      disp([10 '  Error: position must contain 4 numbers' 10])
      return
    end
  end
end

 % Begin color processing .........................................
if isbgc
  if isstr(bgc)
    [bgc,head] = findecho(ppmacr,bgc);
    if strcmp(head,' color')
      bgc = eval(bgc);
    end
  else
    if length(bgc)~=3
      disp([10 '  Error: color must contain 3 numbers' 10])
      return
    end
  end
end
if bgc==[], bgc = [0 0 0]; end

 % Make full extent axes to support the background color ..........
if isbgc    % (This is spied from Chuck Packard's "slideprint.m")
  set(gcf,'color',bgc)
  ah = axes('units','normal','pos',[0 0 1 1]);
  set(ah,'xcolor',bgc, 'ycolor',bgc)
  set(ah,'xtick',[], 'ytick',[])
end

 % Make figure size as it will appear on paper ....................
oldunits = get(gcf,'units');        % Original figure units
oldpos = get(gcf,'position');       % Original figure position
ppos = get(gcf,'paperposition');    % Paper position
punits = get(gcf,'paperunit');      % Paper units
set(gcf,'units',punits)
ppos(3:4) = ppos(3:4)*.95;
set(gcf,'pos',ppos)                 % Set figure units and position

 % Update links (composed text object).............................
if exist('link.m')
  drawnow, pause(.5)
  link('update');
end
