forked from aantthony/graph.tk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathquill_modified.js
1965 lines (1870 loc) · 54.4 KB
/
mathquill_modified.js
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Usage:
* $(thing).mathquill();
* turns thing into a live editable math thingy.
* AMAZORZ.
*
* Note: turning into a live editable math thingmajiggie works, but
* any LaTeX math in it won't be rendered.
*
*/
jQuery.fn.mathquill = (function($){ //takes in the jQuery function as an argument
//Note: if the following is no longer on line 15, please modify publish.sh accordingly
//$('head').append('<link rel="stylesheet" type="text/css" href="http://laughinghan.github.com/mathquill/mathquill.css">');
var todo = function(){ alert('BLAM!\n\nAHHHHHH!\n\n"Oh god, oh god, I\'ve never seen so much blood!"\n\nYeah, that doesn\'t fully work yet.'); };
/**********************************************************
* Back-end code: Core abstract classes and architecture.
*********************************************************/
var $ = jQuery, todo = function(){ alert('BLAM!\n\nAHHHHHH!\n\n"Oh god, oh god, I\'ve never seen so much blood!"\n\nYeah, that doesn\'t fully work yet.'); };
/**
* MathElement is the core Math DOM tree node prototype.
* Both MathBlock's and MathCommand's descend from it.
*/
function MathElement(){}
MathElement.prototype = {
prev: null,
next: null,
parent: null,
firstChild: null,
lastChild: null,
eachChild: function(fn)
{
for(var child = this.firstChild; child !== null; child = child.next)
if(fn.call(child) === false)
break;
return this;
},
reduceChildren: function(fn, initVal)
{
this.eachChild(function(){
initVal = fn.call(this, initVal);
});
return initVal;
},
keydown: function(e)
{
return this.parent.keydown(e);
},
keypress: function(e)
{
return this.parent.keypress(e);
}
};
/**
* Commands and operators, like subscripts, exponents, or fractions.
* Descendant commands are organized into blocks.
* May be passed a MathFragment that's being replaced.
*/
function MathCommand(cmd, html_template, replacedBlock)
{
if(!arguments.length)
return;
this.cmd = cmd;
if(html_template)
this.html_template = html_template;
this.jQ = $(this.html_template[0]).data('[[mathquill internal data]]', {cmd: this});
this.initBlocks(replacedBlock);
}
MathCommand.prototype = $.extend(new MathElement, {
initBlocks: function(replacedBlock)
{
//single-block commands
if(this.html_template.length === 1)
{
this.firstChild = this.lastChild = this.jQ.data('[[mathquill internal data]]').block =
replacedBlock || new MathBlock;
this.firstChild.parent = this;
this.firstChild.jQ = this.jQ.prepend(this.firstChild.jQ);
return;
}
//otherwise, the succeeding elements of html_template should be child blocks
var newBlock, prev, num_blocks = this.html_template.length;
this.firstChild = newBlock = prev = replacedBlock || new MathBlock;
newBlock.parent = this;
newBlock.jQ = $(this.html_template[1]).data('[[mathquill internal data]]', {block: newBlock}).appendTo(this.jQ).prepend(newBlock.jQ);
newBlock.setEmpty();
for(var i = 2; i < num_blocks; i += 1)
{
newBlock = new MathBlock;
newBlock.parent = this;
newBlock.prev = prev;
prev.next = newBlock;
prev = newBlock;
newBlock.jQ = $(this.html_template[i]).data('[[mathquill internal data]]', {block: newBlock}).appendTo(this.jQ);
newBlock.setEmpty();
}
this.lastChild = newBlock;
},
latex: function()
{
return this.cmd + this.reduceChildren(function(initVal){
return initVal + '{' + this.latex() + '}';
}, '');
},
remove: function()
{
if(this.prev)
this.prev.next = this.next;
else
this.parent.firstChild = this.next;
if(this.next)
this.next.prev = this.prev;
else
this.parent.lastChild = this.prev;
this.jQ.remove();
return this;
},
//placeholder for context-sensitive spacing.
respace: $.noop,
placeCursor: function(cursor)
{
cursor.prependTo(this.firstChild);
},
isEmpty: function()
{
return this.reduceChildren(function(initVal){
return initVal && this.isEmpty();
}, true);
}
});
/**
* Lightweight command without blocks or children.
*/
function Symbol(cmd, html)
{
MathCommand.call(this, cmd, [ html ]);
}
Symbol.prototype = $.extend(new MathCommand, {
initBlocks: $.noop,
latex: function()
{
return this.cmd;
},
placeCursor: $.noop,
isEmpty: function(){ return true; }
});
/**
* Children and parent of MathCommand's. Basically partitions all the
* symbols and operators that descend (in the Math DOM tree) from
* ancestor operators.
*/
function MathBlock(){}
MathBlock.prototype = $.extend(new MathElement, {
latex: function()
{
return this.reduceChildren(function(initVal){
return initVal + this.latex();
}, '');
},
isEmpty: function()
{
return this.firstChild === null && this.lastChild === null;
},
setEmpty: function()
{
if(this.isEmpty())
{
this.jQ.addClass('empty');
if(this.parent)
this.jQ.html('∅');
}
return this;
},
removeEmpty:function()
{
if(this.jQ.hasClass('empty'))
this.jQ.html('').removeClass('empty');
return this;
}
});
/**
* An entity outside the Math DOM tree with one-way pointers (so it's only
* a "view" of part of the tree, not an actual node/entity in the tree)
* that delimit a list of symbols and operators.
*/
function MathFragment(parent, prev, next)
{
if(!arguments.length)
return;
this.parent = parent;
this.prev = prev || null; //so you can do 'new MathFragment(block)' without
this.next = next || null; //ending up with this.prev or this.next === undefined
this.jQinit(this.reduce(function(initVal){ return this.jQ.add(initVal); }, $()));
}
MathFragment.prototype = {
remove: MathCommand.prototype.remove,
jQinit: function(children)
{
return this.jQ = children;
},
each: function(fn)
{
for(var el = (this.prev ? this.prev.next : this.parent.firstChild); el !== this.next; el = el.next)
if(fn.call(el) === false)
break;
return this;
},
reduce: function(fn, initVal)
{
this.each(function()
{
initVal = fn.call(this, initVal);
});
return initVal;
},
blockify: function()
{
var newBlock = new MathBlock;
if(this.prev)
{
newBlock.firstChild = this.prev.next;
this.prev.next = this.next;
}
else
{
newBlock.firstChild = this.parent.firstChild;
this.parent.firstChild = this.next;
}
if(this.next)
{
newBlock.lastChild = this.next.prev;
this.next.prev = this.prev;
}
else
{
newBlock.lastChild = this.parent.lastChild;
this.parent.lastChild = this.prev;
}
newBlock.firstChild.prev = this.prev = null;
newBlock.lastChild.next = this.next = null;
this.parent = newBlock;
this.each(function(){ this.parent = newBlock; });
newBlock.jQ = this.jQ;
return newBlock;
}
};
/********************************************
* All the symbols, operators and commands.
*******************************************/
function VanillaSymbol(ch, html)
{
Symbol.call(this, ch, '<span>'+(html || ch)+'</span>');
}
VanillaSymbol.prototype = Symbol.prototype;
function NonSymbolaSymbol(ch, html) //does not use Symbola font
{
Symbol.call(this, ch, '<span class="nonSymbola">'+(html || ch)+'</span>');
}
NonSymbolaSymbol.prototype = Symbol.prototype;
function BigSymbol(ch, html)
{
Symbol.call(this, ch, '<big>'+(html || ch)+'</big>');
}
BigSymbol.prototype = Symbol.prototype;
function Variable(ch, html)
{
Symbol.call(this, ch, '<var>'+(html || ch)+'</var>');
}
Variable.prototype = Symbol.prototype;
function BinaryOperator(cmd, html)
{
Symbol.call(this, cmd, '<span class="binary-operator">'+html+'</span>');
}
BinaryOperator.prototype = new Symbol; //so instanceof will work
function PlusMinus(cmd, html)
{
VanillaSymbol.apply(this, arguments);
}
PlusMinus.prototype = new BinaryOperator; //so instanceof will work
PlusMinus.prototype.respace = function()
{
if(!this.prev)
this.jQ[0].className = '';
else if(this.prev instanceof BinaryOperator && this.next && !(this.next instanceof BinaryOperator))
this.jQ[0].className = 'unary-operator';
else
this.jQ[0].className = 'binary-operator';
return this;
};
function SupSub(cmd, html, replacedBlock)
{
MathCommand.call(this, cmd, [ html ], replacedBlock);
var me = this;
this.jQ.change(function()
{
me.respace();
if(me.next)
me.next.respace();
if(me.prev)
me.prev.respace();
});
}
SupSub.prototype = new MathCommand;
SupSub.prototype.latex = function()
{
var latex = this.firstChild.latex();
if(latex.length === 1)
return this.cmd + latex;
return this.cmd + '{' + latex + '}';
};
SupSub.prototype.respace = function()
{
if(this.respaced = this.prev instanceof SupSub && this.prev.cmd != this.cmd && !this.prev.respaced)
this.jQ.css({
left: -this.prev.jQ.innerWidth(),
marginRight: 1-Math.min(this.jQ.innerWidth(), this.prev.jQ.innerWidth()) //1px adjustment very important!
});
else if(this.cmd === '_' && this.prev && this.prev.cmd === '\\int ')
this.jQ.css({
left: '-.1em',
marginRight: ''
});
else
this.jQ.css({
left: '',
marginRight: ''
});
return this;
};
function Fraction(replacedBlock)
{
MathCommand.call(this, '\\frac', undefined, replacedBlock);
this.jQ.append('<span style="width:0"> </span>');
if($.browser.mozilla && +$.browser.version.slice(0,3) < 1.9) //Firefox 2 and below
this.jQ.css('display','-moz-groupbox');
}
Fraction.prototype = new MathCommand;
Fraction.prototype.html_template = ['<span class="fraction"></span>', '<span class="numerator"></span>', '<span class="denominator"></span>'];
function LiveFraction(replacedBlock)
{
Fraction.apply(this, arguments);
}
LiveFraction.prototype = new Fraction;
LiveFraction.prototype.placeCursor = function(cursor)
{
if(this.firstChild.isEmpty())
{
var prev = this.prev;
while(prev && !(prev instanceof BinaryOperator)) //lookbehind for operator
prev = prev.prev;
if(prev !== this.prev)
{
var newBlock = new MathFragment(this.parent, prev, this).blockify();
newBlock.jQ = this.firstChild.removeEmpty().jQ.prepend(newBlock.jQ);
newBlock.next = this.lastChild;
newBlock.parent = this;
this.firstChild = this.lastChild.prev = newBlock;
}
}
cursor.appendTo(this.lastChild);
};
// Parens/Brackets/Braces etc
function Paren(open, close, replacedBlock)
{
MathCommand.call(this, '\\left'+open,
['<span><span class="paren">'+open+'</span><span></span><span class="paren">'+close+'</span></span>'],
replacedBlock);
this.end = '\\right'+close;
this.firstChild.jQ.change(function()
{
var block = $(this);
block.prev().add(block.next()).css('fontSize', block.height()/(+block.css('fontSize').slice(0,-2)*.8+3)+'em');
});
}
Paren.prototype = $.extend(new MathCommand, {
initBlocks: function(replacedBlock)
{
this.firstChild = this.lastChild = replacedBlock || new MathBlock;
this.firstChild.parent = this;
this.firstChild.jQ = this.firstChild.jQ ? this.jQ.children().eq(1).prepend(this.firstChild.jQ) : this.jQ.children().eq(1);
},
latex: function()
{
return this.cmd + this.firstChild.latex() + this.end;
}
});
// Closing parens/brackets/braces matching Parens/Brackets/Braces above
function CloseParen(open, close, replacedBlock)
{
Paren.call(this, open, close, replacedBlock);
}
CloseParen.prototype = new Paren;
CloseParen.prototype.placeCursor = function(cursor)
{
//if I'm at the end of my parent who is a matching open-paren, and I was not passed
// a selection fragment, get rid of me and put cursor after my parent
if(!this.next && this.parent.parent && this.parent.parent.end === this.end && this.firstChild.isEmpty())
cursor.backspace().insertAfter(this.parent.parent);
else
{
cursor.insertAfter(this);
this.firstChild.setEmpty().jQ.change();
}
};
function Brace(replacedBlock)
{
Paren.call(this, '{', '}', replacedBlock);
}
Brace.prototype = new Paren;
Brace.prototype.latex = function()
{
return '\\left\\{' + this.firstChild.latex() + '\\right\\}';
};
function CloseBrace(replacedBlock)
{
CloseParen.call(this, '{', '}', replacedBlock);
}
CloseBrace.prototype = new CloseParen;
CloseBrace.prototype.latex = Brace.prototype.latex;
function Pipes(replacedBlock)
{
Paren.call(this, '|', '|', replacedBlock);
}
Pipes.prototype = new Paren;
Pipes.prototype.placeCursor = function(cursor)
{
if(!this.next && this.parent.parent && this.parent.parent.end === this.end && this.firstChild.isEmpty())
cursor.backspace().insertAfter(this.parent.parent);
else
cursor.prependTo(this.firstChild);
};
function TextBlock(replacedBlock)
{
MathCommand.call(this, '\\text', undefined, new InnerTextBlock);
if(replacedBlock instanceof MathBlock)
{
this.replacedText = replacedBlock.jQ.text();
replacedBlock.jQ.remove();
}
else
this.replacedText = replacedBlock;
}
TextBlock.prototype = $.extend(new MathCommand, {
html_template: ['<span class="text"></span>'],
placeCursor: function(cursor)
{
this.cursor = cursor.prependTo(this.firstChild);
if(this.replacedText)
for(var i = 0; i < this.replacedText.length; i += 1)
this.write(this.replacedText.charAt(i));
},
write: function(ch)
{
this.cursor.insertNew(new VanillaSymbol(ch)).show();
},
keydown: function(e)
{
//backspace and delete and ends of block don't unwrap
if(!this.isEmpty() &&
((e.which === 8 && !this.cursor.prev && !this.cursor.selection) ||
(e.which === 46 && !this.cursor.next)))
return false;
return this.parent.keydown(e);
},
keypress: function(e)
{
var ch = String.fromCharCode(e.which);
if(ch === '$')
if(this.isEmpty())
this.cursor.insertAfter(this).backspace().insertNew(new VanillaSymbol('\\$','$')).show();
else if(!this.cursor.next)
this.cursor.insertAfter(this);
else if(!this.cursor.prev)
this.cursor.insertBefore(this);
else //split apart
{
var next = new TextBlock(new MathFragment(this.firstChild, this.cursor.prev).blockify());
next.firstChild.removeEmpty = function(){ return this; };
this.cursor.insertAfter(this).insertNew(next).insertBefore(next);
delete next.firstChild.removeEmpty;
}
else
this.write(ch);
return false;
}
});
function InnerTextBlock(){}
InnerTextBlock.prototype = $.extend(new MathBlock, {
setEmpty: function()
{
if(this.isEmpty())
{
var textblock = this.parent;
setTimeout(function() //defer execution until after completion of this thread
//not the wrong way to do things, merely poorly named
{
if(textblock.cursor.prev === textblock)
textblock.cursor.backspace();
else if(textblock.cursor.next === textblock)
textblock.cursor.deleteForward();
//else must be blur, don't remove textblock
},0);
};
return this;
},
removeEmpty: function()
{
if(this.parent.prev instanceof TextBlock)
{
var me = this, textblock = this.parent, prev = textblock.prev.firstChild;
setTimeout(function() //defer
{
prev.eachChild(function(){
this.parent = me;
this.jQ.insertBefore(me.firstChild.jQ);
});
prev.lastChild.next = me.firstChild;
me.firstChild.prev = prev.lastChild;
me.firstChild = prev.firstChild;
textblock.prev.remove();
if(textblock.cursor.next)
textblock.cursor.insertBefore(textblock.cursor.next);
else
textblock.cursor.appendTo(me);
me.jQ.change();
},0);
}
else if(this.parent.next instanceof TextBlock)
if(this.parent.cursor.next)
this.parent.next.firstChild.removeEmpty();
else
this.parent.cursor.prependTo(this.parent.next.firstChild);
return this;
}
});
// input box to type a variety of LaTeX commands beginning with a backslash
function LatexCommandInput(replacedBlock, replacedFragment)
{
MathCommand.call(this, '\\');
this.firstChild.setEmpty = function()
{
if(this.isEmpty())
{
this.jQ.addClass('empty');
if(this.parent)
this.jQ.html('<span> </span>');
}
return this;
};
if(replacedBlock)
{
replacedBlock.jQ.detach();
this.replacedBlock = replacedBlock;
this.replacedFragment = replacedFragment;
this.isEmpty = function(){ return false; };
}
}
LatexCommandInput.prototype = $.extend(new MathCommand, {
html_template: ['<span class="latex-command-input"></span>'],
placeCursor: function(cursor)
{
this.cursor = cursor.prependTo(this.firstChild);
if(this.replacedFragment)
this.jQ = this.jQ.add(this.replacedFragment.jQ.addClass('blur').insertAfter(this.jQ));
},
latex: function()
{
return '\\' + this.firstChild.latex() + ' ';
},
keydown: function(e)
{
if(e.which === 9 || e.which === 13) //tab or enter
{
this.renderCommand();
return false;
}
return this.parent.keydown(e);
},
keypress: function(e)
{
var char = String.fromCharCode(e.which);
if(char.match(/[a-z]/i))
{
this.cursor.insertNew(new VanillaSymbol(char)).show();
return false;
}
this.renderCommand();
if(char === ' ' || (char === '\\' && this.firstChild.isEmpty()))
return false;
return this.parent.keypress(e);
},
renderCommand: function()
{
this.jQ = this.jQ.first();
this.remove();
if(this.prev)
this.cursor.insertAfter(this.prev);
else
this.cursor.prependTo(this.parent);
this.cursor.insertNew(this.firstChild.isEmpty() ?
new VanillaSymbol('\\\\','\\') : createLatexCommand(this.firstChild.latex(), this.replacedBlock));
}
});
function SquareRoot(replacedBlock)
{
MathCommand.call(this, '\\sqrt', undefined, replacedBlock);
this.firstChild.jQ.change(function()
{
var block = $(this), height = block.height();
block.css({
borderTopWidth: height/30+1 // NOTE: Formula will need to change if our font isn't Symbola
}).prev().css({
fontSize: height/+block.css('fontSize').slice(0,-2)+'em'
});
});
}
SquareRoot.prototype = new MathCommand;
SquareRoot.prototype.html_template = ['<span><span class="sqrt-prefix">√</span></span>','<span class="sqrt-stem"></span>'];
function NonItalicizedFunction(fn)
{
Symbol.call(this, '\\'+fn+' ', '<span class="non-italicized-function">'+fn+'</span>');
}
NonItalicizedFunction.prototype = Symbol.prototype;
var SingleCharacterCommands = {
//Symbols:
' ': function(){ return new VanillaSymbol('\\,', ' '); },
"'": function(){ return new VanillaSymbol("'", '′'); },
'f': function(){ return new Symbol('f', '<var class="florin">ƒ</var>'); },
'@': function(){ return new NonSymbolaSymbol('@'); },
'&': function(){ return new NonSymbolaSymbol('\\&', '&'); },
'%': function(){ return new NonSymbolaSymbol('\\%', '%'); },
'*': function(){ return new BinaryOperator('\\cdot ', '·'); },
//semantically should be ⋅, but · looks better in both Symbola and Times New Roman
'=': function(){ return new BinaryOperator('=', '='); },
'<': function(){ return new BinaryOperator('<', '<'); },
'>': function(){ return new BinaryOperator('>', '>'); },
'+': function(){ return new PlusMinus('+'); },
'-': function(){ return new PlusMinus('-', '−'); },
//Commands and Operators:
'^': function(replacedBlock){ return new SupSub('^', '<sup></sup>', replacedBlock); },
'_': function(replacedBlock){ return new SupSub('_', '<sub></sub>', replacedBlock); },
'/': function(replacedBlock){ return new LiveFraction(replacedBlock); },
'(': function(replacedBlock){ return new Paren('(', ')', replacedBlock); },
'[': function(replacedBlock){ return new Paren('[', ']', replacedBlock); },
'{': function(replacedBlock){ return new Brace(replacedBlock); },
')': function(replacedBlock){ return new CloseParen('(', ')', replacedBlock); },
']': function(replacedBlock){ return new CloseParen('[', ']', replacedBlock); },
'}': function(replacedBlock){ return new CloseBrace(replacedBlock); },
'|': function(replacedBlock){ return new Pipes(replacedBlock); },
'$': function(replacedBlock){ return new TextBlock(replacedBlock); },
'\\': function(replacedBlock, replacedFragment){
return new LatexCommandInput(replacedBlock, replacedFragment);
}
};
function createLatexCommand(latex, replacedBlock)
{
if(latex.match(/^(a|arc)?(sin|cos|tan)h?$/))
return new NonItalicizedFunction(latex);
switch(latex)
{
//"real" commands
case 'sqrt':
return new SquareRoot(replacedBlock);
case 'frac':
return new Fraction(replacedBlock);
case 'text':
return new TextBlock(replacedBlock);
//non-italicized functions
case 'ln':
case 'lg':
case 'log':
case 'span':
case 'proj':
case 'det':
case 'dim':
case 'min':
case 'max':
case 'mod':
case 'lcm':
case 'gcd':
case 'lim':
return new NonItalicizedFunction(latex);
/*** Symbols and Special Characters ***/
//the following are all greek to me, but this helped a lot: http://www.ams.org/STIX/ion/stixsig03.html
//lowercase greek letter variables
case 'alpha':
case 'beta':
case 'gamma':
case 'delta':
case 'zeta':
case 'eta':
case 'theta':
case 'iota':
case 'kappa':
case 'mu':
case 'nu':
case 'xi':
case 'omicron':
case 'rho':
case 'sigma':
case 'tau':
case 'upsilon':
case 'chi':
case 'psi':
case 'omega':
return new Variable('\\'+latex+' ','&'+latex+';');
//why can't anybody FUCKING agree on these
case 'phi': //W3C or Unicode?
return new Variable('\\phi ','ϕ');
case 'phiv': //Elsevier and 9573-13
case 'varphi': //AMS and LaTeX
return new Variable('\\varphi ','φ');
case 'epsilon': //W3C or Unicode?
return new Variable('\\epsilon ','ϵ');
case 'epsiv': //Elsevier and 9573-13
case 'varepsilon': //AMS and LaTeX
return new Variable('\\varepsilon ','ε');
case 'sigmaf': //W3C/Unicode
case 'sigmav': //Elsevier
case 'varsigma': //LaTeX
return new Variable('\\varsigma ','ς');
//these aren't even mentioned in the HTML character entity references
case 'gammad': //Elsevier
case 'Gammad': //9573-13 -- WTF, right? I dunno if this was a typo in the reference (see above)
case 'digamma': //LaTeX
return new Variable('\\digamma ','ϝ');
case 'kappav': //Elsevier
case 'varkappa': //AMS and LaTeX
return new Variable('\\varkappa ','ϰ');
case 'piv': //Elsevier and 9573-13
case 'varpi': //AMS and LaTeX
return new Variable('\\varpi ','ϖ');
case 'rhov': //Elsevier and 9573-13
case 'varrho': //AMS and LaTeX
return new Variable('\\varrho ','ϱ');
case 'thetav': //Elsevier and 9573-13
case 'vartheta': //AMS and LaTeX
return new Variable('\\vartheta ','ϑ');
//greek constants, look best in un-italicised Times New Roman
case 'pi':
case 'lambda':
return new NonSymbolaSymbol('\\'+latex+' ','&'+latex+';');
//uppercase greek letters
case 'Gamma':
case 'Delta':
case 'Theta':
case 'Lambda':
case 'Xi':
case 'Pi':
case 'Sigma':
case 'Upsilon':
case 'Phi':
case 'Psi':
case 'Omega':
//other symbols with the same LaTeX command and HTML character entity reference
case 'perp':
case 'nabla':
case 'forall':
return new VanillaSymbol('\\'+latex+' ','&'+latex+';');
//sum, product, coproduct, integral
case 'sum':
case 'summation':
return new BigSymbol('\\sum ','∑');
case 'prod':
case 'product':
return new BigSymbol('\\prod ','∏');
case 'coprod':
case 'coproduct':
return new BigSymbol('\\coprod ','∐');
case 'int':
case 'integral':
return new BigSymbol('\\int ','∫');
//the canonical sets of numbers
case 'N':
case 'naturals':
case 'Naturals':
return new VanillaSymbol('\\mathbb{N}','ℕ');
case 'P':
case 'primes':
case 'Primes':
case 'projective':
case 'Projective':
case 'probability':
case 'Probability':
return new VanillaSymbol('\\mathbb{P}','ℙ');
case 'Z':
case 'integers':
case 'Integers':
return new VanillaSymbol('\\mathbb{Z}','ℤ');
case 'Q':
case 'rationals':
case 'Rationals':
return new VanillaSymbol('\\mathbb{Q}','ℚ');
case 'R':
case 'reals':
case 'Reals':
return new VanillaSymbol('\\mathbb{R}','ℝ');
case 'C':
case 'complex':
case 'Complex':
case 'complexes':
case 'Complexes':
case 'complexplane':
case 'Complexplane':
case 'ComplexPlane':
return new VanillaSymbol('\\mathbb{C}','ℂ');
case 'H':
case 'Hamiltonian':
case 'quaternions':
case 'Quaternions':
return new VanillaSymbol('\\mathbb{H}','ℍ');
//spacing
case 'quad':
case 'emsp':
return new VanillaSymbol('\\quad ',' ');
case 'qquad':
return new VanillaSymbol('\\qquad ',' ');
/* spacing special characters, gonna have to implement this in LatexCommandInput.prototype.keypress somehow
case ',':
return new VanillaSymbol('\\, ',' ');
case':':
return new VanillaSymbol('\\: ',' ');
case ';':
return new VanillaSymbol('\\; ',' ');
case '!':
return new Symbol('\\! ','<span style="margin-right:-.2em"></span>');
*/
//various symbols
case 'AA':
case 'Angstrom':
case 'angstrom':
return new VanillaSymbol('\\text{\\AA}','Å');
case 'ring':
case 'circ':
case 'circle':
return new VanillaSymbol('\\circ ','∘');
case 'bull':
case 'bullet':
return new VanillaSymbol('\\bullet ','•');
case 'setminus':
case 'smallsetminus':
return new VanillaSymbol('\\setminus ','∖');
case 'not':
//return new Symbol('\\not ','<span class="not">/</span>');
case 'neg':
return new VanillaSymbol('\\neg ','¬');
case 'dots':
case 'ellip':
case 'hellip':
case 'ellipsis':
case 'hellipsis':
return new VanillaSymbol('\\dots ','…');
case 'converges':
case 'darr':
case 'dnarr':
case 'dnarrow':
case 'downarrow':
return new VanillaSymbol('\\downarrow ','↓');
case 'dArr':
case 'dnArr':
case 'dnArrow':
case 'Downarrow':
return new VanillaSymbol('\\Downarrow ','⇓');
case 'diverges':
case 'uarr':
case 'uparrow':
return new VanillaSymbol('\\uparrow ','↑');
case 'uArr':
case 'Uparrow':
return new VanillaSymbol('\\Uparrow ','⇑');
case 'to':
return new BinaryOperator('\\to ','→');
case 'rarr':
case 'rightarrow':
return new VanillaSymbol('\\rightarrow ','→');
case 'implies':
return new BinaryOperator('\\Rightarrow ','⇒');
case 'rArr':
case 'Rightarrow':
return new VanillaSymbol('\\Rightarrow ','⇒');
case 'gets':
return new BinaryOperator('\\gets ','←');
case 'larr':
case 'leftarrow':
return new VanillaSymbol('\\leftarrow ','←');
case 'impliedby':
return new BinaryOperator('\\Leftarrow ','⇐');
case 'lArr':
case 'Leftarrow':
return new VanillaSymbol('\\Leftarrow ','⇐');
case 'harr':
case 'lrarr':
case 'leftrightarrow':
return new VanillaSymbol('\\leftrightarrow ','↔');
case 'iff':
return new BinaryOperator('\\Leftrightarrow ','⇔');
case 'hArr':
case 'lrArr':
case '\Leftrightarrow':
return new VanillaSymbol('\\Leftrightarrow ','⇔');
case 'Re':
case 'Real':
case 'real':
return new VanillaSymbol('\\Re ','ℜ');
case 'Im':
case 'imag':
case 'image':
case 'imagin':
case 'imaginary':
case 'Imaginary':
return new VanillaSymbol('\\Im ','ℑ');
case 'part':
case 'partial':
return new VanillaSymbol('\\partial ','∂');
case 'inf':
case 'infin':
case 'infty':
case 'infinity':
return new VanillaSymbol('\\infty ','∞');
case 'alef':
case 'alefsym':
case 'aleph':
case 'alephsym':
return new VanillaSymbol('\\aleph ','ℵ');
case 'xist': //LOL
case 'xists':
case 'exist':
case 'exists':
return new VanillaSymbol('\\exists ','∃');
case 'and':
case 'land':
case 'wedge':
return new VanillaSymbol('\\wedge ','∧');
case 'or':
case 'lor':
case 'vee':
return new VanillaSymbol('\\vee ','∨');
case 'o':
case 'O':
case 'empty':
case 'emptyset':
case 'oslash':
case 'Oslash':
case 'nothing':
case 'varnothing':
return new BinaryOperator('\\varnothing ','∅');
case 'cup':
case 'union':