Re: [mathjax-users] xalignat, xxalignat, subequations and flalign environments

306 views
Skip to first unread message

Davide Cervone

unread,
Oct 29, 2018, 2:20:58 PM10/29/18
to mathja...@googlegroups.com
I finally got the chance to look into these environments, and have made up some extensions to allow them.  For xalignat, xxalignat, flalign, and the starred versions, you can use the following:

<script type="text/x-mathjax-config">
MathJax.Extension['TeX/xalign'] = {
  version : '1.0.0'
};

MathJax.Hub.Register.StartupHook('TeX Jax Ready', function () {
  var TEX = MathJax.InputJax.TeX;
  TEX.Definitions.Add({
    environment: {
      xalignat:       ['ExtensionEnv', null, 'AMSmath'],
      'xalignat*':    ['ExtensionEnv', null, 'AMSmath'],
      xxalignat:      ['ExtensionEnv', null, 'AMSmath'],
      flalign:        ['ExtensionEnv', null, 'AMSmath'],
      'flalign*':     ['ExtensionEnv', null, 'AMSmath']
    }
  });
});

MathJax.Hub.Register.StartupHook('TeX AMSmath Ready', function () {
  var MML = MathJax.ElementJax.mml,
      TEX = MathJax.InputJax.TeX,
      TEXDEF = TEX.Definitions,
      STACKITEM = TEX.Stack.Item;

  TEXDEF.Add({
    environment: {
      xalignat:       ['xAlignAt', null, true, true],
      'xalignat*':    ['xAlignAt', null, false, true],
      xxalignat:      ['xAlignAt', null, true, false],
      flalign:        ['xAlignArray', null, true, false, true, 'rlcrlcrlcrlcrlcrlc',
                         ['',' ',' ',' ',' ',' ',''].join('auto auto fit')],
      'flalign*':     ['xAlignArray', null, false, false, true, 'rlcrlcrlcrlcrlcrlc',
                         ['',' ',' ',' ',' ',' ',''].join('auto auto fit')]
    }
  });

  TEX.Parse.Augment({
    xAlignAt: function (begin, numbered, padded) {
      var n, align = [], width = [];
      n = this.GetArgument("\\begin{"+begin.name+"}");
      if (n.match(/[^0-9]/)) {
        TEX.Error(["PositiveIntegerArg","Argument to %1 must me a positive integer",
                  "\\begin{"+begin.name+"}"]);
      }
      if (padded) {align.push(""); width.push("")}
      while (n > 0) {
        align.push("rl");
        width.push("auto auto");
        n--;
      }
      if (padded) {align.push(""); width.push("")}
      return this.xAlignArray(begin, numbered, padded, false, 
                              align.join("c"), width.join(" fit "));
    },

    xAlignArray: function (begin, numbered, padded, center, align, width) {
      this.Push(begin);
      this.checkEqnEnv();
      align = align.split('').join(' ')
                   .replace(/r/g, 'right')
                   .replace(/l/g, 'left')
                   .replace(/c/g, 'center');
      return STACKITEM.xAlignArray(begin.name, numbered, padded, center, this.stack).With({
        arraydef: {
          width: "100%",
          displaystyle: true,
          columnalign: align,
          columnspacing: "0em",
          columnwidth: width,
          rowspacing: "3pt",
          side: TEX.config.TagSide,
          minlabelspacing: TEX.config.TagIndent
        }
      });
    }
  });

  STACKITEM.xAlignArray = STACKITEM.AMSarray.Subclass({
    type: "xAlignArray",
    Init: function (name, numbered, padded, center, stack) {
      this.SUPER(arguments).Init(name, numbered, padded || center, stack);
      this.padded = padded;
      this.center = center;
      this.maxrow = 0;
    },
    EndRow: function () {
      var cell, row = this.row;
      this.row = [];
      if (this.padded) this.row.push(MML.mtd());
      while ((cell = row.shift())) {
        this.row.push(cell);
        cell = row.shift();
        if (cell) this.row.push(cell);
        if (row.length || this.padded) this.row.push(MML.mtd());
      }
      if (this.row.length > this.maxrow) this.maxrow = this.row.length;
      this.SUPER(arguments).EndRow.call(this);
    },
    EndTable: function () {
      this.SUPER(arguments).EndTable.call(this);
      if (this.center) {
        var def = this.arraydef;
        if (this.maxrow <= 2) delete def.width;
        def.columnalign = def.columnalign.split(/ /).slice(0,this.maxrow).join(' ');
        def.columnwidth = def.columnwidth.split(/ /).slice(0,this.maxrow).join(' ');
      }
    }
  });

  MathJax.Hub.Startup.signal.Post("TeX xalign Ready");
});
</script>

