summaryrefslogtreecommitdiff
path: root/graphics/drawing/drawing.tex
blob: 50a9abdc4a9053df9a5e9a594fba3985197a917f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is DRAWING.TEX v.1.0a revised 1994/01/25
% This contribution is written in LaTeX so that everybody can make a
% "pretty printout".
% In order to print it you need VERBATIM.STY by Rainer Sch"opf. This
% style is available from many archives.
% You of course need METAFONT and GFTOPK to generate the pictures.
% (Ask your local wizard for help)
% DRAWING.MF will be generated when you run this file through
% LaTeX the first time.

\ifx\selectfont\undefined
\documentstyle[verbatim]{article}
\else
\documentstyle[oldlfont,verbatim]{article}
\fi
\makeatletter
% Read the documentation of verbatim.sty and you will understand
% what I am doing.
\newif\ifmf@open \mf@openfalse
\newwrite\mf@out
\def\mfcode{\@bsphack
 \ifmf@open \else
  \typeout{Writing "drawing.mf"}
  \immediate\openout\mf@out\mf@name\relax
  \global\mf@opentrue
 \fi
 \let\do\@makeother\dospecials
 \catcode`\^^M\active%
 \def\verbatim@processline{%
  \immediate\write\mf@out{\the\verbatim@line}}%
 \verbatim@start}
\def\ednmfcode{\@esphack}
% We must specify the file name where the METAFONT source is to be
% written
\def\mf@name{drawing.mf}
\let\DW\relax  % the font does not exist at the first run
% We define a macro which writes into drawing.aux. It is similar
% to \mfcode
\def\auxwrite{\@bsphack
 \let\do\@makeother\dospecials
 \catcode`\^^M\active%
 \def\verbatim@processline{%
  \immediate\write\@mainaux{\the\verbatim@line}}%
 \verbatim@start}
\def\ednmfcode{\@esphack}
% Now we define \info to be used at the end of the document
\def\info{\ifmf@open\immediate\closeout\mf@out\fi
\typeout{^^J
==================================================================^^J
This was the first run. It created "drawing.mf" and "\jobname.dvi"^^J
is useless. Now you have to generate the pictures. You should ask^^J
the local wizard to help you with METAFONT. You should invoke it^^J
by the command:^^J
^^J
>mf &plain mode:=localfont; \string\input\space drawing^^J
^^J
where "localfont" should most probably be replaced with another^^J
mode depending upon your hardware. METAFONT will generate the^^J
metric file "drawing.tfm" and the font file "drawing.XXX" where^^J
the extension depends on the mode and METAFONT will tell you.^^J
Afterwards you have to call^^J
^^J
>gftopk drawing.XXX^^J
^^J
Your local wizard will tell you whether you have to move^^J
"drawing.tfm" and "drawing.pk" into another directory. Then you^^J
run LaTeX again and you will get the document with pictures (it^^J
should work even on a PC).^^J
Remember that you might need a different font for the screen and^^J
the printer.^^J
=================================================================^^J
Note: If you do not see the whole message on screen, you can find^^J
it in "\jobname.log".}}
\makeatother
% METAFONT logo
\newfont{\logo}{logo10}
\newfont{\llogo}{logo10 scaled \magstep2}
\newfont{\Llogo}{logo10 scaled \magstep3}
\def\mf{{\logo META}\-{\logo FONT}}
\def\lmf{{\llogo META}\-{\llogo FONT}}
\def\Lmf{{\Llogo META}\-{\Llogo FONT}}
% We will need hash, therefore we define it.
\begingroup \catcode`\#12
 \gdef\hash{{\catcode`\#12#}}
\endgroup
% Page layout
\textwidth 16cm
\oddsidemargin 0pt
\evensidemargin 0pt
\marginparwidth 0pt
\marginparsep 0pt
% Useful macro
\def\vs{\vspace{5mm}\par}
% Special hack
\newif\ifdoc \doctrue
% Now the document begins
\begin{document}
% Now we use a dirty trick to write something to the aux-file
\begin{auxwrite}
\ifdoc
\global\font\DW=drawing
\global\let\info\relax
\global\let\mfcode\verbatim
\global\let\endmfcode\endverbatim
\fi
\end{auxwrite}
%%%%%%%%%%%%%%%%%%%% Read the text from here %%%%%%%%%%%%%%%%
\title{Simple drawings with \Lmf}\author{Zden\v{e}k Wagner}
\maketitle
\section*{Introduction}
This contribution explains how to use \mf\ for simple drawings. To
make the text shorter, some commands are not described. The reader
is kindly asked to look into this source to see how it was done.

