[comp.lang.postscript] How to write T lambda, select 0.01 1.0E-2 1e-2 etc?

xiaofei@ACSU.BUFFALO.EDU (X. F. Wang) (06/02/91)

I wonder if there is an easier way to write
things like [normal]T[subscript]lambda ?
Or one has to write a ``T''
and then change position and font and write the 
lambda and then find position for next character?
In TeX, one doesn't need to write about it, one
simply writes $T_\lambda$. 

How do I select among
0.01 1.0E-2 1e-2 etc for show? I need them for labels.
I used to program in FORTRAN and this is done
by FORMAT control E5.2 F5.2 etc.

green@ohrd.uucp (Martin Green KR260_x7060) (06/04/91)

I wrote the following functions to provide flexible manipulation of
complex text.  The function xshow is used to do precise centering as would
be required when marking points on a data plot.

%!	text.pro
%   	Copyright (c) 1990 by M.A. Green,
%		Ontario Hydro Research Division, Toronto, CANADA
% ----------------------------------------------------------------------
%	Text manipulation functions including super- and sub-scripts,
%	multiple fonts and sizes, centering and right alignment, and
%	full justification.

%	Functions are also provided to permit printing of characters precisely
%	centred on the current point, such as would be required for marking
%	data points on a plot.

%	[string1 sups [string2 string3 subs string4] font2 string5] display
%		should print string1 with the current font
%		then go into superscript mode for string2 and string3
%		then print string4 as a subscript to string3
%		then return to the original font, followed by a change to font2
%		and finally print string5.
%		The positioning and angle of the printed text are
%		determined by the /textalign and /textangle variables.

%	Font selection
%		To select Times-Roman 12 point font use:	12 TR
%
/TR	{dup /PS exch def 
		/Times-Roman findfont exch scalefont setfont} def
/TB	{dup /PS exch def
		/Times-Bold findfont exch scalefont setfont} def
/TI	{dup /PS exch def
		/Times-Itatic findfont exch scalefont setfont} def
/TBI	{dup /PS exch def
		/Times-BoldItalic findfont exch scalefont setfont} def
/H	{dup /PS exch def
		/Helvetica findfont exch scalefont setfont} def
/HB	{dup /PS exch def
		/Helvetica-Bold findfont exch scalefont setfont} def
/HO	{dup /PS exch def
		/Helvetica-Oblique findfont exch scalefont setfont} def
/HBO	{dup /PS exch def
		/Helvetica-BoldOblique findfont exch scalefont setfont} def
/S	{dup /PS exch def /Symbol findfont exch scalefont setfont} def
/C	{dup /PS exch def /Courier findfont exch scalefont setfont} def

/STRING (a) type cvlit def
/ARRAY [0] type cvlit def
/LEFT -1 def
/CENTRE 0 def
/RIGHT 1 def
/CENTER CENTRE def

/fontstore 5 array def
/vshift 5 array def
/fontlevel 0 def
/pushfont {
	fontstore exch [ exch PS ] fontlevel exch put
	/fontlevel fontlevel 1 add def
} def
/popfont {
	0 vshift fontlevel get neg rmoveto
	vshift fontlevel 0 put
	/fontlevel fontlevel 1 sub def
	fontstore fontlevel get aload pop /PS exch def setfont
} def

/sups {		%	adjust position and reduce font size
	{currentfont dup pushfont .8 scalefont setfont
	vshift fontlevel PS .4 mul put
	0 PS .4 mul rmoveto
	/PS PS .8 mul def}
} def
/subs {		%	adjust position and reduce font size
	{currentfont dup pushfont .8 scalefont setfont
	vshift fontlevel PS -.3 mul put
	0 PS -.3 mul rmoveto
	/PS PS .8 mul def}
} def

/isstring {		%	return true if argument is a string
	type cvlit STRING eq
} def
/isarray {		%	return true if argument is an array
	type cvlit ARRAY eq
} def

/displaywidth {		%	compute width of complex text array
	/textwidth 0 def
	/textheight PS def
	/fontlevel 0 def
	/displevel 0 def
	{		%	step through array elements
		dup isstring {
			stringwidth pop /textwidth exch textwidth add def
			fontlevel displevel sub 1 eq {popfont} if
		} {
		dup xcheck {
			exec
		} {
		dup isarray {
			displaywidth2
			fontlevel displevel sub 1 eq {popfont} if
		} { %	error handler
			pop
		} ifelse } ifelse } ifelse
	} forall
} def

/displaywidth2 {
	/displevel displevel 1 add def
	{		%	step through array elements
		dup isstring {
			stringwidth pop /textwidth exch textwidth add def
			fontlevel displevel sub 1 eq {popfont} if
		} {
		dup xcheck {
			exec
		} {
		dup isarray {
			displaywidth2
			fontlevel displevel sub 1 eq {popfont} if
		} { %	error handler
			pop
		} ifelse } ifelse } ifelse
	} forall
	/displevel displevel 1 sub def
} def

