dolanmiu / docx

Easily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
https://docx.js.org/
MIT License
4.42k stars 486 forks source link

Add support for MathML Equations #290

Closed andywgarcia closed 4 years ago

andywgarcia commented 5 years ago

In Microsoft word, I can paste the following MathML equation and it will become an editable equation <math xmlns="http://www.w3.org/1998/Math/MathML"><msqrt><mn>56</mn></msqrt></math> which is square root of 56.

image

However, when I add a textrun of the same string using Docx, it just appears as text.

image

Can there be support for the equations? If there already is, can there be documentation for it?

dolanmiu commented 5 years ago

Will prioritize!

Thanks

andywgarcia commented 5 years ago

Will prioritize!

Thanks

Thank you so much! Please do keep this post updated and if you can, an ETA would be greatly appreciated. We are also willing to help as time is available if you need (though we would need quite a bit of direction).

dolanmiu commented 5 years ago

Did some research, sadly it's not so easy as Office uses a variation of MathML:

image

I think pasting MathML works in Word is because Word does some conversion

andywgarcia commented 5 years ago

@dolanmiu I have looked around, and it looks like it is using something call OMML (Ecma Math or Office Math) and it comes with Microsoft office. The python docx package seems to have done this here: https://github.com/python-openxml/python-docx/issues/320#issuecomment-288073960

Do you think it would be possible to do something like this?

andywgarcia commented 5 years ago

I think I found something! Is this what we would need? https://stackoverflow.com/questions/10993621/openxml-sdk-and-mathml/11004619#11004619

dolanmiu commented 5 years ago

I was thinking of implementing it all in JS, so the API will be in code, there wont be any copy and pasting the raw MathML going on.

In the link you provided, it says:

In conjunction with the .Net Framework class XslCompiledTransform we are able to transform presentation MathML into Office MathML.

Not going to use the .NET framework in this library

zahrafali commented 5 years ago

I was thinking of implementing it all in JS, so the API will be in code, there wont be any copy and pasting the raw MathML going on.

In the link you provided, it says:

In conjunction with the .Net Framework class XslCompiledTransform we are able to transform presentation MathML into Office MathML.

Not going to use the .NET framework in this library

Any update on this? If not, any suggestion on how i can proceed with implementing it?

dolanmiu commented 5 years ago

Leave this feature out until after 5.0.0 is released! (Will be soon)

dolanmiu commented 5 years ago

This feature is in progress now

Eduardogbg commented 4 years ago

Any progress on this feature? I know it's obscure and ill-documented, but I think can amount to just adding mml2omml.xsl as a style header.

sergiofm commented 4 years ago

Hi @dolanmiu! Is there any prevision of merging this PR? We are really needing this feature at Mettzer :D We can help if this is necessary.

dolanmiu commented 4 years ago

Soon! After v5 is released

This will be in 5.1.0

miroslavvojtus commented 4 years ago

Hi @dolanmiu. How it goes? Now we are on 5.3.0. Do you expect it anytime soon?

dolanmiu commented 4 years ago

Hi @dolanmiu. How it goes? Now we are on 5.3.0. Do you expect it anytime soon?

https://github.com/dolanmiu/docx/pull/381

Done now! Will be released in 5.4.0

Documentation here: https://docx.js.org/#/usage/math

douglance commented 3 years ago

@dolanmiu Thanks for the great Math implementation! It works really well.

Is there a way to configure a Word doc to display equations as "Professional" by default?

dolanmiu commented 3 years ago

@douglance Could you elaborate what that means?

douglance commented 3 years ago

@dolanmiu Happy to!

By default, embedding a LaTeX equation in a MathRun displays like this:

Screen Shot 2020-12-15 at 7 46 47 AM

But if you click this in Word:

Screen Shot 2020-12-15 at 7 46 55 AM

It displays the equation in "Professional" mode that displays the equations as, well, equations:

Screen Shot 2020-12-15 at 7 46 59 AM

I am wondering if there's a way to configure the equations to display in that "Professional" mode by default using DOCX.js.

dolanmiu commented 3 years ago

@douglance That is very cool! Does this mean people can write in LateX and directly paste the equation into docx? I will look into this

douglance commented 3 years ago

@dolanmiu Yes. It even works using DOCX.js to simply create a MathRun() using the LaTeX syntax directly. Word has its own specific syntax, so not everything translates directly, but for ~80% of things it works as expected.

I am building a tool that lets people compose with LaTeX in the browser (using Katex and ProseMirror) and to download their draft for Word using DOCX.js. So this Math implementation has been a godsend for my project. Thanks again!

dolanmiu commented 3 years ago

Did some digging, and it does not seem possible

Microsoft Word has some sort of Latex equation to MathML transpiler in their software.

Something like this: https://dlmf.nist.gov/LaTeXML/

Unless you would like to write one for docx :P

douglance commented 3 years ago

@dolanmiu Sorry if I was unclear. I am asking only about the "Professional" view. Inserting LaTeX already works right now. It doesn't cover all the possible operations.

If you do MathRun('e=mc^2') in the latest docx it inserts it into the Word Doc, no problem. Then you just need to change the display mode to "Professional" to see it with the nice notation.

I was wondering if you can set that display mode in the Word Doc itself while generating it. I couldn't find the setting in the Word XML.

dolanmiu commented 3 years ago

Yeah I understand what you're saying, but I am saying that the "Professional view" you speak of is not part of the OOXML spec, and is part of Microsoft Word itself

It will be difficult to re-create that feature

It isn't a simple on/off switch

RBSUS commented 1 year ago

Great library, thank you. This may be an ignorant question: If I have some MathML, is it necessary for me to build a custom parser that translates the MathML nodes to corresponding docx.js MathComponents? Is there a fast track way to insert the MathML directly into the word doc?

xu756 commented 1 year ago

How can I convert latex to Word format

dolanmiu commented 1 year ago

How can I convert latex to Word format

Not possible with this library im afraid

RBSUS commented 1 year ago

How can I convert latex to Word format

Not possible with this library im afraid

What makes this the case? The scope of LateX cant be mapped to docx commands?

RBSUS commented 1 year ago

How can I convert latex to Word format

Could you not build a parser for LateX that translates it into docx? You could try using a different library to convert certain mathematical LateX notation into images and then insert the images into docx, if needed

dolanmiu commented 1 year ago

How can I convert latex to Word format

Not possible with this library im afraid

What makes this the case? The scope of LateX cant be mapped to docx commands?

Latex's MathML is another language, which needs to have a tokenizer, parser, and then converted to OOXML

Could you not build a parser for LateX that translates it into docx?

Yes precisely that. I could build it, but it sounds difficult no?

You could try using a different library to convert certain mathematical LateX notation into images and then insert the images into docx, if needed

Where is this library?

RBSUS commented 1 year ago

Thanks for the reply. This is interesting and I appreciate the effort that would be needed to build a custom tokenizer, parser and translator! I believe some python libraries may be able to help with this.

In terms of libraries that enable image generation from latex, I believe there are a few. For example, https://github.com/KaTeX/KaTeX or https://github.com/mathjax/MathJax

I'm by no means suggesting this is within scope of docx but posting here for those exploring this functionality like myself

YaoKaiLun commented 4 months ago

@RBSUS I wrote a tool to do that https://github.com/seewo-doc/docx-math-converter

StephanMeijer commented 1 month ago

Please kindly provide me the appropriate .docx file(s) including examples of math equations and I will look into this.