by adding this just BEFORE the script that loads MathJax.js itself.  It is also possible to make a separate file that contains the contents of the script (not the script tags themselves) and load it as a secondary configuration file.  If you do, you need to add the line

MathJax.Ajax.loadComplete('full-url-to file/file.js');

where "full-url-to-file/file.js" is the full URL for the extension file.  Then you load it by adding ",full-url-to-file/file.js" after the "config=config-file" in the script that loads MathJax.  E.g.


The only caveat is that there is a bug in the CommonHTML table output that interfere with the results.  But the HTML-CSS and SVG output should work properly.


So that takes care of everything but subequations.  You can get a limited version of that using the following:

<script type="text/x-mathjax-config">
MathJax.Extension['TeX/subequations'] = {
  version: "1.0.0",
  subEq: false,
  subNo: 0
};

MathJax.Hub.Register.StartupHook('TeX Jax Ready', function () {
  var TEX = MathJax.InputJax.TeX,
      TEXDEF = TEX.Definitions;
  var SUBEQ = MathJax.Extension['TeX/subequations'];
  var CONFIG = MathJax.Hub.config.TeX.equationNumbers;

  SUBEQ.oldFormat = CONFIG.formatNumber;
  CONFIG.formatNumber = function (n) {
    n = SUBEQ.oldFormat(n);
    if (!SUBEQ.subEq) return n;
    MathJax.Extension['TeX/AMSmath'].number--;
    var m = ++SUBEQ.subNo;
    return n + (n.match(/\./) ? String.fromCharCode(0x60 + m) : '.' + m);
  };

  TEXDEF.Add({
    environment: {
      subequations: ['SubEquationsBegin', 'SubEquationsEnd']
    }
  });

  TEX.Parse.Augment({
    SubEquationsBegin: function (begin) {
      SUBEQ.subEq = true;
      SUBEQ.subNo = 0;
      return begin;
    },
    SubEquationsEnd: function (begin, row) {
      SUBEQ.subEq = false;
      MathJax.Extension['TeX/AMSmath'].number++;
      return row;
    }
  });

  MathJax.Hub.Startup.signal.Post("TeX subequations Ready");
});
</script>

Again, you include this BEFORE the script that loads MathJax.js itself, or you can make a separate configuration file for it as described above.

For this, you must use \begin{subequations}...\end{subequations} directly around the complete math expression that you want to have sub-numbers.  So

\begin{subequations}
\begin{align}
x &= 123\\
y &= x + 5
\end{align}
\end{subequations}

You have to put each expression into its own subequations environment; you can't put subequations around multiple alignments.  Also, there is no mechanism for labeling the main equation number.  Labels can only be made for the individual sub-equation's numbers.

Perhaps this is sufficient for your needs.  It at least shows how such an environment might be implemented.

Davide


On Oct 9, 2018, at 6:21 AM, m.mos...@gmx.de wrote:

Unfortunately MathJax does not support environments like xalignat, xxalignat, subequations and flalign environments. I am not looking for the accurate rendering of the formula. Thus I thought I could replace the name of the environment, e. g. replacing xalignat with alignat.

