demo: include roff sources of neat* documentation
[neatroff_make.git] / demo / neateqn.ms
blob16da5008ec3edefa6c7ad643798443d8f4fe41dd
1 .\" PSTITLE: Typesetting Mathematics with Neateqn
2 .so neat__.ms
3 .HD
4 .ds en.cl "#936
5 .TL
6 \f(HD\s+8Typesetting Mathematics with Neateqn\m0\s-8\fP
7 .AU
8 \fIA. G. Rudi\fP
9 .sp 3
10 This document briefly introduces Neateqn, an eqn implementation for
11 typesetting mathematical formulas in Neatroff.  For further
12 information about Neatroff and Neateqn, see
13 http:/\h'-.3n'/litcave.rudi.ir/.
15 .SH "The Algorithm"
16 Neatroff follows the rules described in appendix \s-1G\s+1 of Knuth's
17 Texbook, which explains Tex's algorithm for typesetting mathematical
18 formulas.  In Tex, the sub-formulas of a formula are always typeset in
19 one of the eight predefined styles.  The formulas inside
20 \&\s-1.EQ/.EN\s+1 blocks are rendered in displayed styles and inline
21 equations are rendered in text styles.
22 Their main difference is that in text styles formulas are
23 vertically more compact to reduce the amount of extra
24 spacing required to be inserted between lines.
26 The default value of the parameters of the typesetting algorithm,
27 such as the position of subscripts, can be changed.
28 These parameters are described in appendix \s-1G\s+1 of the Texbook
29 and can be modified in Neatroff with Groff eqn-style \(lqset\(rq command.
30 See Groff eqn manual page or the Texbook for their list and explanations.
32 .SH "Defining Custom Brackets
33 It is possible to adjust the default brackets or define new ones.  Two
34 commands are available for this purpose: one for specifying different
35 bracket sizes (bracketsizes) and one for specifying bracket building
36 glyphs (bracketpieces):
38 .cc.beg
39 bracketsizes sign N glyph1 glyph2 ... glyphN
40 bracketpieces sign top mid bot cen
41 .cc.end
43 .LP
44 In these commands, sign is the token placed after the \(lqleft\(rq and
45 \(lqright\(rq keywords in equations.  In bracketsizes command, the
46 glyphs should be ordered based on their size with the smallest glyph
47 appearing first.  Neateqn tries the specified glyphs in the same order
48 until it finds the glyph that is large enough for the enclosed
49 formula.  If this fails, it tries to build the bracket if its pieces
50 are defined (by default or with bracketpieces).  The four arguments of
51 bracketpieces specify the glyphs required for building the bracket.
52 The last argument can be empty if the bracket has no centre (\\(lk is
53 the centre of {, for instance).
55 As an example, the following lines show how the default opening and
56 closing parenthesis can be defined:
58 .cc.beg
59 bracketpieces ( "\\(LT" "\\(LX" "\\(LB" ""
60 bracketpieces ) "\\(RT" "\\(RX" "\\(RB" ""
61 .cc.end
63 .LP
64 The following lines do so for braces:
66 .cc.beg
67 bracketpieces { "\\(lt" "\\(bv" "\\(lb" "\\(lk"
68 bracketpieces } "\\(rt" "\\(bv" "\\(rb" "\\(rk"
69 .cc.end
71 .LP
72 Also the following line instructs Neateqn to use Tex's open
73 parenthesis glyphs with different sizes (note that in Neatroff \\\s-1N\s+1'gid' is
74 the glyph with device-dependent name gid):
76 .cc.beg
77 bracketsizes ( 5 "(" "\\N'parenleftbig'" "\\N'parenleftBig'"
78         "\\N'parenleftbigg'" "\\N'parenleftBigg'"
79 .cc.end
81 .EQ
82 delim $$
83 .EN
84 .SH "Adjusting the Syntax"
85 The logic used in eqn to partition equations may seem odd to its new
86 users; for instant in \(lqO(n sup 2)\(rq, the expected result may
87 be $O ( n sup 2 )$, instead of $O(n sup {2)}$.
88 Even experienced eqn users occasionally make these
89 mistakes and some insert spaces around most tokens to prevent possible
90 surprises.  Equations like \(lqO ( n sup 2 )\(rq, which prevent
91 most of these problems, do not look as good as the alternative, however.  This
92 issue is one of the main advantages of Tex's more concise syntax.  In
93 Neateqn it is possible to make equations like the first work.
95 Neateqn splits (chops) equations at specific characters.  Equations
96 are always chopped at spaces and before and after open and close
97 braces.  By default, equations are also chopped before and after \(ha,
98 \(ti, and " (but these can be changed).  The -c option of Neateqn
99 allows specifying the characters, around which equations are chopped.
100 For instance, if \(lq\(ti\(ha"(),\(rq is passed with -c to Neateqn,
101 \(lqO(n sup 2)\(rq is interpreted as \(lqO ( n sup 2 )\(rq.  This
102 may be considered an improvement but a more important advantage in
103 the author's opinion is that these characters may be redefined.  For
104 instance, one may redefine open and close parenthesis as follows:
105 .cc.beg
106 define ( @{ left (@
107 define ) @right ) }@
108 .cc.end
110 Then, it is possible to write \(lq(a over b) sup (c + 5)\(rq
111 to get $left ( a over b right ) sup left ( c + 5 right )$.
112 Note that macro arguments are never split away from macro name,
113 thus one can safely call \(lqlog(a, n)\(rq, if log is defined
114 as \(lqroman "log" sub {\N'dollar'1}({\N'dollar'2})\(rq.
116 .SH "Assigning Character Type
117 Neateqn determines the spacing between characters in an equation based
118 on their type (see chapter 18 of the Texbook for more information).
119 It is possible to specify or change the type of a character in Neateqn
120 with \(lqchartype\(rq command.  Possible types are \(lqord\(rq for
121 ordinary atoms, \(lqop\(rq for large operators, \(lqbin\(rq for
122 binary operators, \(lqrel\(rq for relations, \(lqopen\(rq for opening
123 brackets, \(lqclose\(rq for closing brackets, \(lqpunct\(rq for
124 punctuations, \(lqinner\(rq for fractions.
125 As an example, the following line declares backslash as a binary operator:
126 .cc.beg
127 chartype bin \\(rs
128 .cc.end
131 The second argument of \(lqchartype\(rq command should be a Troff
132 character name.  If the operator is not a character, it can be defined as one.
133 For instance, for \(lq>>\(rq and \(lqlog\(rq
134 operators, one may define the following two characters (note that
135 the following two lines are Neatroff requests and should be outside
136 \&\s-1.EQ/.EN\s+1 blocks):
137 .cc.beg
138 \&.char \\[eqn.log] "log
139 \&.char \\[eqn.>>]  ">\\h'-.1n'>
140 .cc.end
143 Then, the type of the operators can be specified as explained above:
144 .cc.beg
145 chartype op  \\[eqn.log]
146 chartype rel \\[eqn.>>]
147 .cc.end
150 Finally, macros like the following may be defined to improve the readability:
151 .cc.beg
152 define >>  @\\[eqn.>>]@
153 define log @\\[eqn.log]@
154 .cc.end
156 .SH "Breaking Equations"
157 Neateqn can break equations after top-level operators; This is
158 important especially when there are long inline equations in the text.
159 The \(lqbreakcost\(rq command can specify the cost of a line break
160 after different character types: its first argument is the character
161 type, as enumerated in the previous section, and its second argument
162 is the cost of line breaks after the given character type.  Costs are
163 specified via Neatroff's \\j escape sequence (\(lqNeatroff
164 Introduction\(rq explains the meaning of these costs).  The default
165 values are:
167 .cc.beg
168 breakcost rel 100
169 breakcost bin 200
170 breakcost punct 1000
171 .cc.end
174 A value of 0 disables breaking equations for the specified character.
175 Note that Neateqn breaks equations after top-level operators only.
176 Thus, equations surrounded by braces will not be broken.  The
177 following command instructs Neateqn never to break equations:
179 .cc.beg
180 breakcost any 0
181 .cc.end
183 .SH "Using Tex Mathematical Symbols"
184 In order to use Tex's mathematical symbols in Neatroff, \s-1CMEX10\s+1 and
185 \s-1CMSY10\s+1 fonts (or their equivalents, for instance \s-1TXEX\s+1 and \s-1TXSY\s+1 for
186 Txfonts or \s-1PXEX\s+1 and \s-1PXSY\s+1 for Pxfonts) should be mounted and declared
187 as the special font of eqn Roman font (the font declared as grfont in
188 Neateqn).
190 .cc.beg
191 \&.fp 11 CMEX CMEX10
192 \&.fp 12 CMSY CMSY10
193 \&.fspecial R CMEX CMSY
194 .cc.end
197 If the italic font lacks Greek characters, \s-1CMMI10\s+1 (or its equivalents,
198 like \s-1RTXMI\s+1 for Txfonts or \s-1RPXMI\s+1 for Pxfonts) can be mounted and
199 declared as a special font of eqn italic font (the font declared as
200 gfont in Neateqn).
202 .cc.beg
203 \&.fp 13 CMMI RPXMI
204 \&.fspecial I CMMI
205 .cc.end
208 Standard symbol can also be redefined to use Computer Modern glyphs,
209 like those for summation and product:
211 .cc.beg
212 define sum @{vcenter roman "\\N'summationdisplay'"}@
213 define tsum @{vcenter roman "\\N'summationtext'"}@
214 define prod @{vcenter roman "\\N'productdisplay'"}@
215 define tprod @{vcenter roman "\\N'producttext'"}@
216 .cc.end
219 define sum @{vcenter roman "\N'summationdisplay'"}@
220 define tsum @{vcenter roman "\N'summationtext'"}@
221 define prod @{vcenter roman "\N'productdisplay'"}@
222 define tprod @{vcenter roman "\N'producttext'"}@
223 define small @size -4@
226 .SH "Some Samples For Different Fonts
229 Palatino and Computer Modern mathematical symbols:
231 (x + y) sup n = sum from i=0 to n left ( pile {n above i} right ) x sup i y sup n-i
233 .sp -.5
235 left [ a right ] +
236 left ( a over b right ) +
237 left { {x + a over b} over {y + c over d} }
239 sqrt {a} + sqrt {a over b} + sqrt { {x + a over b} over {y + c over d} }
243 Palatino and Pxfonts mathematical symbols:
244 .fp 11 CMEX Pxex
245 .fp 12 CMSY Pxsy
247 (x + y) sup n = sum from i=0 to n left ( pile {n above i} right ) ^ x sup i y sup n-i
249 .sp -.5
251 left [ a right ] +
252 left ( a over b right ) +
253 left { {x + a over b} over {y + c over d} }
255 sqrt {a} + sqrt {a over b} + sqrt { {x + a over b} over {y + c over d} }
259 Times Roman and Txfonts mathematical symbols:
260 .fp 1 R R
261 .fp 2 I I
262 .fp 3 B B
263 .fp 11 CMEX txex
264 .fp 12 CMSY txsy
266 (x + y) sup n = sum from i=0 to n left ( pile {n above i} right ) ^ x sup i y sup n-i
268 .sp -.5
270 left [ a right ] +
271 left ( a over b right ) +
272 left { {x + a over b} over {y + c over d}  }
274 sqrt {a} + sqrt {a over b} + sqrt { {x + a over b} over {y + c over d} }
278 Computer Modern:
279 .fp 1 R CMR10
280 .fp 2 I CMMI10
281 .fp 3 B CMB10
282 .fp 4 HD CMBX12
283 .fp 11 CMSY CMSY10
284 .fp 12 CMEX CMEX10
285 .fp 13 CMMI CMMI10
286 .fspecial R CMSY CMEX10
287 .fspecial I CMMI
289 (x + y) sup n = sum from i=0 to n left ( pile {n above i} right ) x sup i y sup n-i
291 .sp -.5
293 left [ a right ] +
294 left ( a over b right ) +
295 left { {x + a over b} over {y + c over d}  }
297 sqrt {a} + sqrt {a over b} + sqrt { {x + a over b} over {y + c over d} }