/display {
	currentpoint 3 -1 roll
	/textwidth 0 def
	exactwidth 0 ne textalign LEFT ne or  % compute dimensions if required
		{dup displaywidth} if

	%	adjust starting position and angle
	3 1 roll moveto
	textalign LEFT ne {
		textalign RIGHT eq { textwidth neg 0 rmoveto }
		{ textwidth 2 div neg 0 rmoveto } ifelse
	} if
	%	print text
	/fontlevel 0 def
	/displevel 0 def
	{
		dup isstring {
			show
			fontlevel displevel sub 1 eq {popfont} if
		} {
		dup xcheck {
			exec
		} {
		dup isarray {
			display2
			fontlevel displevel sub 1 eq {popfont} if
		} { %	error handler
			pop
		} ifelse } ifelse } ifelse
	} forall
} def

/display2 {
	/displevel displevel 1 add def
	{	%	step through array elements
		dup isstring {
			show
			fontlevel displevel sub 1 eq {popfont} if
		} {
		dup xcheck {
			exec
		} {
		dup isarray {
			display2
			fontlevel displevel sub 1 eq {popfont} if
		} { %	error handler
			pop
		} ifelse } ifelse } ifelse
	} forall
	/displevel displevel 1 sub def
} def

/xshow {
	dup pointdict exch known {
		pointdict begin load end currentpoint 3 -1 roll
		currentfont exch aload pop
		rmoveto exec show setfont moveto
	} { pop } ifelse
} def

/xsetup {
	%	/name (string) {nn FF} xsetup
	pointdict begin
	3 -1 roll [ 4 2 roll		% /name [ ( ) { }
	dup exec 1 index		% /name [ ( ) { } ( )   -- font set
	newpath 0 0 moveto false charpath flattenpath pathbbox
	4 2 roll 2 copy 6 -2 roll
	3 -1 roll sub -.5 mul 3 1 roll sub .5 mul 	% /name [ ( ) { } dy dx
	4 -1 roll sub 3 1 roll exch sub
	] def		% /name [ ( ) { } dx dy ] def
	end
} def

/pointdict 50 dict def
/. (\264) {12 TR} xsetup
/a (a) {8 H} xsetup
/b (b) {8 H} xsetup
/c (c) {8 H} xsetup
/d (d) {8 H} xsetup
/e (e) {8 H} xsetup
/f (f) {8 H} xsetup
/*1 (1) {8 H} xsetup
/*2 (2) {8 H} xsetup
/*3 (3) {8 H} xsetup
/*4 (4) {8 H} xsetup
/*5 (5) {8 H} xsetup
/*6 (6) {8 H} xsetup
/*7 (7) {8 H} xsetup
/*8 (8) {8 H} xsetup
/*9 (9) {8 H} xsetup
/x (x) {8 H} xsetup
/o (o) {8 H} xsetup
/O (O) {8 H} xsetup
/+ (+) {10 H} xsetup
/- (-) {10 S} xsetup
/* (*) {10 S} xsetup
/| (|) {8 H} xsetup
/de (o) {5 H} xsetup
/cix (\250) {8 H} xsetup
/*D (D) {8 S} xsetup
/diamond (\250) {8 S} xsetup
/mu (\264) {8 S} xsetup
/bu (\267) {8 S} xsetup
/xci (\304) {8 S} xsetup
/+ci (\305) {8 S} xsetup
/opendiamond (\340) {8 S} xsetup

/exactwidth 0 def
/textalign CENTRE def

/i {72 mul} def
4.25 i 6 i moveto
12 TR
%[(test) subs [(double) subs (sub)] sups (sup) ( test2)] display
%4.25 i 2 i moveto 0 2 i rlineto
%3.25 i 3 i moveto 2 i 0 rlineto stroke
%4.25 i 3 i moveto /x xshow
%4.75 i 3.5 i moveto /. xshow

/showx { isarray {
	st 1 blank putinterval
	currentpoint 3 -1 roll dup xshow 3 1 roll
	2 copy exch 36 add exch 3 sub moveto 12 H 3 -1 roll
	(                 ) cvs st exch 1 exch putinterval st show
	13 sub moveto
	} {pop} ifelse
} def

/showmarkers {
	/st 18 string def st 0 (/) putinterval
	/blank (                 ) def
	pointdict {showx} forall
} def

/markerpage {
	erasepage
	4.25 i 10 i moveto
	16 HB [(Data Markers For PSplot)] display
	/textalign LEFT def
	1 i 9 i moveto 13 H
	(To print marker at current point use, for example:  /+ci xshow) show
	1 i 8.5 i moveto
	showmarkers
	showpage
} def
%------------------------------------------------------------------------
-- 
Martin Green                        UUCP : torsqnt!ohrd!green@cs.toronto.edu
Ontario Hydro Research Division     Tel. :  (416)231-4111 Ext7060
800 Kipling Ave
Toronto, Ontario, CANADA, M8Z5S4