Nobody can guarantee that \mf\ is exactly what you need. the
simple rules say:
\begin{itemize}
\item Use \TeX\ where \TeX\ is good.
\item Use \mf\ where \mf\ is good.
\item Use other tools where other tools are good.
\end{itemize}
Here you can see examples where \mf\ is good.

\section*{Why not other packages?}
Though it might look strange I prefer explaining it here. The
selection of tools is more (or less) a matter of personal taste.
But there should be some reasoning behind it. If you want to make
simple drawings, you may choose either MFpic or a similar package
or directly \mf. In either case you must learn some new commands.
However, MFpic supports only a subset of \mf. Later, if you need
more complex pictures, you have to learn a new tool. With \mf\
it's a bit easier. You just learn some more commands.

It's not a good practice to reject everything what has been done.
you can find files with \mf\ macros which can be used in a similar
way as \LaTeX\ styles. This can make life much easier.

\section*{Principle of \lmf\ pictures}
The principle is to make a new font where a picture is some
``character''. When we want to place the picture into the
document, we change the font and type appropriate character. If
the picture is too large or too complex, it is better to divide it
into several characters and overlay them with \verb|\llap| or
\verb|\rlap| commands or simply place the characters in the
correct order (we will see it later).

\section*{Initial commands}
It is clever not to use absolute dimensions in the drawings. If we
measure everything as a multiple of a unit length, we can easily
scale the whole font. The unit length should be specified in sharp
units (designated with \hash) which are device independent. We
must then convert it into device dependent number of pixels by
calling \verb|define_pixels|. To do this, \mf\ must know the
properties of the output device. To set everything up, you should
call \verb|mode_setup| at the very beginning and supply the
correct mode when you call \mf.

It may be interesting to see on screen how \mf\ is drawing the
picture. It is accomplished by  \verb|screenstrokes|. The
beginning of \mf\ source file may therefore look as:
\begin{mfcode}
% This is drawing.mf, an example file
% (C) Z. Wagner, 23 Jan 1993, revised 25 Jan 1994
% This file must not be distributed separately. It is an integral
% part of drawing.tex. It may be placed at any computer in case
% drawing.tex is available at an appropriate directory.
mode_setup;
u# := 1.0mm#;
define_pixels(u);
screenstrokes;
\end{mfcode}

\section*{Assignments and equations}
As the title says, \mf\ can solve equations. Thus you can write
($3a$ is a shorthand for $3 * a$)
\begin{verbatim}
3a + b = 5;
2a - 3b = 7;
\end{verbatim}
After reading these equations the values of $a$ and $b$ are fully
defined.

In the previous section we used \verb|:=| which denotes
assignment. If you now say
\begin{verbatim}
a := 13;
\end{verbatim}
it will instruct \mf\ to forget whatever value $a$ might have had
and assign 13 ti it. In the previous case, when reading the
equation $2a - 3b = 7$, \mf\ already knows that $a = (-b + 5)/3$
and these together enable the evaluation of $a$ and $b$. This is
the difference between assignments and equations.

\section*{Points, coordinates and simple curves}
The position of points are specified using Cartesian coordinates.
since \mf\ works inside a plane, we need a pair of numbers, namely
the x and y coordinates.

The \mf\ character is usually defined by many points. It is
therefore comfortable to index them. \mf\ uses convention known
from programming languages, i.e. the index is placed in square
bracket as in z[7k-6]. It would be very tedious to write z[3].
Therefore \mf\ offers a shorthand: one simply types z3.

When defining the position of any point, you can either use the
pair variable in the equation or you can access the x and y
coordinates directly. Thus
\begin{verbatim}
z3 = (7.3u,-13.4u);
\end{verbatim}
is equivalent to
\begin{verbatim}
x3 = 7.3u;  y3 = -13.4u;
\end{verbatim}
whereas $u$ was defined above.

Later in the \mf\ definitions we will use commands as $z3 =
t[z1,z2]$. This means that z3 lies on the straight line defined by
z1 and z2. If $t = 0$, z3 is equivalent to z1. Equivalence between
z2 and z3 holds if $t = 1$. In case $t = \frac{1}{2}$, z3 lies in
the middle between z2 and z3.

