Skip to content

MathML 4 extensions for alignment and possible deprecation of <maligngroup/> and <malignmark/> #181

Closed

Description

@NSoiffer

Having just implemented a polyfill for elementary math, that got me thinking about some related ideas:

  1. The most obvious concept related to long division is synthetic division. It is basically the same idea as long division except that you are dividing polynomials. With synthetic division, the columns contain numbers (the coefficients of the polynomial), not just digits. As a refresher, see this page and the example taken from it below):
Polynomial Division Synthetic Division
image image
  1. Synthetic division is a shorthand for long division of polynomials (left example above). Long division of polynomials is basically the same idea as long division of numbers except that instead of digits, you have monomials that need to go into their own column. Doing that automatically requires knowing the variable you want to "sort" on so that each monomial goes into the proper column.
  2. A very similar property is needed when displaying systems of equations -- each monomial wants to be in it's own column (in this case, the top level element would not be mlongdiv, but mstack).

There are a few complications such as decimal alignment of the coefficients:

      8.44x + 55  y =  0
      3.1 x -  0.7y = -1.1

Note that alignment requires knowing what characters/operators act as column separators (e.g., +and -, along with = and a few other relational operators). These would be inside of mo elements, so potentially any mo element could be a separator, or maybe an attribute specifies what the separators are (something to think about/discuss).

The above example is taken from the MathML 3 spec formaligngroup and malignmark. I think only MathPlayer ever implemented those elements and I suspect that you can count on your fingers the number of times they have been used. It is a very complicated feature to implement and to use. In contrast, I think the above features are an incremental extension to elementary math layout, so implementation (especially via an extension to the polyfill I wrote), means that supporting these features would be universal (assuming I or someone else extended the polyfill). Just as important, using this extension would be simple as it is a declarative notation that doesn't require modifying the generated layout other than at a high level (wrapping with mstack or mlongdiv). It would be less powerful though.

I suspect that this proposed extension to elementary math handles the large majority of cases where people play games with tables to achieve alignment, both in MathML and in TeX. @davidcarlisle: do you have any estimate of how many uses of table for alignment in TeX would be covered by this proposed extension? What are some of the cases that are missed by it?

