1#!/usr/bin/env python
2
3"""
4C.7 Mathematical Formulas (p187)
5
6"""
7
8from plasTeX.Base.LaTeX.Arrays import Array
9from plasTeX import Command, Environment, sourceChildren
10from plasTeX import DimenCommand, GlueCommand
11
12
13#
14# C.7.1
15#
16
17# These space commands are only documented as being available in math mode,
18# but it was requested to have them be in the global namespace.
19
20class ThinSpace(Command):
21    macroName = '.'
22    str = '\u2009'
23
24class NegativeThinSpace(Command):
25    macroName = '!'
26
27class MediumSpace(Command):
28    macroName = ':'
29    str = '\u8196'
30
31class ThickSpace(Command):
32    macroName = ';'
33    str = '\u8194'
34
35class ThinSpace_(Command):
36    macroName = '/'
37    str = '\u2009'
38
39class MathEnvironment(Environment):
40    mathMode = True
41
42class MathEnvironmentPre(MathEnvironment):
43    """
44    A math environment whose source property keeps the begin and end markup.
45    """
46    @property
47    def source(self):
48        return u"\\begin{{{0}}}{1}\\end{{{0}}}".format(
49                self.tagName,
50                sourceChildren(self))
51
52# Need \newcommand\({\begin{math}} and \newcommand\){\end{math}}
53
54class math(MathEnvironment):
55    @property
56    def source(self):
57        if self.hasChildNodes():
58            return u'$%s$' % sourceChildren(self)
59        return '$'
60
61class displaymath(MathEnvironment):
62    blockType = True
63    @property
64    def source(self):
65        if self.hasChildNodes():
66            return r'\[ %s \]' % sourceChildren(self)
67        if self.macroMode == Command.MODE_END:
68            return r'\]'
69        return r'\['
70
71class BeginDisplayMath(Command):
72    macroName = '['
73    def invoke(self, tex):
74        o = self.ownerDocument.createElement('displaymath')
75        o.macroMode = Command.MODE_BEGIN
76        self.ownerDocument.context.push(o)
77        return [o]
78
79class EndDisplayMath(Command):
80    macroName = ']'
81    def invoke(self, tex):
82        o = self.ownerDocument.createElement('displaymath')
83        o.macroMode = Command.MODE_END
84        self.ownerDocument.context.pop(o)
85        return [o]
86
87class BeginMath(Command):
88    macroName = '('
89    def invoke(self, tex):
90        o = self.ownerDocument.createElement('math')
91        o.macroMode = Command.MODE_BEGIN
92        self.ownerDocument.context.push(o)
93        return [o]
94
95class EndMath(Command):
96    macroName = ')'
97    def invoke(self, tex):
98        o = self.ownerDocument.createElement('math')
99        o.macroMode = Command.MODE_END
100        self.ownerDocument.context.pop(o)
101        return [o]
102
103class ensuremath(Command):
104    args = 'self'
105
106class equation(MathEnvironment):
107    blockType = True
108    counter = 'equation'
109
110class EqnarrayStar(Array):
111    macroName = 'eqnarray*'
112    blockType = True
113    mathMode = True
114
115    class lefteqn(Command):
116        args = 'self'
117        def digest(self, tokens):
118            res = Command.digest(self, tokens)
119            obj = self.parentNode
120            while obj is not None and not isinstance(obj, Array.ArrayCell):
121                obj = obj.parentNode
122            if obj is not None:
123                obj.attributes['colspan'] = 3
124                obj.style['text-align'] = 'left'
125            return res
126
127    class ArrayCell(Array.ArrayCell):
128        @property
129        def source(self):
130            return '$\\displaystyle %s $' % sourceChildren(self, par=False)
131
132class eqnarray(EqnarrayStar):
133    macroName = None
134    counter = 'equation'
135
136    class EndRow(Array.EndRow):
137        """ End of a row """
138        counter = 'equation'
139        def invoke(self, tex):
140            res = Array.EndRow.invoke(self, tex)
141            res[1].ref = self.ref
142            self.ownerDocument.context.currentlabel = res[1]
143            return res
144
145    def invoke(self, tex):
146        res = EqnarrayStar.invoke(self, tex)
147        if self.macroMode == self.MODE_END:
148            return res
149        res[1].ref = self.ref
150        return res
151class nonumber(Command):
152
153    def invoke(self, tex):
154        self.ownerDocument.context.counters['equation'].addtocounter(-1)
155
156    def digest(self, tokens):
157        try:
158            row = self.parentNode
159            while not isinstance(row, Array.ArrayRow):
160                row = row.parentNode
161            row.ref = None
162        except AttributeError as e:
163            print('problem encountered %s' % e)
164
165class notag(nonumber):
166    pass
167
168class lefteqn(Command):
169    args = 'self'
170
171#
172# Style Parameters
173#
174
175class jot(DimenCommand):
176    value = DimenCommand.new(0)
177
178class mathindent(DimenCommand):
179    value = DimenCommand.new(0)
180
181class abovedisplayskip(GlueCommand):
182    value = GlueCommand.new(0)
183
184class belowdisplayskip(GlueCommand):
185    value = GlueCommand.new(0)
186
187class abovedisplayshortskip(GlueCommand):
188    value = GlueCommand.new(0)
189
190class belowdisplayshortskip(GlueCommand):
191    value = GlueCommand.new(0)
192
193
194#
195# C.7.2 Common Structures
196#
197
198# _
199# ^
200# '
201
202class frac(Command):
203    args = 'numer denom'
204
205class sqrt(Command):
206    args = '[ n ] self'
207
208class ldots(Command):
209    str = '\u2026'
210
211class cdots(Command):
212    pass
213
214class vdots(Command):
215    pass
216
217class ddots(Command):
218    pass
219
220#
221# C.7.3 Mathematical Symbols
222#
223
224#
225# Table 3.3: Greek Letters
226#
227
228class MathSymbol(Command):
229    pass
230
231# Lowercase
232class alpha(MathSymbol): str = chr(945)
233class beta(MathSymbol): str = chr(946)
234class gamma(MathSymbol): str = chr(947)
235class delta(MathSymbol): str = chr(948)
236class epsilon(MathSymbol): str = chr(949)
237class varepsilon(MathSymbol): str = chr(949)
238class zeta(MathSymbol): str = chr(950)
239class eta(MathSymbol): str = chr(951)
240class theta(MathSymbol): str = chr(952)
241class vartheta(MathSymbol): str = chr(977)
242class iota(MathSymbol): str = chr(953)
243class kappa(MathSymbol): str = chr(954)
244class GreekLamda(MathSymbol):
245    macroName = 'lambda'
246    str = chr(955)
247class mu(MathSymbol): str = chr(956)
248class nu(MathSymbol): str = chr(957)
249class xi(MathSymbol): str = chr(958)
250class pi(MathSymbol): str = chr(960)
251class varpi(MathSymbol): str = chr(982)
252class rho(MathSymbol): str = chr(961)
253class varrho(MathSymbol): str = chr(1009)
254class sigma(MathSymbol): str = chr(963)
255class varsigma(MathSymbol): str = chr(962)
256class tau(MathSymbol): str = chr(964)
257class upsilon(MathSymbol): str = chr(965)
258class phi(MathSymbol): str = chr(966)
259class varphi(MathSymbol): str = chr(981)
260class chi(MathSymbol): str = chr(967)
261class psi(MathSymbol): str = chr(968)
262class omega(MathSymbol): str = chr(969)
263
264# Uppercase
265class Gamma(MathSymbol): str = chr(915)
266class Delta(MathSymbol): str = chr(916)
267class Theta(MathSymbol): str = chr(920)
268class Lambda(MathSymbol): str = chr(923)
269class Xi(MathSymbol): str = chr(926)
270class Pi(MathSymbol): str = chr(928)
271class Sigma(MathSymbol): str = chr(931)
272class Upsilon(MathSymbol): str = chr(978)
273class Phi(MathSymbol): str = chr(934)
274class Psi(MathSymbol): str = chr(936)
275class Omega(MathSymbol): str = chr(8486)
276
277
278#
279# Table 3.4: Binary Operation Symbols
280#
281
282class pm(MathSymbol): str = chr(177)
283class mp(MathSymbol): str = chr(8723)
284class times(MathSymbol): str = chr(215)
285class div(MathSymbol): str = chr(247)
286class ast(MathSymbol): str = chr(42)
287class star(MathSymbol): str = chr(8902)
288class circ(MathSymbol): str = chr(9675)
289class bullet(MathSymbol): str = chr(8226)
290class cdot(MathSymbol): str = chr(183)
291class cap(MathSymbol): str = chr(8745)
292class cup(MathSymbol): str = chr(8746)
293class uplus(MathSymbol): str = chr(8846)
294class sqcap(MathSymbol): str = chr(8851)
295class sqcup(MathSymbol): str = chr(8852)
296class vee(MathSymbol): str = chr(8744)
297class wedge(MathSymbol): str = chr(8743)
298class setminus(MathSymbol): str = chr(8726)
299class wr(MathSymbol): str = chr(8768)
300class diamond(MathSymbol): str = chr(8900)
301class bigtriangleup(MathSymbol): str = chr(9651)
302class bigtriangledown(MathSymbol): str = chr(9661)
303class triangleleft(MathSymbol): str = chr(9667)
304class triangleright(MathSymbol): str = chr(9657)
305class lhd(MathSymbol): pass
306class rhd(MathSymbol): pass
307class unlhd(MathSymbol): pass
308class unrhd(MathSymbol): pass
309class oplus(MathSymbol): str = chr(8853)
310class ominus(MathSymbol): str = chr(8854)
311class otimes(MathSymbol): str = chr(8855)
312class oslash(MathSymbol): str = chr(8856)
313class odot(MathSymbol): str = chr(8857)
314class bigcirc(MathSymbol): str = chr(9711)
315class dagger(MathSymbol): str = chr(8224)
316class ddagger(MathSymbol): str = chr(8225)
317class amalg(MathSymbol): str = chr(8720)
318
319#
320# Table 3.5: Relation Symbols
321#
322
323class Not(MathSymbol):
324    macroName = 'not'
325    args = 'symbol'
326class leq(MathSymbol): str = chr(8804)
327class le(MathSymbol): str = chr(8804)
328class prec(MathSymbol): str = chr(8826)
329class preceq(MathSymbol): str = chr(8828)
330class ll(MathSymbol): str = chr(8810)
331class subset(MathSymbol): str = chr(8834)
332class subseteq(MathSymbol): str = chr(8838)
333class sqsubseteq(MathSymbol): str = chr(8849)
334class In(MathSymbol):
335    macroName = 'in'
336class vdash(MathSymbol): str = chr(8866)
337class geq(MathSymbol): str = chr(8805)
338class ge(MathSymbol): str = chr(8805)
339class succ(MathSymbol): str = chr(8827)
340class succeq(MathSymbol): str = chr(8829)
341class gg(MathSymbol): str = chr(8811)
342class supset(MathSymbol): str = chr(8835)
343class supseteq(MathSymbol): str = chr(8839)
344class sqsupset(MathSymbol): str = chr(8848)
345class sqsupseteq(MathSymbol): str = chr(8850)
346class ni(MathSymbol): str = chr(8715)
347class dashv(MathSymbol): str = chr(8867)
348class equiv(MathSymbol): str = chr(8801)
349class sim(MathSymbol): str = chr(8764)
350class simeq(MathSymbol): str = chr(8771)
351class asymp(MathSymbol): str = chr(8781)
352class approx(MathSymbol): str = chr(8776)
353class cong(MathSymbol): str = chr(8773)
354class neq(MathSymbol): str = chr(8800)
355class ne(MathSymbol): str = chr(8800)
356class doteq(MathSymbol): str = chr(8784)
357class notin(MathSymbol): pass
358class models(MathSymbol): str = chr(8871)
359class perp(MathSymbol): str = chr(8869)
360class mid(MathSymbol): str = chr(8739)
361class parallel(MathSymbol): str = chr(8741)
362class bowtie(MathSymbol): str = chr(8904)
363class Join(MathSymbol): pass
364class smile(MathSymbol): str = chr(8995)
365class frown(MathSymbol): str = chr(8994)
366class propto(MathSymbol): str = chr(8733)
367
368#
369# Table 3.6: Arrow Symbols
370#
371
372class leftarrow(MathSymbol): str = chr(8592)
373class Leftarrow(MathSymbol): str = chr(8656)
374class rightarrow(MathSymbol): str = chr(8594)
375class Rightarrow(MathSymbol): str = chr(8658)
376class leftrightarrow(MathSymbol): str = chr(8596)
377class Leftrightarrow(MathSymbol): str = chr(8660)
378class mapsto(MathSymbol): str = chr(8614)
379class hookleftarrow(MathSymbol): str = chr(8617)
380class leftharpoonup(MathSymbol): str = chr(8636)
381class leftharpoondown(MathSymbol): str = chr(8637)
382class rightleftharpoons(MathSymbol): str = chr(8652)
383class longleftarrow(MathSymbol): pass
384class Longleftarrow(MathSymbol): pass
385class longrightarrow(MathSymbol): pass
386class Longrightarrow(MathSymbol): pass
387class longleftrightarrow(MathSymbol): pass
388class Longleftrightarrow(MathSymbol): pass
389class longmapsto(MathSymbol): pass
390class hookrightarrow(MathSymbol): str = chr(8618)
391class rightharpoonup(MathSymbol): str = chr(8640)
392class rightharpoondown(MathSymbol): str = chr(8641)
393class leadsto(MathSymbol): pass
394class uparrow(MathSymbol): str = chr(8593)
395class Uparrow(MathSymbol): str = chr(8657)
396class downarrow(MathSymbol): str = chr(8595)
397class Downarrow(MathSymbol): str = chr(8659)
398class updownarrow(MathSymbol): str = chr(8597)
399class Updownarrow(MathSymbol): str = chr(8661)
400class nearrow(MathSymbol): str = chr(8599)
401class searrow(MathSymbol): str = chr(8600)
402class swarrow(MathSymbol): str = chr(8601)
403class nwarrow(MathSymbol): str = chr(8598)
404
405#
406# Table 3.7: Miscellaneous Symbols
407#
408
409class aleph(MathSymbol): str = chr(8501)
410class hbar(MathSymbol): str = chr(8463)
411class imath(MathSymbol): pass
412class jmath(MathSymbol): pass
413class ell(MathSymbol): str = chr(8467)
414class wp(MathSymbol): str = chr(8472)
415class Re(MathSymbol): str = chr(8476)
416class Im(MathSymbol): str = chr(8465)
417class mho(MathSymbol): str = chr(8487)
418class prime(MathSymbol): str = chr(8242)
419class emptyset(MathSymbol): str = chr(8709)
420class nabla(MathSymbol): str = chr(8711)
421class surd(MathSymbol): str = chr(8730)
422class top(MathSymbol): str = chr(8868)
423class bot(MathSymbol): str = chr(8869)
424class VerticalBar(MathSymbol):
425    macroName = '|'
426class forall(MathSymbol): str = chr(8704)
427class exists(MathSymbol): str = chr(8707)
428class neg(MathSymbol): pass
429class flat(MathSymbol): str = chr(9837)
430class natural(MathSymbol): str = chr(9838)
431class sharp(MathSymbol): str = chr(9839)
432class backslash(MathSymbol): str = chr(92)
433class partial(MathSymbol): str = chr(8706)
434class infty(MathSymbol): str = chr(8734)
435class Box(MathSymbol): pass
436class Diamond(MathSymbol): pass
437class triangle(MathSymbol): str = chr(9653)
438class clubsuit(MathSymbol): str = chr(9827)
439class diamondsuit(MathSymbol): str = chr(9830)
440class heartsuit(MathSymbol): str = chr(9829)
441class spadesuit(MathSymbol): str = chr(9824)
442
443#
444# Table 3.8: Variable-sized Symbols
445#
446
447class sum(MathSymbol): str = chr(8721)
448class prod(MathSymbol): str = chr(8719)
449class coprod(MathSymbol): str = chr(8720)
450class int(MathSymbol): str = chr(8747)
451class oint(MathSymbol): str = chr(8750)
452class bigcap(MathSymbol): pass
453class bigcup(MathSymbol): pass
454class bigsqcup(MathSymbol): pass
455class bigvee(MathSymbol): pass
456class bigwedge(MathSymbol): pass
457class bigodot(MathSymbol): pass
458class bigotimes(MathSymbol): pass
459class bigoplus(MathSymbol): pass
460class biguplus(MathSymbol): pass
461
462#
463# Table 3.9: Log-like Functions
464#
465
466class Logarithm(MathSymbol):
467    macroName = 'log'
468class bmod(MathSymbol): pass
469class pmod(MathSymbol):
470    args = 'self'
471class arccos(MathSymbol): pass
472class arcsin(MathSymbol): pass
473class arctan(MathSymbol): pass
474class arg(MathSymbol): pass
475class cos(MathSymbol): pass
476class cosh(MathSymbol): pass
477class cot(MathSymbol): pass
478class coth(MathSymbol): pass
479class csc(MathSymbol): pass
480class deg(MathSymbol): pass
481class det(MathSymbol): pass
482class dim(MathSymbol): pass
483class exp(MathSymbol): pass
484class gcd(MathSymbol): pass
485class hom(MathSymbol): pass
486class inf(MathSymbol): pass
487class ker(MathSymbol): pass
488class lg(MathSymbol): pass
489class lim(MathSymbol): pass
490class liminf(MathSymbol): pass
491class limsup(MathSymbol): pass
492class ln(MathSymbol): pass
493class log(MathSymbol): pass
494class max(MathSymbol): pass
495class min(MathSymbol): pass
496class Pr(MathSymbol): pass
497class sec(MathSymbol): pass
498class sin(MathSymbol): pass
499class sinh(MathSymbol): pass
500class sup(MathSymbol): pass
501class tan(MathSymbol): pass
502class tanh(MathSymbol): pass
503
504
505#
506# C.7.4 Arrays (see Arrays.py)
507#
508
509#
510# C.7.5 Delimiters
511#
512
513class left(Command):
514    args = 'delim'
515
516class right(Command):
517    args = 'delim'
518
519# Table 3.10: Delimiters and TeXbook (p359)
520
521class Delimiter(Command):
522    pass
523
524class langle(Delimiter): pass
525class rangle(Delimiter): pass
526class lbrace(Delimiter): pass
527class rbrace(Delimiter): pass
528class lceil(Delimiter): pass
529class rceil(Delimiter): pass
530class lfloor(Delimiter): pass
531class rfloor(Delimiter): pass
532class lgroup(Delimiter): pass
533class rgroup(Delimiter): pass
534class lmoustache(Delimiter): pass
535class rmoustache(Delimiter): pass
536class uparrow(Delimiter): pass
537class Uparrow(Delimiter): pass
538class downarrow(Delimiter): pass
539class Downarrow(Delimiter): pass
540class updownarrow(Delimiter): pass
541class Updownarrow(Delimiter): pass
542class arrowvert(Delimiter): pass
543class Arrowvert(Delimiter): pass
544class vert(Delimiter): pass
545class Vert(Delimiter): pass
546class backslash(Delimiter): pass
547class bracevert(Delimiter): pass
548
549class bigl(Delimiter): pass
550class bigm(Delimiter): pass
551class bigr(Delimiter): pass
552class Bigl(Delimiter): pass
553class Bigm(Delimiter): pass
554class Bigr(Delimiter): pass
555class biggl(Delimiter): pass
556class biggr(Delimiter): pass
557class Biggl(Delimiter): pass
558class Biggr(Delimiter): pass
559class biggm(Delimiter): pass
560class Biggm(Delimiter): pass
561class Big(Delimiter):
562    args = 'char'
563class bigg(Delimiter):
564    args = 'char'
565class Bigg(Delimiter):
566    args = 'char'
567
568class choose(Command):
569    pass
570
571class brack(Command):
572    pass
573
574class brace(Command):
575    pass
576
577#class sqrt(Command):
578#    pass
579
580#
581# C.7.6 Putting One Thing Above Another
582#
583
584class overline(Command):
585    args = 'self'
586
587class underline(Command):
588    args = 'self'
589
590class overbrace(Command):
591    args = 'self'
592
593class underbrace(Command):
594    args = 'self'
595
596# Accents
597
598class MathAccent(Command):
599    args = 'self'
600
601class hat(MathAccent): pass
602class check(MathAccent): pass
603class breve(MathAccent): pass
604class acute(MathAccent): pass
605class grave(MathAccent): pass
606class tilde(MathAccent): pass
607class bar(MathAccent): pass
608class vec(MathAccent): pass
609class dot(MathAccent): pass
610class ddot(MathAccent): pass
611
612class widehat(MathAccent): pass
613class widetilde(MathAccent): pass
614class imath(MathAccent): pass
615class jmath(MathAccent): pass
616class stackrel(MathAccent):
617    args = 'top bottom'
618
619#
620# C.7.7 Spacing
621#
622
623# These are nested inside the MathEnvironemnt
624
625
626#
627# C.7.8 Changing Style
628#
629
630# Type Style
631
632class mathrm(Command):
633    args = 'self'
634
635class mathit(Command):
636    args = 'self'
637
638class mathbf(Command):
639    args = 'self'
640
641class mathsf(Command):
642    args = 'self'
643
644class mathtt(Command):
645    args = 'self'
646
647class mathcal(Command):
648    args = 'self'
649
650class boldmath(Command):
651    pass
652
653class unboldmath(Command):
654    pass
655
656# Math Style
657
658class displaystyle(Command):
659    pass
660
661class textstyle(Command):
662    pass
663
664class scriptstyle(Command):
665    pass
666
667class scriptscriptstyle(Command):
668    pass
669