Annotation of loncom/html/adm/jsMath/plugins/mimeTeX.js, revision 1.1

1.1     ! albertel    1: /*
        !             2:  *  Treat ~ as space
        !             3:  */
        !             4: jsMath.Parser.prototype.nextIsSpace = function () {
        !             5:   return this.string.charAt(this.i) == ' ' ||
        !             6:          this.string.charAt(this.i) == '~';
        !             7: }
        !             8: jsMath.Parser.prototype.special['~'] = 'Space';
        !             9: 
        !            10: /*
        !            11:  *  Implement \[ ... \], \( ... \), etc.
        !            12:  */
        !            13: jsMath.Macro('[','\\left[');  jsMath.Macro(']','\\right]');
        !            14: jsMath.Macro('(','\\left(');  jsMath.Macro(')','\\right)');
        !            15: jsMath.Macro('<','\\left<');  jsMath.Macro('>','\\right>');
        !            16: // can't do \. in a reasonable way
        !            17: jsMath.Parser.prototype.macros['|'] = ['HandleLR','|','|'];
        !            18: jsMath.Parser.prototype.macros['='] = ['HandleLR','\\|','\\|'];
        !            19: 
        !            20: /*
        !            21:  *  Make non-standard \left{ and \right} work
        !            22:  */
        !            23: jsMath.Parser.prototype.delimiter['}'] = [5,2,0x67,3,0x09];
        !            24: jsMath.Parser.prototype.delimiter['{'] = [4,2,0x66,3,0x08];
        !            25: 
        !            26: 
        !            27: /*
        !            28:  *  Immitate mimeTeX \big... and \Big... ops
        !            29:  */
        !            30: 
        !            31: // make the normal ones in text mode
        !            32: jsMath.Macro('int','\\intop\\nolimits');
        !            33: jsMath.Macro('oint','\\ointop\\nolimits');
        !            34: jsMath.Macro('sum','\\sumop\\nolimits');
        !            35: jsMath.Macro('prod','\\prodop\\nolimits');
        !            36: jsMath.Macro('coprod','\\coprodop\\nolimits');
        !            37: 
        !            38: jsMath.Macro('bigint','\\bigintop\\nolimits'); 
        !            39: jsMath.Macro('bigoint','\\bigointop\\nolimits');
        !            40: jsMath.Macro('bigsum','\\bigsumop\\nolimits');
        !            41: jsMath.Macro('bigprod','\\bigprodop\\nolimits');
        !            42: jsMath.Macro('bigcoprod','\\bigcoprodop\\nolimits');
        !            43: 
        !            44: jsMath.Macro('Bigint','\\bigintop\\limits');
        !            45: jsMath.Macro('Bigoint','\\bigointop\\limits');
        !            46: jsMath.Macro('Bigsum','\\bigsumop\\limits');
        !            47: jsMath.Macro('Bigprod','\\bigprodop\\limits');
        !            48: jsMath.Macro('Bigcoprod','\\bigcoprod\\limits');
        !            49: 
        !            50: /*
        !            51:  *  The characters needed for the macros above
        !            52:  */
        !            53: jsMath.Parser.prototype.mathchardef['coprodop'] = [1,3,0x60];
        !            54: jsMath.Parser.prototype.mathchardef['prodop']   = [1,3,0x51];
        !            55: jsMath.Parser.prototype.mathchardef['sumop']    = [1,3,0x50];
        !            56: 
        !            57: jsMath.Parser.prototype.mathchardef['bigintop']    = [1,3,0x5A];
        !            58: jsMath.Parser.prototype.mathchardef['bigointop']   = [1,3,0x49];
        !            59: jsMath.Parser.prototype.mathchardef['bigcoprodop'] = [1,3,0x61];
        !            60: jsMath.Parser.prototype.mathchardef['bigprodop']   = [1,3,0x59];
        !            61: jsMath.Parser.prototype.mathchardef['bigsumop']    = [1,3,0x58];
        !            62: 
        !            63: /*
        !            64:  * Unlink the small versions so they don't enlarge in display mode
        !            65:  */
        !            66: jsMath.TeX['cmex10'][0x48].n = null;
        !            67: jsMath.TeX['cmex10'][0x50].n = null;
        !            68: jsMath.TeX['cmex10'][0x51].n = null;
        !            69: jsMath.TeX['cmex10'][0x52].n = null;
        !            70: jsMath.TeX['cmex10'][0x60].n = null;
        !            71: 
        !            72: 
        !            73: /*
        !            74:  *  Some other missing items
        !            75:  */
        !            76: jsMath.Macro('/','{}'); // insert an empty box \/
        !            77: jsMath.Macro('raisebox','\\raise #1px ',1); // convert to \raise
        !            78: jsMath.Macro('hfill','\\quad ',1); // punt
        !            79: jsMath.Macro('fbox','\\oldfbox{$#1$}',1); // do fbox in math mode
        !            80: 
        !            81: /*
        !            82:  *  These get new JavaScript routines
        !            83:  */
        !            84: jsMath.Parser.prototype.macros['unitlength'] = 'unitlength';
        !            85: jsMath.Parser.prototype.macros['hspace']     = 'hspace';
        !            86: jsMath.Parser.prototype.macros['fs']         = 'fs';
        !            87: jsMath.Parser.prototype.macros['oldfbox']    = 'FBox';
        !            88: 
        !            89: /*
        !            90:  *  Add some JavaScript functions to the parser
        !            91:  */
        !            92: jsMath.Package(jsMath.Parser,{
        !            93:   
        !            94:   /*
        !            95:    *  Implement \left x ... \right x
        !            96:    */
        !            97:   HandleLR: function (name,data) {
        !            98:     var arg = this.GetUpto(name,name); if (this.error) return;
        !            99:     this.string = '\\left'+data[0]+arg+'\\right'+data[1];
        !           100:     this.i = 0;
        !           101:   },
        !           102: 
        !           103:   /*
        !           104:    *  Hold the unit length in mlist.data
        !           105:    */
        !           106:   unitlength: function (name) {
        !           107:     var n = this.GetArgument(this.cmd+name); if (this.error) return;
        !           108:     if (!n.match(/^-?(\d+(\.\d*)?|\.\d+)$/)) {
        !           109:       this.Error("Argument for "+this.cmd+name+" must be a number");
        !           110:       return;
        !           111:     }
        !           112:     this.mlist.data['unitlength'] = n;
        !           113:   },
        !           114: 
        !           115:   /*
        !           116:    *  Get the length (converted to ems) and multiply by the unit length
        !           117:    */
        !           118:   hspace: function (name) {
        !           119:     var w = this.GetArgument(this.cmd+name); if (this.error) return;
        !           120:     if (!w.match(/^-?(\d+(\.\d*)?|\.\d+)$/)) {
        !           121:       this.Error("Argument for "+this.cmd+name+" must be a number");
        !           122:       return;
        !           123:     }
        !           124:     w /= jsMath.em
        !           125:     if (this.mlist.data['unitlength']) {w *= this.mlist.data['unitlength']}
        !           126:     this.mlist.Add(jsMath.mItem.Space(w));
        !           127:   },
        !           128:   
        !           129:   /*
        !           130:    *  Implement \fs{...} for font-size changing
        !           131:    */
        !           132:   fs: function (name) {
        !           133:     var n = this.GetArgument(this.cmd+name); if (this.error) return;
        !           134:     if (!n.match(/^[-+]?\d+$/)) {
        !           135:       this.Error("Argument for "+this.cmd+name+" must be an integer");
        !           136:       return;
        !           137:     }
        !           138:     if (n.match(/[-+]/)) {n = n - 0; n += this.mlist.data.size}
        !           139:     this.mlist.data.size = n = Math.max(0,Math.min(9,n));
        !           140:     this.mlist.Add(new jsMath.mItem('size',{size: n}));
        !           141:   },
        !           142: 
        !           143:   /*
        !           144:    *  Repalce the Array function by one that accepts an optional
        !           145:    *  parameter for the column types, and that handle's mimeTeX's
        !           146:    *  "preamble" format.
        !           147:    */
        !           148:   Array: function (name,delim) {
        !           149:     var columns = delim[2]; var cspacing = delim[3];
        !           150:     if (!columns && this.GetNext() == '{') {
        !           151:       columns = this.GetArgument(this.cmd+'begin{'+name+'}');
        !           152:       if (this.error) return;
        !           153:     } else {
        !           154:       columns = '';
        !           155:     }
        !           156:     columns = columns.replace(/[^clr]/g,'');
        !           157:     columns = columns.split('');
        !           158:     var data = this.mlist.data;
        !           159:     var arg = this.GetEnd(name); if (this.error) return;
        !           160:     if (arg.match(/\$/)) {arg = arg.replace(/^([^$]*)\$/,''); columns = RegExp.$1}
        !           161:     var parse = new jsMath.Parser(arg+'\\\\',null,data.size);
        !           162:     parse.matrix = name; parse.row = []; parse.table = [];
        !           163:     parse.Parse(); if (parse.error) {this.Error(parse); return}
        !           164:     parse.HandleRow(name,1);  // be sure the last row is recorded
        !           165:     var box = jsMath.Box.Layout(data.size,parse.table,columns,cspacing);
        !           166:     // Add parentheses, if needed
        !           167:     if (delim[0] && delim[1]) {
        !           168:       var left  = jsMath.Box.Delimiter(box.h+box.d,this.delimiter[delim[0]],'T');
        !           169:       var right = jsMath.Box.Delimiter(box.h+box.d,this.delimiter[delim[1]],'T');
        !           170:       box = jsMath.Box.SetList([left,box,right],data.style,data.size);
        !           171:     }
        !           172:     this.mlist.Add(jsMath.mItem.Atom((delim[0]? 'inner': 'ord'),box));
        !           173:   },
        !           174: 
        !           175:   /*
        !           176:    *  Similarly for Matrix (used by \matrix and \array)
        !           177:    */
        !           178:   Matrix: function (name,delim) {
        !           179:     var data = this.mlist.data;
        !           180:     var arg = this.GetArgument(this.cmd+name); if (this.error) return;
        !           181:     if (arg.match(/\$/)) {arg = arg.replace(/^([^$]*)\$/,''); delim[2] = RegExp.$1}
        !           182:     var parse = new jsMath.Parser(arg+'\\\\',null,data.size);
        !           183:     parse.matrix = name; parse.row = []; parse.table = [];
        !           184:     parse.Parse(); if (parse.error) {this.Error(parse); return}
        !           185:     parse.HandleRow(name,1);  // be sure the last row is recorded
        !           186:     var box = jsMath.Box.Layout(data.size,parse.table,delim[2]);
        !           187:     // Add parentheses, if needed
        !           188:     if (delim[0] && delim[1]) {
        !           189:       var left  = jsMath.Box.Delimiter(box.h+box.d,this.delimiter[delim[0]],'T');
        !           190:       var right = jsMath.Box.Delimiter(box.h+box.d,this.delimiter[delim[1]],'T');
        !           191:       box = jsMath.Box.SetList([left,box,right],data.style,data.size);
        !           192:     }
        !           193:     this.mlist.Add(jsMath.mItem.Atom((delim[0]? 'inner': 'ord'),box));
        !           194:   }
        !           195: });

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>