function preprint(varargin)
%
%PREPRINT sets properties of a figure and prepares it for nice printing. 
% 
%     PREPRINT   makes printing adjustments for current figure
%  
%     PREPRINT(AXLIM)   also sets axis scalings on the current plot. See 
%     AXIS for details. NAN's are ignored. Therefore AXLIM = [nan nan -2 2]
%     will lead to a result same as YLIM([-2 2]).     
%
%     PREPRINT(XLAB,YLAB,ZLAB,TIT)  also adds x-, y-, z- labels and the 
%     title TIT. 
%
%     PREPRINT(AXLIM, XLAB ,...) does both the scaling and labelling.
%
%     PREPRINT(AX, ...)   processes the axes with handle AX.
%
%     To use default values for any argument, pass in the empty matrix ([]). 
%
% Example:
% 
%     x = 2*pi*linspace(-1,1,1000).';
%     plot(x,[tan(x),sin(x),cos(x)]);
%     legend('tan','sin','cos')
%     preprint(pi*[-2 2 -1 1], 'angle  (rad)', 'ratio', [], 'Trignometric ratios')
%

% Mukhtar Ullah
% mukhtar.ullah@informatik.uni-rostock.de
% December 20, 2004

[ax,args,nargs] = axescheck(varargin{:});
if isempty(ax), ax = gca; end
axlims = [];
if (nargs > 0) && ~isempty(args{1}) && isnumeric(args{1})
    axlims = args{1};
    args = args(2:end);
    nargs = nargs - 1;
end
labchk = nargs > 0:3;
if labchk(1), xlabel(ax, args{1}), end
if labchk(2), ylabel(ax, args{2}), end
if labchk(3), zlabel(ax, args{3}), end
if labchk(4), title(ax, args{4}), end
h = get(ax,{'position','TightInset','XLabel','YLabel','ZLabel','Title'});
set([h{3:end}], 'FontSize', 8);
dy = [.035-h{1}(2)+h{2}(2); h{1}(2)+h{1}(4)+h{2}(4)-.965];
dy(dy<0) = 0;
h{1}([2,4]) = [h{1}(2) + dy(1); h{1}(4) - labchk(4).*dy(2)];
set(ax, 'Position', h{1}, 'FontSize', 8);
set(gcf, 'PaperType', 'A4', 'PaperOrientation', 'portrait', ...
         'PaperPositionMode', 'manual', 'PaperUnits', 'centimeters', ...
         'PaperPosition', [2 2 8 6.3]);
if ~isempty(axlims)
    nnchk = zeros(1,8);
    n = numel(axlims);
    nnchk(1:n) = ~isnan(axlims);    
    if any(n==[4 6 8]) && all(nnchk(1:n))
        axis(ax,axlims);
    elseif nnchk(1:2)
        xlim(ax,axlims(1:2))
    elseif nnchk(3:4)
        ylim(ax,axlims(3:4))
    elseif nnchk(5:6)
        zlim(ax,axlims(5:6))
    end
end
box(ax,'on')