home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / lout2.lzh / LOUT2 / DOC / TR.LOUT / ch4.01 < prev    next >
Text File  |  1994-01-25  |  7KB  |  187 lines

  1. @Section
  2.    @Title { An equation formatting package }
  3.    @Tag { eq }
  4. @Begin
  5. @PP
  6. In this section we describe the design and implementation of the Eq
  7. eq @Index { Eq equation formatting package }
  8. equation formatting package.  Equation formatting makes a natural first
  9. example, partly because its requirements have strongly influenced the
  10. design of Lout, and partly because no cross references or galleys are
  11. required.
  12. @PP
  13. To the author's knowledge, Eq is the first equation formatter to be
  14. implemented as a collection of high-level definitions.  This approach
  15. has significant advantages:  the basics of language and layout are
  16. trivial, so the implementor can concentrate on fine-tuning; and the
  17. definitions, being readily available, can be improved, extended, or even
  18. replaced.
  19. @PP
  20. As described in the Eq user manual [{@Ref kingston92eq}], an equation is
  21. entered in a format based on the one introduced by the eqn language of
  22. Kernighan and Cherry [{@Ref kernighan75}]:
  23. kernighan.b @Index { Kernighan, B. }
  24. cherry.l @Index { Cherry, L. }
  25. @ID @Code {
  26. "@Eq { { x sup 2  +  y sup 2 }  over  2 }"
  27. }
  28. The result is
  29. @ID @Eq { { x sup 2  +  y sup 2 }  over  2 }
  30. In outline, the definition of the @Code "@Eq" symbol is
  31. eq.example @Index { @Code "@Eq" example }
  32. @ID @Code {
  33. "export  sup  over \"+\"  \"2\"  \"<=\""
  34. "def @Eq"
  35. "   body @Body"
  36. "{"
  37. "   def sup precedence 60 left x right y { ... }"
  38. "   def over precedence 54 left x right y { ... }"
  39. "   def \"2\" { Base @Font \"2\" }"
  40. "   def \"+\" { {Symbol Base} @Font \"+\" }"
  41. "   def \"<=\" { {Symbol Base} @Font \"\\243\" }"
  42. "   ..."
  43. ""
  44. "   Slope @Font 1.2f @Break 0c @Space @Body"
  45. "}"
  46. }
  47. A body parameter is used to restrict the visibility of the equation
  48. formatting symbols (there are hundreds of them).  The equation as a whole
  49. is set in Slope (i.e. Italic) font, and symbols such as @Code "\"2\"" and
  50. @Code "\"+\"" are defined when other fonts are needed.  Precedences are
  51. used to resolve ambiguities such as {@Code "a sup b over c"}.  Eq takes
  52. all spacing decisions on itself, so to prevent white space
  53. typed by the user from interfering, the equation is enclosed in
  54. {@Code "0c @Space"}.  We will discuss the {@Code "1.2f @Break"} later.
  55. @PP
  56. Thus have we disposed of the language design part of the equation
  57. formatting problem; it remains now to define the twenty or so symbols
  58. with parameters, and get the layout right.
  59. @PP
  60. Every equation has an {@I axis}:  an imaginary horizontal line through
  61. the centre of variables, through the bar of built-up fractions, and so
  62. on.  We can satisfy this requirement by ensuring that the result of each
  63. symbol has a single row mark, on the axis.  For example, the
  64. superscripting symbol is defined as follows:
  65. sup.example @Index { @Code "sup" example }
  66. @ID @Code {
  67. "def sup"
  68. "    precedence 60"
  69. "    associativity left"
  70. "    left x"
  71. "    named gap { @SupGap }"
  72. "    right y"
  73. "{"
  74. "    @HContract @VContract {"
  75. "        |  @Smaller y"
  76. "        ^/gap  x"
  77. "    }"
  78. "}"
  79. }
  80. The @Code "@VContract" and @Code "^/" symbols together ensure that the axis
  81. of the result is the axis of the left parameter.  A @Code "gap"
  82. parameter has been provided for varying the height of the superscript,
  83. with default value @Code "@SupGap"  defined elsewhere as
  84. {@Code "0.40fk"}.  It is important that such gaps be expressed in units
  85. that vary with the font size, so that they remain correct when the size
  86. changes.  Collecting the default values into symbols like @Code
  87. "@SupGap" ensures consistency and assists when tuning the values.  Here
  88. is another characteristic definition:
  89. over.example @Index { @Code "over" example }
  90. @ID @Code {
  91. "def over"
  92. "    precedence 54"
  93. "    associativity left"
  94. "    left x"
  95. "    named gap { 0.2f }"
  96. "    right y"
  97. "{"
  98. "    @HContract @VContract {"
  99. "        |0.5rt  @OneCol x"
  100. "        ^//gap  @HLine"
  101. "        //gap  |0.5rt @OneCol y"
  102. "    }"
  103. "}"
  104. }
  105. Both parameters are centred, since we do not know which will be the
  106. wider; we use @@OneCol to make sure that the entire parameter is
  107. centred, not just its first column, and @@HContract ensures that the
  108. fraction will never expand to fill all the available space, as Lout objects
  109. have a natural tendency to do (Section {@NumberOf size}).  @Code "@HLine"
  110. is a horizontal line of the width of the column:
  111. hline.example @Index { @Code "@Hline" example }
  112. @ID @Code {
  113. "def @HLine"
  114. "    named line { \"0.05 ft setlinewidth\" }"
  115. "{  "
  116. "    { \"0 0 moveto xsize 0 lineto\" line \"stroke\" } @Graphic {}"
  117. "}"
  118. }
  119. Here we are relying on the expanding tendency just mentioned.
  120. @PP
  121. The remaining symbols are quite similar to these ones.  We conclude with
  122. a few fine points of mathematical typesetting mentioned by a leading
  123. authority, D. E. Knuth [{@Ref knuth84}].
  124. knuth.d @Index { Knuth, D. }
  125. @PP
  126. Some symbols, such as @Eq {lessequal} and @Eq { notequal }, should have a
  127. thick space on each side; others, such as @Eq {plus} and @Eq {minus},
  128. have a medium space; others have a thin space on the right only.  This
  129. would be easy to do except that these spaces are not wanted in
  130. superscripts and subscripts:
  131. @ID @Eq { r sup n+1 - 1 }
  132. In effect, the definition of such symbols changes depending on the
  133. context; but Lout does not permit such a change.  Luckily, the so-called
  134. `style' information set by the @@Font, @@Break, and @@Space symbols can
  135. change in this way.  Accordingly, Eq commandeers the @Code v unit,
  136. normally used for line gaps, and uses it for these spaces instead:
  137. @ID @Code {
  138. "def @MedGap { 0.20v }"
  139. ""
  140. "def \"+\" { &@MedGap plus &@MedGap }"
  141. ""
  142. "def @Smaller right x { 0.7f @Font 0c @Space 0.2f @Break x }"
  143. }
  144. The @Code "@Smaller" symbol is applied to all superscripts and subscripts,
  145. reducing the font size and also reducing the @Code "v" unit,
  146. thereby reducing the space around @Code "+" and similar symbols.
  147. @PP
  148. Some objects, notably matrices and large summation signs, must be
  149. vertically centred in the sense that their axis must be placed halfway
  150. down the object.  This seems quite different to the usual kind of
  151. centring of one object within another handled by the @Code "0.5rt" gap.
  152.  With the aid of the @Code w unit used with concatenation symbols (one
  153. w.unit.example @SubIndex { example of use of }
  154. @Code w is the size of the following object) and some ingenuity we find that
  155. vctr.example @Index { @Code vctr example }
  156. @ID @Code {
  157. "def vctr right x"
  158. "{ @OneRow { /0.5wo @OneRow { @OneRow x ^/ } }"
  159. "}"
  160. }
  161. will vertically centre its parameter: @Code "@OneRow { @OneRow x ^/ }"
  162. replaces {@Code x}'s mark by one mark along its lower boundary; then
  163. @Code "/0.5wo" overstrikes the desired mark, and the outer @@OneRow
  164. hides the lower mark.  Unfortunately, although the parameter is
  165. correctly placed and printed, the overstriking hides its top half, and
  166. @ID @Code {
  167. "vctr sum from i=0 to n i"
  168. }
  169. appears as
  170. @ID @Eq { 
  171. @OneRow { /0.5wo @OneRow { @OneRow sum ^/ } }
  172. from i=0 to n i
  173. }
  174. using this definition.  The version of @Code "vctr" in Eq
  175. overcomes this problem by overstriking two copies of the parameter, one
  176. of which has been rotated twice by 180 degrees:
  177. @ID @Code {
  178. "def vctr  right x"
  179. "{  @OneRow"
  180. "   {  -180d @Rotate { /0.5wo 180d @Rotate { / @OneRow @OneCol x  } }"
  181. "      ^/  @OneRow { /0.5wo @OneRow { @OneRow @OneCol x ^/ } }"
  182. "   }"
  183. "}"
  184. }
  185. This is very ugly and suggests that something is lacking from Lout's features.
  186. @End @Section
  187.