\begin{xalignat}{3}
i_
{11} & =0.25 & i_{12} & =i_{21} & i_{13} & =i_{23}\nonumber\\
i_
{21} & =\frac{1}{3}i_{11} & i_{22} & =0.5i_{12}& i_{23} & =i_{31}\\
i_
{31} & =0.33i_{22}\quad & i_{32} & =0.15i_{32}\quad & i_{33} & =i_{11}
\end{xalignat}

\begin{alignat}{3}
i_
{11} & =0.25 & i_{12} & =i_{21} & i_{13} & =i_{23}\nonumber\\
i_
{21} & =\frac{1}{3}i_{11} & i_{22} & =0.5i_{12}& i_{23} & =i_{31}\\
i_
{31} & =0.33i_{22}\quad & i_{32} & =0.15i_{32}\quad & i_{33} & =i_{11}
\end{alignat}

The result is satisfying. I replaced

.xalignat with alignat

xalignat* with alignat*

xxalignat with alignat*

flalign wih align

flalign* wih align*

I couldn't find any replacement for subequations.

My question is would the replacement work for every possible content? E. g. is there a structure for flalign environment that would cause an error in align environment?


--
You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

abdulrahma...@gmail.com

unread,
May 14, 2020, 10:30:32 AM5/14/20
to MathJax Users
Does this work in Anki? If it does, how to use it?
To unsubscribe from this group and stop receiving emails from it, send an email to mathja...@googlegroups.com.

Davide Cervone

unread,
May 18, 2020, 9:38:45 AM5/18/20
to mathja...@googlegroups.com
Does this work in Anki? If it does, how to use it?

It looks like Anki has built-in support for MathJax, so you should probably be able to get this to work.  You may be able to just include the <script type="tex/x-mathjax-config"> into the card(s) that need it, or you may be able to insert the contents of the scripts into the configuration in the AnkiDroid/src/main/assets/mathjax/conf.js file


in the AuthorInit() function.

I haven't used Anki, but that is my suggested approach.

Davide


To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mathjax-users/8c641642-1daa-4669-9a34-b333f6b6fc41%40googlegroups.com.

abcd arl

unread,
May 18, 2020, 4:05:50 PM5/18/20
to mathja...@googlegroups.com
Thank you so much for taking your time answering my questions, I really appreciate it. You are right about Anki has built-in support of Mathjax, but I think Mathjax does not support flalign. I really wanted to use this feature on my Anki Desktop. Do you have any idea how to make this happen? I have tried copy-pasting your code on my cards and it still doesn't work. Does the link you gave me from github work for flalign in AnkiDesktop?

You received this message because you are subscribed to a topic in the Google Groups "MathJax Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mathjax-users/8YxzxF-m_hM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mathjax-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mathjax-users/F55137B2-55AD-43FE-BD30-2B6B81DC1B1E%40mathjax.org.

Davide Cervone

unread,
May 19, 2020, 10:23:25 AM5/19/20
to mathja...@googlegroups.com
Thank you so much for taking your time answering my questions, I really appreciate it. You are right about Anki has built-in support of Mathjax, but I think Mathjax does not support flalign. I really wanted to use this feature on my Anki Desktop. Do you have any idea how to make this happen? I have tried copy-pasting your code on my cards and it still doesn't work. Does the link you gave me from github work for flalign in AnkiDesktop?

If inserting into the cards directly didn't work, my suggestion was to modify the AnkiDroid/src/main/assets/mathjax/conf.js file to include the contents of the <script> that defines the xalignat and falling macros into the AuthorInit() function in that file.  That file should be part of the Anki assets somewhere (I don't know exactly where), so if you can find it and modify it, you should be able to add the needed definitions.  You might have to build a custom version of the Anki app, but I haven't looked into that.  That is outside of the scope of the help that I can give you.

Davide


abcd arl

unread,
May 19, 2020, 10:28:18 AM5/19/20
to mathja...@googlegroups.com
I see. I will follow your instruction and see if it works. Any way, I am glad for your help. Thank you so much and God bless.

Reply all
Reply to author
Forward
0 new messages