\begin{figure}[hbt]
\centerline{{\DW A}}\vs
\caption{Simple curves}\label{fig:curves}
\end{figure}
The drawings are composed of Bezier curves. To draw a Bezier curve
through z1, z2, z3, we simply write:
\begin{verbatim}
draw z1..z2..z3;
\end{verbatim}

Sometimes we need better control. It may be achieved by specifying
a direction at one or more points. This was illustrated in
fig.~\ref{fig:curves}. The angles are always specified in degrees.
Before we write the \mf\ program for the curves, we must say
something about other commands.

The \verb|beginchar| commands starts the definition of a
character. The first parameter says what character it is to be
assigned to. The next parameters specify the width, height, and
depth (how far below the baseline it should extend), respectively.
The dimensions must be given in sharp units unless they are zero.
To draw something we must first pickup a pen of appropriate
thickness. To make a dot wit a pen, we use \verb|drawdot|. The
program for the character ends with \verb|endchar|. Now we can
look at it:
\begin{mfcode}
beginchar("A",50u#,50u#,0);
z1 = (5u,3u);  z2 = (37u,43u);
x3 = 1/3[x2,x1];  y3 = 1/3[y1,y2];
pickup pencircle scaled .4pt;
draw z1..z3{dir 45}..z2;
draw z1..z3{dir 105}..z2;
draw z1..z3{dir -20}..z2;
pickup pencircle scaled 2u;
drawdot z1;  drawdot z2; drawdot z3;
endchar;
\end{mfcode}

The command \verb|flex(z1,z2,z3)| draws a Bezier curves through
z1, z2, z3, where the direction at z2 is equal to the slope of the
straight line from z1 to z3. Make it as your own exercise.

\section*{Changing curvature with tension}
Bezier spline is a cubic curve. Therefore, you need four points
for full specification. However, every Bezier segment in previous
examples was defined only by two endpoints. It means that \mf\ has
its own algorithm for finding the remaining two points. If you
want to control the curvature, you must have the possibility to
influence this algorithm. One way is to use \verb|tension|.

You can define tension at any point of any segment. The general
syntax is
\begin{verbatim}
z1..tension a and b..z2
\end{verbatim}
If $a = b$, we can simplify this to
\begin{verbatim}
z1..tension a..z2
\end{verbatim}
The simple case \verb|z1..z2| is a shorthand for\footnote{This is
not the whole truth but you can live with this small lie. If you
wish to know more details, you have to study \mf\-book.}
\begin{verbatim}
z1..tension 1..z2
\end{verbatim}

\begin{figure}[hbt]
\centerline{{\DW abc}}\vs
\caption{Frame}\label{fig:frame}
\end{figure}
In the next example, we will need some more definitions. You
should already understand the first two lines. The next line
defines some parameters which will be used later. Then we declare
the array of paths to be drawn, the array of cnt and pen widths
which are both numeric variables. Then we assign some values to
them.
\begin{mfcode}
height#=50u#;  width#=50u#;
define_pixels(height,width);

relsh:=.005;  tens:=3;

path p[];
numeric cnt[], penw[];
penw0=3pt;  penw1=1pt;  penw2=.3pt;
cnt1=.95;   cnt2=.92;
\end{mfcode}
Afterwards we have to specify a number of points and the three
curves. We will use a predefined constant $origin$ which means
(0,0). To simplify the task we also use loops. The loop starts with
the \verb|for| token and carries out the commands up to
\verb|endfor| for the specified values of the loop control
variable. In the firs loop, when $k = 1$, the inner loop defines
points $z20, z21, \ldots, z32$. The token \verb|shifted| means
that the point is shifted by the specified vector. The vectors
\verb|right|, \verb|left|, \verb|up|, \verb|down|, are the unit
vectors in the named direction. The token \verb|scaled| means
scaling (multiplication) of the vector. In the equations below it
defines the length of the vector.

The definition of paths (curves) end with \verb|cycle|. This means
that the curve is cyclical. If you say
\begin{verbatim}
draw z1..z2..z3..z1;
\end{verbatim}
the curve will most probably have a sharp edge at z1. You must
say
\begin{verbatim}
draw z1..z2..z3..cycle;
\end{verbatim}
in order to make the curve smooth.

\begin{mfcode}
z0=origin;  z1=(0,height);  z2=(width,height);  z3=(width,0);
z5=.5[z0,z1];  z6=.5[z3,z2];
z7=.64[z0,z5] shifted (right scaled (relsh*width));
z8=.64[z1,z5] shifted (right scaled (relsh*width));
z9=.64[z2,z6] shifted (left scaled (relsh*width));
z10=.64[z3,z6] shifted (left scaled (relsh*width));
z11=.5[z1,z2] shifted (down scaled (relsh*height));
z12=.5[z0,z3] shifted (up scaled (relsh*height));
z13=(.5width,.5height);
for k:=1 upto 2:
  m:=20k;
  for j:=0 upto 12 :
    z[j+m]=cnt[k][z13,z[j]];
  endfor;
endfor;
for k:=0 upto 2:
m:=20k;
p[k]=z[m]..tension tens and 1..z[m+7]..z[m+5]..z[m+8]..tension 1 and tens..
  z[m+1]..tension tens and 1..z[m+11]..tension 1 and tens..z[m+2]..
  tension tens and 1..z[m+9]..z[m+6]..z[m+10]..tension 1 and tens..z[m+3]..
  tension tens and 1..z[m+12]..tension 1 and tens..cycle;
endfor;
\end{mfcode}

Now we draw the curves. Notice that we used zero widths for the
first two characters in order to simplify overlapping (look how
fig.~\ref{fig:curves} was done).

\begin{mfcode}
beginchar ("a",0,height#,0);
pickup pencircle scaled penw0;   draw p0;
endchar;

beginchar ("b",0,height#,0);
pickup pencircle scaled penw1;   draw p1;
endchar;

beginchar ("c",width#,height#,0);
pickup pencircle scaled penw2;   draw p2;
endchar;
\end{mfcode}

\section*{Skip this at the first reading}
We have made some global definitions which might spoil further
work. We therefore undefine the points. It is achieved by
assigning \verb|whatever|. It is done here for safety because the
examples are extracted from several \mf\ files designed by the
author and the global definitions might interfere with something.
However, a normal user usually does not need it.
\begin{mfcode}
for k:=0 upto 60: z[k]=(whatever,whatever); endfor;
\end{mfcode}

\section*{Scientific graph}
Now we make an example of presentation of scientific results.
Imagine that we have measured vapour pressures of some chemical
species and afterwards we have found the best fit in the form

\begin{equation}
\log p = A - \frac{B}{t+C}\label{eqn:vap}
\end{equation}

where $t$ is temperature in degrees Centigrade and $p$ is pressure
in kilopascals. Numerical values of parameters $A, B, C$ are
defined later in the \mf\ source.

\newcommand{\dgC}{$~{\circ}$C}
As you can see, the temperature ranges from 60\dgC\ to 90\dgC\ and
pressure ranges from 80\,kPa to 170\,kPa. We therefore need some
scaling and shift of the origin. A novice might read about
\verb|currenttransform| and try to harness it. This, however, has
undesirable side-effects and therefore we suggest to avoid it. It
is better to use simple linear transform defined with macros.
\begin{mfcode}
def degC = degCa + degCb enddef;
def kPa = kPaa + kPab enddef;
\end{mfcode}

Now let's examine what happens if we write 75degC. This expression
expands to 75degCa + degCb. It's clear how the transform works. We
must only emphasize that 75degC is not equal to degC*75 because
degC is not a variable but a macro.

Now we can start the plot. We specify the dimensions of the
character, define the temperature--pressure coordinates of the
lower left and upper right corners (\mf\ evaluates degCa, degCb,
kPaa, kPab for us) and specify parameters A, B, C and seven
experimental points.

You will see special variables $w$ and $h$. At the time of reading
\verb|beginchar| \mf\ assigns width to $w$, height to $h$ and
depth to $d$. All these variables are expressed in pixels rounded
to whole numbers.
\begin{mfcode}
beginchar("B",100u#,99u#,0);
origin = (50degC,50kPa);  (w,h) = (100degC,200kPa);
A = 3194;  B = 605;  C = 232;
z1 = (60degC,80kPa);  z2 = (65degC,92kPa);  z3 = (70degC,105kPa);
z4 = (75degC,119kPa);  z5 = (80degC,134kPa);
z6 = (85degC,151kPa);  z7 = (90degC,170kPa);
\end{mfcode}
It is tedious to type this by hand but it can be prepared by the
program which is used for finding the best fit.

In this case the best fit was expressed in a way which can be
evaluated with \mf. This is not a frequent situation. The easiest
way is to tabulate the best fit in many points (do it with your
software and make the output suitable for input to \mf) and
connect them with a crooked line. You will use a similar
technique as below. The only difference is that you will define
the points but we are calculating them. It is of course possible
to draw a curve which is not mathematically defined as the best
fit. In such a case you should specify a very small number of
points and play with directions and tensions. The next part shows
that the index expression may even be a real number.

\begin{mfcode}
for t := 55 step .3 until 95:
x[t] = t*degC;
y[t] = (mexp(A - 1000/(t+C)*B))*kPa;
endfor;
pickup pencircle scaled 1.5pt;
draw z55 for t:= (55+.3) step .3 until 95: --z[t] endfor;
\end{mfcode}

\mf\ has some limitation for calculations. Value 4096 is treated
as infinity. Greater values can appear in calculations but they
must be less that 32768. Therefore, the values $B = 605000$ would
cause arithmetic overflow. Due to it we had to modify
equation~\ref{eqn:vap}.

We have seen another useful feature of \mf. The loop command may
even be used in the middle of expression. Here it was used inside
the \verb|draw| command.

We also want to see the experimental points. We will draw them as
squares.

\begin{mfcode}
pickup pensquare scaled 4u;
for k:= 1 upto 7: drawdot z[k]; endfor;
\end{mfcode}

At last we draw the frame with marks for 75\dgC, 100\,kPa, and
150\,kPa.
\begin{mfcode}
pickup pensquare scaled .7pt;
draw origin--(0,h)--(w,h)--(w,0)--cycle;
pickup pencircle scaled .3pt;
draw (75degC,0)--(75degC,5u);
draw (0,100kPa)--(5u,100kPa);
draw (0,150kPa)--(5u,150kPa);
endchar;
\end{mfcode}

Notice that we specified the position of marks in the
corresponding units. We could as well use $w/2$ or even $50u$
instead of $75degC$. Such things are, however, too absolute. If
you for some reason change the with to $150u\hash$, $50u$ will no
longer correspond to 75\dgC. You can change the temperature range
to 60\dgC--120\dgC\ and now $w/2$ corresponds to 90\dgC. It is
clear that $75degC$ is invariate under such changes.

\begin{figure}[hbt]
\setlength{\unitlength}{1truemm}
\newcommand{\x}{-5}
\newcommand{\y}{-6}
\begin{picture}(130,120)(-30,-20)\sf
\put(0,0){\makebox(100,99)[lb]{{\DW B}}}
\put(0,\y){\makebox(4,4)[lt]{50}}
\put(48,\y){\makebox(4,4)[t]{75}}
\put(96,\y){\makebox(4,4)[tr]{100}}
\put(\x,0){\makebox(4,4)[br]{50}}
\put(\x,31){\makebox(4,4)[r]{100}}
\put(\x,64){\makebox(4,4)[r]{150}}
\put(\x,95){\makebox(4,4)[tr]{200}}
\put(0,-9){\makebox(90,4)[br]{t\,[\dgC]}}
\put(-30,90){\makebox(20,4)[br]{p\,[kPa]}}
\end{picture}
\caption{Vapour pressure curve}\label{fig:vap}
\end{figure}

Look how figure~\ref{fig:vap} has been done. It might look
horrible but after some practice you will find it easy.

\mf\ has more advanced mechanisms which could be harnessed for
transfer of dimensions and coordinates. Some macro packages as
\verb|incpic.mf| and \verb|incpic.tex| by Old\v{r}ich Ulrych make
use of it. But this is for experts (or those who do not care how
it works inside). A novice would have hard times to understand it.
If you know the mechanism, you cam find your own bugs and you can
modify it so that it satisfies your personal needs.

The easiest way seems to be the standard \LaTeX' picture
environment. To avoid some calculations, we place the origin of
the environment into the origin of our graph. All texts are aligned
using \verb|\makebox| commands. The dimensions are specified in
truemm and truecm. These units remain the same if you change the
\verb|\magnification|. It cannot be done in \LaTeX\ but it is used
here in case someone would like to incorporate similar concepts
into plain \TeX.

\section*{More complex examples}
This section can be too difficult for novices. We show more
advanced macro definitions. If you cannot understand it at the
first reading, just skip this chapter and return here after you
make several own pictures. However, {\bf do not forget to read the
important warning later in this document!}

The next part of \mf\ code is best placed at the beginning of the
file so that you can fiddle with the parameters. In this example
it is placed here in order not to disturb the initial explanation
with hard to understand commands.

We start with some parameter definitions. Note that two variables
are declared as \verb|pair|.

\begin{mfcode}
pair tieshift, tiedepth;
smallcorner = 1.5u;  bigcorner = 7.5u;
slope = 3;
tieshift = down scaled 2.5u;
tiedepth = down scaled 4.5u;
tv = 3;  % this is tension for ties
\end{mfcode}

We have already seen commands for pen selection. They are quite
slow. If we pick up the same pen many times, it is faster to store
the pen in some variable using \verb|savepen|. We do that with two
different pens.\footnote{It may seem rather useless for two
pictures but remember that this example is a small part of a large
font.}

\begin{mfcode}
pickup pencircle scaled 1pt;
normalpen := savepen;

pickup pencircle scaled .4pt;
penfortie := savepen;
\end{mfcode}

\begin{figure}[hbt]
\centerline{{\DW C\hspace{1cm}D}}\vs
\caption{Drawings of marquees}\label{fig:marquee}
\end{figure}

Now we define a macro with parameters. This macro should draw a
single segment of a tie. As you can see in
fig.~\ref{fig:marquee}, the tie is quite a complicated path. It
should be composed of many segments. Therefore there is no
semicolon inside the macro definition.

\begin{mfcode}
def tiebelowline(expr l, r, t, u) =
(t[l,r] shifted tieshift)..tension tv and 1..
 ((.5[t,u])[l,r] shifted tiedepth)..tension 1 and tv
enddef;
\end{mfcode}

We will need two different round corners. There's another place
for macro with parameters. It contains cryptic commands. So we
write the macro and explain it below.

\begin{mfcode}
def roundcorner(expr a, b, c, r) =
begingroup save q, w;  pair q, w;
hide(save __p;  path __p;
__p = fullcircle scaled r shifted b;
q := (a--b) intersectionpoint __p;
w := (b--c) intersectionpoint __p; )
a--q{b-a}..{c-b}w--c
endgroup
enddef;
\end{mfcode}

This macro should draw a line from $a$ to $c$ where the sharp
corner at $b$ is replaced by a part of circle of diameter $r$. the
macro uses its own internal variables. Not to spoil other things
in our source, we close the calculations into a group. It is
similar to \TeX\ groups but the behaviour is slightly different.
We must explicitly \verb|save| the variables. After that \mf\
forgets whatever meaning they might have had and then we can
define them. similarly as \verb|tie|, macro \verb|roundcorner|
expands to a segment of a longer path. Therefore we must hide the
calculations so that \mf\ does not see them when constructing the
path. This is by saying \verb|hide| and closing the hidden code
into parentheses.

The hidden code starts with saving \verb|__p| and declaring it as a
variable of type \verb|path|. It is then defined to be a circle of
diameter $r$ with the centre at point $b$. The next two lines of
code calculate the points of intersection of the full circle (path
\verb|__p|) with straight lines (\verb|a--b|) and (\verb|b--c|)
and assigns them to $q$ and $w$, respectively. now we can
construct the segment. We specify directions st $q$ and $w$. Again
semicolon does not appear here because it should be used as a part
of a longer path.

We have said that $w$ contains the width of the character rounded
to the whole number of pixels. Now we use it as a variable of type
pair. You may wonder why \mf\ does not get confused. The reason is
that we did the change inside a group. We saved the old meaning
which is automatically restored when \mf\ performs
\verb|endgroup|.

We are going to draw two similar marquises. They will differ in
one parameter only. Therefore we write another macro. First we
define some points. These definitions must be global. We will use
variable $i$ for some calculations. We adopt a rule that this
variable serves as a loop control variable and is not used for any
other purpose. Therefore we need not save it.

We will see a new token \verb|rotated|. This denotes rotation of
the endpoint around origin.

Scaling, shift and rotation are transformations. Shift and
rotation are not commutative. It means that it is important to
apply them in the correct order. In the following macro you can
find
\begin{verbatim}
z[i+10] = right scaled 35u rotated slope shifted z[i];
\end{verbatim}
As an exercise change it to\footnote{If you happen to corrupt
drawing.mf, do not despair. Just erase drawing.aux and run
drawing.tex through {\rm\normalsize\LaTeX}. It will recreate
drawing.mf.}
\begin{verbatim}
z[i+10] = right shifted z[i] scaled 35u rotated slope;
\end{verbatim}
and look what it makes with the marquise\footnote{Are you really
doing the exercise or just reading the text? your own practice
will give you much more. Of course you can also try your own
pictures.}

\begin{mfcode}
def Kcxi(expr corner) =
z1 = origin;
z2 = right scaled 13u rotated -16;
z3 = right scaled 13u rotated 16;
z4 = right scaled 13u rotated 40;
z5 = right scaled 14u rotated 65;
z6 = (0,15u);
for i := 2 upto 6:
z[i+10] = right scaled 35u rotated slope shifted z[i];
endfor;
pickup normalpen;
draw roundcorner(z1,z2,z12,corner);
for i := 3 upto 6:
  draw roundcorner(z1,z[i],z[i+10],corner)--z[i+9];
endfor;
pickup penfortie;
draw z1--tiebelowline(z1,z2,0,1/3)..tiebelowline(z1,z2,1/3,2/3)
  ..tiebelowline(z1,z2,2/3,1)..
  for i := 1 upto 8: tiebelowline(z2,z12,(i-1)/8,i/8)..endfor
  z12 shifted tieshift--z12;
enddef;
\end{mfcode}

The forming of the marquises is now easy. We just call the macro
with the correct corner. Please notice that those characters have
nonzero depths.

\begin{mfcode}
beginchar("C",50u#,16u#,12u#);
Kcxi(smallcorner);
endchar;

beginchar("D",50u#,16u#,12u#);
Kcxi(bigcorner);
endchar;
\end{mfcode}

\section*{Important warning}
At the end of the \mf\ code we have to place
\begin{mfcode}
end
\end{mfcode}
Semicolon is not required here (but you can use it) because \mf\
ignores everything which might appear after the \verb|end| token.
{\bf It is important to place end--of--line character at the last
line of code.} If you forget it, \mf\ will award you with a
horrible message
\begin{verbatim}
! METAFONT capacity exceeded, sorry [buffer size=500].
l.132
      end^^?^^?^^?^^?^^?^^?^^?^^?^^?^^?^^?^^?^^?^^?^^?^^?^^?...
If you really absolutely need more capacity,
you can ask a wizard to enlarge me.
\end{verbatim}

\section*{Conclusion}
We have seen simple examples of drawings produced by \mf. There
are plenty of other commands which we have not discussed here. One
of them is \verb|fill| which fills in a cyclic path (try to define
one and fill it by saying e.g.
\begin{verbatim}
fill z1..z2..z3..cycle;
\end{verbatim}
where all points were previously defined). If you master \mf, you
can make lots of tricks.

I said that MFpic inserts an additional step which slows down the
progress when you need to fine tune the curves. From this document
it may seem that I did not make any improvement. The truth is that
in your life you will not use the \verb|mfcode| environment. It is
used in this example to ensure that everything is distributed
together. Normally the \mf\ source code is written directly and is
not created by running \TeX.

I wanted to demonstrate that you have to know relatively small
number of \mf\ commands in order to draw simple pictures. If you
try this, it will encourage you to study \mf\-book. It is useful
although you will probably never design your own letters.

It may happen that \mf\ is extremely cumbersome for some
particular case. Then you are free to scan an image using your
scanner, modify it with e.g. Corel Draw, overlap it with pictures
designed with \mf\ and texts written in \TeX, cut and paste it
using the functions provided by dvidot or other drivers and polish
it by means of {\sl PostScript\/} features. The only limitation is
the availability of different soft- and hardware tools and your
own invention.
\nopagebreak\vspace{1cm}\nopagebreak\par\nopagebreak
\begin{flushright}\interlinepenalty10000
Zden\v{e}k Wagner\\
E. H\'ala Laboratory of Thermodynamics\\
Institute of Chemical Process Fundamentals\\
Academy of Sciences of the Czech Republic\\
CZ--165 02 Prague\\[2ex]
e--mail: {\tt wagner@csearn.bitnet, wagner@earn.cvut.cz}
\end{flushright}
\docfalse\info
\end{document}