Activity

  1. added
    compatibility
    MathML 4
    need polyfill
    need resolution
    need specification update
    on Jan 2, 2020
  2. removed
    need resolution
    on Aug 12, 2020
  3. dginev commented on Oct 9, 2020

    @dginev
    Member

    Hello. I was looking for an appropriate issue to attach a recent piece of news I spotted, and since this issue discusses malignmark, it seems appropriate. There is a recent post about bypassing the sanitization of DOMPurify through an abuse of parsing MathML in HTML, details here:
    https://portswigger.net/daily-swig/dompurify-mutation-xss-bypass-achieved-through-mathml-namespace-confusion

    Summarized as:

    In the MathML namespace, two special elements – mglyph and malignmark – allow the creation of a markup that is “in HTML namespace, but on reparsing it is in MathML namespace, [meaning that] the subsequent style tag [is] parsed differently and leading to XSS,” the researcher explained.

    This might be relevant if you're searching for additional reasons for deprecation.

  4. NSoiffer commented on Oct 15, 2020

    @NSoiffer
    ContributorAuthor
  5. davidcarlisle commented on Oct 15, 2020

    @davidcarlisle
    Collaborator
  6. NSoiffer commented on Oct 17, 2020

    @NSoiffer
    ContributorAuthor
  7. davidcarlisle commented on Aug 11, 2022

    @davidcarlisle
    Collaborator

    the schema has been updated to restrict use of malignmark, and to remove grouplaign attribute except in legacy schema

    w3c/mathml-schema@4e897dc

  8. NSoiffer commented on Aug 15, 2024

    @NSoiffer
    ContributorAuthor

    The group tentatively agreed that these are no longer needed: they aren't implemented in browsers and are rarely generated. The main use is to get alignment right at the character level (e.g., decimal alignment). @davidcarlisle pointed out that there is a Unicode space character (U+2007) that is meant to be the width of a digit and that this can be used as padding to get decimal alignment to work.

    I pointed out that with intent table properties, the accessibility problem of splitting equations across columns goes away.

    This section (which is somewhat simplified from MathML 3) still is quite large, so removing this would be a good simplification to the spec and help align it with it.

    @MurrayIII currently uses malignmark in his UnicodeMath implementation. He will investigate whether this is really needed. If not, then we can remove these from the spec.

  9. MurrayIII commented on Nov 14, 2024

    @MurrayIII

    For the equation

    image

    Word copies the following MathML

    <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"><mml:mtable><mml:mtr><mml:mtd><mml:mrow><mml:maligngroup/><mml:mn>10</mml:mn><mml:malignmark/><mml:mi>x</mml:mi><mml:mi> </mml:mi><mml:mo>+</mml:mo><mml:maligngroup/><mml:mn>3</mml:mn><mml:malignmark/><mml:mi>y</mml:mi><mml:mo>=</mml:mo><mml:mn>2</mml:mn></mml:mrow></mml:mtd></mml:mtr><mml:mtr><mml:mtd><mml:mrow><mml:maligngroup/><mml:mn>3</mml:mn><mml:malignmark/><mml:mi>x</mml:mi><mml:mi> </mml:mi><mml:mo>+</mml:mo><mml:maligngroup/><mml:mn>13</mml:mn><mml:malignmark/><mml:mi>y</mml:mi><mml:mo>=</mml:mo><mml:mn>4</mml:mn></mml:mrow></mml:mtd></mml:mtr></mml:mtable></mml:math>
    
  10. NSoiffer commented on Nov 14, 2024

    @NSoiffer
    ContributorAuthor

    Reformatted the above MathML to be more readable:

    <math><mtable>
      <mtr><mtd><mrow>
            <maligngroup/><mn>10</mn><malignmark/><mi>x</mi><mi/><mo>+</mo>
            <maligngroup/><mn>3</mn><malignmark/><mi>y</mi><mo>=</mo><mn>2</mn>
       </mrow></mtd></mtr>
       <mtr><mtd><mrow>
            <maligngroup/><mn>3</mn><malignmark/><mi>x</mi><mi/><mo>+</mo>
            <maligngroup/><mn>13</mn><malignmark/><mi>y</mi><mo>=</mo><mn>4</mn>
       </mrow></mtd></mtr>
    </mtable></math>
    

    This makes it easier to see the maligngroup and malignmark elements.

  11. davidcarlisle commented on Nov 14, 2024

    @davidcarlisle
    Collaborator

    If I change that example to 103 rather than 13, so the expressions have different lengths

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8"/>
        <title>malign</title>
      </head>
      <body>
       <math><mtable>
      <mtr><mtd><mrow>
            <maligngroup/><mn>10</mn><malignmark/><mi>x</mi><mi/><mo>+</mo>
            <maligngroup/><mn>3</mn><malignmark/><mi>y</mi><mo>=</mo><mn>2</mn>
       </mrow></mtd></mtr>
       <mtr><mtd><mrow>
            <maligngroup/><mn>3</mn><malignmark/><mi>x</mi><mi/><mo>+</mo>
            <maligngroup/><mn>103</mn><malignmark/><mi>y</mi><mo>=</mo><mn>4</mn>
       </mrow></mtd></mtr>
    </mtable></math>
      </body>
    </html>
    

    that renders (in Edge here) as

    image

    So there is no alignment at all visually (similarly in firefox)

    If I add

    <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_SVG"></script>
    

    to get mathjax rendering it still doesn't align:

    image

  12. 28 remaining items

  13. davidcarlisle commented on Feb 19, 2025

    @davidcarlisle
    Collaborator

    Use of float:right makes the table cell display:block and while, between us, we have managed to hide most the bad effects of that, it's still problematic.

    It turns out that while text-align:right doesn't work in chrome, text-align:-webkit-right does, and as always in css you can use both and the webkit version will be ignored by systems that should ignore it so I get

    Firefox
    Image

    Chrome

    Image

    with the correct column alignment, and the equation numbers at the page page margin in chrome (not yet in firefox)

    Using this simpler css

    
    <!DOCTYPE html>
    <html lang="en"/>
    <head>
    <title>align test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    
    <style>
    mtable.rightleft > mtr > mtd:nth-child(even) {
    padding-right:0;text-align:right;text-align:-moz-right;text-align:-webkit-right;
    }
    
    mtable.rightleft > mtr > mtd:nth-child(odd) {
    padding-left:0;text-align:left;text-align:-moz-left;text-align:-webkit-left;
    }
    
    mtd[intent=":noequationlabel"], mtd[intent=":equationlabel"] {
       margin-right: 1em;display:block; position:absolute; right:0em
    }
    </style>
    </head>
    <body lang="en">
    
    <p>align</p>
    
    <math display="block">
    
    <mtable class="rightleft" columnspacing="0" displaystyle="true">
     <mtr>
     <mtd intent=":equationlabel"><mtext>(1)</mtext></mtd>
    <mtd><mi>𝑎</mi><mo>+</mo><mi>𝑏</mi> </mtd>
    <mtd><mo>=</mo>
    <msup><mi>𝑥</mi><mn>2</mn></msup>
    <mo>+</mo><mi>𝑥</mi><mo>+</mo><mn>2</mn>
    </mtd>
     </mtr>
    <mtr>
     <mtd intent=":equationlabel"><mtext>(2)</mtext></mtd>
     <mtd><mi>𝑏</mi></mtd>
     <mtd><mo>=</mo><mi>𝑥</mi> </mtd>
    </mtr>
    </mtable>
    </math>
    <p>xxxx</p>
    </body>
    </html>
    
  14. MurrayIII commented on Feb 20, 2025

    @MurrayIII

    Unfortunately, these choices for replacing malignmark and maligngroup don't work well for Safari, but we could have different choices for different browsers. Another problem is that any mtd emulation of malignmark and maligngroup messes up the vertical alignment of an equation more than 1`em in height. High quality mathematical typography needs malignmark and maligngroup since they allow vertical alignment within a single mtd. Ditto for equation number work arounds for not having mlabeledtr when the equation has a height of more than 1em. Still another problem is that MathJax doesn't work with the mlabeledtr work arounds, although it does work with the malignmark and maligngroup emulations.

  15. davidcarlisle commented on Feb 20, 2025

    @davidcarlisle
    Collaborator

    @MurrayIII I agree that the alignment still needs tweaking some more (probably we'll need to offer a version using javascript not just css) which is a shame in a way but it is what it is. javascript would anyway be needed to polyfill from the <malignmark/> markup as you need to generate extra table columns on which the css can act.

    If you are generating <malignmark/> for a current browsers you will need some javascript support to make it work. That could be mathjax or it could be a more specific polyfil we provide here, I don't think it's necessary that it works well if both are used in combination.

    We could decide not to remove mlabeledtr from the spec, but that doesn't really help as it's not implemented in browsers whatever the spec might say about it. So I think practically speaking we don't have any choice but to remove it from mathml4 to match mathml-core, and then work out the best way of getting the alignments without it.

    I think in the end three things will be needed

    • for documents using <maligngroup/>; a javascript polyfil
    • For documents using mtd and column alignment; a css version
    • and (possibly if in the end we need it) a javascript poylfill version

    and basically the same again for equation numbers with or without <mlabeledtr>

  16. davidcarlisle commented on Feb 24, 2025

    @davidcarlisle
    Collaborator

    @MurrayIII

    Unfortunately, these choices for replacing malignmark and maligngroup don't work well for Safari,

    which was a polite way of saying they don't work at all in safari:-)

    I got access to a Mac and saw what you meant....

    Attached is a file that uses the float:right mechanism just for safari and I think looks reasonable.

    The whole file tests various kinds of latex alignment structures with and without equation numbers,
    the mathml including intents for the alignment structures is written by latex during its typesetting
    and the generation to html uses ngpdf's implementation of the derivation to html algorithm for tagged pdf, not any tex-specific convertor.

    I attach the html here but you can run the entire pipeline starting from latex source using the texlive.net server
    at

    https://tinyurl.com/mrc5krb6

    after latex has finished running it should offer a link to "open in the ngpdf editor" which will show a side by side view of the latex typeset pdf and html/mathml rendering.

    html version (as a .txt file due to file restrictions on this site)

    align-html.txt

  17. davidcarlisle commented on Feb 27, 2025

    @davidcarlisle
    Collaborator

    I just used mathpix to generate mathml from an example on screen (actually from @dginev 's codepen in another issue) and selected the output it describes as "Copy Word" but actually it means put MathML rather than LaTeX on the clipboard.

    I get

    <math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
      <mtable displaystyle="true" columnalign="right left right left right left right left right left right left" columnspacing="0em 2em 0em 2em 0em 2em 0em 2em 0em 2em 0em" rowspacing="3pt">
        <mtr>
          <mtd>
            <mrow>
              <mrow>
                <maligngroup/>
                <mfrac>
                  <mrow>
                    <mi>x</mi>
                    <mo>+</mo>
                    <mi>y</mi>
                  </mrow>
                  <mn>2</mn>
                </mfrac>
              </mrow>
              <mrow>
                <maligngroup/>
                <mi></mi>
                <mo>=</mo>
                <mn>2</mn>
              </mrow>
            </mrow>
          </mtd>
        </mtr>
        <mtr>
          <mtd>
            <mrow>
              <mrow>
                <maligngroup/>
                <msqrt>
                  <mfrac>
                    <mrow>
                      <mi>x</mi>
                      <mo>+</mo>
                      <mi>y</mi>
                    </mrow>
                    <mn>2</mn>
                  </mfrac>
                </msqrt>
              </mrow>
              <mrow>
                <maligngroup/>
                <mi></mi>
                <mo>=</mo>
                <msub>
                  <mi>x</mi>
                  <mn>2</mn>
                </msub>
              </mrow>
            </mrow>
          </mtd>
        </mtr>
        <mtr>
          <mtd>
            <mrow>
              <mrow>
                <maligngroup/>
                <mroot>
                  <mfrac>
                    <mrow>
                      <mi>x</mi>
                      <mo>+</mo>
                      <mi>y</mi>
                    </mrow>
                    <mn>3</mn>
                  </mfrac>
                  <mn>3</mn>
                </mroot>
              </mrow>
              <mrow>
                <maligngroup/>
                <mi></mi>
                <mo>=</mo>
                <msub>
                  <mi>x</mi>
                  <mn>3</mn>
                </msub>
              </mrow>
            </mrow>
          </mtd>
        </mtr>
      </mtable>
    </math>
    

    Which doesn't really fit the proposed "cut down" description of only allowing maligngroup at the top level and only in conjunction with malignmark (which isn't used at all here).

    pasting that in to Word gives the expected result

    Image

    This makes me a bit nervous of mandating any specific minimal use of maligngroup

    We could keep things as they are now in the spec with just the big "not in core" warning. Or I think we could make any use valid but not defined which would make this conforming but essentially mathml3 use.
    am not sure we can specify any "simple" version of maligngroup that specifies the above behaviour

    @MurrayIII interestingly if I copy the formula out of Word (without editing it) then, after adjusting the namespace prefixes, and indenting for clarity you get this with alternating <malignmark/> and <maligngroup/> as you have shown previously (but still within one level of <mrow>

    <math xmlns="http://www.w3.org/1998/Math/MathML">
     <mtable>
      <mtr>
       <mtd>
        <mrow>
         <maligngroup/>
         <mfrac>
          <mrow>
           <mi>x</mi>
           <mo>+</mo>
           <mi>y</mi>
          </mrow>
          <mrow>
           <mn>2</mn>
          </mrow>
         </mfrac>
         <malignmark/>
         <mo>=</mo>
         <mn>2</mn>
        </mrow>
       </mtd>
      </mtr>
      <mtr>
       <mtd>
        <mrow>
         <maligngroup/>
         <msqrt>
          <mfrac>
           <mrow>
    	<mi>x</mi>
    	<mo>+</mo>
    	<mi>y</mi>
           </mrow>
           <mrow>
    	<mn>2</mn>
           </mrow>
          </mfrac>
         </msqrt>
         <malignmark/>
         <mo>=</mo>
         <msub>
          <mrow>
           <mi>x</mi>
          </mrow>
          <mrow>
           <mn>2</mn>
          </mrow>
         </msub>
        </mrow>
       </mtd>
      </mtr>
      <mtr>
       <mtd>
        <mrow>
         <maligngroup/>
         <mroot>
          <mrow>
           <mfrac>
    	<mrow>
    	 <mi>x</mi>
    	 <mo>+</mo>
    	 <mi>y</mi>
    	</mrow>
    	<mrow>
    	 <mn>3</mn>
    	</mrow>
           </mfrac>
          </mrow>
          <mrow>
           <mn>3</mn>
          </mrow>
         </mroot>
         <malignmark/>
         <mo>=</mo>
         <msub>
          <mrow>
           <mi>x</mi>
          </mrow>
          <mrow>
           <mn>3</mn>
          </mrow>
         </msub>
        </mrow>
       </mtd>
      </mtr>
     </mtable>
    </math>
    

    I think this re-inforces the thought I had above that we can't specify a "simplified" <malign... that validates and explains both these forms, we need to either keep more or less what we have now in the spec, or declare any use valid, but with undefined behaviour so this behaviour would be valid mathml4, but explained in mathml3.

    I suspect keeping what we have now in the spec is most reasonable.

  18. davidcarlisle commented on Feb 28, 2025

    @davidcarlisle
    Collaborator

    It may (should?) be possible to drastically simply the alignment algorithm at

    https://w3c.github.io/mathml/#presm_alignment_algo

    now alignmark is not allowed in "weird" places, instead of defining it as measuring and aligning in the rendered view we should be able to specify in generic terms how it can be transformed to multiple mtd columns (then the alignment is automatic) and we can say that a polyfill is (or will be) available for such a transform.

  19. davidcarlisle commented on Mar 12, 2025

    @davidcarlisle
    Collaborator

    A possible algorithm to rewrite <aligngroup/> to right-left aligned pairs of mtd.


    Rewriting <malignmark/> and <maligngroup/>

    • Remove all columnalign attributes from <mtable> and <mrow>
      by adding the implied columnalign to each cell.

    • If any cell with columnspan attribute greater than 1 has <maligngroup/>
      or <malignmark/>, remove them (such use is effectively not supported.

    • Modify the table one column at a time:

    • For each column of the table than contains a <maligngroup/> let
      n be the maximum number of <maligngroup/> in a cell in that column.
      (contains here means a descendent of any depth, but not contained within a tested mtable)

    • For each cell in the column (examples assume n = 2):

      • If the cell has no <maligngroup/> add columnspan=2n of if the cell already
        has a columnspan and spans this column, increase the columnspan attribute by 2n.

        <mtd><mi>a</mi></mtd> ⟹ <mtd columnspan="4"><mi>a</mi></mtd>

      • If the cell has a <maligngroup/> but fewer than _n_ add additional `
        to the right (left in rtl direction) of the cell content, so the cell contains n groups.

    • Cells that have <maligngroup/> descendents (not counting nested mtable) are transformed in to 2n mtd table cells

      • first make n cells, each cell corresponding to one alignment group.

      • Traverse in markup order, when reaching <maligngroup/> close all open elements until the <mtd> is closed,
        then open a new mtd cell and re-open all the elements, repeating their attributes, with the exception of id and arg.

        <mtd>
          <mrow mathcolor="red">
            <maligngroup/>
            <mi>a</mi>
            <mrow mathcolor="blue">
              <mo>+</mo>
              <maligngroup/>
              <mi>c</mi>
            </mrow>
          </mrow>
          <malignmark/>
          <mo>=</mo>
          <mi>d</mi>
        </mtd>

        ⟹

        <!-- discarded first group
        <mtd>
          <mrow mathcolor="red">
          </mrow>
        </mtd>
        -->
        <mtd>
          <mrow mathcolor="red">
            <mi>a</mi>
            <mrow mathcolor="blue">
              <mo>+</mo>
            </mrow>
          </mrow>
        </mtd>
        <mtd>
          <mrow mathcolor="red">
            <mrow mathcolor="blue">
              <mi>c</mi>
            </mrow>
          </mrow>
          <malignmark/>
          <mo>=</mo>
          <mi>d</mi>
        </mtd>
      • Now split each alignment group cell in to two, the first being right aligned and the second left.

      • If the mtd has no <alignmark/> then the alignment point is implicitly at the start
        add <mtd columnalign="right"/> before the cell and add columnalign="left" to this element.

      • If the cell does contain (at any depth not counting nested mtable) an <malignmark/> split the
        cell as for <maligngroup/> and add columnalign="right" to the first mtd and columnalign="left"
        to the second.

        ⟹

         <mtd columnalign="right"/>
         <mtd columnalign="left">
           <mrow mathcolor="red">
             <mi>a</mi>
             <mrow mathcolor="blue">
               <mo>+</mo>
             </mrow>
           </mrow>
         </mtd>
         <mtd columnalign="right"/>
           <mrow mathcolor="red">
             <mrow mathcolor="blue">
               <mi>c</mi>
             </mrow>
           </mrow>
         </mtd>
         <mtd columnalign="left"/>
           <mo>=</mo>
           <mi>d</mi>
         </mtd>
  20. davidcarlisle commented on Mar 13, 2025

    @davidcarlisle
    Collaborator

    @NSoiffer the algorithm in the above comment will need review but some algorithm along those lines would work but... unless someone can think of a better phrasing, it wouldn't shorten the spec much if added inline.

    It occurred to me that if we implemented this and added to https://github.com/w3c/mathml-polyfills/ we could document it there
    and in the spec just say

    To rewrite a table that uses <malignedgroup/>, each column using alignment groups should be split into multiple columns using a pair of cells for each alignment group, one right aligned and one left aligned. Then link to the polyfill.

  21. davidcarlisle commented on Oct 6, 2025

    @davidcarlisle
    Collaborator

    Note there is now a standard css property that should be able to replace -webkit-right and -moz-right (and already works in Chrome w3c/mathml-core#156 (comment)

  22. removed
    need specification update
    on Apr 30, 2026
  23. NSoiffer commented on Aug 21, 2026

    @NSoiffer
    ContributorAuthor

    Word now generates columnalignments instead of maligngroup/malignmark. The WG agreed today to deprecate this feature, so the spec needs updating again.

    I have created a polyfill that supports the MathML 3 (full) version of maligngroup/malignmark.

  24. added
    need specification update
    and removed
    need polyfill
    on Aug 21, 2026
  25. davidcarlisle commented on Oct 8, 2026

    @davidcarlisle
    Collaborator

    PR #585 removed malignmark from MathML 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    MathML 4compatibilityneed specification update

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions