%Started May 1996, C.G. van der laan, cgl@rc.service.rug.nl
%Version June 1996
\newif\ifdraft\drafttrue
\draftfalse
\ifdraft\else\input psfig \fi
%Watch out for double ! in verbatim, especially with %!!PS
\input blue.tex \loadtocmacros\tolerance500\hbadness=499\hfuzz=5pt
\parindent=1pc\everyverbatim{\unmc}
%Needed EPS files: baker.eps, barnwindowii.eps, bentley.eps,
% eschera.eps, escherb.eps, escherknotmf.eps,
% escherknotiii.epsabr, flower.eps,
% gurariabc.eps, gurarisq.eps
% jvr.eps, linebundle.eps, malbork.eps,
% polygon.eps, polygonii.eps, prpie.eps,
% sine.eps, sinedisks.eps, seal.eps,
% textalongarc.eps, textalongspiral.eps.
\bluetitle Paradigms: Just a little bit of PostScript
\blueissue \maps{96}2
\bluepictures\metapostpic\pstexpic
\blueabstract It is all about creating EPS\Dash with graphics\Dash
to be merged with \AllTeX{} scripts.
The emphasis is on creating raw PostScript for simple symmetrical pictures.
Asides, like
incorporating accurate graphs of math functions,
typesetting text along curved paths, or
tables set sideways have been addressed.
A poor man's mftoeps approach is touched upon:
(declarative) METAFONT into (imperative) PostScript.
\bluekeywords Declarative program, education, EPS, Escher, graphics,
imperative code, \AllTeX,
math graphs, METAFONT, merging EPS and \AllTeX,
MetaPost, mftoeps, PostScript,
sideways tables, text along curved paths.
\beginscript
\bluehead BLUe's Design X
Hi folks.
The user's guide which comes with BLUe's format system\Dash
Publishing with \TeX, PWT for short\Dash
is processed {\em completely\/} by \TeX, {\em no\/} other tools such as
\PS{} are needed.\ftn{Nobody knows what the future has in store, but for
the moment I consider it a good thing that the PWT guide can be processed
just by \TeX, well \dots with BLUe's format.}
However, of late I exercised \MF
\Dash well, eventually MetaPost with the help of Jos Winnink\Dash
for graphics to be included in \TeX{} documents, and finally embarked PostScript
straightaway to create EPS pictures.
$$\metapostpic$$
\PS{} is involuntary needed to (electronically) paste up the graphics, and
as resulting file format.\ftn{For exchange the {\tt.tex} and
(hand-coded) {\tt.eps} files are much better suited
because of their conciseness. This can't be beaten, not even by
Adobe's PDF\Dash Portable Document Format.}
If we come to think of graphics as
$$\vbox{\hbox{just doing the `right' strokes or fills}}$$
then \PS{} provides the means for this:
lines, splines and circular arcs,
to be drawn or filled.\ftn{PostScript II also provides for colors and
processing in a network.}
I use the sidestep
$$\vbox{\hbox{\MF{} $\rightarrow $MetaPost $\rightarrow$ EPS}}$$
for general pictures but also for obtaining the right (control) points
explicitly from a decalarative specification in \MF,
as shown by Escher's knot at the end.\ftn{Another way
for arriving at the EPS code is
to use Jackowski's |mftoeps| package or to use MetaPost.}
$$\pstexpic$$
With respect to graphics \PS{} can be seen as a {\em little\/} language
in the UNIX tradition.\ftn{See Bentley's Little languages in
`More programming pearls\Dash\-Confes\-sions from a coder.' Addison-Wesley.}
A little bit of \PS{} adheres the 80\%--20\% adage:
80\% of the effects (or more) with
20\% of the energy (or less).
One can with a little knowledge of \PS{} code graphics immediately
and {\em completely\/} in \PS.
The more so because of the ubiquitous public domain GhostScript
previewers to verify the result,
next to of course the \PS{} laser printers.
Furthermore, text is just a special case of graphics, and merging
just a little bit of text\Dash malenki Russians would say\Dash
with the graphics goes equally simple at first glance.
And to end the lovesong the inclusion of accurate graphs of mathematical functions
goes well via coding these in \PS{} and including these as figures.
(Of course Hobby's graph extension could be used as well, or other advanced
graphics packages.)
This is illustrated by a graph of the sine function to convey the idea.
PStricks is about {\em interfacing}. Not assuming knowledge of \PS.
This note discusses mainly {\em merging}.
Is about extending your \TeX pertise
with just a little\Dash tsjut-tsjut Russians would say\Dash
knowledge of \PS{} rewarded by high returns.
Below I'll summarize what is needed from \PS, and illustrate the use of it
with a few examples, introducing en route the operators we need given the example.
\bluehead PostScript
\bluesubhead Processing
\PS{} comes with a user's guide (cookbook) and reference manual,
the so-called blue and red books in the Adobe \PS{} series.
For processing \PS{} an interpreter is needed, such as
a \PS{} laser printer or a GhostScript previewer.
For inclusion in \AllTeX{} I use the psfig macros.\ftn{Courtesy Trevor J. Darrell.}
Goossens in his PostScript and \AllTeX, \maps{92}1, nicely details about
inclusion of PostScript.\ftn{See also the \LaTeX{} Companion.}
As with PDF I consider the post-processing capability {\em independently\/} from
the tool which created the \PS{} source, very powerful and flexible.
\bluesubhead Subset 0 from the language
\PS{} is stack-oriented.
This means that operations are prescribed in polish-reverse notation,
also known as prefix notation,
similar to the HP pocket calculators.
Addition\Dash use of operator |add|\Dash for example is notated as follows.
\beginverbatim
2 3 add%yields 5 on the stack, 2 3 consumed
!endverbatim
\PS{} is artificially structured via structure information in
comments, double \%-ed comments. Programs which obey the Adobe
structure are called conforming and this is usually needed for inclusion
within \AllTeX, especially the BoundingBox line is required.
\blueexample Conforming EPS structure
\beginverbatim
%!!PS EPS
%%Title:
%%Creator:
%%CreationData:
%%BoundingBox:
%%DocumentFonts: (atend)
%%EndComments
%%EndProlog
%%Page: 0 1
%%Page: 1 2
%%Trailer
<...>
%%DocumentFonts: Times-Roman ...
%%Pages: 3
%%EOF
!endverbatim
Creating and drawing paths is done by separate operators.
For creating paths operations like
|moveto| are provided while drawing goes via |stroke|.
\beginverbatim
0 0 moveto 0 10 lineto%create path
stroke%draw a v-line of 10pt height
!endverbatim
Variables\Dash names to be associated with their values\Dash
are handled via the so-called dictionaries.
The functionality can also be obtained via procedures.
\beginverbatim
/size {10} def
!endverbatim
The so-called literal name is preceded by a slash to distinguish the declaration
from its invoke. The invoke is done by just the name, also called executable name.
The procedure text is surrounded by curly braces.
Parameters are absent too. The (operand) stack is used.
For graphics we have a CurrentTransformMatrix\Dash CTM\Dash which maps the
user space on the device space, the printer or screen.
Equally powerful is the concept of encapsulating graphics via
|gsave| and |grestore|, that is the graphics state is local\Dash encapsulated\Dash
after |gsave| until |grestore|.
Next to the CTM \PS{} maintains the currentpoint and currentpath.
Batagelj, \maps{95}1\Dash Combining \TeX{} and \PS\Dash
provides an in a nutshell overview.\ftn
{Nice are the hints to remove repeated parts from files which are
generated by CorelDRAW and Mathematica, in order to reduce the size
of the automatically generated and to be included files.
(The idea is to remove duplicate `dictionaries' which are included with each result.)
The example of how to include graphs of math functions in a document is
{\em very\/} useful.
However, with respect to his first picture I would prefer to use the
inherent symmetry in the data as opposed to providing all the data.}
Another introduction is in Fokker en van Oostrum's `Plaatjes in een tekst,'
\maps{94}2, next to a survey of drawing software.
\bluesubhead Summary of (graphics) commands
The following snapshot is borrowed from Gurari, well \dots a little modified.\ftn{%
Gurari E.M (1994):
\TeX{} \& \LaTeX\Dash Drawing \& Literate Programming.
McGraw Hill. ISBN 0-07-025208-4.}
Its main purpose is to show that the number of relevant graphics primitives
is low.
The functionalities will be dealt with along the way in the examples.
For the details of the commands or the list of operators
see the red book.\ftn{A complete list with functional summaries is
in the red book section 6.2 Operator summary.}
$$\everyverbatim{\emc}
\vbox{\halign{\hfil#\cr
\bf Arithmetic and math operators\hfill\cr
|| || mul $num$ \cr
|| sine $num$ \cr
%
\bf Path construction operators\hfill\cr
currentpoint $x$ $y$ \cr
|| || moveto \cr
|| || rmoveto \cr
|| ||lineto \cr
|| || rlineto \cr
$\langle q_{1x}\rangle$ $\langle q_{1y}\rangle$ $\langle q_{2x}\rangle$
$\langle q_{2y}\rangle$ $\langle p_{2x}\rangle$ $\langle p_{2y}\rangle$
curveto \cr
$\langle c_x\rangle$ $\langle c_y\rangle$ $\langle r\rangle$
$\langle ang_1\rangle$ $\langle ang_2\rangle$ arc \cr
%
\bf String operators\hfill\cr
|| || || getinterval \cr
%
\bf Character and font operators\hfill\cr
{\tt\char92}|| findfont \cr
\ || scalefont setfont \cr
|| show \cr
|{}| || kshow \cr
%
\bf Graphics state operators\hfill\cr
|| setgray \cr
|| setlinewidth \cr
%
\bf Dictionary operators\hfill\cr
/| {}| def \cr
%
\bf Coordinate system and matrix operators\hfill\cr
|| || translate \cr
|| || scale \cr
|| rotate \cr
%
\bf Relational, boolean, and bitwise operators\hfill\cr
||\||| ||\||| le $bool$\cr
%
\bf Control operators\hfill\cr
| {} {}| ifelse \cr
| {}| repeat \cr
| {}| for \cr }
}$$
With prefix
$\langle in\rangle\> \langle operator\rangle\>\langle result\rangle$
is elegant.
\bluesubhead What is not allowed as EPS?
I'm not knowledgeable enough to answer that question, nor do I know of
a full-blown definition of EPS.
For the moment it is some subset which works with all interpretors,
with my subset 0 in there.
When one restricts oneself to the basics of graphics, arithmetic and similar
operations then the boundary area between EPS and full \PS\Dash
or its various implementations\Dash is not in sight.
\bluesubhead Documentation
The red book\Dash the reference manual\Dash is generally recommended, though
the blue book\Dash the tutorial and cookbook\Dash is also nice.\ftn{I used the
\PS I red book and this is well-suited to get the flavour.
For \TeX{} and METAFONT %\MF{} %font not available
this is similar. To grasp the basic ideas
Knuth's first book is a more concise survey of the main lines of thought than
\TB{} and {\sl The METAFONTbook}.}
\blueexample Pie chart from the blue book
$$\ifdraft\vbox to3in{\vss\hbox{Pie chart}\vss}
\else\psfig{file=prpie.eps,height=3in}
\fi$$
The invoke is essentially as follows and shows that the codes can be used
straightforwardly.\ftn{It is not standard \PS. We have to construct some kind of
library to use the PostScript programs from.
Maybe the CTAN as global network library?
Copied on the various CD-ROMS?}
It is no longer necessary to mesh around with the picture environment or so,
to achieve the effect.
\beginverbatim
%preliminary matter
(January Pie Sales)
24 12 %... array size
[ [(Blueberry) .12 ]
[(Cherry) .30 ]
[(Apple) .26 ]
[(Boston Cream) .16 ]
[(Other) .04 ]
[(Vanilla Cream) .12 ]
] 306 396%translate center to
140 %size
DrawPieChart
showpage
!endverbatim
There is also a \PS-FAQ, consult
|ftp wilma.cs.brown.edu:|\\
\null\qquad \qquad |pub/comp.lang.postscript|.
It contains an annotated bibliography as well.
The examples from Adobe's blue book are available on the net.
\bluesubhead Proofing
For previewing or printing, {\em as such\/}
I have to include a shift to move the picture away from the lower left corner,
say
\beginverbatim
300 500 translate
!endverbatim
\bluesubhead Inclusion
I usually build a figure symmetrically around the origin and then include
it via
\beginverbatim
$$\psfig{file=,height=}$$
!endverbatim
A unit can be |in|(ch), |cm|, and ilks. \cs{psfig} is very vulnerable to
spaces because of \TeX's parsing. So no spaces in there.
Now and then I forget to inactivate the |translate| needed while previewing.
No real problem.
\bluesubsubhead BoundingBox
Providing the right BoundingBox coordinates has all to do with
proper placement within context, the look-and-feel.
Default \PS{} assumes the origin\Dash in user space\Dash
at the lower left corner of the paper\Dash in device space.
Surround the picture by as-if lines and supply the coordinates, in points as units
in user space coordinates,
of the lower left corner and the upper right corner in the
BoundingBox specification.
Simple is to build a picture around its symmetry point\Dash
and let this coincide with the origin\Dash
with as pleasing result that the horizontal positioning comes out centered,
when used within math display.
Vertically, I add a 10 or so
extra on either side in the BoundingBox specification,
but that depends on the character of the picture.
Some preview systems can measure the BoundingBox and allow adjustment
interactively.\ftn{For a summary of tools to assist finding the BoundingBox
coordinates see, Reckdahl K (1995): Using EPS graphics in \LaTeX{}
documents. reckdahl@leland.stanford.edu.}
\bluesubhead Writing PostScript
A line bundle and a variant of it are introduced to
show how to create simple EPS.
\bluesubsubhead A line bundle
$$\ifdraft\vbox to1in{\vss\hbox{Line bundle}\vss}
\else\psfig{file=linebundle.eps,height=1in}
\fi$$
How to do this in \PS? A line as such is simple.
First a |moveto| and then a |lineto|. So a way is to create a loop and repeatedly
draw from the origin to the end of the various lines.
This can be done elegantly by using appropriately the CTM.
\beginverbatim
%!!PS EPS
%%Title: Line bundle
%%Creator: cgl
%%CreationDate: June 4 1996
%%BoundingBox: -40 -45 40 45
%%Pages: 1
%%EndProlog
%%Page: 1 1
/r 36 def
10{0 0 moveto r 0 lineto
36 rotate
}repeat stroke showpage
!endverbatim
Explanation.
The idea is that first a simple line is draw, for example along the x-axes.
What happens if after that we rotate? Right, the mapping is changed.
And what happens if we supply the {\em same\/} line after this?
Indeed, it will show up rotated. Because \PS{} is an interpretive
language we can realize this specification after the rotate via a loop,
which for this simple case reads |10{...}repeat|.\ftn{Do you see the variant
for drawing a polygon? This duality linebundle and polygon has been used
by Gabo and is about what he called stereometry versus perimetry,
the structure versus the surface}
\beginquote
Appropriately maintaining the CTM for symmetrical pictures can yield simple
looking \PS{} programs.
\endquote
\bluesubsubhead A flower
$$\ifdraft\vbox to1in{\vss\hbox{Flower}\vss}
\else\psfig{file=flower.eps,height=1in}
\fi$$
This exercises the use of |arc|.
\beginverbatim
%!!PS EPS
%%Title: Flower
%%Creator: cgl (Courtesy Papert)
%%CreationDate: June 4 1996
%%BoundingBox: -40 -45 40 45
%%Pages: 1
%%EndProlog
%%Page: 1 1
/r 36 def
10{r r moveto%begin drawing point
r 0 r 90 180 arc
currentpoint%origin
0 r r 270 360 arc
36 rotate
}repeat stroke showpage
!endverbatim
Explanation.
We have the same structure as the previous program but the `line' is now
a little more elaborated: two arcs of a circle.
\PS{} provides an operator for drawing circular arcs, called arc.
The arc has $(x,y)$ as centre, r as radius,
$ang_1$ the angle of a vector from $(x,y)$ of length $r$
to the first endpoint of the arc, and
$ang_2$ the angle of a vector from $(x,y)$ of length $r$
to the second endpoint of the arc.\ftn{The arc is drawn counter clockwise.
|arcn| draws clockwise.}
These arguments are expected to be on the stack.
\beginverbatim
x y r ang1 ang2 !bf arc
!endverbatim
Important is to realize that arc {\em counts\/} its angle from (x, 0) and that
the {\em drawing\/} starts from the point on the stack
The specification of the flower in \MF/\MP{} reads essentially as follows.
\beginverbatim
for k:= 1 upto 10:
draw(origin{up}..{right}(up+right){down}..
{left}origin) rotated 36k;
endfor
!endverbatim
Explanantion.
\MF{} allows for specification of the directions\ftn{There is also a
quartercircle which apart from orientation is drawn similarly.}
$up =(0,1), right=(1,0)$.
IMHO, with all respect the \MF{} and \PS{} programs are similar
modulo some syntactic sugar.
However, the extra possibility of specifying the directions is more convenient
than using control points.
But perhaps that is a matter of taste, although the handling of control points
is powerful as B\'ezier himself has shown in the past.
From this I conclude that for these simple kinds of pictures we can as well use
\PS{} straightaway.
\bluehead Some more Graphics
\blueexample Malbork window
This is all about using |curveto|, especially choosing suitable control points.
$$\ifdraft\vbox to1in{\vss\hbox{Malbork window}\vss}
\else\psfig{file=malbork.eps,height=1in}
\fi$$
\beginverbatim
%!!PS EPS
%%Title: Malbork Window
%%Creator: cgl
%%CreationDate: May 21 1996
%%BoundingBox: -40 -40 40 40
%%Pages: 1
%%EndProlog
%%Page: 1 1
45 rotate 10 0 moveto
4{20 0 37.5 12.5 25 25 curveto
12.5 37.5 0 20 0 10 curveto
90 rotate
}repeat%inside lops next
5 0 moveto
4{5 35 35 5 0 5 curveto
90 rotate
}repeat%enclosing circle next
36 0 moveto
0 0 36 0 360 arc
stroke showpage
!endverbatim
Explanation.
|translate| changes the CTM,
with the effect that the device coordinates are shifted. (Useful for
use of \PS{} alone out of context.)
|rotate| changes the CTM, and because of being an interpretive language
the various loop traversals map the {\em same\/} user coordinates on the
rotated device coordinates.
$\langle number\rangle \{\dots\}$ |repeat| is a loop to be traversed
$\langle number\rangle$ of times.
|curveto| adds a spline to the current path
from the currentpoint to the last point on the stack.
The first two points are the so-called control points of the spline.\ftn{Much similar
as in METAFONT. Choosing for the inner lop the control
points in this way is borrowed from
Haralambous Y (1995): Some METAFONT techniques.
\tubissue{16}(1), 46\dash53. It is also supplied in the description
of |curveto| in the red book.}
|arc| adds a circular arc to the current path from the currentpoint.
The details of the arguments for the operators are nicely documented in the
red book.
\blueexample Escher's impossible triangle
This is all about {\em wrong\/} projections.
However, these kinds of pictures are intriguing and fun.
I consider them well-suited to illustrate \PS's drawing capabilities.
$$\ifdraft\vbox to1in{\vss\hbox{Escher's impossible triangle}\vss}
\else\psfig{file=eschera.eps,height=1in}
\fi$$
\beginverbatim
%!!PS EPS
%%Title: Escher's impossible triangle
%%Creator: cgl (inspired by Guy Shaw)
%%CreationDate: May 23 1996
%%BoundingBox: -40 -40 40 40
%%Pages: 1
%%EndProlog
%%Page: 1 1
3{25 34 moveto
25 -34 lineto
17 -38.2 lineto
17 20 lineto
-17.6 0 lineto
120 rotate
}repeat stroke showpage
!endverbatim
\on5{} points, the right stroke and a rotation or two, that's it.
End of story.
However, it is all about finding those \on5{} points.
$$\ifdraft\vbox to1in{\vss\hbox{the 5 points}\vss}
\else\psfig{file=escherb.eps,height=1in}
\fi$$
\beginverbatim
%!!PS EPS
%%Title: Essential stroke
%%Creator: cgl (inspired by Guy Shaw)
%%CreationDate: May 23 1996
%%BoundingBox: -40 -40 40 40
%%Pages: 1
%%EndProlog
%%Page: 0 1
25 34 moveto currentpoint
0 -68 rlineto currentpoint%down
-120 rotate
25 34 lineto%preserve symmetry
120 rotate currentpoint
17 20 lineto currentpoint
-17.6 0 lineto currentpoint
%labels
/Courier findfont 8 scalefont setfont
moveto -5 -3 rmoveto (5) show
moveto 1 1 rmoveto (4) show
moveto 2 -5 rmoveto (3) show
moveto 2 0 rmoveto (2) show
moveto 2 0 rmoveto (1) show
stroke showpage
!endverbatim
Explanation.
The essential stroke figure also illustrates the integration
of text in this case digits.
|currentpoint| pushes the point on the stack.
The last |moveto|-s pop these coordinates up.
|rmoveto| moves {\em relatively.}
\smallbreak
And what about their relationships, and
what about the minimal information to be prescribed?
Looking more closely it turns out that {\em only\/} the first point is all
that is needed.
The rest is implicit to the nature of the figure.\ftn{Of course
one can also think of other equivalent parameters like size and thickness.}
\beginverbatim
%!!PS EPS
%%Title: Escher's Impossible triangle II
%%Creator: cgl
%%CreationDate: May 23 1996
%%BoundingBox: -40 -40 40 40
%%Pages: 1
%%EndProlog
%%Page: 1 1
%Parameterized over p1
/point {25 34} def%note x