MathML

I am trying to render MathML in a word document. I believe this is possible but I am not sure of the steps. Could someone help please?


I am using version 15.11.0

Example:

<m:math>
<m:mfenced open="{" close="">
<m:mtable columnalign=“left”>
<m:mtr>
<m:mtd>
<m:mn>
2
</m:mn>
<m:mi>
x
</m:mi>
<m:mo>
 
</m:mo>
<m:mo>
+
</m:mo>
<m:mo>
 
</m:mo>
<m:mi>
y
</m:mi>
<m:mo>
 
</m:mo>
<m:mo>
=
</m:mo>
<m:mo>
 
</m:mo>
<m:mn>
3
</m:mn>
</m:mtd>
</m:mtr>
<m:mtr>
<m:mtd>
<m:mi>
x
</m:mi>
<m:mo>
 
</m:mo>
<m:mo>
+
</m:mo>
<m:mo>
 
</m:mo>
<m:mn>
3
</m:mn>
<m:mi>
y
</m:mi>
<m:mo>
 
</m:mo>
<m:mo>
=
</m:mo>
<m:mo>
 
</m:mo>
<m:mn>
2
</m:mn>
</m:mtd>
</m:mtr>
</m:mtable>
</m:mfenced>
</m:math>

Hi Steven,


Thanks for your inquiry. Please use following code example to achieve your requirements.

<pre style=“font-family: “Courier New”; font-size: 9pt; background-color: rgb(255, 255, 255);”>Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
String mathML = “<m:math> \n+
" <m:mfenced open="{" close=""> \n" +
" <m:mtable columnalign=left> \n" +
" <m:mtr> \n" +
" <m:mtd> \n" +
" <m:mn>\n" +
" 2\n" +
" </m:mn> \n" +
" <m:mi>\n" +
" x\n" +
" </m:mi> \n" +
" <m:mo>\n" +
"  \n" +
" </m:mo> \n" +
" <m:mo>\n" +
" +\n" +
" </m:mo> \n" +
" <m:mo>\n" +
"  \n" +
" </m:mo> \n" +
" <m:mi>\n" +
" y\n" +
" </m:mi> \n" +
" <m:mo>\n" +
"  \n" +
" </m:mo> \n" +
" <m:mo>\n" +
" =\n" +
" </m:mo> \n" +
" <m:mo>\n" +
"  \n" +
" </m:mo> \n" +
" <m:mn>\n" +
" 3\n" +
" </m:mn> \n" +
" </m:mtd> \n" +
" </m:mtr> \n" +
" <m:mtr> \n" +
" <m:mtd> \n" +
" <m:mi>\n" +
" x\n" +
" </m:mi> \n" +
" <m:mo>\n" +
"  \n" +
" </m:mo> \n" +
" <m:mo>\n" +
" +\n" +
" </m:mo> \n" +
" <m:mo>\n" +
"  \n" +
" </m:mo> \n" +
" <m:mn>\n" +
" 3\n" +
" </m:mn> \n" +
" <m:mi>\n" +
" y\n" +
" </m:mi> \n" +
" <m:mo>\n" +
"  \n" +
" </m:mo> \n" +
" <m:mo>\n" +
" =\n" +
" </m:mo> \n" +
" <m:mo>\n" +
"  \n" +
" </m:mo> \n" +
" <m:mn>\n" +
" 2\n" +
" </m:mn> \n" +
" </m:mtd> \n" +
" </m:mtr> \n" +
" </m:mtable> \n" +
" </m:mfenced> \n" +
" </m:math>\n" +
\n;
builder.insertHtml(mathML);
doc.save(MyDir + “Out v16.7.0.docx”);

Thanks for the help.


What I am getting doesn’t look like the HTML representation of the equation.

In the attached screenshot, the above is the HTML rendered equation, and below the Word version.

Am I missing something?


Hi Steven,


Thanks for your inquiry. Could you please attach complete html here for testing? We will investigate the issue on our side and provide you more information.

Just run the above code in the first reply, it will yield the result I posted.


Thanks.
Hi Steven,

Thanks for your inquiry. Could you please share here the steps you used to create the expected output? We will then provide you more information about your query.

license.setLicense(is);
com.aspose.words.Document doc = new com.aspose.words.Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);
Font font = docBuilder.getFont();
font.setSize(10);
font.setName("Arial");

String mathML = " \n" + " \n"
+ " \n" + " \n" + " \n" + " \n"
+ " 2\n" + " \n" + " \n" + " x\n" + " \n"
+ " \n" + "  \n" + " \n" + " \n" + " +\n"
+ " \n" + " \n" + "  \n" + " \n" + " \n"
+ " y\n" + " \n" + " \n" + "  \n" + " \n"
+ " \n" + " =\n" + " \n" + " \n" + "  \n"
+ " \n" + " \n" + " 3\n" + " \n" + " \n"
+ " \n" + " \n" + " \n" + " \n" + " x\n"
+ " \n" + " \n" + "  \n" + " \n" + " \n"
+ " +\n" + " \n" + " \n" + "  \n" + " \n"
+ " \n" + " 3\n" + " \n" + " \n" + " y\n"
+ " \n" + " \n" + "  \n" + " \n" + " \n"
+ " =\n" + " \n" + " \n" + "  \n" + " \n"
+ " \n" + " 2\n" + " \n" + " \n" + " \n"
+ " \n" + " \n" + " \n" + "\n";
docBuilder.insertHtml(mathML);
doc.save(documentPath);
Hi Steven,

If you view the shared MathML in browser or MS Word, you will get the same output. Please share here the steps you used to create the expected output. We will then provide you more information about your query.

We use MathJax for Web Browser rendering of MathML.



If you put the following into the demo on the MathJax page above, you get this:

<span id=“MathJax-Element-12-Frame” class=“mjx-chtml MathJax_CHTML” tabindex=“0” data-mathml=“2x + y = 3x + 3y = 2” role=“presentation” style=“box-sizing: border-box; display: inline-table; line-height: 0; font-size: 18.72px; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; margin: 0px; padding: 1px 0px; color: rgb(51, 51, 51); font-family: “Helvetica Neue”, Helvetica, Arial, sans-serif; background-color: rgb(255, 255, 255); position: relative;”>{2x + y = 3x + 3y = 22x + y = 3x + 3y = 2

2
x
+
y
=
3
x
+
3
y
=
2


If you put the above into a Word document in Aspose via the insertHtml() method, you get the following:

2 x + y = 3 x

  • 3 y = 2

    The equations are not stacked with the brace, but rather side by side.

    What am I missing here?

    Thanks for the help


Hi Steven,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for Java 16.11.0 with following code example and have not found the shared issue. Please use Aspose.Words for Java 16.11.0. We have attached the output document with this post for your kind reference.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertHtml("\n" +
"<mfenced open=\"{\" close=\"\">\n" +
" <mtable columnalign=\"left\">\n" +
" \n" +
" \n" +
" 2\n" +
" x\n" +
" \n" +
" +\n" +
" \n" +
" y\n" +
" \n" +
" =\n" +
" \n" +
" 3\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" x\n" +
" \n" +
" +\n" +
" \n" +
" 3\n" +
" y\n" +
" \n" +
" =\n" +
" \n" +
" 2\n" +
" \n" +
" \n" +
" \n" +
"\n" +
"");
doc.save(MyDir + "Out v16.11.0.docx");

Yes. You are correct, the latest version does work. We are using version 15.11 and it does not work there. Time for an upgrade.


Thanks.
Hi Steven,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.