www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

AMScd.js (5346B)


      1 /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 
      4 /*************************************************************
      5  *
      6  *  MathJax/extensions/TeX/AMScd.js
      7  *  
      8  *  Implements the CD environment for commutative diagrams.
      9  *  
     10  *  ---------------------------------------------------------------------
     11  *  
     12  *  Copyright (c) 2013-2015 The MathJax Consortium
     13  * 
     14  *  Licensed under the Apache License, Version 2.0 (the "License");
     15  *  you may not use this file except in compliance with the License.
     16  *  You may obtain a copy of the License at
     17  * 
     18  *      http://www.apache.org/licenses/LICENSE-2.0
     19  * 
     20  *  Unless required by applicable law or agreed to in writing, software
     21  *  distributed under the License is distributed on an "AS IS" BASIS,
     22  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     23  *  See the License for the specific language governing permissions and
     24  *  limitations under the License.
     25  */
     26 
     27 MathJax.Extension["TeX/AMScd"] = {
     28   version: "2.6.0",
     29   config: MathJax.Hub.CombineConfig("TeX.CD",{
     30     colspace: "5pt",
     31     rowspace: "5pt",
     32     harrowsize: "2.75em",
     33     varrowsize: "1.75em",
     34     hideHorizontalLabels: false
     35   })
     36 };
     37 
     38 MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
     39   var MML = MathJax.ElementJax.mml,
     40       TEX = MathJax.InputJax.TeX,
     41       STACKITEM = TEX.Stack.Item,
     42       TEXDEF = TEX.Definitions,
     43       CONFIG = MathJax.Extension["TeX/AMScd"].config;
     44 
     45   TEXDEF.environment.CD = "CD_env";
     46   TEXDEF.special["@"] = "CD_arrow";
     47   TEXDEF.macros.minCDarrowwidth = "CD_minwidth";
     48   TEXDEF.macros.minCDarrowheight = "CD_minheight";
     49 
     50   TEX.Parse.Augment({
     51     //
     52     //  Implements \begin{CD}...\end{CD}
     53     //
     54     CD_env: function (begin) {
     55       this.Push(begin);
     56       return STACKITEM.array().With({
     57         arraydef: {
     58           columnalign: "center",
     59           columnspacing: CONFIG.colspace,
     60           rowspacing: CONFIG.rowspace,
     61           displaystyle: true
     62         },
     63         minw: this.stack.env.CD_minw || CONFIG.harrowsize,
     64         minh: this.stack.env.CD_minh || CONFIG.varrowsize
     65       });
     66     },
     67 
     68     CD_arrow: function (name) {
     69       var c = this.string.charAt(this.i);
     70       if (!c.match(/[><VA.|=]/)) {return this.Other(name)} else {this.i++}
     71 
     72       var top = this.stack.Top();
     73       if (!top.isa(STACKITEM.array) || top.data.length) {
     74         this.CD_cell(name);
     75         top = this.stack.Top();
     76       }
     77       //
     78       //  Add enough cells to place the arrow correctly
     79       //
     80       var arrowRow = ((top.table.length % 2) === 1);
     81       var n = (top.row.length + (arrowRow ? 0 : 1)) % 2;
     82       while (n) {this.CD_cell(name); n--}
     83 
     84       var mml;
     85       var hdef = {minsize: top.minw, stretchy:true},
     86           vdef = {minsize: top.minh, stretchy:true, symmetric:true, lspace:0, rspace:0};
     87 
     88       if (c === ".") {}
     89       else if (c === "|") {mml = this.mmlToken(MML.mo("\u2225").With(vdef))}
     90       else if (c === "=") {mml = this.mmlToken(MML.mo("=").With(hdef))}
     91       else {
     92         //
     93         //  for @>>> @<<< @VVV and @AAA, get the arrow and labels
     94         //
     95         var arrow = {">":"\u2192", "<":"\u2190", V:"\u2193", A:"\u2191"}[c];
     96         var a = this.GetUpTo(name+c,c),
     97             b = this.GetUpTo(name+c,c);
     98 
     99         if (c === ">" || c === "<") {
    100           //
    101           //  Lay out horizontal arrows with munderover if it has labels
    102           //
    103           mml = MML.mo(arrow).With(hdef);
    104           if (!a) {a = "\\kern "+top.minw} // minsize needs work
    105           if (a || b) {
    106             var pad = {width:"+11mu", lspace:"6mu"};
    107             mml = MML.munderover(this.mmlToken(mml));
    108             if (a) {
    109               a = TEX.Parse(a,this.stack.env).mml();
    110               mml.SetData(mml.over,MML.mpadded(a).With(pad).With({voffset:".1em"}));
    111             }
    112             if (b) {
    113               b = TEX.Parse(b,this.stack.env).mml();
    114               mml.SetData(mml.under,MML.mpadded(b).With(pad));
    115             }
    116             if (CONFIG.hideHorizontalLabels)
    117               {mml = MML.mpadded(mml).With({depth:0, height:".67em"})}
    118           }
    119         } else {
    120           //
    121           //  Lay out vertical arrows with mrow if there are labels
    122           //
    123           mml = arrow = this.mmlToken(MML.mo(arrow).With(vdef));
    124           if (a || b) {
    125             mml = MML.mrow();
    126             if (a) {mml.Append(TEX.Parse("\\scriptstyle\\llap{"+a+"}",this.stack.env).mml())}
    127             mml.Append(arrow.With({texClass: MML.TEXCLASS.ORD}));
    128             if (b) {mml.Append(TEX.Parse("\\scriptstyle\\rlap{"+b+"}",this.stack.env).mml())}
    129           }
    130         }
    131       }
    132       if (mml) {this.Push(mml)};
    133       this.CD_cell(name);
    134     },
    135     CD_cell: function (name) {
    136       var top = this.stack.Top();
    137       if ((top.table||[]).length % 2 === 0 && (top.row||[]).length === 0) {
    138         //
    139         // Add a strut to the first cell in even rows to get
    140         // better spacing of arrow rows.
    141         // 
    142         this.Push(MML.mpadded().With({height:"8.5pt",depth:"2pt"}));
    143       }
    144       this.Push(STACKITEM.cell().With({isEntry:true, name:name}));
    145     },
    146 
    147     CD_minwidth: function (name) {
    148       this.stack.env.CD_minw = this.GetDimen(name);
    149     },
    150     CD_minheight: function (name) {
    151       this.stack.env.CD_minh = this.GetDimen(name);
    152     }
    153 
    154   });
    155 
    156 });
    157 
    158 MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/AMScd.js");